<template>
  <div class="enterprise-edit">
    <basic-info ref="baseInfoRef" :base-info="baseInfo" />
    <PersonInfo ref="legalPersonInfoRef" :person-type="'legal'" :details-info="legalPersonInfo" :regionList="regionList" />
    <PersonInfo ref="controlPersonInfoRef" :person-type="'control'" :details-info="controlPersonInfo" :regionList="regionList" />
    <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 } from '@/api/myStoreEnterprise'

export default {
  name: 'MyEnterpriseReInvite',
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    PersonInfo: () => import('./components/PersonInfo.vue'),
    ContactInfo: () => import('./components/ContactInfo.vue')
  },
  data() {
    return {
      baseInfo: {},
      legalPersonInfo: {},
      controlPersonInfo: {},
      contactInfo: {},
      regionList: []
    }
  },
  created() {
    const { code, name  } = this.$route.query

    this.baseInfo = {
      socialCreditCode: code || '',
      enterpriseName: name || ''
    }

    this.queryReginList()
  },
  methods: {
    async queryReginList() {
      getRegion({ flag: false }).then(({ data }) => {
        this.regionList = treeChildrenEmpty(data)
      })
    },
    async submitClick(type) {
      const fileIds = []
      const legalParams = await this.$refs.legalPersonInfoRef.getParams(type)

      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)

      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 supParams = {
        companyId: this.$route.query.id,
        companyType: this.$refs.baseInfoRef.ruleForm.companyType,
        ...legalReqParams,
        ...controlReqParams,
        ...contactReqParams,
        fileIds,
        flag: type === 'temp' ? 0 : 1
      }

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

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

      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: {
                taskId: res.data.taskId
              }
            })
          }
        }
      }
    }
  }
}
</script>

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