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

const records = [
  {label: "序号", type: "index"},
  {label: "解押凭证号", prop: "releaseNo"},
  {label: "审批状态", prop: "auditStatus", dict: "auditStatus"},
  {label: "审批时间", prop: "auditTime"},
  {label: "审批人", prop: "auditName"},
]
export default {
  name: "sellAdd",
  components: {AiEartagPicker},
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: {detailList: []},
      records
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    pageTitle: v => {
      const appName = v.$parent.menuName || v.$parent.$options.label
      return v.isEdit ? `新增${appName}` : `${appName}详情`
    },
    isEdit: v => v.$route.hash == "#edit",
    formImages: v => [
      {label: "合同/协议", prop: "contractPicture", rules: {required: v.isEdit, message: '请上传 合同/协议'}},
    ],
    columns: v => [
      {label: "序号", type: "index"},
      {label: "生物芯片耳标号", prop: "biochipEarNumber"},
      {label: "身长测量照片", prop: "heightPicture", upload: {instance: v.instance, readonly: !v.isEdit, valueIsUrl: !0, limit: 1}},
      {label: "电子耳标照片", prop: "earNumberPicture", upload: {instance: v.instance, readonly: !v.isEdit, valueIsUrl: !0, limit: 1}},
      {label: "防疫耳标照片", prop: "preventionPicture", upload: {instance: v.instance, readonly: !v.isEdit, valueIsUrl: !0, limit: 1}},
      {label: "解押办结凭证号", prop: "releaseNo", hide: v.isEdit},
    ].filter(e => !e.hide),
    selectedEartags: v => v.detail.list?.length || 0,
  },
  methods: {
    back(params = {}) {
      this.$router.push(params)
    },
    getDetail() {
      const {id} = this.$route.query
      return id && this.instance.post("/api/sell/apply/getInfo", null, {params: {contractNo: id}}).then(res => {
        if (res?.data) {
          const detail = res.data
          detail.detailList = detail.detailList || []
          detail.list = detail.list || []
          return this.detail = {...detail}
        }
      })
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        this.instance.post("/api/sell/apply/add", {...this.detail}).then(res => {
          if (res?.code == '0') {
            this.$message.success("提交成功!")
            this.back()
          }
        })
      })
    }
  },
  created() {
    this.getDetail()
  }
}
</script>

<template>
  <ai-page :title="pageTitle" class="sellAdd" 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="标的信息">
        <template #right v-if="isEdit">
          <ai-eartag-picker @select="v=>detail.detailList=v" :instance="instance"
                            :action="`/api/sell/apply/getClaimEarNumberList?contractNo=${detail.contractNo}`">
            <el-button type="text">选择</el-button>
          </ai-eartag-picker>
        </template>
        <ai-highlight class="mar-b8 font-14" :content="`投保标的共${detail.insureNumber||0}只，已理赔标的共 @v 只`" color="red" :value="selectedEartags"/>
        <ai-table :tableData="detail.detailList" :colConfigs="columns" :isShowPagination="!1" hideOptions/>
      </ai-card>
      <ai-card title="解押材料" v-if="isEdit">
        <div class="font-12 mar-b8">只能上传JPG/PNG文件，且不超过2M，一次最多5张</div>
        <el-form-item v-for="(img,i) in formImages" :key="i" v-bind="img">
          <ai-uploader v-model="detail[img.prop]" :instance="instance" value-is-url :limit="5"/>
        </el-form-item>
      </ai-card>
      <ai-card title="出栏解押记录" v-else>
        <ai-table :tableData="detail.list" :colConfigs="records" :isShowPagination="!1" hideOptions/>
      </ai-card>
    </el-form>
    <div slot="footer">
      <template v-if="isEdit">
        <el-button type="primary" @click="submit(1)">提交</el-button>
      </template>
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

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

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