<template>
  <section class="productDetail">
    <ai-detail>
      <ai-title slot="title" title="金融产品详情" isShowBottomBorder isShowBack @onBackClick="$router.push({query:{}})">
        <template #rightBtn>
          <el-button v-if="isAdmin&&detail.status==0" type="primary" @click="dialog=true">审核</el-button>
        </template>
      </ai-title>
      <template #content>
        <el-form v-if="detail.id" size="small" label-width="120px">
          <ai-card title="基本信息">
            <template #content>
              <el-row type="flex" class="flexWrap">
                <el-form-item class="half" label="产品名称">{{ detail.productName }}</el-form-item>
                <el-form-item class="half" label="状态">{{ dict.getLabel('productStatus', detail.status) }}</el-form-item>
                <el-form-item class="half" label="贷款额度(万)">
                  {{ [detail.loanMin, detail.loanMax].join(" ~ ") }}
                </el-form-item>
                <el-form-item class="half" label="参考利率(%)">
                  {{ [detail.interestRateMin, detail.interestRateMax].join(" ~ ") }}
                </el-form-item>
                <el-form-item class="half" label="还款期限">
                  {{ dict.getLabel('productRepaymentTimeline', detail.repaymentTimeline) }}
                </el-form-item>
                <el-form-item class="half" label="还款方式">
                  {{ dict.getLabel('productRepaymentMethod', detail.repaymentMethod) }}
                </el-form-item>
              </el-row>
              <el-form-item label="担保方式">{{ detail.guaranteeModeLabel }}</el-form-item>
              <el-row type="flex" class="flexWrap">
                <el-form-item label="客户经理姓名" class="half">{{ detail.bankCounterpart }}</el-form-item>
                <el-form-item label="客户经理电话" class="half">{{ detail.consultationTelephone }}</el-form-item>
              </el-row>
              <el-form-item label="企业微信二维码" v-if="detail.qrCodeUrl">
                <el-image class="qrCode" :src="detail.qrCodeUrl" :preview-src-list="[detail.qrCodeUrl]"/>
              </el-form-item>
            </template>
          </ai-card>
          <ai-card title="适用范围">
            <template #content>
              <el-form-item label="适用范围">{{ detail.areaName }}</el-form-item>
              <el-form-item label="面向对象">{{ detail.faceUserLabel }}</el-form-item>
            </template>
          </ai-card>
          <ai-card title="产品信息">
            <template #content>
              <el-form-item label="申请条件">{{ detail.applyCondition }}</el-form-item>
              <el-form-item label="所需材料">{{ detail.needMaterial }}</el-form-item>
              <el-form-item label="产品特色">{{ detail.productFeatures }}</el-form-item>
            </template>
          </ai-card>
          <ai-card title="办理渠道">
            <template #content>
              <el-form-item label="业务办理渠道">{{ dict.getLabel('financialProductChannels', detail.handlingChannels) }}</el-form-item>
              <el-form-item label="关联贷款联审" v-if="detail.handlingChannels==1">{{ detail.zwspName }}</el-form-item>
            </template>
          </ai-card>
          <ai-card title="审核结果" v-if="!isAuthing">
            <template #content>
              <el-row type="flex" class="flexWrap">
                <el-form-item class="half" label="审核结果">
                  {{ dict.getLabel("productStatus", detail.status) }}
                </el-form-item>
                <el-form-item class="half" label="审核人">{{ detail.auditUserName }}</el-form-item>
                <el-form-item class="half" label="审核时间">{{ detail.auditTime }}</el-form-item>
              </el-row>
              <el-form-item label="备注">{{ detail.auditDescription }}</el-form-item>
            </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="status">
          <ai-select v-model="form.status" :selectList="dict.getDict('enterpriseAuditStatus')"/>
        </el-form-item>
        <el-form-item label="审批意见" prop="remark" :required="form.status==2">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入" maxlength="200" rows="4" show-word-limit/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

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

export default {
  name: "productDetail",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    isAuthing() {
      return this.detail.status == "0"
    },
    isAdmin() {
      return this.user?.financeUser?.userRole == 1
    },
    rules() {
      return {
        status: [{required: true, message: "请选择审批结果"}],
        remark: [{validator: (r, v, cb) => this.form.status == 2 && !v ? cb("请输入审批意见") : cb()}],
      }
    }
  },
  data() {
    return {
      dialog: false,
      detail: {},
      form: {},
    }
  },
  methods: {
    getDetail() {
      let {id} = this.$route.query
      this.instance.post("/appfinancialproduct/queryDetailById", null, {
        params: {id}
      }).then(res => {
        if (res?.data) {
          this.detail = res.data
          this.detail.guaranteeModeLabel = res.data.guaranteeMode?.split(",").map(g => this.dict.getLabel("productGuaranteeMode", g))?.toString()
          this.detail.faceUserLabel = res.data.faceUser?.split(",").map(g => this.dict.getLabel("productFaceUser", g))?.toString()
          if (this.detail.handlingChannels == 1) {
            this.getZWSP(this.detail.zwspId).then(zwsp => this.$set(this.detail, 'zwspName', zwsp.processName))
          }
        } else {
          this.$message.error("该产品已下架!")
        }
      })
    },
    getZWSP(id) {
      //贷款联审信息
      return this.instance.post("/approval-process-def/info-id", null, {params: {id}}).then(res => {
        if (res?.data) {
          return res.data
        }
      })
    },
    submitAudit() {
      this.$refs.AuditForm.validate(v => {
        if (v) {
          let {id} = this.detail
          this.instance.post("/appfinancialproduct/auditProduct", null, {
            params: {id, ...this.form}
          }).then(res => {
            if (res?.code == 0) {
              this.dialog = false
              this.$message.success("提交成功!")
              this.getDetail()
            }
          })
        }
      })
    }
  },
  created() {
    this.getDetail()
  }
}
</script>

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

  .flexWrap {
    flex-wrap: wrap;
  }

  ::v-deep.half {
    width: 50%;
  }

  .qrCode {
    width: 120px;
    height: 120px;
  }
}
</style>
