<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"

    >
      <el-form-item label="供应商" prop="supplierId">
        <el-select
          v-model="queryParams.supplierId"
          filterable
          placeholder="请选择供应商"
        >
          <el-option
            v-for="item in supplierOptions"
            :key="item.id"
            :label="item.supplierName"
            :value="item.id"
          ></el-option>
        </el-select>
      </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 label="进场时间" prop="entranceTime">
        <el-date-picker
          clearable
          v-model="queryParams.entranceTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择进场时间"
        >
        </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="['app:entranceBatch:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:entranceBatch:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="entranceBatchList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column
        label="进场时间"
        align="center"
        prop="entranceTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entranceTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检疫证号" align="center" prop="animalCertNo" />
      <el-table-column label="货主" align="center" prop="cargoOwner" />
      <el-table-column label="数量" align="center" prop="amount" />
      <el-table-column label="供应商" align="center" prop="supplier.supplierName" />
      <el-table-column label="品种" align="center" prop="variety" />
      <el-table-column label="病死头数" align="center" prop="harmlessTreatment.illnessDeadAmount">
        <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.illnessDeadAmount)}}
            </div>
          </template>>
      </el-table-column>
      <el-table-column label="病害头数" align="center" prop="harmlessTreatment.illnessDiseaseAmount">
        <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.illnessDiseaseAmount)}}
            </div>
          </template>>
      </el-table-column>
      <el-table-column label="急宰头数" align="center" prop="harmlessTreatment.snapKillAmount">
        <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.snapKillAmount)}}
            </div>
          </template>>
      </el-table-column>
      <el-table-column label="准宰头数" align="center" prop="animalCategory">
          <template slot-scope="scope">
            <div>
              {{ getCanKillSum(scope.row) }}
            </div>
          </template>>
      </el-table-column>
      <el-table-column label="不合格产品重量(kg)" align="center" prop="harmlessTreatment.unqualifiedWeight">
        <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.unqualifiedWeight)}}
            </div>
          </template>>
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        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="['app:entranceBatch:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:entranceBatch:remove']"
            >删除</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="1200px"
      append-to-body
    >
      <el-tabs tab-position="left" v-model="tabSel" :stretch="true">
        <el-tab-pane name="basic">
          <div slot="label" class="pane-label-style">
            基本信息
          </div>
          <basic-info :entranceBatchId="entranceBatchId" :disabled="isView" @submit="submit" />
        </el-tab-pane>
        <el-tab-pane name="inspection" :disabled="!(entranceBatchId>0)">
          <div slot="label" class="pane-label-style">
            检验检疫
          </div>
          <inspection :entranceBatchId="entranceBatchId" :disabled="isView" @submit="submit"/>
        </el-tab-pane>
        <el-tab-pane name="harmlessTreatment" :disabled="!(entranceBatchId>0)">
          <div slot="label" class="pane-label-style">
            无害化处理
          </div>
          <harmlessTreatment :entranceBatchId="entranceBatchId" :disabled="isView" @submit="submit" />
        </el-tab-pane>
        <el-tab-pane name="distributeBatch" :disabled="!(entranceBatchId>0)">
          <div slot="label" class="pane-label-style">
            分销信息
          </div>
          <distributeBatch :entranceBatchId="entranceBatchId" :supplierId="supplierId" :disabled="isView" @submit="submit" />
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEntranceBatch,
  getEntranceBatch,
  delEntranceBatch,
  addEntranceBatch,
  updateEntranceBatch,
} from "@/api/app/entranceBatch";
import { getAllSupplier } from "@/api/app/supplier";
import basicInfo from './components/basicInfo';
import inspection from './components/inspection';
import harmlessTreatment from './components/harmlessTreatment';
import distributeBatch from './components/distributeBatch';
export default {
  name: "EntranceBatch",
  components: {
    basicInfo,
    inspection,
    harmlessTreatment,
    distributeBatch
  },
  data() {
    return {
      tabSel:'basic',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      supplierOptions: [],
      // 入场批次表格数据
      entranceBatchList: [],
      // 弹出层标题
      title: "",
      isView:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierId: null,
        entranceTime: null,
        animalCertNo: null,
      },
      //批次ID
      entranceBatchId:null,
      supplierId:null,
    };
  },
  created() {
    this.getList();
    this.getSupplierOptions();
  },
  methods: {
    //计算准宰头数
    getCanKillSum(row){
        return row.amount - row.harmlessTreatment.illnessDeadAmount - row.harmlessTreatment.illnessDiseaseAmount - row.harmlessTreatment.snapKillAmount;
    },
    //空值显示 -
    showValue(value){
      return value!=null? value:"-"
    },
    getSupplierOptions() {
      getAllSupplier().then((response) => {
        this.supplierOptions = response.data;
      });
    },
    /** 查询入场批次列表 */
    getList() {
      this.loading = true;
      listEntranceBatch(this.queryParams).then((response) => {
        this.entranceBatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    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.open = true;
      this.isView = false;
      this.entranceBatchId = null;
      this.title = "添加入场批次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.isView = false;
      this.entranceBatchId = row.id;
      console.log(row);
      this.supplierId = row.supplierId
      this.title = "修改入场批次";
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.open = true;
      this.isView = true;
      this.entranceBatchId = row.id;
      console.log(row);
      this.supplierId = row.supplierId
      this.title = "查看入场批次";
    },
    //
    submit(){
      this.open = false;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除入场批次编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEntranceBatch(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "app/entranceBatch/export",
        {
          ...this.queryParams,
        },
        `entranceBatch_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.pane-label-style {
  /* font-size: 18px; */
}
</style>
