import { browser } from '@/helpers/utils'
import { url } from 'inspector'
import { Button, Toast } from 'vant'
import { defineComponent } from 'vue'
import styles from './index.module.less'

export const getAssetsHomeFile = (fileName: string) => {
  const path = `./images/${fileName}`
  const modules = import.meta.globEager('./images/*')
  return modules[path].default
}

export default defineComponent({
  name: 'transfer',
  data() {
    return {
      wxStatus: false
    }
  },
  mounted() {
    const { origin, pathname } = location
    let str = origin + pathname + '#/'
    let params = this.$route.query
    // 判断是否有跳转连接, 如果连接和动作没有时, 则不跳转
    if (!params.url && !params.action) {
      return
    }
    str += params.url

    const query = {
      url: str,
      action: params.action || 'h5', // app, h5
      pageTag: params.pageTag || 1 // 页面标识
      // params: {}
    }
    const iosStr = encodeURIComponent(JSON.stringify(query))
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
      // ColexiuStudent 唤起学生端
      // ColexiuTeacher 唤起老师端
      window.location.href = `ColexiuStudent://linkUrl=${iosStr}`
    } else if (/(Android)/i.test(navigator.userAgent)) {
      window.location.href = `colexiustudent://html:8888/SplashActivity?url=${iosStr}`
    } else {
      Toast('请用手机或移动设备打开')
    }
  },
  methods: {
    downLoadApp() {
      if (browser().weixin) {
        this.wxStatus = true
        return
      }

      // clx-teacher-domain.apk
      // clx-student-domain.apk
      if (
        browser().ios ||
        /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
      ) {
        window.location.href =
          'https://itunes.apple.com/cn/app/id1626971695?mt=8'
      } else if (/(Android)/i.test(navigator.userAgent)) {
        window.location.href =
          'https://appstore.ks3-cn-beijing.ksyuncs.com/clx-student-domain.apk'
      } else {
        this.$toast('请用手机或移动设备打开')
      }
    }
  },
  render() {
    return (
      <div class={styles.downContainer}>
        <div class={styles.logo}>
          <img src={getAssetsHomeFile('logo.png')} alt="" />
        </div>
        <div class={styles.down}>
          <Button type="primary" round plain onClick={this.downLoadApp}>
            点击下载App
          </Button>
        </div>
        <div class={styles.mainImg}>
          <img src={getAssetsHomeFile('student_bg.png')} alt="" />
        </div>

        {this.wxStatus && (
          <div
            class={styles.wxpopup}
            onClick={() => {
              this.wxStatus = false
            }}
          >
            <img src={getAssetsHomeFile('wx_bg.png')} alt="" />
          </div>
        )}
      </div>
    )
  }
})
