<template>
  <div class="auditing-main">
    <cy-info-title>基础信息</cy-info-title>
    <basic-info :details-info="detailsInfo.dueDiligenceAllocation" />
    <cy-info-title type="collapse" name="主企业审核">
      <div v-for="(item, index) in detailsInfo.enterprise.creditSituations" :key="index">
        <main-enterprise :ref="'mainEnterprise'+index" :details-info="item" :disabled="disabled" type-name="主企业审核" />
      </div>
    </cy-info-title>
    <cy-info-title type="collapse" name="企业商誉情况" style="margin-top: 20px">
      <corporate-goodwill ref="mainCorporate" :disabled="disabled" :details-info="detailsInfo.enterprise" />
    </cy-info-title>
    <cy-info-title v-if="detailsInfo.hasGuaranteeEnterprise" type="collapse" name="担保企业审核">
      <div v-for="(item, index) in detailsInfo.guaranteeEnterprise.creditSituations" :key="index">
        <main-enterprise :ref="'guaranteeEnterprise' + index" :details-info="item" type-name="担保企业审核" />
      </div>
    </cy-info-title>
    <cy-info-title v-if="detailsInfo.hasGuaranteeEnterprise" type="collapse" name="企业商誉情况">
      <corporate-goodwill ref="guaranteeCorporate" :details-info="detailsInfo.guaranteeEnterprise" />
    </cy-info-title>
    <cy-info-title>
      综合评估
      <div v-if="!disabled" slot="right">
        <el-button type="primary" @click="oneClickEvaluation">一键评估</el-button>
      </div>
    </cy-info-title>
    <assess :details-info="assessData" :has-guarantee-enterprise="detailsInfo.hasGuaranteeEnterprise" />
  </div>
</template>

<script>
import { getFirstReviewInfo, oneClickEvaluationSubmit } from '@/api/creditManage/supplierManage'
export default {
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    MainEnterprise: () => import('./components/MainEnterprise'),
    CorporateGoodwill: () => import('./components/CorporateGoodwill/index'),
    Assess: () => import('./components/Assess')
  },
  props: {
    deatailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  inject: {
    disabled: {
      form: 'disabled',
      default: false
    }
  },
  data() {
    return {
      detailsInfo: {
        dueDiligenceAllocation: {},
        enterprise: {
          creditSituations: []
        },
        guaranteeEnterprise: {
          creditSituations: []
        }
      },
      assessData: {}
    }
  },
  watch: {
    deatailsInfo: {
      handler(newV) {
        if (newV && Object.keys(newV)) {
          // const { name, socialCreditCode, riskManagerId, assignDate, cSupplierInfoId } = newV
          this.detailsInfo = newV
          this.assessData = newV.supplierApprovalFirst
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.getFirstReview()
  },
  methods: {
    getFirstReview() {
      const { id } = this.$route.params
      const { type } = this.$route.query
      if (!type) {
        getFirstReviewInfo({ id }).then(({ data }) => {
          this.detailsInfo = data
        })
      }
    },
    async getParams(type) {
      const { id } = this.$route.params
      const { taskId } = this.$route.query
      const { enterprise, historyId, guaranteeEnterprise } = this.detailsInfo
      var params = {
        taskId,
        id,
        historyId,
        enterprise: {
          id: enterprise.id,
          creditSituations: []
        }
      }
      enterprise.creditSituations.forEach(async(item, index) => {
        const mainEnterpriseData = await this.$refs[`mainEnterprise${index}`][0].validate(type)
        params.enterprise.creditSituations.push(mainEnterpriseData)
      })
      const mainCorporate = await this.$refs.mainCorporate.validate(type)
      params.enterprise = {
        ...params.enterprise,
        ...mainCorporate
      }

      if (this.detailsInfo.hasGuaranteeEnterprise) {
        params.guaranteeEnterprise = {
          id: guaranteeEnterprise.id,
          creditSituations: []
        }
        guaranteeEnterprise.creditSituations.forEach(async(item, index) => {
          const guaranteeEnterpriseData = await this.$refs[`guaranteeEnterprise${index}`][0].validate(type)
          params.guaranteeEnterprise.creditSituations.push(guaranteeEnterpriseData)
        })
        const guaranteeCorporate = await this.$refs.guaranteeCorporate.validate(type)
        // eslint-disable-next-line require-atomic-updates
        params.guaranteeEnterprise = Object.assign({}, params.guaranteeEnterprise, guaranteeCorporate)
      }
      return params
    },
    async oneClickEvaluation() {
      const data = await this.getParams()
      oneClickEvaluationSubmit({ Loading: true, ...data }).then(({ data }) => {
        this.assessData = data
      })
    }

  }

}
</script>

<style lang="scss" scoped>
.auditing-main {

}
</style>
