const range = [
  'PersonRealnameStep1',
  'PersonRealnameStep2',
  'PersonRealnameStep3'
]
export default {
  beforeRouteLeave(to, from, next) {
    if (range.indexOf(to.name) === -1) {
      range.forEach(name => {
        this.cacheDelStepData(name)
      })
    }
    next()
  },
  created() {
    this._isInitMount = true
    this.init()
  },
  activated() {
    if (this._isInitMount) {
      this._isInitMount = false
      return
    }
    this.show()
  },
  methods: {
    // 从前一个页面进入当前页时，先从缓存恢复数据，否则重建
    // 其他情况进入当前页，重建
    initRenderPage() {
      if (this.$route.params.from === 'prev') {
        this.restorePageDataFromCache()
      } else {
        this.initEmptyPageData()
      }
    },

    // 获取车卡关系验证请求参数
    getVerifyVinCardsInfoParams(data, addition) {
      const iccidList = data.iccidList.filter(item => item.iccid !== '').map(item => item.iccid)
      const vin = data.vin
      const rnrId = data.rnrId
      return {
        iccidList,
        vin,
        rnrId,
        ...addition
      }
    },
    // 获取提交自然人实名认证请求参数
    getSubmitPersonRealNameAuthParams(data) {
      const {
        step1Data,
        step2Data,
        step3Data
      } = data
      const busTypeData = step1Data.busTypeData
      const carCardData = step1Data.carCardData
      const masterData = step2Data.masterData
      const delegateData = step2Data.delegateData
      const ret = {
        // 业务类型
        customerType: busTypeData.userType === 'new' ? 0 : 1,
        isConsigner: busTypeData.isDelegate,

        // 车卡信息
        vin: carCardData.vin,
        iccidList: carCardData.iccidList.filter(item => item.iccid !== '').map(item => item.iccid),

        // 基本信息
        fullName: masterData.basicInfoData.name,
        gender: masterData.basicInfoData.sex,

        // 证件信息
        certAddress: masterData.certInfoData.address,
        certExpirationDate: masterData.certInfoData.validDate,
        certEffectiveDate: masterData.certInfoData.certEffectiveDate,
        certPic: masterData.certInfoData.photoUrls.filter(item => item.url.id !== null).map(item => item.url.id),
        certNumber: masterData.certInfoData.no,
        certType: masterData.certInfoData.type,
        contactAddress: masterData.certInfoData.connectAddress,

        // 文件信息
        contractPic: masterData.fileInfoData.protocolUrl.map(item => item.uuid),
        purchaseContractPic: busTypeData.userType === 'new' ? [] : masterData.fileInfoData.contractUrl.map(item => item.uuid),
        purchaseInvoicePic: busTypeData.userType === 'new' ? [] : masterData.fileInfoData.billUrl.map(item => item.uuid),
        transferCertificatePic: busTypeData.userType === 'new' ? [] : masterData.fileInfoData.transferUrl.map(item => item.uuid),
        dutyPic: masterData.fileInfoData.notifyUrl.map(item => item.uuid),

        // 联系信息
        phone: masterData.concatInfoData.phone,
        verificationCode: masterData.concatInfoData.veCode,

        // 委托人
        consignerInfo: {
          // 基本信息
          fullName: '',
          gender: '',

          // 证件信息
          certType: '',
          certPic: [],
          certNumber: '',
          certAddress: '',
          certExpirationDate: '',
          contactAddress: '',

          // 委托书
          attorneyLetterPic: '',

          // 联系信息
          phone: '',
          verificationCode: ''
        },

        liveVerificationVideo: step3Data ? step3Data.fileData.fileList[0].uuid : '',
        requestId: step3Data ? step3Data.requestId : ''
      }
      if (busTypeData.isDelegate) {
        ret.consignerInfo.fullName = delegateData.basicInfoData.name
        ret.consignerInfo.gender = delegateData.basicInfoData.sex
        ret.consignerInfo.certType = delegateData.certInfoData.type
        ret.consignerInfo.certPic = delegateData.certInfoData.photoUrls.filter(item => item.url.id !== null).map(item => item.url.id)
        ret.consignerInfo.certNumber = delegateData.certInfoData.no
        ret.consignerInfo.certAddress = delegateData.certInfoData.address
        ret.consignerInfo.certExpirationDate = delegateData.certInfoData.validDate
        ret.consignerInfo.certEffectiveDate = delegateData.certInfoData.certEffectiveDate
        ret.consignerInfo.contactAddress = delegateData.certInfoData.connectAddress
        ret.consignerInfo.attorneyLetterPic = delegateData.fileInfoData.fileList.map(item => item.uuid)
        ret.consignerInfo.phone = delegateData.concatInfoData.phone
        ret.consignerInfo.verificationCode = delegateData.concatInfoData.veCode
      }
      return ret
    }
  }
}
