/*
  @description: 专家-完善基本信息-公共方法
  @Modifier: 公共方法
  @routerPath:
  @date: 2020/8/10-14:41
*/
/* 接口*/
import { queryExpertBaseInfo, updateExpertBaseInfo } from '@/api/expert/expertInfo'// 获取用户基本信息 提交
import { updateTBExpertInfoHistory, queryTBExpertInfoHistoryByApplyId } from '@/api/expert/expertChange.js'// 申请变更基本信息
import { upLoadFileUrl } from '@/api/common/uploadCenter'
/* 公用方法*/
import {
  linkerName,
  phoneRandCode,
  randCode,
  makeSureId,
  phone,
  // validateInput,
  // creditCode,
  characters1_auto,
  email
  // checkCode,
  // bankCardNumber
} from '@/assets/js/validate'
import { randomData } from '@assets/js/utils'
import { mapGetters } from 'vuex'
export const expertBase = {
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    // const ddd = function(rule, value, callback) {
    //   const reg = /[`~!@#$%^&*_\+=<>?:"{}|.\/;'\\[\]·~！@#￥%……&*——\+={}|？：“”；‘’]/g
    //   if (!value || !value.trim()) {
    //     callback(new Error('请填写开户行'))
    //   }
    //   if (value && reg.test(value)) {
    //     callback(new Error('不能含有非法字符'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      // changeData: this.$route.query.metaData,
      // 基本信息字段
      formData: {
        certificateCode: '', // 身份证号码
        // comCode: '', // 组织机构代码
        comName: '', // 工作单位
        creditCard: '', // 银行卡号
        expProName: '', // 通讯地址
        expArea: '', // 通讯地址详情
        expPerCityName: '', // 常驻地区
        expPerCountyCode: '',
        // expArea: '', // 通讯详细地址
        // expPerCityCode: '', // 常驻城市
        // expPerProCode: '', // 常驻城市省
        // expProCode: '', // 通讯地址省
        // expCityCode: '', // 通讯地址市
        // expCountyCode: '', // 通讯地址区
        expertId: '', // 专家id
        homeTelphone: '', // 家庭电话
        mail: '', // 邮箱
        mobile: '', // 手机号码
        openingBank: '', // 开户银行
        orgCode: '', // 统一社会信用代码
        postCode: '', // 收件人邮编
        realName: '', // 真实姓名
        retired: '01', // 是否退休 00是 01否
        sex: '', // 男女
        // willingToEmergency: '01', // 是否愿意成为应急专家 00否 01是
        // workTelphone: '', // 办公电话
        health: '01' // 健康状况 00良好；01健康
        // expProCode: '', // 所在地省编号
        // projectAreaCode: '' // 项目区域编码
      },
      // 身份证扫描件
      idImportFileUrl: upLoadFileUrl,
      // 证件照
      photoIdType: {
        bizId: '', // 业务主键Id
        fileType: 'EXPERT_UPLOAD_CERTIFICATEPHOTO' // 文件类型
      },
      // 身份证扫描件
      bprincipalType: {
        bizId: '', // 业务主键Id
        fileType: 'EXPERT_UPLOAD_IDCARD' // 文件类型
      },
      rules: {
        // postCode: [{ required: true, validator: checkCode, trigger: 'blur' }], // 收件人邮编
        certificateCode: [{ required: true, validator: makeSureId, message: '请输入身份证号', trigger: 'blur' }], // 身份证号
        comName: [{ required: true, message: '请填写工作单位', trigger: 'blur' }], // 工作单位
        // openingBank: [{ required: true, validator: ddd, trigger: 'blur' }], // 开户银行
        // willingToEmergency: [{ required: true, message: '请选择', trigger: 'change' }], // 专家类别 基本信息
        retired: [{ required: true, message: '请选择', trigger: 'change' }], // 是否退休
        realName: [{ required: true, validator: linkerName, trigger: 'blur' }], // 联系人姓名
        mobile: [{ required: true, validator: phone, trigger: 'blur' }], // 手机号
        // comCode: [{ required: true, validator: creditCode, trigger: 'blur' }], // 组织机构代码
        projectAreaCode: [{ required: true, message: '请选择项目区域编码', trigger: 'change' }], // 项目所在区域
        majorStartTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
        confirmCode: [{ required: true, validator: randCode, trigger: 'blur' }], // 格式校验
        messageConfirmCode: [{ required: true, validator: phoneRandCode, trigger: 'blur' }], // 手机验证码
        address: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 所在区
        mail: [{ required: true, validator: email, trigger: 'blur' }], // 邮箱
        // creditCard: [{ required: true, validator: bankCardNumber, trigger: 'blur' }], // 银行账号
        // photoId: [{ required: true, message: '请上传', trigger: 'change' }], // 证件照
        // idImportFileUrl: [{ required: true, message: '请上传', trigger: 'change' }], // 身份证扫描证件
        expPerProName: [{ required: true, message: '完善常驻地址', trigger: 'change' }], // 常驻地区
        // expPerProCode: [{ required: true, message: '完善常驻地址', trigger: 'blur' }], // 常驻省
        bprincipalType: [{ required: true, message: '请上传', trigger: 'change' }], // 法人/负责人身份证号或护照号
        // photoIdType: [{ required: true, message: '请上传', trigger: 'change' }], // 法人/负责人身份证号或护照号
        // sex: [{ required: true, message: '请选择性别', trigger: 'change' }], // 性别
        health: [{ required: true, message: '请选择健康状况', trigger: 'change' }] // 健康状况
      }
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    // 获取用户的userId
    this.queryExpertBaseInfo()
  },
  // watch: {
  //   expPerCityCode: {
  //     if (expPerCityName.indexOf('陕西省') !== -1) { // 陕西省内到市，陕西省外到省就行
  //       this.$refs.expPerCityCode.ref = 'expPerCityCode'
  //     } else {
  //       this.$refs.expPerCityCode.ref = ''
  //     }
  //   }
  // },
  mounted() {
  },
  methods: {
    //  身份证扫描件
    principalFile(file, fileList) {
      if (fileList.length > 0) {
        this.$set(this.formData, 'idImportFileUrl', 'true')// 上传了
      } else {
        this.$set(this.formData, 'idImportFileUrl', '')// 未上传
      }
    },
    //  证件扫描件
    photoIdFile(file, fileList) {
      if (fileList.length > 0) {
        this.$set(this.formData, 'photoId', 'true')// 上传了
      } else {
        this.$set(this.formData, 'photoId', '')// 未上传
      }
    },
    // 工作单位弹窗
    selectBuyersShowDialogBtn() {
      this.$refs.expertSelectworkunitDialog.openOrganizationDialog(true)
    },
    // 获取基本注册信息
    queryExpertBaseInfo() {
      const data = {
        userId: this.info.id,
        applyId: this.projectData.applyId
      }
      if (this.projectData.type === 'declarationChange') {
        queryTBExpertInfoHistoryByApplyId(data).then(res => {
          this.formData = res.data.returnData
          this.formData.expertId = res.data.returnData.id // 专家id
          this.formData.certificateCode = res.data.returnData.certificateCode// 身份号码
          // this.formData.comCode = res.data.returnData.comCode// 统一社会编码
          this.formData.comName = res.data.returnData.comName// 工作单位
          this.formData.mobile = res.data.returnData.mobile// 手机
          this.formData.realName = res.data.returnData.realName// 真实姓名
          this.formData.retired = res.data.returnData.retired// 是否退休
          // this.formData.sex = res.data.returnData.sex// 男女
          this.formData.expArea = res.data.returnData.expArea // 通讯地址
          this.formData.openingBank = res.data.returnData.openingBank // 开户银行
          this.formData.creditCard = res.data.returnData.creditCard //  银行卡号
          this.formData.postCode = res.data.returnData.postCode //   邮编
          this.formData.mail = res.data.returnData.mail //   电子邮箱
          // this.formData.workTelphone = res.data.returnData.workTelphone //   办公电话
          this.formData.homeTelphone = res.data.returnData.homeTelphone //   家庭电话
          this.formData.health = res.data.returnData.health || '01' // 健康情況
          // this.formData.willingToEmergency = res.data.returnData.willingToEmergency || '01'// 是否愿意成为应急专家
          const location = [this.formData.expProCode, this.formData.expCityCode, this.formData.expCountyCode]
          const perLocation = [this.formData.expPerProCode, this.formData.expPerCityCode, this.formData.expPerCountyCode]
          this.$refs.expAreaCascader.addressChange(location) // 通讯地址
          this.$refs.expPerCascader.addressChange(perLocation) // 常驻地区
          if (!this.formData.id) {
            this.formData.id = randomData()// 随机生成Id
          }
          this.photoIdType.bizId = this.formData.id// 营业执照扫描件
          this.bprincipalType.bizId = this.formData.id// 法人/负责人身份证号或护照号
          if (this.formData.certificateCode) {
            if (this.formData.certificateCode.substr(-2).substring(0, 1) % 2 === 0) {
              this.formData.sex = '00'
            } else {
              this.formData.sex = '01'
            }
          }
        })
      } else {
        const expertId = this.$route.query.id
        if (expertId) {
          data.expertId = expertId
        }
        queryExpertBaseInfo(data).then(res => {
          this.formData = res.data.returnData
          this.formData.expertId = res.data.returnData.id // 专家id
          this.formData.certificateCode = res.data.returnData.certificateCode// 身份号码
          // this.formData.comCode = res.data.returnData.comCode// 统一社会编码
          this.formData.comName = res.data.returnData.comName// 工作单位
          this.formData.mobile = res.data.returnData.mobile// 手机
          this.formData.realName = res.data.returnData.realName// 真实姓名
          this.formData.retired = res.data.returnData.retired// 是否退休
          // this.formData.sex = res.data.returnData.sex// 男女
          this.formData.expArea = res.data.returnData.expArea // 通讯地址
          this.formData.openingBank = res.data.returnData.openingBank // 开户银行
          this.formData.creditCard = res.data.returnData.creditCard //  银行卡号
          this.formData.postCode = res.data.returnData.postCode //   邮编
          this.formData.mail = res.data.returnData.mail //   电子邮箱
          // this.formData.workTelphone = res.data.returnData.workTelphone //   办公电话
          this.formData.homeTelphone = res.data.returnData.homeTelphone //   家庭电话
          this.formData.health = res.data.returnData.health || '01' // 健康情況
          // this.formData.willingToEmergency = res.data.returnData.willingToEmergency || '01'// 是否愿意成为应急专家
          const location = [this.formData.expProCode, this.formData.expCityCode, this.formData.expCountyCode]
          const perLocation = [this.formData.expPerProCode, this.formData.expPerCityCode, this.formData.expPerCountyCode]
          this.$refs.expAreaCascader.addressChange(location)
          this.$refs.expPerCascader.addressChange(perLocation)
          if (!this.formData.id) {
            this.formData.id = randomData()// 随机生成Id
          }
          this.photoIdType.bizId = this.formData.id// 营业执照扫描件
          this.bprincipalType.bizId = this.formData.id// 法人/负责人身份证号或护照号
          if (this.formData.certificateCode) {
            if (this.formData.certificateCode.substr(-2).substring(0, 1) % 2 === 0) {
              this.formData.sex = '00'
            } else {
              this.formData.sex = '01'
            }
          }
        })
      }
    },
    // 选择工作单位
    dialogDataBuyersInfo(currentData) {
      this.formData.comName = currentData.comName
      this.formData.comCode = currentData.comCode
    },
    // 所在区域三级联动组件使用 常驻地区1
    permregionData(projectAreaName, location) {
      this.formData.expPerCityCode = location[1]
      this.formData.expPerProCode = location[0]
      this.formData.expPerCountyCode = location[2]
      this.formData.expPerProName = projectAreaName
      // this.$set(this.formData, 'expPerCityName', projectAreaName)
    },
    // 所在区域三级联动组件使用 通讯地址2
    commregionData(projectAreaName, location) {
      this.formData.expCityCode = location[1]
      this.formData.expCountyCode = location[2]
      this.formData.expProCode = location[0]
      this.formData.expProName = projectAreaName
      // this.$set(this.formData, 'expProName', projectAreaName)
    },
    /*
      * 保存数据接口 暂存，下一步
      * */
    saveData(type) {
      this.confirmLoading = true// btnLoding
      const data = { ...this.formData }
      const pathData = {
        id: data.id
      }
      if (this.projectData.type === 'declarationChange') {
        updateTBExpertInfoHistory(data).then(res => {
          this.$parent.$emit('stepPage', 'next', pathData)
          this.confirmLoading = false// btnLoding
        }).catch(() => {
          this.confirmLoading = true// btnLoding
        })
      } else {
        updateExpertBaseInfo(data).then(res => {
          this.$parent.$emit('stepPage', 'next', pathData)
          this.confirmLoading = false// btnLoding
        }).catch(() => {
          this.confirmLoading = true// btnLoding
        })
      }
    },
    // 清空工作单位
    clearUnit() {
      this.formData.comName = ''
    },
    // 暂存，进行下一步
    nextStep() {
      // 校验输入内容
      this.$refs.form.validate((valid, field) => {
        if (valid) { // 校验通过
          this.saveData()
        } else { // 校验不通过
          this.$message.warning('请填写基本信息')
        }
      })
    },
    // 保存本页基本信息
    lastStep() {
      const data = { ...this.formData }
      if (this.projectData.type === 'declarationChange') {
        updateTBExpertInfoHistory(data).then(res => {
          this.$message.success('基本信息保存成功')
        })
      } else {
        updateExpertBaseInfo(data).then(res => {
          this.$message.success('基本信息保存成功')
        })
      }
    },
    // ysg后加的方法
    changeCertificateCode(e) {
      const value = e.target.value || e
      if (value.substr(-2).substring(0, 1) % 2 === 0) {
        this.formData.sex = '00'
      } else {
        this.formData.sex = '01'
      }
    }
  }
}
