<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--仓库数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="warehouseName"
            placeholder="请输入仓库名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="warehouseList"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="warehouseId"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>


      <!--库位数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="库位编号" prop="locationCode">
            <el-input
              v-model="queryParams.locationCode"
              placeholder="请输入库位编号"
              style="width: 200px"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="层" prop="layerNum">
            <el-input
              v-model="queryParams.layerNum"
              placeholder="请输入层"
              style="width: 100px"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="排" prop="locRow">
            <el-input
              v-model="queryParams.locRow"
              placeholder="请输入排"
              style="width: 100px"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="列" prop="locColumn">
            <el-input
              v-model="queryParams.locColumn"
              placeholder="请输入列"
              style="width: 100px"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="人工处理标识" prop="manualFlag" label-width="98px">
            <el-select
              v-model="queryParams.manualFlag"
              placeholder="请选择人工处理标识"
              style="width:170px;"
              clearable
            >
              <el-option
                v-for="dict in dict.type.wms_location_manual_flag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="入库过度标识" prop="instockFlag" label-width="98px">
            <el-select
              v-model="queryParams.instockFlag"
              placeholder="请选择入库过度标识"
              style="width:160px;"
              clearable
            >
              <el-option
                v-for="dict in dict.type.wms_location_instock_flag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出库过度标识" prop="outstockFlag" label-width="98px">
            <el-select
              v-model="queryParams.outstockFlag"
              placeholder="请选择出库过度标识"
              style="width:160px;"
              clearable
            >
              <el-option
                v-for="dict in dict.type.wms_location_outstock_flag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="激活标识" prop="activeFlag">
            <el-select
              v-model="queryParams.activeFlag"
              placeholder="请选择激活标识"
              style="width:160px;"
              clearable
            >
              <el-option
                v-for="dict in dict.type.wms_location_active_flag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>

          </el-form-item>

          <el-form-item label="库位状态" prop="locationStatus">
            <el-select
              v-model="queryParams.locationStatus"
              placeholder="请选择库位状态"
              style="width:160px;"
              clearable
            >
              <el-option
                v-for="dict in dict.type.wms_location_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </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="['wms:wmslocation:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['wms:wmslocation:edit']"
            >修改
            </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="['wms:wmslocation:remove']"
            >删除
            </el-button>
          </el-col>
          <!--el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['wms:wmslocation:export']"
            >导出
            </el-button>
          </el-col-->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="wmslocationList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="库位编号" align="center" prop="locationCode"/>
          <el-table-column label="仓库" align="center" prop="warehouseName"/>
          <el-table-column label="层" align="center" prop="layerNum"/>
          <el-table-column label="排" align="center" prop="locRow"/>
          <el-table-column label="列" align="center" prop="locColumn"/>
          <el-table-column label="数量限制" align="center" prop="qtyLimit"/>
          <el-table-column label="人工处理标识" align="center" prop="manualFlag">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_location_manual_flag" :value="scope.row.manualFlag"/>
            </template>
          </el-table-column>
          <el-table-column label="入库过度标识" align="center" prop="instockFlag">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_location_instock_flag" :value="scope.row.manualFlag"/>
            </template>
          </el-table-column>
          <el-table-column label="出库过度标识" align="center" prop="outstockFlag">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_location_outstock_flag" :value="scope.row.manualFlag"/>
            </template>
          </el-table-column>
          <el-table-column label="激活标识" align="center" prop="activeFlag">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_location_active_flag" :value="scope.row.manualFlag"/>
            </template>
          </el-table-column>
          <el-table-column label="库位状态" align="center" prop="locationStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_location_status" :value="scope.row.locationStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['wms:wmslocation:edit']"
              >修改
              </el-button>
              <el-button
                v-if="scope.row.locationStatus=='3'"
                size="mini"
                type="text"
                icon="el-icon-unlock"
                @click="handleUnlock(scope.row)"
                v-hasPermi="['wms:wmslocation:edit']"
              >
                解锁
              </el-button>
              <el-button
                v-if="scope.row.locationStatus=='1'"
                size="mini"
                type="text"
                icon="el-icon-lock"
                @click="handleLock(scope.row)"
                v-hasPermi="['wms:wmslocation:edit']"
              >
                锁定
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['wms:wmslocation: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-col>
    </el-row>


    <!-- 添加或修改库位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="660px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="请选择仓库" style="width:300px;">
            <el-option
              v-for="item in warehouseList"
              :key="item.warehouseId"
              :label="item.warehouseName"
              :value="item.warehouseId"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-row>
          <el-col :span="12">
            <el-form-item label="库位编号" prop="locationCode">
              <el-input v-model="form.locationCode" placeholder="请输入库位编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量限制" prop="qtyLimit">
              <el-input v-model="form.qtyLimit" placeholder="请输入数量限制"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="7">
            <el-form-item label="层" prop="layerNum">
              <el-input-number v-model="form.layerNum" placeholder="请输入层" :min="1" :max="10000"
                               style="width:130px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="排" prop="locRow">
              <el-input-number v-model="form.locRow" placeholder="请输入排" :min="1" :max="10000" style="width:130px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="列" prop="locColumn">
              <el-input-number v-model="form.locColumn" placeholder="请输入列" :min="1" :max="10000"
                               style="width:130px;"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="入库过度标识" prop="instockFlag">
              <el-radio-group v-model="form.instockFlag">
                <el-radio
                  v-for="dict in dict.type.wms_location_instock_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库过度标识" prop="outstockFlag">
              <el-radio-group v-model="form.outstockFlag">
                <el-radio
                  v-for="dict in dict.type.wms_location_outstock_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="人工处理标识" prop="manualFlag">
              <el-radio-group v-model="form.manualFlag">
                <el-radio
                  v-for="dict in dict.type.wms_location_manual_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="激活标识" prop="activeFlag">
              <el-radio-group v-model="form.activeFlag">
                <el-radio
                  v-for="dict in dict.type.wms_location_active_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWmslocation,
  getWmslocation,
  delWmslocation,
  addWmslocation,
  updateWmslocation,
  getWarehouses,
  lockWmslocation,
  unlockWmslocation
} from "@/api/wms/wmslocation";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import toggleMenuLeftImg from '@/assets/images/togglemenu-left.png'
import toggleMenuRightImg from '@/assets/images/togglemenu-right.png'

export default {
  name: "Wmslocation",
  components: {Treeselect},
  dicts: ['wms_location_active_flag', 'wms_location_manual_flag', 'wms_location_instock_flag', 'wms_location_outstock_flag', 'wms_location_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组code
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: null,
        locationCode: null,
        locRow: null,
        layerNum: null,
        locColumn: null,
        activeFlag: null,
        manualFlag: null,
        materialTypeId: null,
        qtyLimit: null,
        instockFlag: null,
        outstockFlag: null,
        locationStatus: null,
        batchMix: null,
        shelfOrder: null,
        checkOrder: null,
        pickOrder: null,
        pickFlag: null,
        isOpenKnFlag: null,
        locationScrapType: null,
        locationAttr: null,
        turnDemand: null,
        checkCode: null,
        workArea: null,
        volumeLimit: null,
        weightLimit: null,
        boxLimit: null,
        palletLimit: null,
        length: null,
        width: null,
        height: null,
        xPixels: null,
        yPixels: null,
        zPixels: null,
        bord: null,
        productMix: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseId: [
          {required: true, message: "仓库不能为空", trigger: "blur"}
        ],
        locationCode: [
          {required: true, message: "库位编号不能为空", trigger: "blur"},
          {
            pattern: /^[a-zA-Z0-9_]+$/,
            message: "由字母、数字或下划线组成",
            trigger: "blur"
          }
        ],
        activeFlag: [
          {required: true, message: "激活标记 1是 0否不能为空", trigger: "blur"}
        ],
        materialTypeId: [
          {required: true, message: "存放物料类型,关联material_type的material_type_id不能为空", trigger: "blur"}
        ],
        qtyLimit: [
          {required: true, message: "数量限制不能为空", trigger: "blur"}
        ],

      },

      globalVariables: {
        WMS_LOCATION_STATUS_NORMAL: 1,//正常
        WMS_LOCATION_STATUS_MANUAL_LOCK: 3 //人工锁定
      },
      toggleMenuLeftImg: toggleMenuLeftImg,
      toggleMenuRightImg: toggleMenuRightImg,
      toggleLeftDisplay: true,
      toggleRightDisplay: false,
      // 仓库名称
      warehouseName: undefined,
      warehouseList: [],
      defaultProps: {
        children: "children",
        label: "warehouseName"
      },
      // 库位表格数据
      wmslocationList: [],
      lockBtn: "锁定",
      lockIcon: 'el-icon-lock'

    };
  },
  watch: {
    // 根据名称筛选仓库列表
    warehouseName(val) {
      this.$refs.tree.filter(val);
    }
  },

  created() {
    this.getWarehouseList();
    this.getList();
  },
  methods: {
    /** 查询库位列表 */
    getList() {

      this.loading = true;
      listWmslocation(this.queryParams).then(response => {
        this.wmslocationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        locationId: null,
        warehouseId: this.queryParams.warehouseId,
        locationCode: null,
        locRow: null,
        layerNum: null,
        locColumn: null,
        activeFlag: null,
        manualFlag: null,
        materialTypeId: null,
        qtyLimit: null,
        instockFlag: null,
        outstockFlag: null,
        locationStatus: null,
        batchMix: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        shelfOrder: null,
        checkOrder: null,
        pickOrder: null,
        pickFlag: null,
        isOpenKnFlag: null,
        locationScrapType: null,
        locationAttr: null,
        turnDemand: null,
        checkCode: null,
        workArea: null,
        volumeLimit: null,
        weightLimit: null,
        boxLimit: null,
        palletLimit: null,
        length: null,
        width: null,
        height: null,
        xPixels: null,
        yPixels: null,
        zPixels: null,
        bord: null,
        productMix: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.warehouseId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.locationId)
      this.codes = selection.map(item => item.locationCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const locationId = row.locationId || this.ids
      getWmslocation(locationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.locationId != null) {
            updateWmslocation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWmslocation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const locationIds = row.locationId || this.ids;
      const locationCodes = row.locationCode || this.codes;
      this.$modal.confirm('是否确认删除库位编号为"' + locationCodes + '"的数据项？').then(function () {
        return delWmslocation(locationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/wmslocation/export', {
        ...this.queryParams
      }, `wmslocation_${new Date().getTime()}.xlsx`)
    },

    toggleClick() {
      if (this.toggleLeftDisplay) {
        this.toggleRightDisplay = true;
        this.toggleLeftDisplay = false;
      } else if (this.toggleRightDisplay) {
        this.toggleRightDisplay = false;
        this.toggleLeftDisplay = true;
      }
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.warehouseName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.warehouseId = data.warehouseId;
      this.handleQuery();
    },

    getWarehouseList() {
      this.loading = true;
      getWarehouses().then(response => {
        this.warehouseList = response.data;
        this.loading = false;
      });
    },

    handleLock(row) {
      const locationId = row.locationId;
      const locationCode = row.locationCode;
      this.$modal.confirm('是否确认锁定库位编号为"' + locationCode + '"的数据项？').then(function () {
        return lockWmslocation(locationId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("锁定成功");
      }).catch(() => {
        this.getList();
      });
    },

    handleUnlock(row) {
      const locationId = row.locationId;
      const locationCode = row.locationCode;
      this.$modal.confirm('是否确认解锁库位编号为"' + locationCode + '"的数据项？').then(function () {
        return unlockWmslocation(locationId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("解锁成功");
      }).catch(() => {
        this.getList();
      });
    },

  }
};
</script>
<style scoped>
.hamburger {
  display: inline-block;
  width: 20px;
  height: 20px;
  vertical-align: middle;
  margin: 5px 5px 0 0;
  cursor: pointer;
}
</style>
