<template>
  <div class="resolution-box" v-loading="isLoading">
    <cy-info-title>基础信息</cy-info-title>
    <basic-info :info="infoDetail" />
    <cy-info-title>授信信息</cy-info-title>
    <credit-info :info="infoDetail" @setResultInfo="setResultInfo" ref="ref1" />
    <cy-info-title style="margin-top: 10px;">放款条件</cy-info-title>
    <pay-condition :info="infoDetail" ref="ref2" />
    <cy-info-title>授信结论</cy-info-title>
    <resolution-result :info="infoDetail" ref="ref3" />
    <div class="submit-btn">
      <el-button @click="$router.go(-1)">取消</el-button>
      <el-button type="primary" @click="confirm('temp')">暂存</el-button>
      <el-button type="primary" @click="confirm('submit')">提交</el-button>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  name: 'CreditResolution',
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    CreditInfo: () => import('./components/CreditInfo'),
    PayCondition: () => import('./components/PayCondition'),
    ResolutionResult: () => import('./components/ResolutionResult')
  },
  data() {
    return {
      infoDetail: {
        projectInformation: {},
        coreEnterprise: {},
        supplierEnterprise: {}
      },
      isLoading: false
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    getDetail() {
      this.isLoading = true
      request({
        url: '/creditResolution/get',
        params: {
          id: this.$route.params.infoId
        },
        method: 'get'
      }).then(({ data }) => {
        this.infoDetail = { randomId: Math.random(), ...data }
        this.isLoading = false
      }, () => {
        this.isLoading = false
      })
    },
    setResultInfo(val) {
      if(val) {
        Object.keys(val).forEach(key => {
          this.infoDetail[key] = val[key]
        })
        this.infoDetail.randomId = Math.random()
      }
    },
    confirm(type) {
      let p1 = this.$refs.ref1.confirm(type)
      let p2 = this.$refs.ref2.confirm(type)
      let p3 = this.$refs.ref3.confirm(type)
      Promise.all([p1, p2, p3]).then(res => {
        let data = { ...res[0], ...res[1], ...res[2]}
        data.cProjectInformationId = this.infoDetail.cProjectInformationId
        data.taskId = this.$route.params.taskId
        data.id = this.$route.params.infoId
        data.Loading = true
        if(type === 'temp') {
          data.visible = 0
        }else {
          data.visible = 1
        }
        this.submit(type, data)
      }, () => {
        this.$message.error('必填项不能为空')
      })
    },
    submit(type, data) {
      request({
        url: '/creditResolution/add',
        data,
        method: 'post'
      }).then(() => {
        if(type === 'temp') {
          this.$message.success('暂存成功')
        }else {
          this.$message.success('提交成功')
        }
        this.$router.go(-1)
      }, () => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
