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

export default defineComponent({
  name: 'download-transfer',
  data() {
    return {
      wxStatus: false
    };
  },
  mounted() {
    const { pn, url, action, pageTag, ...rest } = this.$route.query;
    const { origin, pathname } = location;
    let tempPathname = pathname;
    let beforeIos = 'BandInstrumentTeam://linkUrl=';
    let beforeAndroid = 'colexiukt://html:8888/SplashActivity?url=';

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

    // 处理参数
    const formatParams = qs.stringify({
      ...rest
    });
    str += url + (formatParams ? '?' + formatParams : '');
    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)) {
      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;
      }
      let urlIos = '';
      let urlAndroid = '';
      if (location.origin.indexOf('online.lexiaoya.cn') > -1) {
        urlIos =
          'https://apps.apple.com/cn/app/%E7%AE%A1%E4%B9%90%E5%9B%A2/id1671474346?uo=4 ';
        urlAndroid =
          'https://appstore.ks3-cn-beijing.ksyuncs.com/gyt-student.apk';
      } else {
        urlIos = 'https://www.pgyer.com/dzRRNN';
        urlAndroid = 'https://www.pgyer.com/2meenh';
      }

      if (
        browser().ios ||
        /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
      ) {
        urlIos && (window.location.href = urlIos);
      } else if (/(Android)/i.test(navigator.userAgent)) {
        window.location.href = urlAndroid;
      } else {
        showToast('请用手机或移动设备打开');
      }
    }
  },
  render() {
    return (
      <div class={[styles.downloadContainer]}>
        <Image src={student} />

        <div class={styles.buttonGroup}>
          <Button
            round
            size="large"
            class={styles.btn}
            onClick={this.onDownload}></Button>
        </div>

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