<template>
  <div class="enterprise-supplement">
    <basic-info ref="baseInfoRef" :base-info="baseInfo" class="base-info" />
    <PersonInfo ref="legalPersonInfoRef" :regionList="regionList" :person-type="'legal'" :details-info="legalPersonInfo" class="legal-info" />
    <PersonInfo ref="controlPersonInfoRef" :regionList="regionList" :person-type="'control'" :details-info="controlPersonInfo" />
    <ContactInfo ref="contactInfoRef" :details-info="contactInfo" />
    <div class="submit-btn">
      <el-button @click="() => { $router.push({ name: 'MyStoreEnterprise', query: { activeName: 'enterprise' } }) }">关闭</el-button>
      <el-button type="primary" @click="submitClick('temp')">暂存</el-button>
      <el-button type="primary" @click="submitClick('submit')">下一步</el-button>
    </div>
  </div>
</template>

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


export default {
  name: 'MyBusinessProjectSupplement',
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    PersonInfo: () => import('./components/PersonInfo.vue'),
    ContactInfo: () => import('./components/ContactInfo.vue')
  },
  data() {
    return {
      baseInfo: {},
      legalPersonInfo: {},
      controlPersonInfo: {},
      contactInfo: {},
      regionList: []
    }
  },
  created() {
    this.queryReginList()

    if (this.$route.query.id && this.$route.query.companyZrId) {
      this.getDetails(this.$route.query.id)
    } else {
      const { code, name } = this.$route.query
      this.baseInfo = {
        companyType: '',
        socialCreditCode: code || '',
        enterpriseName: name || ''
      }
    }
  },
  methods: {
    async queryReginList() {
      getRegion({ flag: false }).then(({ data }) => {
        this.regionList = treeChildrenEmpty(data)
      })
    },
    async getDetails(queryId) {
      const res = await getEnterpriseDetail(queryId)
      const { code, name  } = this.$route.query

      if (res.success) {
        this.baseInfo = {
          companyType: res.data.companyZrVo.companyType || '',
          socialCreditCode: code || '',
          enterpriseName: name || ''
        }
        this.legalPersonInfo = res.data.companyZrVo
        this.controlPersonInfo =  res.data.companyZrVo
        this.contactInfo = res.data.companyZrVo
      }
    },
    async submitClick(type) {
      const fileIds = []
      const legalParams = await this.$refs.legalPersonInfoRef.getParams(type)
      console.log(legalParams)
      const legalReqParams = {
        legalPerson: legalParams.legalName || '', // 法人姓名
        legalPersonBirthday: legalParams.bornDate || '', // 法人出生日期
        legalPersonPhone: legalParams.phoneNumber || '', // 法人手机号码
        legalPersonCard: legalParams.personCard || '', // 法人身份证号
        legalPersonEmail: legalParams.email || '', // 法人邮箱
        legalPersonProvince: legalParams.cascaderAddress[0] || '', // 省code
        legalPersonCity: legalParams.cascaderAddress[1] || '',  // 市code
        legalPersonCounty: legalParams.cascaderAddress[2] || '', // 区code
        legalPersonAddress: legalParams.contactAddress || '', // 法人身份证地址
        legalPersonLiveAddress: legalParams.inputAddress || '' // 详细地址
      }
      fileIds.push(legalParams.cardFrontId, legalParams.cardBackId, legalParams.creditId)

      const controlParams = await this.$refs.controlPersonInfoRef.getParams(type)
      console.log(controlParams)
      const controlReqParams = {
        realControlPerson: controlParams.legalName || '', // 实控人姓名
        realControlPersonBirthday: controlParams.bornDate || '', // 实控人出生日期
        realControlPersonPhone: controlParams.phoneNumber || '', // 实控人手机号码
        realControlPersonCard: controlParams.personCard || '', // 实控人身份证号
        realControlPersonEmail: controlParams.email || '', // 法人邮箱
        realControlPersonProvince: controlParams.cascaderAddress[0] || '', // 省code
        realControlPersonCity: controlParams.cascaderAddress[1] || '',  // 市code
        realControlPersonCounty: controlParams.cascaderAddress[2] || '', // 区code
        realControlPersonAddress: controlParams.contactAddress || '', // 法人身份证地址
        realControlPersonLiveAddress: controlParams.inputAddress || '' // 详细地址
      }
      fileIds.push(controlParams.cardFrontId, controlParams.cardBackId, controlParams.creditId)

      const contactParams = await this.$refs.contactInfoRef.getParams(type)
      const contactReqParams = {
        ...contactParams
      }
      const baseInfoParams = await this.$refs.baseInfoRef.getPrams(type)
      const supParams = {
        Loading: true,
        companyId: this.$route.query.id,
        ...legalReqParams,
        ...controlReqParams,
        ...contactReqParams,
        companyType: baseInfoParams.companyType,
        fileIds,
        flag: type === 'temp' ? 0 : 1
      }

      if (this.$route.query.companyZrId) {
        supParams.companyZrId = this.$route.query.companyZrId
      }

      if (this.$route.query.taskId) {
        supParams.taskId = this.$route.query.taskId
      }

      const res = await enterpriseAccessStepOne(supParams)

      if (res.success) {
        this.$message.success('操作成功')
        if (type === 'temp') {
          this.$router.go(-1)
        } else if (type === 'submit') {
          if (res.data.redNo) {
            this.$confirm(res.data.msg, '红否提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$router.go(-1)
            }).catch(() => {})
          } else {
            this.$router.push({
              name: 'StoreEnterpriseAuth',
              query: {
                zrId: res.data.companyZrId,
                taskId: res.data.taskId
              }
            })
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.enterprise-supplement {
  .base-info {
    margin-bottom: 10px;
  }
  .legal-info {
    margin-bottom: 10px;
  }
}
</style>
