<template>
  <div class="base-info">
    <cy-info-title>法人信息</cy-info-title>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item label="法人姓名" prop="name">
        <el-input
          v-model="ruleForm.name"
          :disabled="disabled"
          placeholder="请输入法人姓名"
        />
      </el-form-item>
      <el-form-item label="法人证件类型" prop="type">
        <el-select
          v-model="ruleForm.type"
          :disabled="disabled"
          placeholder="请输入法人证件类型"
        >
          <el-option
            v-for="item in constant.c_credit_investigation_info_type"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="法人证件号码" prop="socialCreditCode">
        <el-input
          v-model="ruleForm.socialCreditCode"
          :disabled="disabled"
          placeholder="请输入法人证件号码"
        />
      </el-form-item>
      <el-form-item
        label="法人证件影像正面"
        class="form-required"
      >
        <cy-upload
          ref="cardFrontRef"
          :upload-params="{
            fileType: 'smb_person_front_file',
            mouldName: 'bocom'
          }"
          :file-data="ruleForm.cardFront"
          :upload-from-orange-status="true"
          :limit="1"
          :is-call-back="true"
          :disabled="disabled"
          placeholder="请上传法人证件影像正面"
          @call-back="frontCardCallBack"
        />
      </el-form-item>
      <el-form-item
        label="法人证件影像反面"
        class="form-required"
      >
        <cy-upload
          ref="cardBackRef"
          :upload-params="{
            fileType: 'smb_person_back_file',
            mouldName: 'bocom'
          }"
          :file-data="ruleForm.cardBack"
          :upload-from-orange-status="true"
          :limit="1"
          :is-call-back="true"
          :disabled="disabled"
          placeholder="请上传法人证件影像反面"
          @call-back="backCardCallBack"
        />
      </el-form-item>
      <el-form-item label="法人手机号" prop="phoneNumber">
        <el-input
          v-model="ruleForm.phoneNumber"
          :disabled="disabled"
          placeholder="请输入法人手机号"
        />
      </el-form-item>
      <el-form-item label="法人证件有效期开始日" prop="validFrom">
        <el-date-picker
          v-model="ruleForm.validFrom"
          :disabled="disabled"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择法人证件有效期开始日"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="法人证件有效期截止日" prop="validTo">
        <el-date-picker
          v-model="ruleForm.validTo"
          :disabled="disabled"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择法人证件有效期截止日"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="法人户籍地址" prop="domicileAddress">
        <el-input
          v-model="ruleForm.domicileAddress"
          :disabled="disabled"
          placeholder="请输入法人户籍地址"
        />
      </el-form-item>
      <el-form-item label="法人常住地址" prop="permanentAddress">
        <el-input
          v-model="ruleForm.permanentAddress"
          :disabled="disabled"
          placeholder="请输入法人常住地址"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { ocrIdentifyCard } from '@/api/capitalFinancialManage'

export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    enterpriseId: {
      type: [String, Number],
      default: ''
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        name: '',
        type: '',
        socialCreditCode: '',
        phoneNumber: '',
        validFrom: '',
        validTo: '',
        domicileAddress: '',
        permanentAddress: '',
        cardFront: [],
        cardBack: []
      },
      rules: {
        name: [{ required: true, message: '请输入法人姓名', trigger: 'blur' }],
        type: [{ required: true, message: '请输入身份证类型', trigger: 'blur' }],
        socialCreditCode: [{ required: true, message: '请输入法人证件号码', trigger: 'blur' }],
        phoneNumber: [{ required: true, message: '请输入法人手机号码', trigger: 'blur' }],
        validFrom: [{ required: true, message: '请选择法人证件有效期开始日', trigger: 'blur' }],
        validTo: [{ required: true, message: '请选择法人证件有效期截止日', trigger: 'blur' }],
        domicileAddress: [{ required: true, message: '请输入法人户籍地址', trigger: 'blur' }],
        permanentAddress: [{ required: true, message: '请输入法人常住地址', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['constant'])
  },
  watch: {
    detailsInfo: {
      handler(val) {
        if (val && Object.keys(val).length) {
          const {
            name,
            type,
            socialCreditCode,
            phoneNumber,
            validFrom,
            validTo,
            domicileAddress,
            permanentAddress
          } = val
          this.ruleForm = {
            name,
            type,
            socialCreditCode,
            phoneNumber,
            validFrom,
            validTo,
            domicileAddress,
            permanentAddress
          }

          if (val.front) {
            const tempFront = []
            tempFront.push(val.front)
            this.ruleForm.cardFront = tempFront
          } else {
            this.ruleForm.cardFront = []
          }

          if (val.back) {
            const tempBack = []
            tempBack.push(val.back)
            this.ruleForm.cardBack = tempBack
          } else {
            this.ruleForm.cardBack = []
          }
        }
      },
      immediate: true
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['c_credit_investigation_info_type'])
  },
  methods: {
    frontCardCallBack() {
      const fileFrontList = this.$refs.cardFrontRef.getFileLists
      const fileBackList = this.$refs.cardBackRef.getFileLists

      if (fileFrontList.length && fileBackList.length) {
        this.ocrIdCard(fileFrontList[0].id, fileBackList[0].id)
      }
    },
    backCardCallBack() {
      const fileFrontList = this.$refs.cardFrontRef.getFileLists
      const fileBackList = this.$refs.cardBackRef.getFileLists

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

        if (res.success) {
          this.ruleForm.name = res.data.name
          this.ruleForm.socialCreditCode = res.data.idcard
          this.ruleForm.validFrom = res.data.validfrom
          this.ruleForm.validTo = res.data.validto
          this.ruleForm.domicileAddress = res.data.address
          this.ruleForm.permanentAddress = res.data.address
        }
      }
    },
    getParams(type) {
      const frontFiles = this.$refs.cardFrontRef.getFileLists
      const backFiles = this.$refs.cardBackRef.getFileLists

      const params = {
        ...this.ruleForm,
        cEnterpriseId: this.enterpriseId,
        frontId: frontFiles.length ? frontFiles[0].id : '',
        backId: backFiles.length ? backFiles[0].id : ''
      }

      delete params.cardFront
      delete params.cardBack

      return new Promise((resolve) => {
        if (type === 'temp') {
          resolve(params)
        } else {
          if (frontFiles.length === 0 || backFiles.length === 0) {
            this.$message.warning('请上传法人身份证信息')
            return
          }

          this.$refs.ruleFormRef.validate((valid) => {
            if (valid) {
              resolve(params)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss"></style>
