<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-form-item label="融资方式">{{ dict.getLabel("financingDemandApplyType", detail.applyType) }}
              </el-form-item>
              <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="企业主体" v-if="detail.applyType==1">
                  <el-row type="flex">
                    {{ detail.enterpriseName }}
                    <el-link type="primary" @click="isShow=true">企业详情</el-link>
                  </el-row>
                </el-form-item>
                <el-form-item v-else/>
                <el-form-item label="联系人">
                  <el-row type="flex">
                    <div v-text="detail.name"/>
                    <person-credit-report :person="detail" :instance="instance" :dict="dict"/>
                  </el-row>
                </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>
        <ai-dialog class="dialog" :title="detail.enterpriseName||'企业详情'" :visible.sync="isShow" width="1200px"
                   customFooter>
          <enterprise-dialog v-if="detail.enterpriseId" :enterprise-id="detail.enterpriseId" :instance="instance"
                             :dict="dict"/>
          <template #footer>
            <el-button @click="isShow=false">关闭</el-button>
          </template>
        </ai-dialog>
      </template>
    </ai-detail>
  </section>
</template>

<script>
import {mapState} from "vuex";
import EnterpriseDialog from "../../../components/enterpriseDialog";
import PersonCreditReport from "../../../components/personCreditReport";

export default {
  name: "needsDetail",
  components: {PersonCreditReport, EnterpriseDialog},
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
  },
  computed: {
    ...mapState(['user']),
    isFinanceUser() {
      return !!this.user.financeUser?.id
    }
  },
  data() {
    return {
      detail: {},
      info: {},
      isShow: false,
    }
  },
  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({})
    }
  },
  created() {
    this.dict.load("productRepaymentTimeline", "financialFundPurpose", "financingDemandApplyType", "enterpriseType", "dishonestPersonSituation", "administrativeSanctionType").then(() => {
      this.getDetail()
    })
  }
}
</script>

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

  .flexWrap {
    flex-wrap: wrap;

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

      .el-link {
        margin-left: 16px;
      }
    }
  }

}
</style>
