<script>
import {mapState} from "vuex"

const formImages = [
  {label: "身长测量照片", prop: "heightPic", rules: {required: true, message: '请上传 身长测量照片'}},
  {label: "生物芯片照片", prop: "biochipPic", rules: {required: true, message: '请上传 生物芯片照片'}},
  {label: "防疫耳标照片", prop: "preventionPic", rules: {required: true, message: '请上传 防疫耳标照片'}},
  {label: "其他说明照片", prop: "otherPic", rules: {required: true, message: '请上传 其他说明照片'}},
]
export default {
  name: "deathAdd",
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      formImages,
      detail: {},
      form: {}
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    isAdd: v => !v.$route.query.id,
    isEdit: v => v.$route.query.edit == 1,
    isAuthing: v => v.detail.auditStatus == 1,
    pageTitle: v => {
      const appName = v.$parent.menuName || v.$parent.$options.label
      return v.$route.query.id ? v.isEdit ? `编辑${appName}` : `${appName}详情` : `新增${appName}`
    },
  },
  methods: {
    back(params = {}) {
      this.$router.push(params)
    },
    getDetail(id) {
      return id && this.instance.post("/api/breed/death/page", {biochipEarNumber: id}).then(res => {
        if (res?.data?.records) {
          const detail = res.data.records[0] || {}
          if (detail.picture) {
            Object.entries(JSON.parse(detail.picture)).forEach(([key, value]) => {
              detail[key] = value
            })
          }
          return this.detail = {...detail}
        }
      })
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        const {biochipEarNumber, id, deathTime, heightPic, biochipPic, preventionPic, otherPic, reason, remarks} = this.detail
        this.instance.post("/api/breed/death/addOrEdit", {
          biochipEarNumber, id, deathTime, picture: {heightPic, biochipPic, preventionPic, otherPic}, reason, remarks
        }).then(res => {
          if (res?.code == 0 && res?.data != 1) {
            this.$confirm("是否返回列表页?", "提交成功").then(() => this.back()).catch(() => this.getDetail(biochipEarNumber))
          }
        })
      })
    },
    handleAudit() {
      this.$refs.form.validate().then(() => {
        const {id} = this.detail
        this.instance.post("/api/breed/death/audit", null, {params: {id, ...this.form}}).then(res => {
          if (res?.code == 0) {
            this.$message.success("操作成功")
            this.back()
          }
        })
      })
    }
  },
  created() {
    this.dict.load("yesOrNo", "category", "variety", "deathReason")
    this.getDetail(this.$route.query.id)
  }
}
</script>

<template>
  <ai-page :title="pageTitle" class="deathAdd" showBack content-string="blank">
    <el-form size="small" label-width="120px" :model="detail" ref="detail">
      <ai-card title="基础信息">
        <div class="grid c-4">
          <ai-input class="row" v-model="detail.biochipEarNumber" placeholder="请输入耳标号按回车查询,或扫描耳标号" @input="getDetail"/>
          <el-form-item label="生物芯片耳标号">
            <b v-text="detail.biochipEarNumber"/>
          </el-form-item>
          <el-form-item label="养殖场" prop="farmId">
            <b v-text="detail.farmName"/>
          </el-form-item>
          <el-form-item label="养殖舍" prop="houseId">
            <b v-text="detail.houseName"/>
          </el-form-item>
          <el-form-item label="养殖栏" prop="penId">
            <b v-text="detail.penName"/>
          </el-form-item>
          <el-form-item label="电子耳标号" prop="electronicEarNumber">
            <b v-text="detail.electronicEarNumber"/>
          </el-form-item>
          <el-form-item label="原厂耳标号" prop="category">
            <b v-text="detail.originalEarNumber"/>
          </el-form-item>
          <el-form-item label="类别" prop="category">
            <b v-text="detail.category"/>
          </el-form-item>
          <el-form-item label="品种" prop="variety">
            <b v-text="detail.variety"/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="上传照片">
        <div class="grid c-4">
          <el-form-item v-for="(img,i) in formImages" :key="i" v-bind="img">
            <ai-uploader v-if="isAdd||isEdit" v-model="detail[img.prop]" :instance="instance" :limit="1" value-is-url/>
            <el-image :src="detail[img.prop]" :preview-src-list="[detail[img.prop]]" v-else/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="死亡录入">
        <div class="grid">
          <el-form-item label="死亡日期" prop="deathTime" :rules="[{required:true,message:'请选择死亡日期'}]">
            <el-date-picker v-if="isAdd||isEdit" v-model="detail.deathTime"/>
            <b v-text="detail.deathTime"/>
          </el-form-item>
          <el-form-item label="死亡原因" prop="reason" :rules="[{required:true,message:'请选择死亡原因'}]">
            <ai-select v-if="isAdd||isEdit" v-model="detail.reason" dict="deathReason"/>
            <b v-text="detail.reason"/>
          </el-form-item>
          <el-form-item label="备注" prop="remark" class="row">
            <ai-input type="textarea" :row="3" v-model="detail.remark" :edit="isAdd||isEdit"/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="审批信息" v-if="isAuthing">
        <el-form :model="form" size="small" ref="form" label-width="120px">
          <el-form-item label="是否同意" prop="auditStatus" :rules="[{required:true,message:'请选择是否同意'}]">
            <ai-select v-model="form.auditStatus" :select-list="[{dictValue: 2, dictName: '同意'}, {dictValue: 3, dictName: '不同意'}]"/>
          </el-form-item>
          <el-form-item label="审批意见" prop="auditReason">
            <el-input type="textarea" :row="3" v-model="form.auditReason" clearable placeholder="请输入"/>
          </el-form-item>
        </el-form>
      </ai-card>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="submit" v-if="isAdd||isEdit">提交</el-button>
      <el-button type="primary" @click="handleAudit" v-if="isAuthing">提交</el-button>
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

<style scoped lang="scss">
.deathAdd {
  .el-date-editor {
    width: 100%;
  }
}
</style>
