<template>
  <div class="supplementary-info">
    <cy-info-title>基础信息</cy-info-title>
    <basic-info :basic-info="basicInfo" />
    <cy-info-title type="collapse" name="补充信息">
      <add-info
        ref="addInfo"
        :add-info="addInfo"
        :has-guarantee-enterprise="hasGuaranteeEnterprise"
        :disabled="disabled"
      />
      <financial-situation
        v-if="!hasGuaranteeEnterprise"
        ref="financial"
        :has-guarantee-enterprise="hasGuaranteeEnterprise"
        :financial-info="addInfo"
        :disabled="disabled"
      />
      <business-situation
        v-if="!hasGuaranteeEnterprise"
        ref="business"
        :business-info="addInfo"
        :disabled="disabled"
      />
      <related-enterprises
        v-if="!hasGuaranteeEnterprise"
        ref="related"
        :related-info="addInfo"
        :disabled="disabled"
      />
    </cy-info-title>
    <template v-if="hasGuaranteeEnterprise">
      <cy-info-title type="collapse" name="担保企业基础信息">
        <basic-info :basic-info="guaranteeBasicInfo" />
      </cy-info-title>
      <cy-info-title type="collapse" name="担保企业补充信息">
        <guarantee-add-info
          ref="guaranteeAddInfo"
          :add-info="guaranteeAddInfo"
          :disabled="disabled"
        />
        <financial-situation
          ref="guaranteeFinancial"
          :has-guarantee-enterprise="hasGuaranteeEnterprise"
          :financial-info="guaranteeAddInfo"
          :disabled="disabled"
        />
        <business-situation
          ref="guaranteeBusiness"
          :business-info="guaranteeAddInfo"
          :disabled="disabled"
        />
        <related-enterprises
          ref="guaranteeRelated"
          :related-info="guaranteeAddInfo"
          :disabled="disabled"
        />
      </cy-info-title>
    </template>

    <AdditionalInfo ref="additionInfoRef" :detail-info="additionalInfo" :disabled="disabled" />
    <div v-if="!disabled" class="submit-btn">
      <el-button
        @click="
          () => {
            $router.push({ name: 'MyBusiness' });
          }
        "
      >取消</el-button>
      <el-button type="primary" @click="nextClick('temp')">暂存</el-button>
      <el-button type="primary" @click="nextClick('submit')">下一步</el-button>
    </div>
  </div>
</template>

<script>
import {
  getSupplementaryInfo,
  submitSupplementaryAddInfo
} from '@/api/myBusiness/supplementaryInfo'
export default {
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    AddInfo: () => import('./components/AddInfo'),
    GuaranteeAddInfo: () => import('./components/GuaranteeAddInfo'),
    FinancialSituation: () => import('./components/FinancialSituation'),
    BusinessSituation: () => import('./components/BusinessSituation'),
    RelatedEnterprises: () => import('./components/RelatedEnterprises'),
    AdditionalInfo: () => import('./components/AdditionalInfo')
  },
  data() {
    return {
      hasGuaranteeEnterprise: null,
      basicInfo: {},
      addInfo: {},
      guaranteeBasicInfo: {},
      guaranteeAddInfo: {},
      disabled: false,
      additionalInfo: {}
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      const { id: pid } = this.$route.params
      this.disabled = pid === 'see'
      const { id } = this.$route.query
      getSupplementaryInfo({ id }).then(({ data }) => {
        const { hasGuaranteeEnterprise, enterprise, guaranteeEnterprise } = data
        this.hasGuaranteeEnterprise = hasGuaranteeEnterprise
        const { corporateName, socialCreditCode, operatingRevenue, peopleEngaged } = enterprise
        // 基础信息
        this.basicInfo = {
          corporateName,
          socialCreditCode
        }
        // 补充信息
        this.addInfo = enterprise

        // 其他补充材料
        this.additionalInfo = {
          operatingRevenue,
          peopleEngaged
        }

        if (hasGuaranteeEnterprise) {
          // 担保企业基础信息
          this.guaranteeBasicInfo = {
            corporateName: guaranteeEnterprise.corporateName,
            socialCreditCode: guaranteeEnterprise.socialCreditCode
          }
          // 担保企业补充信息
          this.guaranteeAddInfo = guaranteeEnterprise
        }
      })
    },
    async nextClick(type) {
      // 主公司
      const addInfo = await this.$refs.addInfo.getParams(type)
      // 其他补充资料
      const additionInfo = await this.$refs.additionInfoRef.getParams(type)

      var parmas = {
        Loading: true,
        visible: type === 'submit',
        id: this.$route.query.id,
        enterprise: {
          ...addInfo,
          ...additionInfo
        }
      }
      if (this.hasGuaranteeEnterprise) {
        // 担保公司
        const guaranteeAddInfo = await this.$refs.guaranteeAddInfo.getParams(type)
        const guaranteeFinancial = await this.$refs.guaranteeFinancial.getParams(type)
        const guaranteeBusiness = await this.$refs.guaranteeBusiness.getParams(type)
        const guaranteeRelated = await this.$refs.guaranteeRelated.getParams(type)
        parmas.guaranteeEnterprise = {
          ...guaranteeAddInfo,
          ...guaranteeFinancial,
          ...guaranteeBusiness,
          ...guaranteeRelated
        }
      } else {
        // 主公司
        const financial = await this.$refs.financial.getParams(type)
        const business = await this.$refs.business.getParams(type)
        const related = await this.$refs.related.getParams(type)
        parmas.enterprise = {
          ...parmas.enterprise,
          ...financial,
          ...business,
          ...related
        }
      }
      submitSupplementaryAddInfo(parmas).then(() => {
        if (type === 'submit') {
          this.$message.success('提交成功')
          this.$router.push({
            name: 'MyBusiness'
            // params: { id: 'child' }
          })
        } else {
          this.$message.success('暂存成功')
          this.$router.push({
            name: 'MyBusiness'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.supplementary-info {
}
</style>
<style lang="scss">
.supplementary-info {
  .iconfont {
    font-size: 26px;
  }
}
</style>
