<template>
  <div class="resolution-box" v-loading="isLoading">
    <cy-up-down-drag>
      <div slot="top">
        <cy-steps :stepsData="stepList"></cy-steps>
        <el-collapse v-if="stepList.length" v-model="activeNames">
          <el-collapse-item v-for="(item, index) in stepList" :key="index" :name="index">
            <template slot="title">
              <cy-view-bar
                :view-bar-data="{ ...item.businessProcessingLogVo, formBusinessName: item.formBusinessName, index: index + 1 }">
              </cy-view-bar>
            </template>
            <template v-if="item.formBusinessName == '授信决议' || item.formBusinessName == '授信决议确认'">
              <cy-info-title>基础信息</cy-info-title>
              <basic-info :info="item.obj" />
              <cy-info-title>授信信息</cy-info-title>
              <credit-info :info="item.obj" :isView="true"></credit-info>
              <cy-info-title>放款条件</cy-info-title>
              <pay-condition :info="item.obj" :isView="true" />
              <cy-info-title>授信结论</cy-info-title>
              <resolution-result :info="item.obj" :isView="true" />
            </template>
            <template v-if="item.formBusinessName == '授信决议确认'">
              <cy-info-title>授信决议</cy-info-title>
              <confirm :info="item.obj" :isView="true" />
            </template>
            <template v-if="item.formBusinessName == '复议调整申请'">
              <cy-info-title>复议调整</cy-info-title>
              <update-info :info="item.obj" :isView="true" />
            </template>
            <template v-if="item.formBusinessName == '复议调整审批'">
              <cy-info-title>审批结论</cy-info-title>
              <audit-info :info="item.obj" :isView="true" />
            </template>
          </el-collapse-item>
        </el-collapse>
      </div>
      <cy-info-title slot="bar">授信决议确认</cy-info-title>
      <div slot="bottom">
        <cy-info-title>基础信息</cy-info-title>
        <basic-info :info="infoDetail" />
        <cy-info-title>授信信息</cy-info-title>
        <credit-info :info="infoDetail" @setResultInfo="setResultInfo" ref="ref1" :isConfirm="1" />
        <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" :isConfirm="1" />
        <cy-info-title>授信决议</cy-info-title>
        <confirm ref="ref4" />
        <div class="submit-btn">
          <el-button type="primary" @click="confirm">提交</el-button>
          <el-button @click="$router.go(-1)">关闭</el-button>
        </div>
      </div>
    </cy-up-down-drag>
  </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'),
    Confirm: () => import('./components/Confirm'),
    UpdateInfo: () => import('./components/UpdateInfo'),
    AuditInfo: () => import('./components/AuditInfo')
  },
  data() {
    return {
      infoDetail: {
        projectInformation: {},
        coreEnterprise: {},
        supplierEnterprise: {}
      },
      activeNames: [],
      stepList: [],
      isLoading: false
    }
  },
  mounted() {
    this.getDetail()
    this.getStepList()
  },
  methods: {
    getStepList() {
      this.isLoading = true
      request({
        url: '/businessProcessingTask/doBusinessHistoryView',
        method: 'get',
        params: { businessKey: this.$route.params.infoId, businessType: 'CreditResolution' }
      }).then(({ data }) => {
        data.forEach(item => {
          item.obj.loanConditionsList = item.obj.loanConditionsHistoryList || []
          item.obj.randomId = Math.random()
        })
        this.stepList = data || []
        this.isLoading = false
      }, () => {
        this.isLoading = false
      })
    },
    getDetail() {
      request({
        url: '/creditResolution/get',
        params: {
          id: this.$route.params.infoId
        },
        method: 'get'
      }).then(({ data }) => {
        this.infoDetail = { randomId: Math.random(), ...data }
      })
    },
    setResultInfo(val) {
      if(val) {
        Object.keys(val).forEach(key => {
          this.infoDetail[key] = val[key]
        })
        this.infoDetail.randomId = Math.random()
      }
    },
    confirm() {
      let p1 = this.$refs.ref1.confirm()
      let p2 = this.$refs.ref2.confirm()
      let p3 = this.$refs.ref3.confirm()
      let p4 = this.$refs.ref4.confirm()
      Promise.all([p1, p2, p3, p4]).then(res => {
        let data = { ...res[0], ...res[1], ...res[2], ...res[3]}
        data.cProjectInformationId = this.infoDetail.cProjectInformationId
        data.taskId = this.$route.params.taskId
        data.id = this.$route.params.infoId
        this.submit(data)
      }, () => {
        this.$message.error('必填项不能为空')
      })
    },
    submit(data) {
      this.isLoading = true
      request({
        url: '/creditResolution/confirm/add',
        data,
        method: 'post'
      }).then(() => {
        this.isLoading = false
        this.$message.success('提交成功')
        this.$router.go(-1)
      }, () => {
        this.isLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
