<template>
  <div class="dialog-body" style="height: 430px; padding: 0px !important">
    <div
      class="search-container"
      ref="searchPanel"
      style="padding-left: 5px; padding-right: 5px"
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="mini"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="物料编号" prop="goodsId">
          <el-input
            v-model="queryParams.goodsId"
            placeholder="请输入物料编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="goodsName">
          <el-input
            v-model="queryParams.goodsName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="物料规格" prop="specification">
          <el-input
            v-model="queryParams.specification"
            placeholder="请输入物料规格"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="物料描述" prop="recoil">
          <el-input
            v-model="queryParams.recoil"
            placeholder="请输入物料描述"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="库级选择" prop="levelType">
          <el-select v-model="queryParams.levelType" placeholder="请择选库级">
            <el-option
              v-for="dict in dict.type.level_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索
          </el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >刷新
          </el-button
          >
          <el-button
            plain
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >发送抽盘
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div ref="mainTablePanel">
      <el-table
        ref="goodsTable"
        v-loading="loading"
        border
        size="mini"
        height="600"
        :data="filteredData"
        @selection-change="handleSelectionChange"
        @row-click="handleClick"
      >
        <el-table-column type="selection" :selectable="selectable" width="55" align="center"/>
        <el-table-column label="所在仓库" align="center" prop="storageId" sortable :show="false"/>
        <el-table-column label="仓库名称" align="center" prop="storageShortName" sortable/>

        <el-table-column label="物料编号" align="center" prop="goodsId" sortable/>
        <el-table-column label="物料名称" align="center" prop="goodsName" sortable/>
        <el-table-column label="规格" align="center" prop="specification" sortable/>
        <el-table-column label="物料描述" align="center" prop="recoil" sortable/>
        <el-table-column label="用途" align="center" prop="useType" sortable/>
        <el-table-column label="单价" align="center" prop="price" sortable/>
        <el-table-column label="总价" align="center" prop="totalMoney" sortable/>
        <el-table-column label="数量" align="center" prop="shelvesNum" sortable/>
        <el-table-column label="单位" align="center" prop="unit" sortable/>
        <el-table-column label="需求部门" align="center" prop="needDept" sortable/>
        <el-table-column label="操作人" align="center" prop="createBy" sortable/>
        <el-table-column label="占用数量" align="center" prop="occupyNum" sortable/>
        <el-table-column label="锁定状态" align="center" prop="status" sortable>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.common_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="锁定数量" align="center" prop="occupyNum" sortable/>
        <el-table-column label="冻结状态" align="center" prop="isFreeze" sortable>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.is_freeze" :value="scope.row.isFreeze"/>
          </template>
        </el-table-column>
        <el-table-column label="库级" align="center" prop="leveType" sortable>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.level_type" :value="scope.row.levelType"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagePanel">
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>



