<template>
  <div class="project-supplement">
    <basic-info ref="baseInfo" :base-info="baseInfo" />
    <cy-info-title v-if="details.status==='c_project_and_enterprise_status_2' || details.status==='c_project_and_enterprise_status_4'" type="collapse" name="项目信息">
      <project-info ref="projectInfo" :project-info="projectInfo" :details="details" />
    </cy-info-title>
    <cy-info-title v-if="details.status==='c_project_and_enterprise_status_2'|| details.status==='c_project_and_enterprise_status_4'" type="collapse" name="本次合作相关">
      <cooperation ref="cooperation" :details="details" />
    </cy-info-title>
    <cy-info-title v-if="coreEnterpriseStatus==='c_project_and_enterprise_status_2' || coreEnterpriseStatus==='c_project_and_enterprise_status_4'" type="collapse" name="核心企业">
      <core-enterprise ref="coreEnterprise" :details="coreEnterprise" @isHistoricalCooperation="isHistoricalCooperation" />
    </cy-info-title>
    <cy-info-title v-if="hasHistory && (coreEnterpriseStatus==='c_project_and_enterprise_status_2'||coreEnterpriseStatus==='c_project_and_enterprise_status_4')" type="collapse" name="历史合作情况">
      <history-cooperation ref="historyCooperation" :details="historyInfo" />
    </cy-info-title>
    <div class="submit-btn">
      <el-button @click="() => { $router.push({ name: 'MyBusiness' }) }">取消</el-button>
      <el-button v-if="!$route.query.type" type="primary" @click="submitClick('temp')">暂存</el-button>
      <el-button v-if="!$route.query.type" type="primary" @click="submitClick('submit')">提交</el-button>
    </div>
  </div>
</template>

<script>
import { getProjectSupplementInfo, submitProjectSupplementInfo } from '@/api/myBusiness/projectInfo'
export default {
  name: 'MyBusinessProjectSupplement',
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    ProjectInfo: () => import('./components/ProjectInfo'),
    Cooperation: () => import('./components/Cooperation'),
    CoreEnterprise: () => import('./components/CoreEnterprise'),
    HistoryCooperation: () => import('./components/HistoryCooperation')
  },
  data() {
    return {
      hasHistory: 0,
      coreEnterpriseStatus: '',
      details: {},
      baseInfo: {},
      projectInfo: {},
      cooperation: {},
      coreEnterprise: {},
      historyInfo: {}
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      const { id } = this.$route.query
      getProjectSupplementInfo({ id }).then(({ data }) => {
        this.details = data
        const { supplierName, projectName, projectType, projectCoreEnterprise } = data

        this.baseInfo = { corporateName: supplierName }
        this.projectInfo = {
          projectName,
          projectType
        }

        // 核心企业
        const { enterprise: { corporateName }, historicalCooperation, status } = projectCoreEnterprise
        this.coreEnterprise = { ...projectCoreEnterprise, corporateName }
        this.hasHistory = historicalCooperation
        this.coreEnterpriseStatus = status

        // 历史
        this.historyInfo = projectCoreEnterprise
      })
    },
    isHistoricalCooperation(v) {
      this.hasHistory = v
    },
    async submitClick(submitType) {
      var postData = {
        Loading: true,
        visible: submitType === 'submit',
        id: this.$route.query.id

      }
      if (this.details.status === 'c_project_and_enterprise_status_2') {
        // 项目信息
        const project = await this.$refs.projectInfo.validate(submitType)
        // 合作相关
        const cooperationInfo = await this.$refs.cooperation.validate(submitType)

        postData = {
          ...postData,
          ...project,
          ...cooperationInfo
        }
      }

      // 核心企业状态为需要补充
      if (this.coreEnterpriseStatus === 'c_project_and_enterprise_status_2') {
        // 核心企业
        const enterprise = await this.$refs.coreEnterprise.validate(submitType)
        delete enterprise.corporateName

        if (this.hasHistory) {
          const historyInfo = await this.$refs.historyCooperation.validate(submitType)

          postData.projectCoreEnterprise = {
            ...enterprise,
            ...historyInfo
          }
        } else {
          postData.projectCoreEnterprise = {
            ...enterprise
          }
        }
      }

      submitProjectSupplementInfo(postData).then(() => {
        if (submitType === 'submit') {
          this.$message.success('提交成功')
          this.$router.push({
            name: 'MyBusiness'
          })
        } else {
          this.$message.success('暂存成功')
        }
      })
    }
  }
}
</script>

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

}
</style>
