<script>
import {mapState} from "vuex"
import AiSelect from "dui/packages/basic/AiSelect.vue";
import AiDialog from "dui/packages/basic/AiDialog.vue";

const columns = [
  {label: "序号", type: "index"},
  {label: "数据来源", prop: "source", dict: "source"},
  {label: "重量", prop: "weight"},
  {label: "称重时间", prop: "createTime"},
  {label: "是否变更过", prop: "isUpdate", dict: "yesOrNo"},
]
export default {
  name: "weightAdd",
  components: {AiDialog, AiSelect},
  props: {
    instance: Function,
    permissions: Function,
    dict: Object
  },
  data() {
    return {
      detail: {detailList: []},
      columns,
      dialog: false,
    }
  },
  computed: {
    ...mapState(["user"]),
    userinfo: v => v.user.info || {},
    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/weight/getInfo", null, {params: {biochipEarNumber: id}}).then(res => {
        if (res?.data) {
          const detail = res.data
          detail.detailList = detail.weightList || []
          return this.detail = {...detail}
        }
      })
    },
    handleDelete(index) {
      this.$confirm("确定删除该条数据？").then(() => {
        this.detail.detailList.splice(index, 1)
      })
    },
    submit() {
      this.$refs.detail.validate().then(() => {
        this.instance.post("/api/breed/weight/addOrEdit", this.detail).then(res => {
          if (res?.code == 0) {
            this.dialog = false
            this.getDetail()
          }
        })
      })
    }
  },
  created() {
    this.dict.load("yesOrNo", "category", "variety", "source")
    this.getDetail()
  }
}
</script>

<template>
  <ai-page :title="pageTitle" class="weightAdd" 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="生物芯片耳标号">
            <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="penId">
            <b v-text="detail.category"/>
          </el-form-item>
          <el-form-item label="品种" prop="penId">
            <b v-text="detail.variety"/>
          </el-form-item>
          <el-form-item label="最近称重时间">
            <b v-text="detail.lastCreateTime"/>
          </el-form-item>
          <el-form-item label="最新体重(公斤)">
            <b v-text="detail.todayWeight||detail.lastWeight"/>
          </el-form-item>
        </div>
      </ai-card>
      <ai-card title="体重录入">
        <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" class="deleteBtn" @click="handleDelete($index)">编辑</el-button>
                <el-button type="text" class="deleteBtn" @click="handleDelete($index)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </ai-table>
      </ai-card>
      <ai-dialog v-model="dialog">
      </ai-dialog>
    </el-form>
    <div slot="footer">
      <el-button @click="back">返回</el-button>
    </div>
  </ai-page>
</template>

<style scoped lang="scss">
.weightAdd {
}
</style>
