<script>
import {mapState} from "vuex"
import AiEartagPicker from "@project/xumu/components/AiEartagPicker.vue";

export default {
  name: "saAdd",
  components: {AiEartagPicker},
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: {detailList: []}
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    pageTitle: v => {
      const appName = v.$parent.menuName || v.$parent.$options.label
      return v.isAudit ? `${appName}审批` : `${appName}详情`
    },
    isAudit: v => v.$route.hash == "#audit",
    formImages: v => [
      {label: "合同/协议", prop: "contractPicture"},
    ],
    columns: v => [
      {label: "序号", type: "index"},
      {label: "生物芯片耳标号", prop: "biochipEarNumber"},
      {label: "身长测量照片", prop: "heightPicture", upload: {instance: v.instance, readonly: !0, valueIsUrl: !0, limit: 1}},
      {label: "电子耳标照片", prop: "earNumberPicture", upload: {instance: v.instance, readonly: !0, valueIsUrl: !0, limit: 1}},
      {label: "防疫耳标照片", prop: "preventionPicture", upload: {instance: v.instance, readonly: !0, valueIsUrl: !0, limit: 1}},
    ].filter(e => !e.hide),
  },
  methods: {
    back(params = {}) {
      this.$router.push(params)
    },
    getDetail() {
      const {id} = this.$route.query
      return id && this.instance.post("/api/sell/apply/getAuditInfo", null, {params: {id}}).then(res => {
        if (res?.data) {
          const detail = res.data
          return this.detail = {...detail}
        }
      })
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        this.instance.post("/api/sell/apply/audit", {...this.detail}).then(res => {
          if (res?.code == '0') {
            this.$message.success("提交成功!")
            this.back()
          }
        })
      })
    }
  },
  created() {
    this.getDetail()
  }
}
</script>

<template>
  <ai-page :title="pageTitle" class="saAdd" showBack content-string="blank">
    <el-form size="small" label-width="120px" :model="detail" ref="detail">
      <ai-card title="基础信息">
        <div class="grid">
          <el-form-item label="养殖场" prop="farmId" :rules="{message:'请选择 养殖场'}">
            <b v-text="detail.farmName"/>
          </el-form-item>
          <el-form-item label="贷款银行" prop="bankId" :rules="{message:'请选择 贷款银行'}">
            <b v-text="detail.bankName"/>
          </el-form-item>
          <el-form-item label="贷款产品" prop="productType" :rules="{message:'请选择 贷款产品'}">
            <b v-text="dict.getLabel('loanProduct',detail.productType)"/>
          </el-form-item>
          <el-form-item label="贷款金额(万)" prop="loanAmount" :rules="{message:'请输入 预期贷款额'}">
            <ai-input v-model.number="detail.loanAmount" :edit="!1"/>
          </el-form-item>
          <el-form-item label="联系人" prop="contacts" :rules="{message:'请输入 联系人'}">
            <ai-input v-model="detail.contacts" :edit="!1"/>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone" :rules="{message:'请输入 联系电话'}">
            <ai-input v-model="detail.phone" :edit="!1"/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="标的信息">
        <ai-table :tableData="detail.detailList" :colConfigs="columns" :isShowPagination="!1" hideOptions/>
      </ai-card>
      <ai-card title="解押材料">
        <el-form-item v-for="(img,i) in formImages" :key="i" v-bind="img">
          <ai-uploader v-model="detail[img.prop]" value-is-url readonly/>
        </el-form-item>
      </ai-card>
      <ai-card title="审核信息">
        <div class="grid">
          <template v-if="isAudit">
            <el-form-item label="审批状态" prop="auditStatus" :rules="{required:true,message:'请选择审批状态'}">
              <ai-select v-model="detail.auditStatus" dict="auditStatus"/>
            </el-form-item>
            <el-form-item label="解压资料" class="sc-3">
              <ai-uploader v-model="detail.picture" value-is-url :instance="instance" :limit="1"/>
            </el-form-item>
            <el-form-item label="解押凭证号" prop="reportNo" :rules="{required:true,message:'请输入 解押凭证号'}">
              <ai-input v-model="detail.reportNo"/>
            </el-form-item>
            <el-form-item label="说明">
              <ai-input type="textarea" :rows="3" v-model="detail.remarks"/>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="审核状态">{{ dict.getLabel('auditStatus', detail.auditStatus) }}</el-form-item>
            <el-form-item label="解压资料" class="sc-3">
              <el-image :src="detail.picture" :preview-src-list="[detail.picture]"/>
            </el-form-item>
            <el-form-item label="审核时间">{{ detail.auditTime }}</el-form-item>
            <el-form-item label="审核人">{{ detail.auditName }}</el-form-item>
            <el-form-item label="说明">{{ detail.remarks }}</el-form-item>
          </template>
        </div>
      </ai-card>
    </el-form>
    <div slot="footer">
      <template v-if="isAudit">
        <el-button type="primary" @click="submit">提交</el-button>
      </template>
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

<style scoped lang="scss">
.saAdd {
  :deep(.el-form--label-top) {
    .el-form-item__label {
      width: 100% !important;
    }

    .el-form-item__content {
      margin-left: unset !important;
    }
  }
}
</style>
