<template>
  <div>
    <BaseInfo
      ref="baseInfoRef"
      :disabled="$route.query.type !== 'add'"
      :details-info="baseInfo"
      @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,
  getDetails
} 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 {
      baseInfo: {},
      legalPersonInfo: {},
      companyInfo: {},
      contractInfo: [],
      authBookes: [], // 授权书
      enterpriseId: ''
    }
  },
  watch: {
    $route: {
      async handler(val) {
        if (val.query && val.query.id) {
          const res = await getDetails({ id: val.query.id })

          console.log('xxxxxxxxxxxxxxxxxx')
          console.log(res)
          // if (Object.keys(res.data.enterprise).length) {
          //   this.baseInfo.socialCreditCode = res.data.enterprise.socialCreditCode
          // }
          this.enterpriseId = res.data.enterprise.id
          this.baseInfo = {
            supplierInfoId: res.data.supplierInfoId,
            socialCreditCode: res.data.enterprise ? res.data.enterprise.socialCreditCode : ''
          }
          this.legalPersonInfo = res.data.financingFromInvestorsPerson
          this.contractInfo = res.data.financingFromInvestorsContracts
          this.authBookes = res.data.financingFromInvestorsEletronicContracts
          this.companyInfo = {
            registeredCapital: res.data.registeredCapital,
            businessLicense: res.data.fileStorageDO,
            bankAccount: res.data.bankAccount,
            bank: res.data.bank,
            operatingRevenue: res.data.operatingRevenue,
            peopleEngaged: res.data.peopleEngaged,
            businessAddress: res.data.businessAddress,
            registeredAddress: res.data.registeredAddress,
            industryTypeOne: res.data.industryTypeOne,
            industryTypeTwo: res.data.industryTypeTwo,
            industryTypeThree: res.data.industryTypeThree,
            industryTypeFore: res.data.industryTypeFore
          }
        }
      },
      immediate: true
    }
  },
  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 (this.$route.query.id) {
        reqParams.id = this.$route.query.id
      }

      if (this.$route.query.credit) {
        reqParams.credit = 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
      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>
