<template>
  <div v-loading="loading" class="project-box">
    <cy-info-title type="collapse" name="项目信息">
      <project-info ref="project" :project-information="projectInfo" />
    </cy-info-title>
    <cy-info-title type="collapse" name="本次合作相关">
      <cooperation ref="cooperate" :cooperation="cooperation" />
    </cy-info-title>
    <cy-info-title type="collapse" name="项目业主方审核">
      <project-owner ref="owner" :owner-info="ownerInfo" />
    </cy-info-title>
    <cy-info-title>
      综合评估
      <div slot="right">
        <el-button type="primary" @click="assessClick('assess')">一键评估</el-button>
      </div>
    </cy-info-title>
    <assess ref="assess" :assess-info="assessResult" />
  </div>
</template>

<script>
import { getReviewInfo, assessProject, auditSubmitProject } from '@/api/creditManage/projectManage'
export default {
  components: {
    ProjectInfo: () => import('./ProjectInfo'),
    Cooperation: () => import('./Cooperation'),
    ProjectOwner: () => import('./ProjectOwner'),
    Assess: () => import('./Assess')
  },
  provide() {
    return {
      disabled: false,
      loading: false
    }
  },
  props: {
    basicInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      detailsInfo: {},
      projectInfo: {},
      cooperation: {},
      ownerInfo: {},
      assessResult: {}
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      this.loading = true
      const { id } = this.basicInfo
      getReviewInfo({ id }).then(({ data }) => {
        this.detailsInfo = data
        this.loading = false
        const { projectInformation, enterpriseNature, regStocks, involvingLitigationAccessCcount, administrativePenaltyAccessCcount, administrativePenaltyAmount, environmentalPenaltyAmount, involvingLitigationAccess, administrativePenaltyAccess, administrativePenaltyAmountAccess, environmentalPenaltyAmountAccess, privateLendings, equityFreezeDataList, majorAnomalies, seriousIllegalData, taxOweNoticeData, taxIllegalData, blacklists, exceptionData, news, projectOwnerComprehensiveEvaluation, evaluationOpinions, holdingRatio, equityHierarchy, equityHierarchyDetail } = data
        // 项目信息
        this.projectInfo = projectInformation

        // 本次合作
        const { cooperationAmount, peakValue, estimatedAccountingPeriod, highPeakUsage, enterprise, companyForProject } = projectInformation
        this.cooperation = { cooperationAmount, peakValue, estimatedAccountingPeriod, highPeakUsage }

        // 项目业主
        this.ownerInfo = { ...data, ...enterprise, companyForProject, enterpriseNature, regStocks, involvingLitigationAccessCcount, administrativePenaltyAccessCcount, administrativePenaltyAmount, environmentalPenaltyAmount, involvingLitigationAccess, administrativePenaltyAccess, administrativePenaltyAmountAccess, environmentalPenaltyAmountAccess, privateLendings, equityFreezeDataList, majorAnomalies, seriousIllegalData, taxOweNoticeData, taxIllegalData, blacklists, exceptionData, news, projectOwnerComprehensiveEvaluation, evaluationOpinions, holdingRatio, equityHierarchy, equityHierarchyDetail }
      })
    },

    async assessClick(type) {
      const projectInformation = await this.$refs.project.passData(type)
      const cooperationData = await this.$refs.cooperate.passData(type)
      const ownerData = await this.$refs.owner.passData(type)

      const { enterprise, companyForProject } = ownerData
      const postData = {
        Loading: true,
        projectInformation: {
          ...projectInformation,
          ...cooperationData,
          enterprise,
          companyForProject
        },
        ...ownerData,
        id: this.detailsInfo.id
      }
      delete postData.enterprise
      delete postData.companyForProject

      if (type === 'assess') {
        assessProject(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
        if (type === 'temp') {
          postData.visible = 0
        } else {
          postData.visible = 1
        }
        auditSubmitProject(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)
          }
        })
      }
    }
  }
}
</script>

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

}
</style>
