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

const records = [
  { label: "序号", type: "index" },
  { label: "报案号", prop: "reportNo" },
  { label: "审批状态", prop: "auditStatus", dict: "auditStatus" },
  { label: "审批时间", prop: "auditTime" },
  { label: "审批人", prop: "auditName" },
]
export default {
  name: "claimAdd",
  components: { AiEartagPicker },
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: { detailList: [], list: [] },
      records
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    pageTitle: v => {
      const appName = v.$parent.menuName || v.$parent.$options.label
      return v.isClaim ? `新增${appName}` : `${appName}详情`
    },
    isClaim: v => v.$route.hash == "#claim",
    formImages: v => [
      { label: "勘察报告书", prop: "surveyPicture", rules: { required: v.isClaim, message: '请上传 勘察报告书' } },
      { label: "无害化回执单", prop: "receiptPicture", rules: { required: v.isClaim, message: '请上传 无害化回执单' } },
    ],
    columns: v => [
      { label: "序号", type: "index" },
      { label: "生物芯片耳标号", prop: "biochipEarNumber" },
      { label: "身长测量照片", prop: "heightPicture", upload: { instance: v.instance, readonly: !v.isClaim, valueIsUrl: !0, limit: 1 } },
      { label: "电子耳标照片", prop: "earNumberPicture", upload: { instance: v.instance, readonly: !v.isClaim, valueIsUrl: !0, limit: 1 } },
      { label: "防疫耳标照片", prop: "preventionPicture", upload: { instance: v.instance, readonly: !v.isClaim, valueIsUrl: !0, limit: 1 } },
      { label: "无害化处理照片", prop: "harmlessPicture", upload: { instance: v.instance, readonly: !v.isClaim, valueIsUrl: !0, limit: 1 } },
      { label: "报案号", prop: "reportNo", hide: v.isClaim },
    ].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/insurance/claim/apply/getInfo", null, { params: { orderNo: id } }).then(res => {
        if (res?.data) {
          const detail = res.data
          detail.detailList = detail.detailList || []
          return this.detail = { ...detail }
        }
      })
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        this.instance.post("/api/insurance/claim/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="claimAdd" 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">
            <b v-text="detail.farmName" />
          </el-form-item>
          <el-form-item label="承保公司" prop="companyId">
            <b v-text="detail.companyName" />
          </el-form-item>
          <el-form-item label="投保类型">
            <ai-input :value="dict.getLabel('insureType', detail.insureType)" :edit="!1" />
          </el-form-item>
          <el-form-item label="保险产品" prop="productType">
            <b v-text="detail.productType" />
          </el-form-item>
          <el-form-item label="联系人">
            <ai-input v-model="detail.contacts" :edit="!1" />
          </el-form-item>
          <el-form-item label="联系电话">
            <ai-input v-model="detail.phone" :edit="!1" />
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="投保对象">
        <template #right v-if="isClaim">
          <ai-eartag-picker @select="v => detail.detailList = v.map(e=>({biochipEarNumber:e}))" :instance="instance"
            :action="`/api/insurance/claim/apply/getClaimEarNumberList?orderNo=${detail.orderNo}`">
            <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="isClaim">
        <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="isClaim">
        <el-button type="primary" @click="submit(1)">提交</el-button>
      </template>
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

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

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