<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="屠宰日期" prop="slaughterTime">
        <el-date-picker clearable
          v-model="queryParams.slaughterTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择屠宰日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="血码" prop="slaughterCode">
        <el-input
          v-model="queryParams.slaughterCode"
          placeholder="请输入血码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检疫证号" prop="animalCertNo">
        <el-input
          v-model="queryParams.animalCertNo"
          placeholder="请输入检疫证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="slaughterBatchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" width="50"/> -->
      <el-table-column label="屠宰日期" align="center" prop="slaughterTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.slaughterTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="血码" align="center" prop="slaughterCode" />
      <el-table-column label="检疫证号" align="center" prop="animalCertNo" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="肉商" align="center" prop="purchaserName" />
      <el-table-column label="品种" align="center" prop="variety" width="80" />
      <el-table-column label="数量" align="center" prop="amount" width="80" />
      <el-table-column label="宰前重量(kg)" align="center" prop="beforeWeight" width="80">
        <template slot-scope="scope">
          {{ showValue(scope.row.beforeWeight) }}
        </template>
      </el-table-column>
      <el-table-column label="白条重量(kg)" align="center" prop="sideWeight" width="80">
        <template slot-scope="scope">
          {{ showValue(scope.row.sideWeight) }}
        </template>
      </el-table-column>
      <el-table-column label="猪头重量(kg)" align="center" prop="headWeight" width="80">
        <template slot-scope="scope">
          {{ showValue(scope.row.headWeight) }}
        </template>
      </el-table-column>
      <el-table-column label="红脏重量(kg)" align="center" prop="redWeight" width="80">
        <template slot-scope="scope">
          {{ showValue(scope.row.redWeight) }}
        </template>
      </el-table-column>
      <el-table-column label="白脏重量(kg)" align="center" prop="whiteWeight" width="80">
        <template slot-scope="scope">
          {{ showValue(scope.row.whiteWeight) }}
        </template>
      </el-table-column>
      <el-table-column label="销售去向" align="center" prop="salePlace" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_origin_type" :value="scope.row.salePlace"/>
        </template>
      </el-table-column>
      <el-table-column label="肉品合格证" align="center" prop="meatCert">
        <template slot-scope="scope">
          {{ showValue(scope.row.meatCert) }}
        </template>
      </el-table-column>
      <el-table-column label="动物检疫合格证" align="center" prop="animalCert">
        <template slot-scope="scope">
          {{ showValue(scope.row.animalCert) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right"  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="['app:slaughterBatch:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改屠宰批次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="isView" label-width="120px">
        <el-form-item label="屠宰日期" prop="slaughterTime">
          <el-date-picker clearable
          v-model="form.slaughterTime"
          type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            disabled
          placeholder="请选择屠宰日期">
        </el-date-picker>
        </el-form-item>
        <el-form-item label="血码" prop="slaughterCode">
          <el-input v-model="form.slaughterCode" disabled />
        </el-form-item>
        <el-form-item label="检疫证号" prop="animalCertNo">
          <el-input v-model="form.animalCertNo" disabled />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplierName">
              <el-input v-model="form.supplierName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="肉商" prop="purchaserName">
          <el-input v-model="form.purchaserName" disabled />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="品种" prop="variety">
              <el-input v-model="form.variety" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="amount">
              <el-input v-model="form.amount" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="白条重量" prop="sideWeight">
              <el-input v-model="form.sideWeight" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="猪头重量" prop="headWeight">
              <el-input v-model="form.headWeight" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="红脏重量" prop="redWeight">
              <el-input v-model="form.redWeight" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="白脏重量" prop="whiteWeight">
              <el-input v-model="form.whiteWeight" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="宰前重量" prop="beforeWeight">
          <el-input v-model="form.beforeWeight" disabled />
        </el-form-item>
        <el-form-item label="销售去向" prop="salePlace">
          <el-input :value="selectDictLabel(dict.type.app_origin_type, form.salePlace)" disabled />
        </el-form-item>
        <el-form-item label="肉品合格证" prop="meatCert">
          <el-input v-model="form.meatCert" placeholder="请输入肉品合格证" />
        </el-form-item>
        <el-form-item label="动物检疫合格证" prop="animalCert">
          <el-input v-model="form.animalCert" 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 { listSlaughterBatch, getSlaughterBatch, delSlaughterBatch, addSlaughterBatch, updateSlaughterBatch } from "@/api/app/slaughterBatch";

export default {
  name: "SlaughterBatch",
  dicts: ['app_origin_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 屠宰批次表格数据
      slaughterBatchList: [],
      // 弹出层标题
      title: "",
      isView:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        slaughterTime: null,
        slaughterCode: null,
        animalCertNo:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        meatCert: [
          { required: true, message: "肉品品质检验合格证号不能为空", trigger: "blur" },
          {
              pattern: /^\d{9}$/,
              message: '肉品品质检验合格证号格式输入错误（9位数字）',
              trigger: 'blur'
          },
        ],
        animalCert: [
          { required: true, message: "动物检疫合格证号不能为空", trigger: "blur" },
          {
              pattern: /^\d{10}$/,
              message: '动物检疫合格证号格式输入错误（10位数字）',
              trigger: 'blur'
          },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  activated(){

    this.getList();
  },
  methods: {
    //空值显示 -
    showValue(value) {
      return (value != null || value != undefined) ? value : "-";
    },
    /** 查询屠宰批次列表 */
    getList() {
      this.loading = true;
      listSlaughterBatch(this.queryParams).then(response => {
        this.slaughterBatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        amount: null,
        animalCert: null,
        animalCertNo: null,
        beforeWeight: null,
        headWeight: null,
        meatCert: null,
        purchaserName: null,
        redWeight: null,
        salePlace: null,
        sideWeight: null,
        slaughterCode: null,
        slaughterTime: null,
        supplierName: null,
        variety: null,
        whiteWeight: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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 || this.ids
      getSlaughterBatch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.isView = false;
        this.title = "修改屠宰批次";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getSlaughterBatch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.isView = true;
        this.title = "查看屠宰批次";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSlaughterBatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSlaughterBatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除屠宰批次编号为"' + ids + '"的数据项？').then(function() {
        return delSlaughterBatch(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('app/slaughterBatch/export', {
        ...this.queryParams
      }, `slaughterBatch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
