import { browser } from '@/helpers/utils'
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: 'download',
  data() {
    const query = this.$route.query
    return {
      type: query.type || 'student',
      wxStatus: false
    }
  },
  mounted() {
    if (this.type === 'student') {
      document.title = '酷乐秀学院下载'
    } else if (this.type === 'teacher') {
      document.title = '酷乐秀下载'
    }
  },
  methods: {
    downLoadApp() {
      if (browser().weixin) {
        this.wxStatus = true
        return
      }

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

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