<template>
  <div class="project-info">
    <cy-info-title>供应商信息</cy-info-title>
    <basic-info ref="baseInfo" :supplier-info="supplierInfo" />
    <project ref="projectInfo" />
    <cy-info-title type="collapse" name="核心企业">
      <!-- <core-enterprise ref="coreEnterprise" /> -->
      <enterprise-inspection ref="enterpriseInspection" :inspection-data="inspectionData" class="enterprise-content" :source="'projectAdd'" :placeholder="'本次拟合作下游买方'" />
    </cy-info-title>
    <div class="submit-btn">
      <el-button type="primary" @click="nextClick">下一步</el-button>
    </div>
  </div>
</template>

<script>
import { addProjectForSupplier } from '@/api/myBusiness/projectInfo'
export default {
  name: 'MyBusinessProjectInfo',
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    Project: () => import('./components/Project'),
    // CoreEnterprise: () => import('./components/CoreEnterprise'),
    EnterpriseInspection: () => import('../components/EnterpriseInspection')
  },
  data() {
    return {
      supplierInfo: {
        enterpriseName: ''
      },
      inspectionData: {
        company: {
          key: 'company',
          name: '核心企业名称'
        }
      }
    }
  },
  created() {
    this.supplierInfo.enterpriseName = this.$route.query.enterpriseName
  },
  methods: {

    async nextClick() {
      const cSupplierInfoId = this.$route.query.id
      // const baseInfo = await this.$refs.baseInfo.validate()
      const projectInfo = await this.$refs.projectInfo.passData()
      // const projectCoreEnterprise = await this.$refs.coreEnterprise.validate()
      const enterprise = await this.$refs.enterpriseInspection.validate()

      const projectCoreEnterprise = {
        enterprise
      }

      addProjectForSupplier({ Loading: true, cSupplierInfoId, ...projectInfo, projectCoreEnterprise }).then(({ data }) => {
        const { status, id } = data
        if (status === 'c_project_and_enterprise_status_1') {
          let errorStr = ''
          if (data.enterprise?.errors.length) {
            data.enterprise.errors.map(item => {
              errorStr += item + ';'
            })
          }
          if (data.projectCoreEnterprise?.enterprise?.errors.length) {
            data.projectCoreEnterprise.enterprise.errors.map(item => {
              errorStr += item + ';'
            })
          }

          if (errorStr !== '') {
            this.$message.warning({ message: errorStr, duration: 5000 })
          }

          this.$router.push({
            name: 'MyBusinessError',
            params: {
              id: 'child'
            },
            query: {
              status: status
            }
          })
        } else if (status === 'c_project_and_enterprise_status_2') { // 补充信息
          this.$router.push({
            name: 'MyBusinessProjectSupplement',
            params: {
              id: 'child'
            },
            query: {
              status: status,
              id
            }
          })
        } else {
          this.$router.push({ name: 'MyBusiness' })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.project-info {
.enterprise-content {
  padding: 10px 20px;
}
}
</style>
