<template>
  <div />
</template>

<script>
import { getJumpPageStatus, createOrder } from '@/api/question';
import { getDetectionName } from '@/api/appoint';
import storejs from 'storejs';
export default {
  data() {
    return {
      routerMap:{
        '0001': '/appoint',
        '0101': null,
        '0102': '/insuranceQuestionBack',
        '0201': null,
        '0202': '/appoint-details',
        '0203': '/appoint-details',
        '0302': '/insuranceDetectionDetail',
        '0303': '/insuranceBindCode',
        '0901': '/insuranceDetectionDetail',
        '0902': '/insuranceQuestionResult',

      },
      dectionData: {},
      projectEquityNo: '',
      externalOrderNo: '',
      query: {},
      yunOrderNo: '',
      hasQuestionnaire:''

    };
  },
  mounted() {
    // orderNo

    this.query = this.$route.query;
    const {externalOrderNo, orderNo, projectEquityNo} = this.$route.query;

    const n = externalOrderNo || orderNo;
    if (!n && !this.query.referer) {
      this.$loading.show('开发中');
      return;
    }
    console.log(this.$route.query, '---', n);
    if (projectEquityNo && n) {
      this.projectEquityNo = projectEquityNo;
      this.externalOrderNo = n;
      const data = {
        yunOrderNo:'',
        projectEquityNo,
        externalOrderNo: n
      };
      this.$sendBuriedData({
        action: 'ACTION_WEB_AFTER', // 7802854
        component_tag: '7802854#0#0#中转页',
        web_data:{projectEquityNo: projectEquityNo, externalOrderNo: n},
      });
      this.initMap(data);
    }
    if (this.query.referer) {
      this.$loading.show();
      const url = this.query.referer;
      const yunOrderNo = url.split('@@')[1];
      console.log('this.query.referer', yunOrderNo);
      const data = {
        yunOrderNo,
        projectEquityNo:'',
        externalOrderNo:'',
      };
      this.$sendBuriedData({
        action: 'ACTION_WEB_AFTER', // 7802854
        component_tag: '7802854#0#0#中转页',
        web_data:{yunOrderNo: yunOrderNo},
      });
      const that = this;
      setTimeout(function () {
        that.initMap(data);
      }, 2000);

    }

  },
  methods: {
     //  高德地图导航只有在线上环境才能使用
     initMap(data) {
      storejs.remove('geolocation');
      this.getJumpPageStatus(data);

    },
     // 目前是配置一个检测信息，可能会拓展，所以返回的是一个list
     getDetectionName(yunOrderNo) {
       return new Promise((resolve, reject) => {
         getDetectionName(yunOrderNo).then(res => {
           if (res.code === '000000' && res.data) {
             const dectionData = res.data[0] || {};
             console.log('getDetectionName', dectionData);
             this.dectionData = dectionData;
             this.$store.dispatch('home/setInsuranceInfo', dectionData);
             resolve(dectionData);
           }
           this.$loading.hide();
         }).catch(err => {
           reject(err);
         });
       });
    },
    // 获取跳转状态
    getJumpPageStatus(data) {
      getJumpPageStatus(data).then(res => {
        console.log('getJumpPageStatus', res);
        if (res.code == '000000') {
          if (res.data.yunOrderNo && res.data.yunOrderNo != '') {
            this.checkStatus(res);
          } else {
            this.createOrder(data);
          }
          this.$loading.hide();
        }
      });
    },
    async checkStatus(res) {
      this.yunOrderNo = res.data.yunOrderNo;
      this.query.yunOrderNo = res.data.yunOrderNo;
      this.projectEquityNo = res.data.projectEquityNo;
      this.externalOrderNo = res.data.externalOrderNo;
      this.query.projectEquityNo = this.projectEquityNo;
      this.query.externalOrderNo = this.externalOrderNo;
      this.status = res.data.statusCode;
      this.hasQuestionnaire = res.data.hasQuestionnaire;
      // 快检结果
      const t = ['0101', '0102', '0902'];
      const result = !t.includes(this.status) && await this.getDetectionName(this.yunOrderNo);
      const {detectionKind} = result;
      console.log('---result', result);
      switch (this.status) {
          case '0101':
          window.location.href = res.data.questionnaireUrl;
          break;
          case '0001':
          case '0102':
          case '0401':
          case '0202':
          case '0303':
          case '0902':
          this.turnToPage(this.routerMap[this.status], this.query);
          break;
        case '0302':
        case '0901':
          this.query.detectionRecordId = res.data.detectionRecordId;
          this.turnToPage(this.pageMap(detectionKind), this.query);
          break;
        case '0201':
          // hasQuestionnaire  有问卷  跳领用，没问卷直接跳 预约
          if (this.hasQuestionnaire) {
            this.query.needDetect = 1;
            console.log('this.query.needDetect', this.query);
            // 是否付费
            // payFlag "付费免费标识 -1未出结果 0无推荐 1免费 2付费
            const {payFlag} = res.data;
            const path = (payFlag && payFlag == 2) ? '/insuranceQuestionResultPay' : '/insuranceQuestionResult';
            this.turnToPage(path, this.query);
          } else {
            this.turnToPage('/appoint', this.query);
          }
          break;
        default:
          this.turnToPage('/insuranceDetectionDetail', this.query);
          break;
      }
    },
    createOrder(data) {
      createOrder(data).then(res => {
        if (res.code == '000000') {
          this.checkStatus(res);
          this.$loading.hide();
        }
      });
    },
    turnToPage(path, query) {
      this.$router.push({
        path, query
      });
    },
    pageMap(s) {
       const m = {
         0:'/insuranceDetectionDetail',
         1: '/insuranceQuickDetectionDetail'
       };
       return m[s];
    },
  }
};
</script>

<style>

</style>
