<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
              <el-form-item label="品种名称" prop="varietyName">
                <el-input
                  v-model="queryParams.varietyName"
                  placeholder="请输入品种名称"
                  clearable
                />
              </el-form-item>
<!--        <el-form-item label="排查日期" prop="timeRange">-->
<!--          <el-date-picker-->
<!--            v-model="queryParams.timeRange"-->
<!--            type="daterange"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            range-separator="至"-->
<!--            start-placeholder="开始日期"-->
<!--            end-placeholder="结束日期"-->
<!--            :picker-options="pickerOptions"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['baseInfo:variety:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDeleteAll"
            v-hasPermi="['baseInfo:variety:remove']"
          >批量删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>
    <br />
    <el-card>
      <el-table border stripe  v-loading="loading" ref="multipleTable" :data="disposalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="varietyNo" />
        <el-table-column label="品种名称" align="center" prop="varietyName" />
        <el-table-column label="畜种"  align="center" prop="breedName" />
        <el-table-column label="大重量>" align="center" prop="bigVarietyGe" />
        <el-table-column label="大重量<=" align="center" prop="bigVarietyLe" />
        <el-table-column label="标准重量>" align="center" prop="midVarietyGe" />
        <el-table-column label="标准重量<=" align="center" prop="midVarietyLe" />
        <el-table-column label="小重量>" align="center" prop="smallVarietyGe" />
        <el-table-column label="小重量<=" align="center" prop="smallVarietyLe" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['baseInfo:variety:edit']"
              style="margin-right: 8px"
            >修改</el-button>
            <el-popconfirm
              v-hasPermi="['baseInfo:variety:remove']"
              :title="'确定要删除这条数据吗？'"
              @confirm="handleDelete(scope.row)"
            >
              <el-button
                size="mini"
                type="text"
                slot="reference"
                icon="el-icon-delete"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改死淘处理方式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="isView" label-width="100px">
        <el-form-item label="序号" prop="varietyNo">
          <el-input
            ref="varietyNo"
            v-model="form.varietyNo"
            placeholder="请输入序号"
            @keydown.enter.native="goNext($event, 'breedId')"
          ></el-input>
        </el-form-item>
        <el-form-item label="畜种" prop="breedId">
          <el-select
            filterable
            ref="breedId"
            style="width: 100%"
            v-model="form.breedId"
            @change="changeBreed"
            placeholder="请选择畜种"
            @keydown.enter.native="goNext($event, 'varietyName')"
          >
            <el-option v-for="(item, i) in breedList" :key="i" :value="item.id" :label="item.breedName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品种名称" prop="varietyName">
          <el-input
            ref="varietyName"
            v-model="form.varietyName"
            placeholder="请输入品种名称"
            @keydown.enter.native="goNext($event, 'bigVarietyGe')"
          ></el-input>
        </el-form-item>
        <el-form-item label="大重量>" prop="bigVarietyGe">
          <el-input
            ref="bigVarietyGe"
            v-model="form.bigVarietyGe"
            placeholder="大重量>"
            @keydown.enter.native="goNext($event, 'bigVarietyLe')"
          ></el-input>
        </el-form-item>
        <el-form-item label="大重量<=" prop="bigVarietyLe">
          <el-input
            ref="bigVarietyLe"
            v-model="form.bigVarietyLe"
            placeholder="大重量>"
            @keydown.enter.native="goNext($event, 'midVarietyGe')"
          ></el-input>
        </el-form-item>
        <el-form-item label="标准重量>" prop="midVarietyGe">
          <el-input
            ref="midVarietyGe"
            v-model="form.midVarietyGe"
            placeholder="大重量>"
            @keydown.enter.native="goNext($event, 'assistantUnit')"
          ></el-input>
        </el-form-item>
        <el-form-item label="标准重量<=" prop="midVarietyLe">
          <el-input
            ref="midVarietyLe"
            v-model="form.midVarietyLe"
            placeholder="大重量>"
            @keydown.enter.native="goNext($event, 'smallVarietyGe')"
          ></el-input>
        </el-form-item>
        <el-form-item label="小重量>" prop="smallVarietyGe">
          <el-input
            ref="smallVarietyGe"
            v-model="form.smallVarietyGe"
            placeholder="大重量>"
            @keydown.enter.native="goNext($event, 'smallVarietyLe')"
          ></el-input>
        </el-form-item>
        <el-form-item label="小重量<=" prop="smallVarietyLe">
          <el-input
            ref="smallVarietyLe"
            v-model="form.smallVarietyLe"
            placeholder="大重量>"
            @keydown.enter.native="goNext($event, 'remark')"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark"  placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getBreedList } from "@/api/baseInfo/breed";
import { getVarietyPage, getVarietyById, addVariety, editVariety, delVariety } from "@/api/baseInfo/variety";
import { getFindByKey } from "@/utils";

export default {
  name: "Variety",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      disposalList: [],
      // 弹出层标题
      title: "",
      isView:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        varietyName: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      },
      pickerOptions: {
        disabledDate(time) {
          // 获取当前日期
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 将时间设置为当天的 00:00:00

          // 禁用今天之后的日期
          return time.getTime() > today.getTime();
        }
      },
      // 表单参数
      form: {},
      rules: {
        varietyNo: [{required: true, message: '请输入序号', trigger: 'blur'}],
        breedId: [{required: true, message: '请选择畜种', trigger: 'blur'}],
        varietyName: [{required: true, message: '请输入品种', trigger: 'blur'}]
      },
      breedList: []
    }
  },
  watch: {
    "queryParams.timeRange": {
      handler(newVal) {
        if (newVal) {
          this.queryParams.startTime = newVal[0]
          this.queryParams.endTime = newVal[1]
        } else {
          this.queryParams.startTime = null
          this.queryParams.endTime = null
        }
      },
      deep: true
    },
  },
  created() {
    this.getBreed()
    this.getList();
  },
  methods: {
    /** 畜种 **/
    getBreed() {
      getBreedList({}).then(res => {
        this.breedList = res.data
      })
    },
    goNext(event, nextRef) {
      event.preventDefault();
      this.$refs[nextRef].focus();
    },
    changeBreed(val) {
      let item = getFindByKey(this.breedList, val, 'id')
      if(item) {
        this.form.breedName = item.breedName
      }
    },
    /** 查询检验项目列表 */
    getList() {
      getVarietyPage(this.queryParams).then(res => {
        this.disposalList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        varietyName: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isView = false;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getVarietyById(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = false;
        this.title = "编辑"
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getVarietyById(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = true;
        this.title = "查看"
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        varietyNo: null,
        varietyName: null,
        bigVarietyGe: null,
        bigVarietyLe: null,
        midVarietyGe: null,
        midVarietyLe: null,
        smallVarietyGe: null,
        smallVarietyLe: null,
        breedName: null,
        breedId: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 编辑
            editVariety(this.form).then(res => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            // 新增
            addVariety(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      let params = {
        ids: ids
      }
      delVariety(params).then(res => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 删除按钮操作 */
    handleDeleteAll() {
      const ids = this.ids;
      this.$modal.confirm('确定要删除这条数据吗？').then(() => {
        let params = {
          ids: ids.join(',')
        }
        delVariety(params).then(res => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
          this.$refs.multipleTable.clearSelection();
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
