<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel" class="toolPanel">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <div class="toolTitle">物料移位</div>
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-plus" size="mini" @click="handleMove">移位
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>
    <div class="table-container">
      <div ref="mainTablePanel" :style="{ height: mainTableDivHeight }">
        <el-table ref="stockTable" :height="mainTableHeight" v-loading="loading" :data="stockList" @selection-change="handleSelectionChange"
          @select="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />

          <el-table-column label="仓库编号" align="center" prop="storageId" />
          <el-table-column label="仓库名称" align="center" prop="storageShortName"/>
          <el-table-column label="库区编号" align="center" prop="storageAreaId" />
          <el-table-column label="库位编号" align="center" prop="locationId" />
          <el-table-column label="物料编号" align="center" prop="goodsId" />
          <el-table-column label="数量" align="center" prop="shelvesNum" />
          <el-table-column label="备注 物资计划明细备注" align="center" prop="remark" />
          <el-table-column label="版本号" align="center" prop="version" />
          <el-table-column label="入库类型" align="center" prop="storageMode" />
          <el-table-column label="中标供应商" align="center" prop="providerId" />
          <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.productionDate, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入库时间" align="center" prop="wareDate" />
          <el-table-column label="单据编号" align="center" prop="doctNumber" />
          <el-table-column label="商品体积" align="center" prop="goodsVolume" />
          <el-table-column label="已占用数量" align="center" prop="occupyNum" />
          <el-table-column label="通知单号" align="center" prop="noticeId" />
          <el-table-column label="图号" align="center" prop="oriBill" />
          <el-table-column label="占用容器" align="center" prop="ctl" />
          <el-table-column label="条形码" align="center" prop="barcode" />
          <el-table-column label="宽" align="center" prop="wgh" />
          <el-table-column label="高" align="center" prop="high" />
          <el-table-column label="物料名称" align="center" prop="goodsName" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <span :style="{ color: scope.row.status === '1' ? 'red' : 'green' }">
                {{ scope.row.status === "1" ? "冻结" : "未冻结" }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="招标员" align="center" prop="customerId" />
          <el-table-column label="生产批号" align="center" prop="batchNo" />
          <el-table-column label="过期时间" align="center" prop="dueDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.dueDate, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="检查时间" align="center" prop="checkDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.checkDate, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="标准" align="center" prop="standard" />
          <el-table-column label="标注过期" align="center" prop="flag" />
          <el-table-column label="过期天数" align="center" prop="mark" />
          <el-table-column label="订单数量" align="center" prop="orderNum" />
          <el-table-column label="物料问题" align="center" prop="problem" />
          <el-table-column label="计划单价" align="center" prop="price" />
          <el-table-column label="出库数量" align="center" prop="packingNum" />
          <el-table-column label="料箱类型" align="center" prop="boxType" />
          <el-table-column label="测量单位" align="center" prop="scaleUnit" />
          <el-table-column label="物料SKU" align="center" prop="goodsSku" />
          <el-table-column label="工厂ID" align="center" prop="factoryId" />
          <el-table-column label="销售订单" align="center" prop="salesOrderNo" />
          <el-table-column label="子需求编号" align="center" prop="salesOrderNumber" />
          <el-table-column label="申请部门" align="center" prop="supplier" />
          <el-table-column label="在途数量" align="center" prop="onOrder" />
          <el-table-column label="操作人" align="center" prop="recordPerson" />
          <el-table-column label="入库任务ID" align="center" prop="lotId" />
          <el-table-column label="堆垛机编号" align="center" prop="decid" />
          <el-table-column label="单位" align="center" prop="unit" />
          <el-table-column label="规格" align="center" prop="specification" />
          <el-table-column label="库龄" align="center" prop="goodsAge" />
          <el-table-column label="需求数量" align="center" prop="arraMount" />
          <el-table-column label="物料描述" align="center" prop="recoil" />
          <el-table-column label="需求组织" align="center" prop="operatorId" />
          <el-table-column label="创建时间" align="center" prop="operateDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.operateDate, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="申请单号" align="center" prop="purchaseId" />
          <el-table-column label="申请人" align="center" prop="applicant" />
          <el-table-column label="主管部门" align="center" prop="respDept" />
          <el-table-column label="计划日期" align="center" prop="planDate" />
          <el-table-column label="计划时间" align="center" prop="planTime" />
          <el-table-column label="需求周期" align="center" prop="needCycle" />
          <el-table-column label="需求类型" align="center" prop="needType" />
          <el-table-column label="物料分类" align="center" prop="goodsTypeId" />
          <el-table-column label="品牌" align="center" prop="brand" />
          <el-table-column label="计划金额" align="center" prop="totalMoney" />
          <el-table-column label="需求时间" align="center" prop="expiryDate" />
          <el-table-column label="建议供应商名称" align="center" prop="providerName" />
          <el-table-column label="收货人" align="center" prop="receiver" />
          <el-table-column label="需求部门" align="center" prop="needDept" />
          <el-table-column label="采购组织" align="center" prop="purchaseDept" />
          <el-table-column label="收货组织" align="center" prop="produclotId" />
          <el-table-column label="收货人部门" align="center" prop="receiverDept" />
          <el-table-column label="送货区域" align="center" prop="receivingArea" />
          <el-table-column label="技术需求附件" align="center" prop="packageId" />
          <el-table-column label="用途" align="center" prop="useType" />
          <el-table-column label="中标供应商联系方式" align="center" prop="telephone" />
          <el-table-column label="总重量" align="center" prop="totalVolume" />
          <el-table-column label="毛重" align="center" prop="totalRoughWeight" />
          <el-table-column label="净重" align="center" prop="totalNetWeigh" />
          <el-table-column label="默认存储库区" align="center" prop="defaultStorageAreaId" />
          <el-table-column label="质检状态" align="center" prop="qualityInspecStatus" />
          <el-table-column label="保质期单位" align="center" prop="periodUnit" />
          <el-table-column label="保质期" align="center" prop="period" />
          <el-table-column label="上报时间" align="center" prop="reportTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.reportTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="特殊标志 空：一般库存、E 销售订单库存、K 寄售库存" align="center" prop="consignment" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                v-hasPermi="['system:stock:remove']">{{ scope.row.status === "0" ? "冻结" : "解冻" }}
              </el-button>
            </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>

    <!-- 添加或修改库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="助记码" prop="pyName">
          <el-input v-model="queryParams.pyName" placeholder="请输入助记码" clearable @keyup.enter.native="handleQuery1" />
        </el-form-item>
        <el-form-item label="库位编码" prop="locationId">
          <el-input v-model="queryParams.locationId" placeholder="请输入库位编码" clearable
            @keyup.enter.native="handleQuery1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery1">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="singTable" :data="positionList" stripe size="mini" highlight-current-row style="width: 100%"
        @current-change="selectionRow">
        <el-table-column type="index" align="center" label="序号" width="50" />
        <el-table-column prop="locationId" align="center" label="库位编码" width="150">
        </el-table-column>
        <el-table-column prop="storageId" align="center" label="仓库编码" width="150">
        </el-table-column>
        <el-table-column prop="pyName" align="center" label="助记码" width="150">
        </el-table-column>
        <el-table-column prop="status" align="center" label="状态" width="100">
          <template slot-scope="scope">
            <span :style="{ color: scope.row.status === '1' ? 'red' : 'green' }">
              {{ scope.row.status === "1" ? "冻结" : "未冻结" }}
            </span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total1 > 0" :total="total1" :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize" @pagination="getPositionList" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addStock,
  frozen,
  getStock,
  getStorageAreaList,
  getStorageList,
  listPosition,
  listStock,
  thaw,
  updateStock,
} from "@/api/system/tMiStock";
import { delUser } from "@/api/system/user";
// 获取数据字典数据

