<template>
  <div>
    <BaseInfo
      ref="baseInfoRef"
      :disabled="$route.query.type !== 'add'"
      @enterpriseInfoBack="enterpriseInfoBack"
      @supplierCallBack="supplierCallBack"
    ></BaseInfo>
    <LegalPerson
      ref="legalPersonRef"
      :details-info="legalPersonInfo"
      :enterprise-id="enterpriseId"
      :disabled="$route.query.type === 'see'"
    ></LegalPerson>
    <CompanyInfo
      ref="companyInfoRef"
      :details-info="companyInfo"
      :disabled="$route.query.type === 'see'"
    ></CompanyInfo>
    <ContractFile
      ref="contractFileRef"
      :details-info="contractInfo"
      :enterprise-id="enterpriseId"
      :disabled="$route.query.type === 'see'"
    ></ContractFile>
    <AuthBook
      v-if="authBookes.length"
      ref="authBookRef"
      :details-info="authBookes"
      :disabled="$route.query.type === 'see'"
    ></AuthBook>
    <div class="submit-btn">
      <el-button
        @click="
          () => {
            $router.go(-1)
          }
        "
      >取消</el-button>
      <el-button type="primary" @click="nextClick('temp')">暂存</el-button>
      <el-button type="primary" @click="nextClick('submit')">提交</el-button>
    </div>
  </div>
</template>

<script>
import { addOrUpdateCapitalAuth, makeCreditContract } from '@/api/capitalFinancialManage/index'

export default {
  components: {
    BaseInfo: () => import('./components/BaseInfo.vue'),
    LegalPerson: () => import('./components/LegalPersonInfo.vue'),
    CompanyInfo: () => import('./components/CompanyInfo.vue'),
    ContractFile: () => import('./components/ContractFile.vue'),
    AuthBook: () => import('./components/AuthBook.vue')
  },
  data() {
    return {
      legalPersonInfo: {},
      companyInfo: {},
      contractInfo: [],
      authBookes: [], // 授权书
      enterpriseId: ''
    }
  },
  methods: {
    async nextClick(type) {
      const baseInfoParams = await this.$refs.baseInfoRef.getParams(type)
      const legalPersonParams = await this.$refs.legalPersonRef.getParams(type)
      const companyInfoParams = await this.$refs.companyInfoRef.getParams(type)
      const contractFileParams = await this.$refs.contractFileRef.getParams(type)
      const authBookParams = await this.$refs.authBookRef.getParams(type)

      const reqParams = {
        ...baseInfoParams,
        financingFromInvestorsPerson: legalPersonParams,
        ...companyInfoParams,
        financingFromInvestorsContracts: contractFileParams,
        financingFromInvestorsEletronicContracts: authBookParams,
        Loading: true
      }

      if (type === 'temp') {
        reqParams.visible = false
      } else {
        reqParams.visible = true
      }

      console.log(reqParams)
      const res = await addOrUpdateCapitalAuth(reqParams)

      if (res.success) {
        const msg = type === 'temp' ? '暂存成功' : '新增成功'
        this.$message.success(msg)
        this.$router.go(-1)
      }
    },
    // 基础信息选择供应商后的回调，查询授权书
    supplierCallBack(supplierId) {
      console.log(supplierId)
      if (supplierId) {
        this.queryAuthBooks(supplierId)
      }
    },
    //
    enterpriseInfoBack(enterprise, info) {
      console.log('enterprise------------')
      console.log(enterprise)

      this.enterpriseId = info.cEnterpriseId
      this.legalPersonInfo = enterprise.creditInvestigationInfo
      this.contractInfo = info.cSalesDocumentsInfoHistories || []
      this.companyInfo = enterprise
    },
    // 查询授权书
    async queryAuthBooks(supplierId) {
      const res = await makeCreditContract({ supplierId })

      this.authBookes = res.data
    }
  }
}
</script>

<style lang="sass" scoped></style>
