<script>
import { mapState } from "vuex"
import AiAudit from "../components/AiAudit.vue";

export default {
  name: "deathAdd",
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: { detailList: [] }
    }
  },
  components:{AiAudit},
  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: "heightPic" },
      { label: "生物芯片照片", prop: "biochipPic" },
      { label: "防疫耳标照片", prop: "preventionPic" },
      { label: "其他说明照片", prop: "otherPic" },
    ],
  },
  methods: {
    back(params = {}) {
      this.$router.push(params)
    },
    getDetail() {
      const { id } = this.$route.query
      return id && this.instance.post("/api/breed/death/getAuditPage", { id }).then(res => {
        if (res?.data?.records?.[0]) {
          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(() => {
        this.instance.post("/api/breed/death/audit", { ...this.detail }).then(res => {
          if (res?.code == '0') {
            this.$message.success("提交成功!")
            this.back()
          }
        })
      })
    }
  },
  created() {
    this.getDetail()
  }
}
</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">
          <el-form-item label="生物芯片耳标号" class="row">
            <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="dict.getLabel('category', detail.category)" />
          </el-form-item>
          <el-form-item label="品种" prop="variety">
            <b v-text="dict.getLabel('variety', detail.variety)" />
          </el-form-item>
          <div class="row flex">
            <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>
          </div>
        </div>
      </ai-card>
      <ai-card title="死亡信息">
        <el-form-item label="死亡原因">
          <b v-text="dict.getLabel('deathReason', detail.reason)" />
        </el-form-item>
        <el-form-item label="死亡日期">
          <b v-text="detail.deathTime" />
        </el-form-item>
        <el-form-item label="备注">
          <b v-text="detail.remark" />
        </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-audit @change="v => $set(detail, 'auditStatus', v)" />
            </el-form-item>
            <el-form-item label="意见">
              <ai-input type="textarea" :rows="3" v-model="detail.remark" />
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="审核状态">{{ dict.getLabel('auditStatus', detail.auditStatus) }}</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">
.deathAdd {
  :deep(.el-form--label-top) {
    .el-form-item__label {
      width: 100% !important;
    }

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