<template>
  <section class="grabDetail">
    <ai-detail>
      <ai-title slot="title" title="融资详情" isShowBottomBorder isShowBack @onBackClick="back">
        <template v-if="isFinanceUser&&detail.status==0" #rightBtn>
          <el-button type="danger" @click="handleCancel">回退订单</el-button>
          <el-button type="primary" @click="dialog=true">放款</el-button>
        </template>
      </ai-title>
      <template #content>
        <el-form size="small" label-width="160px">
          <ai-card title="融资需求">
            <template #content>
              <el-row type="flex" class="flexWrap">
                <el-form-item label="意向金额">{{ detail.loanAmount }}</el-form-item>
                <el-form-item label="期望使用期限">
                  {{ dict.getLabel('productRepaymentTimeline', detail.hopeLifespan) }}
                </el-form-item>
                <el-form-item label="资金用途">
                  {{ dict.getLabel('financialFundPurpose', detail.fundPurpose) }}
                </el-form-item>
                <el-form-item label="企业主体">{{ detail.enterpriseName }}</el-form-item>
                <el-form-item label="联系人">{{ detail.createUserName }}</el-form-item>
                <el-form-item label="所在地区">{{ detail.areaName }}</el-form-item>
                <el-form-item label="联系方式">{{ detail.phone }}</el-form-item>
                <el-form-item label="身份证号">{{ detail.idNumber }}</el-form-item>
                <el-form-item label="发布时间">{{ detail.createTime }}</el-form-item>
              </el-row>
              <el-form-item label="备注">{{ detail.remark }}</el-form-item>
            </template>
          </ai-card>
          <ai-card title="融资进度">
            <template #content>
              <el-row type="flex" class="flexWrap">
                <el-form-item label="融资进度">{{ dict.getLabel('financingDemandStatus', detail.status) }}</el-form-item>
                <el-form-item label="贷款银行">{{ detail.organizationName }}</el-form-item>
                <el-form-item label="贷款经理">{{ detail.auditUserName }}</el-form-item>
                <el-form-item label="联系方式">{{ detail.phone }}</el-form-item>
                <template v-if="detail.status==2">
                  <el-form-item label="放款金额">{{ detail.auditAmount }}</el-form-item>
                  <el-form-item label="放款日期">{{ detail.loanDate }}</el-form-item>
                  <el-form-item label="使用期限">
                    {{ dict.getLabel('productRepaymentTimeline', detail.auditLifespan) }}
                  </el-form-item>
                </template>
              </el-row>
            </template>
          </ai-card>
        </el-form>
      </template>
    </ai-detail>
    <ai-dialog :visible.sync="dialog" title="放款" @closed="form={}" @onConfirm="submitAudit" width="560px">
      <el-form :model="form" :rules="rules" ref="AuditForm" size="small" label-width="120px">
        <el-form-item label="放款金额(万)" prop="auditAmount">
          <el-input v-model="form.auditAmount" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="使用期限" prop="auditLifespan">
          <ai-select v-model="form.auditLifespan" :selectList="dict.getDict('productRepaymentTimeline')"/>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker v-model="form.loanDate" placeholder="请选择"/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "grabDetail",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    isFinanceUser() {
      return !!this.user.financeUser?.id
    }
  },
  data() {
    return {
      dialog: false,
      detail: {},
      form: {},
      rules: {
        auditAmount: [{required: true, message: "请输入 放款金额"}],
        auditLifespan: [{required: true, message: "请选择 使用期限"}],
        loanDate: [{required: true, message: "请选择 放款日期"}],
      }
    }
  },
  methods: {
    getDetail() {
      let {id} = this.$route.query
      this.instance.post("/appfinancingdemand/queryDetailById", null, {
        params: {id}
      }).then(res => {
        if (res?.data) {
          this.detail = res.data
        }
      })
    },
    handleCancel() {
      this.$confirm("是否要回退本次融资需求订单?").then(() => {
        this.instance.post("/appfinancingdemand/cancelObtain", null, {
          params: {id: this.detail.id}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("回退成功")
            this.back()
          }
        })
      }).catch(() => 0)
    },
    submitAudit() {
      this.$refs.AuditForm.validate(v => {
        if (v) {
          let {id} = this.detail
          this.instance.post("/appfinancingdemand/confirmLoan", null, {
            params: {id, ...this.form}
          }).then(res => {
            if (res?.code == 0) {
              this.dialog = false
              this.$message.success("提交成功!")
              this.getDetail()
            }
          })
        }
      })
    },
    back() {
      this.$router.push({})
    }
  },
  created() {
    this.getDetail()
  }
}
</script>

<style lang="scss" scoped>
.grabDetail {
  height: 100%;

  .flexWrap {
    flex-wrap: wrap;

    .el-form-item {
      width: 50%;
    }
  }

  .el-date-editor {
    width: 100%;
  }

}
</style>
