<template>
  <div class="capital-manage-details">
    <basic-info :details-info="detailsInfo" />
    <credit-basic-info :details-info="detailsInfo"></credit-basic-info>
    <credit-info
      ref="creditInfoRef"
      :details-info="detailsInfo"
      @capitalFinancingTermBlur="capitalFinancingTermBlur"
    ></credit-info>
    <letters-info
      ref="lettersInfoRef"
      :details-info="detailsInfo"
      @financingRateBlur="financingRateBlur"
    ></letters-info>
    <rate-config
      ref="rateConfigRef"
      :details-info="detailsInfo"
      :financing-rate="financingRate || detailsInfo.financingRate"
    ></rate-config>
    <div class="submit-btn">
      <el-button>关闭</el-button>
      <el-button type="primary" @click="submit('temp')">暂存</el-button>
      <el-button type="primary" @click="submit('submit')">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getDetails, submitData } from '@/api/capitalManage/index'
export default {
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    CreditBasicInfo: () => import('./components/CreditBasicInfo.vue'),
    CreditInfo: () => import('./components/CreditInfo.vue'),
    LettersInfo: () => import('./components/LettersInfo.vue'),
    RateConfig: () => import('./components/RateConfig.vue')
  },
  data() {
    return {
      detailsInfo: {},
      financingRate: ''
    }
  },
  created() {
    this.getDetailsList()
  },
  methods: {
    getDetailsList() {
      const { id } = this.$route.params
      getDetails({ id }).then(({ data }) => {
        this.detailsInfo = data
      })
    },
    capitalFinancingTermBlur(val) {
      if (this.detailsInfo.businessTypeNo === 'M3A') {
        this.$refs.lettersInfoRef.ruleForm.financingTerm = val
      } else {
        this.$refs.lettersInfoRef.ruleForm.financingTerm =
          val * 1 - 30 < 0 ? 0 : val * 1 - 30
      }
    },
    financingRateBlur(val) {
      this.financingRate = Number(val)
    },
    async  submit(type) {
      const creditInfo = await this.$refs.creditInfoRef.getParams(type)
      const lettersInfo = await this.$refs.lettersInfoRef.getParams(type)
      const rateConfig = await this.$refs.rateConfigRef.getParams(type)
      const params = {
        Loading: true,
        ...this.detailsInfo,
        ...creditInfo,
        ...lettersInfo,
        enterpriseRateAllocations: rateConfig,
        flag: type === 'temp' ? 1 : 0
      }
      submitData(params).then(() => {
        if (type === 'temp') {
          this.$message.success('暂存成功')
        } else {
          this.$message.success('提交成功')
        }
        this.$router.go(-1)
      })
    }
  }
}
</script>

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