</template>
<script>
import {countList,sendCp} from "@/api/system/tMiStock";
import {
  delCkOrders,
  statisticCallShelvesNum
} from "@/api/system/ckOrders";
import { delOutbound } from '../../function/api/outbound';
export default {
  name: "GoodsSelect",
  dicts: ["storage_classification", "common_status", "is_freeze","level_type"],
  props: ["storageIdFrom"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存管理表格数据
      tMiStockList: [],
      filteredData: [], // 过滤后的数据列表
      // 弹出层标题
      title: "",
      mainTableHeight: "0px",
      mainTableDivHeight: "0px",
      businessCodeTrk: "tMiStock",
      rightToolbarShow: false,
      tableNo: 0,
      // 是否显示弹出层
      open: false,
      selectedItems:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        storageId: null,
        storageAreaId: null,
        locationId: null,
        goodsId: null,
        shelvesNum: null,
        version: null,
        storageMode: null,
        providerId: null,
        productionDate: null,
        wareDate: null,
        doctNumber: null,
        goodsVolume: null,
        occupyNum: null,
        noticeId: null,
        oriBill: null,
        ctl: null,
        barcode: null,
        wgh: null,
        high: null,
        goodsName: null,
        status: null,
        customerId: null,
        batchNo: null,
        dueDate: null,
        checkDate: null,
        standard: null,
        flag: null,
        mark: null,
        orderNum: null,
        problem: null,
        price: null,
        packingNum: null,
        boxType: null,
        scaleUnit: null,
        goodsSku: null,
        factoryId: null,
        salesOrderNo: null,
        salesOrderNumber: null,
        supplier: null,
        onOrder: null,
        recordPerson: null,
        lotId: null,
        decid: null,
        unit: null,
        specification: null,
        goodsAge: null,
        arraMount: null,
        recoil: null,
        operatorId: null,
        operateDate: null,
        purchaseId: null,
        applicant: null,
        respDept: null,
        planDate: null,
        planTime: null,
        needCycle: null,
        needType: null,
        goodsTypeId: null,
        brand: null,
        totalMoney: null,
        expiryDate: null,
        providerName: null,
        receiver: null,
        needDept: null,
        purchaseDept: null,
        produclotId: null,
        receiverDept: null,
        receivingArea: null,
        packageId: null,
        useType: null,
        telephone: null,
        totalVolume: null,
        totalRoughWeight: null,
        totalNetWeigh: null,
        defaultStorageAreaId: null,
        qualityInspecStatus: null,
        periodUnit: null,
        period: null,
        reportTime: null,
        consignment: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },

  created() {
    this.getList()
  },
  mounted() {
  },

  methods: {
    filterStatus(value, row, column) {
      return row.status === value;
    },
    selectable(row, index) {
      if (row.status === '0' && row.isFreeze === '0') {
        return true;
      } else {
        return false;
      }
    },
    preventRowSelection(row, event) {
      // 阻止行点击事件冒泡
      event.stopPropagation();
    },
    /** 查询物料列表 */
    getList() {
      this.loading = true;
      countList(this.queryParams).then((response) => {
        this.tMiStockList = response.rows;
        this.filteredData = this.tMiStockList.filter(item => item.storageId === 'FJQR01');
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storageId: null,
        storageAreaId: null,
        locationId: null,
        goodsId: null,
        shelvesNum: null,
        remark: null,
        version: null,
        storageMode: null,
        providerId: null,
        productionDate: null,
        wareDate: null,
        doctNumber: null,
        goodsVolume: null,
        occupyNum: null,
        noticeId: null,
        oriBill: null,
        ctl: null,
        barcode: null,
        wgh: null,
        high: null,
        goodsName: null,
        status: null,
        customerId: null,
        batchNo: null,
        dueDate: null,
        checkDate: null,
        standard: null,
        flag: null,
        mark: null,
        orderNum: null,
        problem: null,
        price: null,
        packingNum: null,
        boxType: null,
        scaleUnit: null,
        goodsSku: null,
        factoryId: null,
        salesOrderNo: null,
        salesOrderNumber: null,
        supplier: null,
        onOrder: null,
        recordPerson: null,
        lotId: null,
        decid: null,
        unit: null,
        specification: null,
        goodsAge: null,
        arraMount: null,
        recoil: null,
        operatorId: null,
        operateDate: null,
        purchaseId: null,
        applicant: null,
        respDept: null,
        planDate: null,
        planTime: null,
        needCycle: null,
        needType: null,
        goodsTypeId: null,
        brand: null,
        totalMoney: null,
        expiryDate: null,
        providerName: null,
        receiver: null,
        needDept: null,
        purchaseDept: null,
        produclotId: null,
        receiverDept: null,
        receivingArea: null,
        packageId: null,
        useType: null,
        telephone: null,
        totalVolume: null,
        totalRoughWeight: null,
        totalNetWeigh: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        defaultStorageAreaId: null,
        qualityInspecStatus: null,
        periodUnit: null,
        period: null,
        reportTime: null,
        consignment: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据

    handleSelectionChange(selection) {
      this.goodsIds = selection.map((item) => item.goodsId); // 假设每个选项都有一个 'id' 属性
      this.multiple = !selection.length;
      // 更新选中项数组，而不是取消之前的选择
      selection.forEach((item, index) => {
        this.$refs.goodsTable.toggleRowSelection(item, true);
      });

      // 更新当前选中的项
      this.selectItem = selection[selection.length - 1];

      this.selectedItems = selection; // 保存所有选中的项
    },

    /** 提交按钮 */
    async  submitForm() {
      if (this.selectedItems.length === 0) {
        this.$modal.msgError("请选择一个或多个物料");
        return;
      }
      //查询该仓库申请出库的数量，根据仓库ID
      let parms = {
        storageId: this.selectedItems[0].storageId,
        goodsId: this.selectedItems[0].goodsId,
      };
      await statisticCallShelvesNum(parms).then((response) => {
        if (response.code == 200) {
          this.$set(this.selectItem,'totalNetWeigh', response.data);

        }
      });
      this.callBack(this.selectItem);
      this.open = false;
    },
    showModal(newCallBack) {
      this.open = true;
      this.callBack = newCallBack;
      this.queryParams.storageId = this.storageIdFrom;
      this.getList();
    },
    /*handleDblClick(row) {
      this.callBack(row);
      this.open = false;
    },*/
    handleClick(row, event) {
      // 阻止行点击事件冒泡
      event.stopPropagation();
      this.$refs.goodsTable.toggleRowSelection(row);
      this.selectItem = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.goodsIds;
      this.$modal
        .confirm('是否确认发送物料编号为"' + goodsIds + '"的数据项？')
        .then(function () {
          return sendCp(goodsIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("发送抽盘任务成功");
        })
        .catch(() => {
        });
    },
  },
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}

::v-deep .el-table .warning-row {
  background: #31a354;
}

::v-deep .el-table .success-row {
  background-color: #e1d1d1;
}

::v-deep .el-table .selected-row {
  background-color: #019fe8 !important;
}
.pagePanel {
  margin-top: 8px;
  text-align: right;
}

</style>



