<template>
  <div v-loading="loading" class="company-box">
    <cy-info-title type="collapse" name="核心企业">
      <core-enterprise ref="core" :details="coreEnterpriseInfo" @isHistoricalCooperation="isHistoricalCooperation" />
    </cy-info-title>
    <cy-info-title v-if="hasHistory" type="collapse" name="历史合作情况">
      <history-cooperation ref="history" :details="historyInfo" />
    </cy-info-title>
    <cy-info-title type="collapse" name="核心企业审核">
      <auditing ref="audit" :details="auditInfo" />
    </cy-info-title>
    <cy-info-title>
      综合评估
      <div slot="right">
        <el-button type="primary" @click="assessClick('assess')">一键评估</el-button>
      </div>
    </cy-info-title>
    <assess :assess-info="assessResult" />
  </div>
</template>

<script>
import { getCoreReviewInfo, assessCore, auditSubmitCore } from '@/api/creditManage/projectManage'
export default {
  components: {
    CoreEnterprise: () => import('./CoreEnterprise'),
    HistoryCooperation: () => import('./HistoryCooperation'),
    Auditing: () => import('./Auditing'),
    Assess: () => import('./Assess')
  },
  provide() {
    return {
      disabled: false,
      loading: false
    }
  },
  props: {
    basicInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      hasHistory: 0,
      detailsInfo: {},
      coreEnterpriseInfo: {},
      historyInfo: {},
      auditInfo: {},
      assessResult: {}
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      this.loading = true
      const { id } = this.basicInfo
      getCoreReviewInfo({ id }).then(({ data }) => {
        this.loading = false
        this.detailsInfo = data
        // 核心企业信息
        const { projectCoreEnterprise, enterpriseNature, regStocks, involvingLitigationAccessCcount, administrativePenaltyAccessCcount, administrativePenaltyAmount, environmentalPenaltyAmount, privateLendings, equityFreezeDataList, majorAnomalies, seriousIllegalData, taxOweNoticeData, taxIllegalData, blacklists, exceptionData, news, projectCoreEnterpriseComprehensiveEvaluations, evaluationOpinions, involvingLitigationAccess, administrativePenaltyAccess, administrativePenaltyAmountAccess, environmentalPenaltyAmountAccess, holdingRatio, equityHierarchy, equityHierarchyDetail } = data
        const { id,
          cEnterpriseHistoryId,
          cProjectInformationId,
          enterprise,
          centralizedProcurementPlatform,
          historicalCooperation,
          historicalCooperationDuration,
          historicalCooperationInvoicingAmount,
          historicalCooperationMaximumAccountingPeriod,
          historicalCooperationScale,
          historicalCooperationTimes,
          projectCoreEnterpriseFiles } = projectCoreEnterprise
        const { corporateName } = enterprise
        this.coreEnterpriseInfo = {
          id,
          cEnterpriseHistoryId,
          cProjectInformationId,
          corporateName,
          centralizedProcurementPlatform,
          historicalCooperation
        }
        this.hasHistory = historicalCooperation

        // 历史合作情况
        this.historyInfo = {
          historicalCooperationDuration,
          historicalCooperationInvoicingAmount,
          historicalCooperationMaximumAccountingPeriod,
          historicalCooperationScale,
          historicalCooperationTimes,
          projectCoreEnterpriseFiles
        }

        // 核心企业审核
        this.auditInfo = { enterpriseNature, regStocks, involvingLitigationAccessCcount, administrativePenaltyAccessCcount, administrativePenaltyAmount, environmentalPenaltyAmount, privateLendings, equityFreezeDataList, majorAnomalies, seriousIllegalData, taxOweNoticeData, taxIllegalData, blacklists, exceptionData, news, evaluationOpinions, involvingLitigationAccess, administrativePenaltyAccess, administrativePenaltyAmountAccess, environmentalPenaltyAmountAccess, holdingRatio, equityHierarchy, equityHierarchyDetail }

        this.assessResult = projectCoreEnterpriseComprehensiveEvaluations
      })
    },

    async assessClick(type) {
      let projectCoreEnterprise = await this.$refs.core.passData(type)

      // 有历史合作
      if (this.hasHistory) {
        const historyInfo = await this.$refs.history.passData(type)
        projectCoreEnterprise = {
          ...projectCoreEnterprise,
          ...historyInfo
        }
      }

      const auditInfo = await this.$refs.audit.passData(type)

      const postData = {
        Loading: true,
        projectCoreEnterprise,
        ...auditInfo
      }

      if (type === 'assess') {
        assessCore(postData).then((res) => {
          if (res.success) {
            this.assessResult = res.data
            if (res.data.admitted === '不准入') {
              let errorStr = ''
              if (res.data.errors.length) {
                res.data.errors.map(item => {
                  errorStr += item + '!'
                })
                const msg = errorStr.substring(0, errorStr.length - 1)
                this.$message.warning({ message: msg, duration: 3500 })
              }
            } else {
              this.$message.success('评估成功')
            }
          } else {
            this.$message.warning(res.msg)
          }
        })
      } else if (type === 'submit' || type === 'temp') {
        postData.taskId = this.basicInfo.taskId
        postData.id = this.detailsInfo.id
        if (type === 'temp') {
          postData.visible = 0
        } else {
          postData.visible = 1
        }
        auditSubmitCore(postData).then((res) => {
          if (res.success) {
            if (type === 'temp') {
              this.$message.success('暂存成功')
            } else {
              this.$message.success('提交成功')
            }
            this.$router.push({
              name: 'ProjectCredit'
            })
          } else {
            this.$message.warning(res.msg)
          }
        })
      }
    },
    isHistoricalCooperation(v) {
      this.hasHistory = v
    }
  }
}
</script>

<style lang="scss" scoped>
.company-box {

}
</style>
