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

const columns = [
  {label: "序号", type: "index"},
  {label: "疫苗名称", prop: "vaccineName"},
  {label: "用药方式", prop: "method"},
  {label: "药量(ml)", prop: "dosage"},
  {label: "生产厂家", prop: "factory"},
  {label: "厂家批号", prop: "batchNumber"},
  {label: "免疫时间", prop: "immunityTime"},
  {label: "登记时间", prop: "createTime"},
  {label: "防疫员", prop: "userName"},
]
export default {
  name: "immunityAdd",
  components: {AiEartagPicker},
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: {detailList: []},
      columns,
      dialog: false,
      form: {}
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    isAdd: v => !v.$route.query.id,
    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() {
      const {id} = this.$route.query
      return id && this.instance.post("/api/breed/immunity/getInfo", null, {params: {biochipEarNumber: id}}).then(res => {
        if (res?.data) {
          const detail = res.data
          detail.detailList = detail.immunityList || []
          return this.detail = {...detail}
        }
      })
    },
    handleDelete(id) {
      this.$confirm("确定删除该条数据？").then(() => {
        this.instance.post("/api/breed/immunity/del", null, {
          params: {id}
        }).then(res => {
          if (res?.code == 0) {
            this.getDetail()
          }
        })
      })
    },
    handleAdd() {
      this.$refs.detail.validate().then(() => {
        this.instance.post("/api/breed/immunity/add", this.detail).then(res => {
          if (res?.code == 0) {
            this.$message.success("新增成功!")
            this.back()
          }
        })
      })
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        const action = !this.form.id ? "/api/breed/immunity/add" : "/api/breed/immunity/update"
        this.instance.post(action, this.form).then(res => {
          if (res?.code == 0) {
            this.dialog = false
            this.getDetail()
          }
        })
      })
    }
  },
  created() {
    this.dict.load("yesOrNo", "category", "variety", "source")
    this.detail.treatmentTime = new Date()
    this.getDetail()
  }
}
</script>

<template>
  <ai-page :title="pageTitle" class="immunityAdd" showBack content-string="blank">
    <el-form size="small" label-width="120px" :model="detail" ref="detail">
      <ai-card title="基础信息">
        <div class="grid c-3" v-if="isAdd">
          <el-form-item label="养殖场" prop="farmId" :rules="{required:true,message:'请选择 养殖场'}">
            <ai-select v-model="detail.farmId" :instance="instance" :action="`/api/siteUser/querySiteByUserId?userId=${userinfo.id}`" :prop="{label:'name'}"/>
          </el-form-item>
          <el-form-item label="养殖舍" prop="houseId" :rules="{required:true,message:'请选择 养殖舍'}">
            <ai-select v-model="detail.houseId" :instance="instance" :action="`/api/siteUser/querySiteById?id=${detail.farmId||-1}`" :prop="{label:'name'}"/>
          </el-form-item>
          <el-form-item label="养殖栏" prop="penId" :rules="{required:true,message:'请选择 养殖栏'}">
            <ai-select v-model="detail.penId" :instance="instance" :action="`/api/siteUser/querySiteById?id=${detail.houseId||-1}`" :prop="{label:'name'}"/>
          </el-form-item>
          <el-form-item label="牲畜" prop="earNumberList" :rules="{required:true,message:'请选择 牲畜'}" class="row">
            <ai-eartag-picker v-model="detail.earNumberList" :instance="instance" :penId="detail.penId"/>
          </el-form-item>
        </div>
        <div v-else class="grid c-4">
          <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="生物芯片耳标号">
            <b v-text="detail.biochipEarNumber"/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="免疫登记">
        <template v-if="isAdd">
          <div class="grid c-3">
            <el-form-item label="免疫日期" prop="immunityTime" :rules="{required:true,message:'请选择 免疫日期'}">
              <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="detail.immunityTime"/>
            </el-form-item>
            <el-form-item label="疫苗名称" prop="vaccineName" :rules="{required:true,message:'请选择 疫苗名称'}">
              <ai-input v-model="detail.vaccineName"/>
            </el-form-item>
            <el-form-item label="用药方式" prop="method">
              <ai-input v-model="detail.method"/>
            </el-form-item>
            <el-form-item label="生产厂家" prop="factory">
              <ai-input v-model="detail.factory"/>
            </el-form-item>
            <el-form-item label="每头药量" prop="dosage">
              <ai-input v-model="detail.dosage"/>
            </el-form-item>
            <el-form-item label="厂家批号" prop="batchNumber">
              <ai-input v-model="detail.batchNumber"/>
            </el-form-item>
          </div>
        </template>
        <template v-else>
          <el-button type="text" slot="right" @click="dialog=true">新增</el-button>
          <ai-table :tableData="detail.detailList" :colConfigs="columns" :isShowPagination="!1">
            <el-table-column slot="options" label="操作" fixed="right" align="center">
              <template slot-scope="{row,$index}">
                <div class="table-options">
                  <el-button type="text" @click="dialog=true,form=row">编辑</el-button>
                  <el-button type="text" class="deleteBtn" @click="handleDelete(row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </ai-table>
        </template>
      </ai-card>
      <ai-dialog v-model="dialog" title="免疫登记" @closed="form={}" @confirm="submit">
        <el-form size="small" label-width="120px" class="grid">
          <el-form-item label="免疫日期" prop="immunityTime" :rules="{required:true,message:'请选择 免疫日期'}">
            <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="form.immunityTime"/>
          </el-form-item>
          <el-form-item label="疫苗名称" prop="vaccineName" :rules="{required:true,message:'请选择 疫苗名称'}">
            <ai-input v-model="form.vaccineName"/>
          </el-form-item>
          <el-form-item label="用药方式" prop="method">
            <ai-input v-model="form.method"/>
          </el-form-item>
          <el-form-item label="生产厂家" prop="factory">
            <ai-input v-model="form.factory"/>
          </el-form-item>
          <el-form-item label="每头药量" prop="dosage">
            <ai-input v-model="form.dosage"/>
          </el-form-item>
          <el-form-item label="厂家批号" prop="batchNumber">
            <ai-input v-model="form.batchNumber"/>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </el-form>
    <div slot="footer">
      <el-button v-if="isAdd" type="primary" @click="handleAdd">提交</el-button>
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

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