export default {
  name: "Stock",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 库位列表
      positionList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库位总条数
      total1: 0,
      // 库位查询条件
      queryParams1: {
        pageNum: 1,
        pageSize: 100,

        pyName: null,
        storageId: null,
        sort: null,
        status: null,
      },
      // 被选择库位
      selectedPosition: null,
      // 被选择仓库
      selectedStore: null,
      // 库存表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        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: {},
      mainTableHeight: "0px",
      mainTableDivHeight: "0px",
    };
  },
  created() {
    this.getStockList();
    this.getStockAreaList();
    this.getList();
  },
  mounted() {
    this.computedTableHeight();
  },
  watch: {
    showSearch: function () {
      this.computedTableHeight();
    },
  },
  methods: {
    computedTableHeight() {
      this.$nextTick(() => {
        var totalHeight = this.$refs.appPanel.clientHeight;
        var toolHeight = this.$refs.toolPanel.clientHeight;

        var tablePanelHeight = totalHeight - toolHeight - 14 - 5;

        this.mainTableDivHeight = tablePanelHeight + "px";
        this.mainTableHeight = tablePanelHeight - 25 + "px";
      });
    },
    getStockList() {
      getStorageList().then((result) => {
        console.log("getStorageList", result);
      });
    },
    getStockAreaList() {
      getStorageAreaList().then((result) => {
        console.log("getStorageAreaList", result);
      });
    },
    /** 查询库存列表  getStorageAreaList*/
    getList() {
      this.loading = true;
      listStock(this.queryParams).then((response) => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询库位列表数据 */
    getPositionList() {
      this.loading = true;
      listPosition(this.queryParams).then((response) => {
        debugger;
        this.positionList = response.rows;
        this.total1 = 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();
    },
    /** 库位搜索按钮操作 */
    handleQuery1() {
      this.queryParams1.pageNum = 1;
      this.getPositionList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection.length > 0) {
        if (selection.length > 1) {
          this.$modal.msgWarning("每次只能选择一条数据进行操作！");
          this.$refs["stockTable"].toggleRowSelection(
            selection[selection.length - 1],
            false
          );
          return false;
        }
        this.selectedStore = selection[0];
      }
    },
    /** 移位按钮操作 */
    handleMove() {
      if (this.selectedStore === null) {
        this.$modal.msgWarning("请选择要移位的库存！");
        return false;
      }
      listPosition().then((res) => {
        this.positionList = res.rows;
        this.total1 = res.total;
      });
      this.open = true;
      this.title = "物料移位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stockId = row.id || this.ids;
      getStock(stockId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存";
      });
    },
    selectionRow(currRow) {
      this.selectedPosition = currRow;

      // this.$refs["singTable"].toggleRowSelection();
    },
    /** 确认选择按钮 */
    submitForm1() {
      if (this.selectedPosition !== null) {
        updateStock({
          ...this.selectedStore,
          locationId: this.selectedPosition.locationId,
        }).then((response) => {
          this.open = false;
          this.$modal.msgSuccess("移位成功");
          this.getList();
        });
      } else {
        this.$modal
          .confirm("请选择目标库位")
          .then(function () {
            return delUser(userIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => { });
      }
      // this.$refs["singTable"]
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStock(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 冻结解冻 */
    handleDelete(row) {
      const stockIds = row.id || this.ids;
      if (row.status === "0") {
        this.$modal
          .confirm('是否确认冻结库存编号为"' + stockIds + '"的数据项？')
          .then(function () {
            return frozen(stockIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("冻结成功");
          })
          .catch(() => { });
      } else if (row.status === "1") {
        this.$modal
          .confirm('是否确认解冻库存编号为"' + stockIds + '"的数据项？')
          .then(function () {
            return thaw(stockIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("解冻成功");
          })
          .catch(() => { });
      }
    },

    frozen(row) {
      const stockIds = row.id || this.ids;
      this.$modal
        .confirm("是否确认冻结选中的库存项")
        .then(function () {
          return frozen(stockIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("冻结成功");
        })
        .catch(() => { });
    },

    thaw(row) {
      const stockIds = row.id || this.ids;
      this.$modal
        .confirm("是否确认解冻选中的库存项")
        .then(function () {
          return thaw(stockIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("解冻成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/stock/export",
        {
          ...this.queryParams,
        },
        `stock_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}
</style>
