<template>
  <div class="person-info">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <cy-info-title>{{ personType === 'legal' ? '法人信息' : '实际控制人信息' }}</cy-info-title>
      <el-form-item label="身份证正面" class="form-required">
        <cy-upload
          ref="idcardStorage_front"
          :upload-params="{ fileType: 'enterprise_idcard_front' }"
          :disabled="disabled"
          :is-call-back="true"
          :upload-from-orange-status="true"
          :limit="1"
          :file-data="ruleForm.frontFile || []"
          placeholder="请上传身份证正面"
          @call-back="frontCardCallBack"
        />
      </el-form-item>
      <el-form-item label="身份证反面" class="form-required">
        <cy-upload
          ref="idcardStorage_back"
          :upload-params="{ fileType: 'enterprise_idcard_back' }"
          :disabled="disabled"
          :is-call-back="true"
          :limit="1"
          :upload-from-orange-status="true"
          :file-data="ruleForm.backFile || []"
          placeholder="请上传身份证反面"
          @call-back="backCardCallBack"
        />
      </el-form-item>
      <el-form-item :label="personType === 'legal' ? '法人姓名': ' 实控人姓名'" prop="legalName">
        <el-input v-model="ruleForm.legalName" :disabled="disabled" placeholder="请输入姓名" readonly></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="bornDate">
        <el-date-picker
          v-model="ruleForm.bornDate"
          :disabled="disabled"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期"
          readonly
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="ruleForm.phoneNumer"
          :disabled="disabled"
          placeholder="请输入手机号码"
          @input="validatePhoneNumber"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="$route.query.type !== 'see'" label="验证码" class="form-required">
        <el-input
          v-model="ruleForm.code"
          placeholder="请输入验证码"
          type="text"
          tabindex="1"
          auto-complete="on"
        >
          <template #append>
            <verification-code :code="ruleForm.phoneNumer" module="other" type="phone" placeholder="请输入手机号码" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="联系地址" prop="contactAddress">
        <el-input
          v-model="ruleForm.contactAddress"
          placeholder="请输入联系地址"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item label="现住地址" class="two-form-item" prop="businessAddress">
        <div class="address">
          <div v-if="!disabled">
            <el-cascader
              v-model="ruleForm.cascaderAddress"
              style="width: 100%;"
              placeholder="请选择省市区"
              :options="regionList"
              :props="{ value: 'name', label: 'name' }"
              @change="getBusinessAddress"
            />
          </div>
          <div>
            <el-input
              v-model="ruleForm.inputAddress"
              style="width: 100%;"
              :disabled="disabled"
              placeholder="请输入详细地址"
              @blur="getBusinessAddress"
            />
          </div>
        </div>
      </el-form-item>
      <el-form-item label="征信" class="form-required" style="width: 100%;">
        <cy-upload
          ref="creditFileRef"
          :upload-params="{ fileType: 'credit_file' }"
          :disabled="disabled"
          :file-type="'pdf'"
          :file-data="ruleForm.creditFile || []"
          :upload-from-orange-status="true"
          placeholder="请上传征信文件"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRegion } from '@/api/dictionary'
import { treeChildrenEmpty, cloneDeep } from '@/utils'

export default {
  components: {
    verificationCode: () => import('@/components/VerificationCode/index.vue')
  },
  props: {
    personType: {
      type: String,
      default: 'legal'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        frontFile: [],
        backFile: [],
        legalName: '',
        bornDate: '',
        phoneNumer: '',
        code: '',
        cascaderAddress: '',
        contactAddress: '',
        creditFile: []
      },
      regionList: [],
      rules: {
        legalName: [{ required: true, message: '请输入法人姓名', trigger: 'blur' }],
        bornDate: [{ required: true, message: '请选择出生日期', trigger: 'blur' }],
        phoneNumber: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
        contactAddress: [{ required: true, message: '请输入联系地址', trigger: 'blur' }],
        businessAddress: [{ required: true, message: '请选择现居地址', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.queryReginList()
  },
  methods: {
    getParams(type) {
      const reqParams = cloneDeep(this.ruleForm)
      return new Promise((resolve) => {
        if (type === 'temp') {
          resolve(reqParams)
        } else {
          const fileFrontList = this.$refs['idcardStorage_front'].getFileLists
          const fileBackList = this.$refs['idcardStorage_back'].getFileLists
          const creditList = this.$refs.creditFileRef.getFileLists

          if (fileFrontList && fileFrontList.length === 0) {
            this.$message.warning('请上传身份证正面')
            return
          }

          if (fileBackList && fileBackList.length === 0) {
            this.$message.warning('请上传身份证反面')
            return
          }

          if (creditList && creditList.length === 0) {
            this.$message.warning('请上传征信文件')
            return
          }

          if (!this.ruleForm.code) {
            this.$message.warning('请输入验证码')
            return
          }

          this.$refs.ruleFormRef.validate((valid) => {
            if (valid) {
              resolve(reqParams)
            }
          })
        }
      })
    },
    async queryReginList() {
      getRegion({ flag: true }).then(({ data }) => {
        this.regionList = treeChildrenEmpty(data)
      })
    },
    getBusinessAddress() {
      const address = this.ruleForm.cascaderAddress.length ? this.ruleForm.cascaderAddress.join('') : ''
      let tempAdress = ''
      if (this.ruleForm.inputAddress) {
        tempAdress = this.ruleForm.inputAddress
      } else {
        tempAdress = address
      }
      this.$set(this.ruleForm, 'inputAddress', tempAdress)
    },
    validatePhoneNumber(val) {
      const phoneRegex = /^1[3-9]\d{9}$/
      if (!phoneRegex.test(val)) {
        this.$message.warning('请输入合法的手机号码')
      }
    },
    // 身份证正面上传成功回调
    async frontCardCallBack() {
      const fileFrontList = this.$refs['idcardStorage_front'].getFileLists

      const fileBackList = this.$refs['idcardStorage_back'].getFileLists

      if (fileFrontList.length && fileBackList.length) {
        this.ocrIdCard(fileFrontList[0].id, fileBackList[0].id)
      }
    },
    // 身份证反面上传成功的回调
    backCardCallBack() {
      const fileFrontList = this.$refs['idcardStorage_front'].getFileLists

      const fileBackList = this.$refs['idcardStorage_back'].getFileLists

      if (fileFrontList.length && fileBackList.length) {
        this.ocrIdCard(fileFrontList[0].id, fileBackList[0].id)
      }
    },
    // OCR身份证
    async ocrIdCard(frontId, backId) {
      // if (frontId !== '' && backId !== '') {
      //   const res = await ocrIdentifyCard({
      //     front: frontId,
      //     back: backId
      //   })

      //   if (res.success) {
      //     this.ruleForm.legalName = res.data.address
      //     tableRow.bornDate = res.data.address
      //     tableRow.socialCreditCode = res.data.idcard
      //     tableRow.validFrom = res.data.validfrom
      //     tableRow.validTo = res.data.validto
      //   }
      // }
    }
  }
}
</script>

<style lang="scss" scoped>
.address {
  display: flex;
  width: 100%;
  div {
    width: calc(50% - 10px);
  }
  div:nth-child(2) {
    margin-left: 18px;
  }
}
</style>
