<template>
  <div class="my-business">
    <p v-if="detailInfo && detailInfo.certificationStatus === 'passed_certification'" class="sign-file">
      <span style="margin-right: 15px;">{{ codeToName(constant.sys_sign_status, detailInfo.contactPerson.authorizationFileSigned, 'dictValue', 'dictLabel' ) }}</span>
      <span v-if="detailInfo.contactPerson.authorizationFile && detailInfo.contactPerson.authorizationFile.id" class="sign-file-text" @click="previewAuthFile">授权委托书</span>
    </p>
    <cy-info-title>基础信息</cy-info-title>
    <basic-info ref="baseInfo" :detail-info="baseInfo" :source="source" />

    <cy-info-title>法定代表人信息</cy-info-title>
    <person ref="legal" :detail-info="legalInfo" :person-type="'legal'" :source="source" @legalIsControl="legalIsOrNotControl" />

    <cy-info-title v-if="!legalIsControl">实际控制人信息</cy-info-title>
    <person v-if="!legalIsControl" ref="control" :detail-info="controlInfo" :source="source" />

    <div v-if="source === 'noAuth'" class="submit-btn">
      <el-button @click="submitClick('save')">暂存</el-button>
      <el-button type="primary" @click="submitClick('submit')">提交</el-button>
    </div>
    <div v-if="source === 'index'" class="submit-btn">
      <el-button @click="() => { $router.push({name: 'Dashboard'}) }">返回</el-button>
      <el-button type="primary" @click="submitClick('save')">暂存</el-button>
      <el-button type="primary" @click="submitClick('submit')">提交</el-button>
    </div>
    <div v-if="source === 'business'" class="submit-btn">
      <el-button @click="() => { $router.push({name: 'Dashboard'}) }">返回</el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { codeChangeName } from '@/utils'
import { getEnterpriseDetailHome, submitEnterpriseInfo } from '@/api/businessManage/index'
export default {
  components: {
    BasicInfo: () => import('./BasicInfo'),
    Person: () => import('./Person')
  },
  props: {
    source: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      id: '',
      detailInfo: {},
      baseInfo: {},
      legalInfo: {},
      controlInfo: {},
      legalIsControl: false
    }
  },
  computed: {
    ...mapGetters([
      'constant'
    ])
  },
  created() {
    this.$store.dispatch('getConstant', ['sys_sign_status'])
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    getDetail() {
      getEnterpriseDetailHome(this.source).then(response => {
        this.detailInfo = response.data
        if (response.data) {
          const {
            changeRecordId,
            certificationStatus,
            enterpriseType,
            name,
            certNo,
            taxNo,
            establishDate,
            registerCapital,
            registerAddress,
            sameRegisterAndBusinessAddress,
            businessAddress,
            businessLicense,
            exportType,
            proxyDistrictCode,
            customsEnterpriseCode,
            emailAuthCode,
            legalPerson,
            contactPerson,
            controlPerson,
            taxAuthorityArea
          } = response.data
          this.baseInfo = {
            changeRecordId,
            certificationStatus,
            enterpriseType,
            name,
            certNo,
            taxNo,
            establishDate,
            registerCapital,
            registerAddress,
            sameRegisterAndBusinessAddress,
            businessAddress,
            businessLicense: businessLicense ? [businessLicense] : [],
            exportType,
            proxyDistrictCode,
            customsEnterpriseCode,
            emailAuthCode,
            contactPerson,
            taxAuthorityArea
          }
          this.legalInfo = legalPerson
          this.legalIsControl = this.legalInfo.isControl === 'Y'
          if (this.legalInfo.isControl === 'N' && controlPerson) {
            this.controlInfo = controlPerson
          }
        }
      })
    },
    legalIsOrNotControl(val) {
      this.legalIsControl = val === 'Y'
    },
    async submitClick(type) {
      const baseInfo = await this.$refs.baseInfo.validate(type)
      const legalInfo = await this.$refs.legal.validate(type)

      var postData = {
        Loading: true,
        ...baseInfo,
        legalPerson: {
          ...legalInfo,
          isLegalPerson: 'Y',
          isControl: 'Y'
        }
      }
      if (!this.legalIsControl) { // 法人不是实控人
        postData.legalPerson.isControl = 'N'
        const controlInfo = await this.$refs.control.validate(type)
        postData = {
          ...postData,
          controlPerson: {
            ...controlInfo,
            isControl: 'Y'
          }
        }
        if (type === 'submit') {
          if (controlInfo.personalCertNo === legalInfo.personalCertNo) {
            this.$message.warning('法定代表人信息与实际控制人信息有重复。')
            return
          }
          if (controlInfo.mobile === legalInfo.mobile) {
            this.$message.warning('法定代表人信息与实际控制人信息有重复。')
            return
          }
        }
      }
      if (type === 'submit') {
        if (baseInfo.contactPerson.personalCertNo === legalInfo.personalCertNo) {
          this.$message.warning('法定代表人信息与联系人信息重复。')
          return
        }
        if (baseInfo.contactPerson.mobile === legalInfo.mobile) {
          this.$message.warning('法定代表人信息与联系人信息重复。')
          return
        }
      }

      const params = { operate: type }
      submitEnterpriseInfo(params, postData).then(() => {
        if (type === 'submit') {
          this.$message.success('提交成功。')
          if (this.source === 'noAuth') {
            this.$router.go(0)
          } else {
            this.$router.push({ name: 'Dashboard' })
          }
        } else {
          this.$message.success('暂存成功')
          if (this.source === 'noAuth') {
            this.$router.go(0)
          }
        }
      })
    },
    codeToName(list, type, code, name) {
      return codeChangeName(list, type, code, name)
    },
    previewAuthFile() {
      const idNum = this.baseInfo.contactPerson.authorizationFile.id
      window.open(`${process.env.VUE_APP_BASE_API}/file_storage_oss/preview/${idNum}?resource=1#toolbar=0&navpanes=0&scrollbar=0`)
    }
  }

}
</script>

<style lang="scss" scoped>
.sign-file {
  text-align: right;
  .sign-file-text {
    text-decoration: underline;
    cursor: pointer;
    color: #409eff
  }
}
</style>
