import { browser } from '@/helpers/utils'
import { Button, showToast } from 'vant'
import { defineComponent } from 'vue'
import styles from './index.module.less'
import wxBg from './images/wx_bg.png'
import qs from 'query-string'

export default defineComponent({
  name: 'download-transfer',
  data() {
    return {
      wxStatus: false,
      type: 'student',
      buttonText: '下载管乐团学生端'
    }
  },
  mounted() {
    const { pn, url, action, pageTag, ...rest } = this.$route.query
    const { origin, pathname } = location
    let tempPathname = pathname
    let beforeIos = 'BandMusicTeam://linkUrl='
    let beforeAndroid = 'colexiustudent://html:8888/SplashActivity?url='

    if (pn === 's') {
      tempPathname = '/orchestra-student/'
      beforeIos = 'BandMusicTeam://linkUrl='
      beforeAndroid = 'orchestrastudent://html:8888/SplashActivity?url='
    } else if (pn === 't') {
      tempPathname = '/orchestra-teacher/'
      beforeIos = 'BandMusicTeamTeacher://linkUrl='
      beforeAndroid = 'orchestrateacher://html:8888/SplashActivity?url='
    } else if (pn === 'm') {
      tempPathname = '/orchestra-school/'
      beforeIos = 'BandMusicTeamManager://linkUrl='
      beforeAndroid = 'orchestramanager://html:8888/SplashActivity?url='
    }

    let str = origin + tempPathname + '#/'
    // 判断是否有跳转连接, 如果连接和动作没有时, 则不跳转
    if (!url && !action) {
      return
    }
    str +=
      url +
      qs.stringify({
        ...rest
      })

    const query = {
      url: str,
      action: action || 'h5', // app, h5
      pageTag: pageTag || 1 // 页面标识
      // params: {}
    } as any
    const iosStr = encodeURIComponent(JSON.stringify(query))
    console.log(query, 'iosStr')
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
      /*  
        唤起前缀 IOS
        BandMusicTeam:// 管乐团
        BandMusicTeamTeacher://
        BandMusicTeamManager:// */
      /* 
        唤起前缀 ANDROID
        orchestrastudent://html:8888/SplashActivity?url=
        orchestrateacher://html:8888/SplashActivity?url=
        orchestramanager://html:8888/SplashActivity?url=
       */
      window.location.href = beforeIos + iosStr
    } else if (/(Android)/i.test(navigator.userAgent)) {
      window.location.href = beforeAndroid + iosStr
    } else {
      showToast('请用手机或移动设备打开')
    }
  },
  methods: {
    onDownload() {
      if (browser().weixin) {
        this.wxStatus = true
        return
      }

      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 {
        showToast('请用手机或移动设备打开')
      }
    }
  },
  render() {
    return (
      <div class={[styles.student]}>
        <Button round size="large" color="#FF8057" class={styles.btn} onClick={this.onDownload}>
          {this.buttonText}
        </Button>

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