<template>
  <section class="needsDetail">
    <ai-detail>
      <ai-title slot="title" title="融资详情" isShowBottomBorder isShowBack @onBackClick="back">
        <template #rightBtn>
          <el-button v-if="isFinanceUser" type="primary" @click="handleGrab">抢单</el-button>
          <el-button v-else type="danger" @click="handleCancel">取消发布</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.name }}</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>
        </el-form>
      </template>
    </ai-detail>
  </section>
</template>

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

export default {
  name: "needsDetail",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    isFinanceUser() {
      return !!this.user.financeUser?.id
    }
  },
  data() {
    return {
      detail: {},
    }
  },
  methods: {
    getDetail() {
      let {id} = this.$route.query
      this.instance.post("/appfinancingdemand/queryDetailById", null, {
        params: {id}
      }).then(res => {
        if (res?.data) {
          this.detail = res.data
        }
      })
    },
    handleGrab() {
      this.instance.post("/appfinancingdemand/obtain", null, {
        params: {id: this.detail.id}
      }).then(res => {
        if (res.code == 0) {
          this.$message.success("抢单成功!")
          this.$router.push({name: "27338cb83e77461dbd44356a6760df86"})
        }
      })
    },
    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)
    },
    back() {
      this.$router.push({query: {}})
    }
  },
  created() {
    this.getDetail()
  }
}
</script>

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

  .flexWrap {
    flex-wrap: wrap;

    .el-form-item {
      width: 50%;
    }
  }
}
</style>
