<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="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="animalType">
          <el-select v-model="queryParams.animalType">
            <el-option
              v-for="item in animalList"
              :key="item.typeValue"
              :label="item.typeName"
              :value="item.typeValue"
            ></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="queryDateRange">
          <el-date-picker
            v-model="queryParams.queryDateRange"
            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="['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-card>
    <br />
    <el-card>
      <el-table
        v-loading="loading"
        :data="entranceBatchList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <!--      <el-table-column label="ID"  prop="id" />-->
        <el-table-column label="进场时间" prop="entranceTime" width="180">
          <!--        <template slot-scope="scope">-->
          <!--          <span>{{ scope.row.entranceTime, "{y}-{m}-{d}") }}</span>-->
          <!--        </template>-->
        </el-table-column>
        <el-table-column label="检疫证号" prop="animalCertNo" />
        <el-table-column label="货主" prop="cargoOwner" />
        <el-table-column label="数量" prop="amount" />
        <el-table-column label="供应商" prop="supplier.supplierName" />
        <el-table-column label="畜种">
          <template slot-scope="scope">
            <span>{{animalName(scope.row.animalType)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="品种" prop="variety" />
        <el-table-column
          label="途亡头数"
          prop="harmlessTreatment.roadDealAmount"
        >
          <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.roadDealAmount) }}
            </div> </template
          >>
        </el-table-column>
        <el-table-column
          label="病死头数"
          prop="harmlessTreatment.illnessDeadAmount"
        >
          <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.illnessDeadAmount) }}
            </div> </template
          >>
        </el-table-column>
        <el-table-column
          label="病害头数"
          prop="harmlessTreatment.illnessDiseaseAmount"
        >
          <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.illnessDiseaseAmount) }}
            </div> </template
          >>
        </el-table-column>
        <el-table-column
          label="急宰头数"
          prop="harmlessTreatment.snapKillAmount"
        >
          <template slot-scope="scope">
            <div>
              {{ showValue(scope.row.harmlessTreatment.snapKillAmount) }}
            </div> </template
          >>
        </el-table-column>
        <el-table-column label="准宰头数" prop="animalCategory">
          <template slot-scope="scope">
            <div>
              {{ getCanKillSum(scope.row) }}
            </div> </template
          >>
        </el-table-column>
        <el-table-column
          label="不合格产品重量（kg）"
          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"
          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-card>

    <!-- 添加或修改入场批次对话框 -->
    <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="basicInfo">
          <div slot="label" class="pane-label-style">基本信息</div>
          <basic-info
            v-show="tabSel == 'basicInfo'"
            :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
            v-show="tabSel == '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
            v-show="tabSel == '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";
import { mapState } from "vuex";

export default {
  name: "EntranceBatch",
  components: {
    basicInfo,
    inspection,
    harmlessTreatment,
    distributeBatch,
  },
  data() {
    return {
      tabSel: "basicInfo",
      // 遮罩层
      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,
        queryDateRange:"",
        startDate: null,
        endDate: null,
        animalCertNo: null,
      },
      //批次ID
      entranceBatchId: null,
      variety: null,
      supplierId: null,
      pickerOptions: {
        disabledDate(time) {
          // 获取当前日期
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 将时间设置为当天的 00:00:00

          // 禁用今天之后的日期
          return time.getTime() > today.getTime();
        }
      },
    };
  },
  watch: {
    "queryParams.queryDateRange": {
      handler(newVal) {

        if (newVal) {
          this.queryParams.startDate = newVal[0]
          this.queryParams.endDate = newVal[1]
        } else {
          this.queryParams.startDate = null
          this.queryParams.endDate = null
        }
      },
      deep: true
    },
  },
  computed: {
    ...mapState({
      animalList: state => state.user.animalList
    }),
    animalName() {
      return (value) => {
        if (value === null) return '未知'
        const animal = this.animalList.find(item =>
          String(item.typeValue) === String(value)
        )
        return animal ? animal.typeName : '未知'
      }
    }
  },
  created() {
    this.getList();
    this.getSupplierOptions();
  },
  methods: {
    //计算准宰头数
    getCanKillSum(row) {
      return (
        row.amount -
        row.harmlessTreatment.illnessDeadAmount -
        row.harmlessTreatment.illnessDiseaseAmount -
        row.harmlessTreatment.snapKillAmount -
        row.harmlessTreatment.roadDealAmount
      );
    },
    //空值显示 -
    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;
      }).catch((e)=>{
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
        pageSize: 10,
        supplierId: null,
        queryDateRange:"",
        startDate: null,
        endDate: null,
        animalCertNo: null,
      }
      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.tabSel = "basicInfo";
      this.title = "添加入场批次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.isView = false;
      this.entranceBatchId = row.id;
      this.tabSel = "basicInfo";
      this.supplierId = row.supplierId;
      this.title = "修改入场批次";
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.open = true;
      this.isView = true;
      this.entranceBatchId = row.id;
      this.tabSel = "basicInfo";
      this.supplierId = row.supplierId;
      this.title = "查看入场批次";
    },
    //
    submit(id) {
      console.log(id);
      if (id) {
        this.entranceBatchId = id;
        //不关闭就跳到下一个窗口
        if (this.tabSel == "basicInfo") {
          this.tabSel = "inspection";
        } else if (this.tabSel == "inspection") {
          this.tabSel = "harmlessTreatment";
        }
      } else {
        this.entranceBatchId = 0;
        this.open = false;
        //关闭窗口就重新回到第一个
        this.tabSel = "basicInfo";
      }
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除所选数据项？")
        .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>
