<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库编码" prop="warehouseCode">
        <el-input
          v-model="queryParams.warehouseCode"
          placeholder="请输入仓库编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库类别" prop="warehouseCategoryId">
        <el-select v-model="queryParams.warehouseCategoryId" placeholder="请选择仓库类别" clearable>
          <el-option
            v-for="(category, index) in categoryOptions"
            :key="index"
            :label="category.categoryName"
            :value="category.categoryId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="仓库类型" prop="warehouseType">
        <el-select
          v-model="queryParams.warehouseType"
          placeholder="请选择仓库类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.wms_warehouse_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="存放类型" prop="warehouseInstockType">
        <el-select
          v-model="queryParams.warehouseInstockType"
          placeholder="请选择存放类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.wms_warehouse_instock_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="楼层" prop="warehouseFloor">
        <el-input
          v-model="queryParams.warehouseFloor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="入库要求" prop="inRequirement">
        <el-select
          v-model="queryParams.inRequirement"
          placeholder="请选择入库要求"
          clearable
        >
          <el-option
            v-for="dict in dict.type.wms_in_requirement"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="出库要求" prop="outRequirement">
        <el-select
            v-model="queryParams.outRequirement"
            placeholder="请选择出库要求"
            clearable
        >
          <el-option
              v-for="dict in dict.type.wms_out_requirement"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="出库要求" prop="outRequirement">
        <el-select
            v-model="queryParams.outRequirement"
            placeholder="请选择出库要求"
            clearable
        >
          <el-option
              v-for="dict in dict.type.wms_out_requirement"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="退库要求" prop="returnRequirement">
        <el-select
            v-model="queryParams.returnRequirement"
            placeholder="请选择退库要求"
            clearable
        >
          <el-option
              v-for="dict in dict.type.wms_in_requirement"
              :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="请选择激活标识"
          clearable
        >
          <el-option
            v-for="dict in dict.type.wms_warehouse_active_flag"
            :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:wmswarehouse: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:wmswarehouse: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:wmswarehouse: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:wmswarehouse:export']"
        >导出</el-button>
      </el-col-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmswarehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库编码" align="center" prop="warehouseCode" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="仓库类别" align="center" prop="warehouseCategoryName" />
      <el-table-column label="仓库类型" align="center" prop="warehouseType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_type" :value="scope.row.warehouseType"/>
        </template>
      </el-table-column>
      <el-table-column label="存放类型" align="center" prop="warehouseInstockType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_instock_type" :value="scope.row.warehouseInstockType"/>
        </template>
      </el-table-column>
      <el-table-column label="楼层" align="center" prop="warehouseFloor" />
      <el-table-column label="入库要求 " align="center" prop="inRequirement" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_in_requirement" :value="scope.row.inRequirement"/>
        </template>
      </el-table-column>
      <el-table-column label="激活标识" align="center" prop="activeFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_active_flag" :value="scope.row.activeFlag"/>
        </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="handleAllocateMaterial(scope.row)"
            v-hasPermi="['wms:wmswarehouse:materialallocate']"
          >物料配置</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:wmswarehouse:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:wmswarehouse: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库编码" prop="warehouseCode">
              <el-input v-model="form.warehouseCode" placeholder="请输入仓库编码" maxlength="64"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库类别" prop="warehouseCategoryId">
              <el-select v-model="form.warehouseCategoryId" placeholder="请选择仓库类别">
                <el-option
                  v-for="item in categoryOptions"
                  :key="item.categoryId"
                  :label="item.categoryName"
                  :value="item.categoryId"
                ></el-option>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
            </el-form-item>
          </el-col>
        </el-row>



        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库类型" prop="warehouseType">
              <el-select v-model="form.warehouseType" placeholder="请选择仓库类型" @input="warehouseTypeChange">
                <el-option
                  v-for="dict in dict.type.wms_warehouse_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存放类型" prop="warehouseInstockType">
              <el-select v-model="form.warehouseInstockType" placeholder="请选择存放物料类型">
                <el-option
                  v-for="dict in dict.type.wms_warehouse_instock_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="多排库" prop="multiRowType">
              <el-select v-model="form.multiRowType" placeholder="请选择多排库类型">
                <el-option
                  v-for="dict in dict.type.wms_multi_row_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="多边库" prop="multiSideType">
              <el-select v-model="form.multiSideType" placeholder="请选择多边库类型">
                <el-option
                  v-for="dict in dict.type.wms_multi_side_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="楼层" prop="warehouseFloor">
              <el-input-number v-model="form.warehouseFloor" placeholder="请输入楼层"  :max="10000"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置" prop="warehouseLocation">
              <el-input v-model="form.warehouseLocation" placeholder="请输入位置" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="混放批次" prop="batchMix">
              <el-radio-group v-model="form.batchMix" :disabled="disableFlag">
                <el-radio
                  v-for="dict in dict.type.wms_batch_mix_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="productMix">
              <el-radio-group v-model="form.productMix" :disabled="disableFlag">
                <el-radio
                  v-for="dict in dict.type.wms_product_mix_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="workbinFlag">
              <el-radio-group v-model="form.workbinFlag">
                <el-radio
                  v-for="dict in dict.type.wms_workbin_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="returnFlag">
              <el-radio-group v-model="form.returnFlag">
                <el-radio
                  v-for="dict in dict.type.wms_return_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>



        <el-row>
            <el-form-item label="入库要求" prop="inRequirement">
              <el-radio-group v-model="form.inRequirement" :disabled="disableFlag">
                <el-radio
                    v-for="dict in dict.type.wms_in_requirement"
                    :key="dict.value"
                    :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="出库要求" prop="outRequirement">
            <el-radio-group v-model="form.outRequirement" :disabled="disableFlag">
              <el-radio
                  v-for="dict in dict.type.wms_out_requirement"
                  :key="dict.value"
                  :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="退库要求" prop="returnRequirement">
            <el-radio-group v-model="form.returnRequirement" :disabled="disableFlag">
              <el-radio
                  v-for="dict in dict.type.wms_return_requirement"
                  :key="dict.value"
                  :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="自动标识" prop="autoFlag">
            <el-radio-group v-model="form.autoFlag" :disabled="disableFlag">
              <el-radio
                  v-for="dict in dict.type.wms_auto_flag"
                  :key="dict.value"
                  :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>


        <el-row>
            <el-form-item label="激活标识" prop="activeFlag">
              <el-radio-group v-model="form.activeFlag">
                <el-radio
                  v-for="dict in dict.type.wms_warehouse_active_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
        </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 {
  listWmswarehouse,
  getWmswarehouse,
  delWmswarehouse,
  addWmswarehouse,
  updateWmswarehouse,
  getWarehouseCategories,
  getDeptTreeForWarehouse
} from "@/api/wms/wmswarehouse";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Wmswarehouse",
  dicts: ['wms_warehouse_instock_type', 'wms_warehouse_type','wms_multi_row_type','wms_multi_side_type','wms_batch_mix_flag','wms_product_mix_flag',
    'wms_in_requirement','wms_out_requirement','wms_return_requirement','wms_auto_flag',
    'wms_return_flag','wms_workbin_flag','wms_warehouse_active_flag'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组的ID
      ids: [],
      // 选中数组的CODE
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      wmswarehouseList: [],

      disableFlag:false,
      // 弹出层标题
      title: "",
      // 仓库类别
      categoryOptions: undefined,
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseInstockType: null,
        warehouseType: null,
        warehouseCode: null,
        warehouseName: null,
        warehouseCategoryId: null,
        warehouseFloor: null,
        deptId: null,
        multiRowType: null,
        multiSideType: null,
        batchMix: null,
        productMix: null,
        workbinFlag: null,
        warehouseLocation: null,
        activeFlag: null,
        inRequirement: null,
        returnFlag: null,
        factoryId: null,
        dataSource: null,
        schame: null,
        lineFlag: null
      },

      globalVariables: {
           WAREHOUSE_TYPE_NORMAL:'1',//普通仓库
           WAREHOUSE_TYPE_AGV:'2',//背负agv仓库
           WAREHOUSE_TYPE_CTU:'3',//料箱agv仓库

           IN_REQUIREMENT_APPLY:'0',

           OUT_REQUIREMENT_APPLY:'0',//申请出库，agv仓库默认
           OUT_REQUIREMENT_APPLY_AUDIT:'1',//申请审核出库，普通仓库默认

           RETURN_REQUIREMENT_APPLY_AUDIT:'1',//申请审核退库

           AUTO_FLAG_MANUAL:'0',//全人工


           BATCH_MIX_NO:'0',

           PRODUCT_MIX_NO:'0',

           WORKBIN_FLAG_NO:'0',

           ACTIVE_FLAG_YES:'1',

           RETURN_FLAG_NO:'0'

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseCategoryId: [
          { required: true, message: "仓库类别不能为空", trigger: "change" }
        ],
        warehouseType: [
          { required: true, message: "仓库类型不能为空", trigger: "change" }
        ],
        warehouseCode: [
          { required: true, message: "仓库编码不能为空", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9_]+$/,
            message: "由字母、数字或下划线组成",
            trigger: "blur"
          }

        ],
        warehouseName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        warehouseFloor: [
          { required: true, message: "楼层不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "归属部门不能为空", trigger: "blur" }
        ],
        multiRowType: [
          { required: true, message: "多排库类型不能为空", trigger: "change" }
        ],
        multiSideType: [
          { required: true, message: "多边库类型不能为空", trigger: "change" }
        ],
        batchMix: [
          { required: true, message: "混放批次不能为空", trigger: "blur" }
        ],
        productMix: [
          { required: true, message: "混放批次不能为空", trigger: "blur" }
        ],
        workbinFlag: [
          { required: true, message: "激活标识不能为空", trigger: "blur" }
        ],
        inRequirement: [
          { required: true, message: "入库要求不能为空", trigger: "blur" }
        ],
        outRequirement: [
          { required: true, message: "出库要求不能为空", trigger: "blur" }
        ],
        returnRequirement: [
          { required: true, message: "退库要求不能为空", trigger: "blur" }
        ],
        autoFlag: [
          { required: true, message: "自动标识不能为空", trigger: "blur" }
        ],
        activeFlag: [
          { required: true, message: "激活标记不能为空", trigger: "blur" }
        ],
        returnFlag: [
          { required: true, message: "返库标识不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getWarehouseCategories();
    this.getDeptTreeForWarehouse();
    this.getList();
  },
  methods: {
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listWmswarehouse(this.queryParams).then(response => {
        this.wmswarehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseId: null,
        warehouseInstockType: null,
        warehouseType: null,
        warehouseCode: null,
        warehouseName: null,
        warehouseCategoryId: null,
        warehouseFloor: null,
        deptId: null,
        multiRowType: null,
        multiSideType: null,
        batchMix: this.globalVariables.BATCH_MIX_NO,
        productMix: this.globalVariables.PRODUCT_MIX_NO,
        workbinFlag: this.globalVariables.WORKBIN_FLAG_NO,
        warehouseLocation: null,
        activeFlag: this.globalVariables.ACTIVE_FLAG_YES,
        inRequirement: this.globalVariables.IN_REQUIREMENT_APPLY,
        returnFlag: this.globalVariables.RETURN_FLAG_NO,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        factoryId: null,
        dataSource: null,
        schame: null,
        lineFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warehouseId)
      this.codes = selection.map(item => item.warehouseCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warehouseId = row.warehouseId || this.ids
      getWmswarehouse(warehouseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库";
      });
    },

    handleAllocateMaterial(row){
      const warehouseId = row.warehouseId;
      const warehouseName = row.warehouseName;
      const warehouseCode = row.warehouseCode;
      const params = {warehouseCode:warehouseCode,warehouseName:warehouseName};
      this.$tab.openPage("仓库物料配置[" + warehouseName + "]", '/warehouse/allocateMaterial/index/' + warehouseId, params);
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != null) {
            updateWmswarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWmswarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseIds = row.warehouseId || this.ids;
      const warehouseCodes = row.warehouseCode || this.codes;
      this.$modal.confirm('是否确认删除仓库编码为"' + warehouseCodes + '"的数据项？').then(function() {
        return delWmswarehouse(warehouseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/wmswarehouse/export', {
        ...this.queryParams
      }, `wmswarehouse_${new Date().getTime()}.xlsx`)
    },


    /** 查询仓库类别 */
    getWarehouseCategories() {
      getWarehouseCategories().then(response => {
        this.categoryOptions = response.data;
      });
    },


    /** 查询部门下拉树结构 */
    getDeptTreeForWarehouse() {
      getDeptTreeForWarehouse().then(response => {
        this.deptOptions = response.data;
      });
    },

    warehouseTypeChange(value){
      if(value == this.globalVariables.WAREHOUSE_TYPE_AGV){
         this.form.productMix = this.globalVariables.PRODUCT_MIX_NO;
         this.form.batchMix = this.globalVariables.BATCH_MIX_NO;
         this.disableFlag = true;
      }else if(value == this.globalVariables.WAREHOUSE_TYPE_CTU){
         this.disableFlag = false;
      }else{
         this.disableFlag = false;
      }

    }

  }
};
</script>
