<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="仓库" prop="warehouseId">-->
<!--        <el-select v-model="queryParams.warehouseId" placeholder="请选择所属仓库" clearable size="small" style="width: 100%">-->
<!--          <el-option-->
<!--            v-for="dict in this.warehouseCombo"-->
<!--            :key="dict.warehouseId"-->
<!--            :label="dict.warehouseName"-->
<!--            :value="dict.warehouseId"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="库区" prop="zoneId">
        <el-select v-model="queryParams.zoneId" placeholder="请选择库区" clearable size="small" style="width: 100%">
          <el-option
            v-for="dict in this.locationZoneCombo"
            :key="dict.zoneId"
            :label="dict.zoneName"
            :value="dict.zoneId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库位id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入库位编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位编码" prop="locationNo">
        <el-input
          v-model="queryParams.locationNo"
          placeholder="请输入库位编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AGV站点" prop="agvStation">
        <el-input
          v-model="queryParams.agvStation"
          placeholder="请输入AGV站点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位状态" prop="stockStatus">
        <el-select v-model="queryParams.stockStatus" placeholder="请选择库位状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.location_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为空" prop="isEmpty">
        <el-select v-model="queryParams.isEmpty" placeholder="请选择是否为空" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="储位类型" prop="locationType">
        <el-select v-model="queryParams.locationType" placeholder="请选择储位类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.location_type"
            :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="['base:locationInfo: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="['base:locationInfo: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="['base:locationInfo: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="['base:locationInfo:export']"
        >导出</el-button>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['base:locationInfo:export']"
          >导入</el-button>
        </el-col>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="locationInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库位id" align="center" prop="id" />
<!--      <el-table-column label="仓库" align="center" prop="warehouseId" />-->
      <el-table-column label="区域" align="center" prop="zoneName" />
      <el-table-column label="库位编码" align="center" prop="locationNo" width="200"/>
      <el-table-column label="AGV站点" align="center" prop="agvStation" />
      <el-table-column label="库位状态" align="center" prop="stockStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.location_status" :value="scope.row.stockStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否为空" align="center" prop="isEmpty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isEmpty"/>
        </template>
      </el-table-column>
      <el-table-column label="储位类型" align="center" prop="locationType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.location_type" :value="scope.row.locationType"/>
        </template>
      </el-table-column>
      <el-table-column label="排号" align="center" prop="rowNo" />
      <el-table-column label="排序号" align="center" prop="rowIndex" />
      <el-table-column label="列号" align="center" prop="colNo" />
      <el-table-column label="列序号" align="center" prop="colIndex" />
      <el-table-column label="层号" align="center" prop="shiftNo" />
      <el-table-column label="层序号" align="center" prop="shiftIndex" />
      <el-table-column label="物料种类" align="center" prop="bindSku" />
      <el-table-column label="物料名称" align="center" prop="userdefine10" />
<!--      <el-table-column label="限高" align="center" prop="heightLimit" />-->
<!--      <el-table-column label="限重" align="center" prop="weightLimit" />-->
      <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="['base:locationInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:locationInfo: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="请选择所属仓库" clearable size="small" style="width: 100%">
            <el-option
              v-for="dict in this.warehouseCombo"
              :key="dict.warehouseId"
              :label="dict.warehouseName"
              :value="dict.warehouseId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="库区" prop="zoneId">
          <el-select v-model="form.zoneId" placeholder="请选择库区" clearable size="small" style="width: 100%">
            <el-option
              v-for="dict in this.locationZoneCombo"
              :key="dict.zoneId"
              :label="dict.zoneName"
              :value="dict.zoneId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="库位编码" prop="locationNo">
          <el-input v-model="form.locationNo" placeholder="请输入库位编码" />
        </el-form-item>
        <el-form-item label="排号" prop="rowNo">
          <el-input v-model="form.rowNo" placeholder="请输入排号" />
        </el-form-item>
        <el-form-item label="排序号" prop="rowIndex">
          <el-input v-model="form.rowIndex" placeholder="请输入排序号" />
        </el-form-item>
        <el-form-item label="列号" prop="colNo">
          <el-input v-model="form.colNo" placeholder="请输入列号" />
        </el-form-item>
        <el-form-item label="列序号" prop="colIndex">
          <el-input v-model="form.colIndex" placeholder="请输入列序号" />
        </el-form-item>
        <el-form-item label="层号" prop="shiftNo">
          <el-input v-model="form.shiftNo" placeholder="请输入层号" />
        </el-form-item>
        <el-form-item label="层序号" prop="shiftIndex">
          <el-input v-model="form.shiftIndex" placeholder="请输入层序号" />
        </el-form-item>
        <el-form-item label="库位状态" prop="stockStatus">
          <el-select v-model="form.stockStatus" placeholder="请选择库位状态" style="width: 100%">
            <el-option
              v-for="dict in dict.type.location_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否为空" prop="isEmpty">
          <el-select v-model="form.isEmpty" placeholder="请选择是否为空" style="width: 100%">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="储位类型" prop="locationType">
          <el-select v-model="form.locationType" placeholder="请选择储位类型" style="width: 100%">
            <el-option
              v-for="dict in dict.type.location_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘架编号" prop="rackId">
          <el-input v-model="form.rackId" placeholder="请输入盘架编号" />
        </el-form-item>
        <el-form-item label="AGV点位" prop="agvStation">
          <el-input v-model="form.agvStation" placeholder="请输入AGV点位" />
        </el-form-item>
        <el-form-item label="是否堆叠" prop="foldedTag">
          <el-select v-model="form.foldedTag" placeholder="请选择是否堆叠" style="width: 100%">
            <el-option label="非堆叠" value="0"></el-option>
            <el-option label="堆叠" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="堆叠层数" prop="foldedCount">
          <el-input :disabled="form.foldedTag === '0'" v-model="form.foldedCount" placeholder="请输入堆叠层数" />
        </el-form-item>
        <el-form-item label="绑定物料" prop="bindSku">
          <el-select style="width: 100%" v-model="form.bindSku"  placeholder="请选择物料种类" clearable size="small">
            <el-option
              v-for="dict in skuTypeCombo"
              :key="dict.itemCode"
              :label="dict.itemName"
              :value="dict.itemCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称" prop="userdefine10">
          <el-input v-model="form.userdefine10" placeholder="请输入物料名称" />
        </el-form-item>
<!--        <el-form-item label="限高" prop="heightLimit">-->
<!--          <el-input v-model="form.heightLimit" placeholder="请输入限高" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="限重" prop="weightLimit">-->
<!--          <el-input v-model="form.weightLimit" placeholder="请输入限重" />-->
<!--        </el-form-item>-->
        <!--<el-form-item label="条形码" prop="locationBarcode">
          <el-input v-model="form.locationBarcode" placeholder="请输入条形码" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine1">
          <el-input v-model="form.userdefine1" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine2">
          <el-input v-model="form.userdefine2" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine3">
          <el-input v-model="form.userdefine3" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine4">
          <el-input v-model="form.userdefine4" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine5">
          <el-input v-model="form.userdefine5" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine6">
          <el-input v-model="form.userdefine6" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine7">
          <el-input v-model="form.userdefine7" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine8">
          <el-input v-model="form.userdefine8" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine9">
          <el-input v-model="form.userdefine9" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="userdefine10">
          <el-input v-model="form.userdefine10" placeholder="请输入${comment}" />
        </el-form-item>-->
        <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>
    <!-- 库位导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的库位数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLocationInfo, getLocationInfo, delLocationInfo, addLocationInfo, updateLocationInfo } from "@/api/base/locationInfo";
