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';
import request from '@/helpers/request';

export default defineComponent({
  name: 'download-transfer',
  data() {
    return {
      wxStatus: false,
      androidUrl: ''
    };
  },
  async mounted() {
    try {
      const { data } = await request.get(
        '/edu-app/open/appVersionInfo/queryLatestByPlatform',
        {
          params: {
            platform: 'android-kt',
            status: 'history'
          }
        }
      );
      this.androidUrl = data.downloadUrl;
    } catch (e) {
      //
      console.log(e);
    }
    const { pn, url, action, pageTag, ...rest } = this.$route.query;
    const { origin, pathname } = location;
    const tempPathname = pathname;
    const beforeIos = 'BandInstrumentTeam://linkUrl=';
    const 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 (browser().huawei || /(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('kt.colexiu.com') > -1) {
        urlIos =
          'https://apps.apple.com/us/app/%E9%9F%B3%E4%B9%90%E6%95%B0%E5%AD%97%E8%AF%BE%E5%A0%82/id6452725878';
        urlAndroid = this.androidUrl; //'https://oss.dayaedu.com/appstore/kt_cooleshow_student.apk';
      } else {
        urlIos = 'https://www.pgyer.com/dzRRNN';
        urlAndroid = this.androidUrl; //'https://www.pgyer.com/2meenh';
      }

      if (
        browser().ios ||
        /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
      ) {
        urlIos && (window.location.href = urlIos);
      } else {
        window.location.href = urlAndroid;
      }
    }
  },
  render() {
    return (
      <div class={[styles.downloadContainer, browser().isTablet ? styles.creationTablet : '']}>
        <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>
    );
  }
});