import { queryLocationZoneDict } from "@/api/base/locationZone";
import { queryWarehouseDict } from "@/api/base/warehouse";
import { querySkuTypeDict } from "@/api/base/skuType";
import { getToken } from "@/utils/auth";

export default {
  name: "LocationInfo",
  dicts: ['location_type', 'location_status', 'sys_yes_no', 'floor_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库位信息表格数据
      locationInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: null,
        id : null,
        zoneId: null,
        zoneName: null,
        locationNo: null,
        stockStatus: null,
        isEmpty: null,
        locationType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseId: [
          { required: true, message: "所属不能为空", trigger: "change" }
        ],
        zoneId: [
          { required: true, message: "库区不能为空", trigger: "change" }
        ],
        locationNo: [
          { required: true, message: "库位编码不能为空", trigger: "change" }
        ],
        colNo: [
          { required: true, message: "列不能为空", trigger: "change" }
        ],
        colIndex: [
          { required: true, message: "列序号不能为空", trigger: "change" }
        ],
        stockStatus: [
          { required: true, message: "库位状态不能为空", trigger: "change" }
        ],
        isEmpty: [
          { required: true, message: "是否为空不能为空", trigger: "change" }
        ],
        locationType: [
          { required: true, message: "库位状态不能为空", trigger: "change" }
        ]
      },
      locationZoneCombo: [],
      warehouseCombo: [],
      skuTypeCombo: [],
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/base/locationInfo/importData"
      }
    };
  },
  created() {
    this.init();
    this.getList();
  },
  methods: {
    init() {
      queryWarehouseDict().then(response => {
        this.warehouseCombo = response.data
      });
      queryLocationZoneDict().then(response => {
        this.locationZoneCombo = response.data
      });
      // 初始化物料分类
      querySkuTypeDict().then(response => {
        this.skuTypeCombo = response.data;
      });
    },
    /** 查询库位信息列表 */
    getList() {
      this.loading = true;
      listLocationInfo(this.queryParams).then(response => {
        this.locationInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseId: null,
        zoneId: null,
        zoneName: null,
        locationNo: null,
        locationBarcode: null,
        rowNo: null,
        rowIndex: null,
        colNo: null,
        colIndex: null,
        shiftNo: null,
        shiftIndex: null,
        stockStatus: '00',
        isEmpty: 'Y',
        bindSku: null,
        locationType: null,
        rackId: null,
        agvStation: null,
        foldedTag: '0',
        foldedCount: 0,
        heightLimit: 0,
        weightLimit: 0,
        userdefine1: null,
        userdefine2: null,
        userdefine3: null,
        userdefine4: null,
        userdefine5: null,
        userdefine6: null,
        userdefine7: null,
        userdefine8: null,
        userdefine9: null,
        userdefine10: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLocationInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLocationInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLocationInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库位信息编号为"' + ids + '"的数据项？').then(function() {
        return delLocationInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/locationInfo/export', {
        ...this.queryParams
      }, `locationInfo_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "库位信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('base/locationInfo/importTemplate', {
      }, `location_${new Date().getTime()}.xlsx`)
    },
// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
