<template>
  <el-dialog
    v-dialogDrag
    :title="`仓库区域-仓库编码-${stockCode}-仓库名称-${names}`"
    :visible.sync="openView"
    width="1200px"
    append-to-body
    @open="dialogOpen"
    @close="dialogClose"
  >
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <SearchBar :show-span="6" :collapse-span="6" :show-more="false">
        <template #header>
          <el-col :span="6">
            <el-form-item label="区域编码" prop="areaCode">
              <el-input
                v-model="queryParams.areaCode"
                placeholder="请输入仓库区域编码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料类别" prop="materilType">
              <el-select
                v-model="queryParams.materilType"
                placeholder="请选择物料类别"
                clearable
                size="small"
              >
                <el-option
                  v-for="dict in getDictDatas(
                    DICT_TYPE.MES_MATERIAL_CATEGORIES
                  )"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="货位数量" prop="locationQty">
              <el-input
                v-model="queryParams.locationQty"
                placeholder="请输入货位数量"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #buttons>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </template>
      </SearchBar>
    </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"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" border size="mini">
      <el-table-column label="区域编码" align="center" prop="areaCode" />
      <el-table-column
        label="区域名称"
        align="center"
        prop="name"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="物料类别" align="center" prop="materilType">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.MES_MATERIAL_CATEGORIES"
            :value="scope.row.materilType"
          />
        </template>
      </el-table-column>
      <el-table-column label="货位数量" align="center" prop="locationQty" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:stock-area:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['wms:stock-area:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="text-danger"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-location-information"
            @click="handleLocation(scope.row)"
            >货位</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :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="wmsStockId">
          <el-input v-model="names" placeholder="请输入仓库名称" disabled />
        </el-form-item>
        <el-form-item label="区域编码" prop="areaCode">
          <el-input
            v-model="form.areaCode"
            placeholder="仓库区域编码自动生成"
            disabled
          />
        </el-form-item>
        <el-form-item label="区域名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入仓库区域名称" />
        </el-form-item>
        <el-form-item label="物料类别" prop="materilType">
          <el-select v-model="form.materilType" placeholder="请选择物料类别">
            <el-option
              v-for="dict in getDictDatas(DICT_TYPE.MES_MATERIAL_CATEGORIES)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="货位数量" prop="locationQty">
          <el-input
            v-model="form.locationQty"
            placeholder="请输入货位数量"
            oninput="value=value.replace(/^\D*(\d*(?:.\d{0,7})?).*$/g, '$1')"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            :autosize="{ minRows: 4, maxRows: 6 }"
            type="textarea"
            maxlength="200"
            show-word-limit
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
    <!-- 对话框(货位) -->
    <StockLocation
      :id="areaId"
      ref="stockLocation"
      :code="codes"
      :names="areaName"
    />
  </el-dialog>
</template>

<script>
import {
  createStockArea,
  updateStockArea,
  deleteStockArea,
  getStockArea,
  getStockAreaPage,
  exportStockAreaExcel,
} from "@/api/wms/base/stockArea";
import StockLocation from "./StockLocation.vue";
export default {
  name: "StockArea",
  components: {
    StockLocation,
  },
  props: {
    ids: {
      type: [Number, String],
      default: "",
    },
    code: {
      type: String,
      default: "",
    },
    names: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      loading: true,
      editDisable: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WMS 仓库 仓库区域列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      viewOpen: false,
      openView: false,
      // 区域id
      areaId: "",
      areaName: "",
      // 货位编码
      codes: "",
      stockCode: null,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        wmsStockId: null,
        areaCode: null,
        materilType: null,
        locationQty: null,
        remark: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wmsStockId: [
          { required: true, message: "对应仓库ID不能为空", trigger: "blur" },
        ],
        // areaCode: [{ required: true, message: '仓库区域编码不能为空', trigger: 'blur' }],
        name: [
          { required: true, message: "仓库区域名称不能为空", trigger: "blur" },
        ],
        materilType: [
          { required: true, message: "物料类别不能为空", trigger: "change" },
        ],
        locationQty: [
          { required: true, message: "货位数量不能为空", trigger: "blur" },
        ],
      },
    };
  },
  // created() {
  //   this.getList()
  // },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      this.queryParams.wmsStockId = this.ids;
      getStockAreaPage(this.queryParams)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsStockId: undefined,
        areaCode: undefined,
        materilType: undefined,
        locationQty: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加WMS 仓库 仓库区域";
      this.form.wmsStockId = this.ids;
      // this.form.areaCode = this.code
      this.form.names = this.names;
      this.editDisable = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.editDisable = true;
      const id = row.id;
      getStockArea(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改WMS 仓库 仓库区域";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交

        if (this.form.id != null) {
          updateStockArea(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createStockArea(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id, areaCode } = row;
      this.$modal
        .confirm(
          '是否确认删除WMS 仓库 仓库区域编码为"' + areaCode + '"的数据项?'
        )
        .then(function () {
          return deleteStockArea(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有WMS 仓库 仓库区域数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportStockAreaExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "WMS 仓库 仓库区域.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    // 货位
    handleLocation(row) {
      const { id, areaCode, name } = row;
      this.codes = areaCode;
      this.areaName = name;
      this.$refs.stockLocation.viewOpen = true;
      this.$refs.stockLocation.queryParams.wmsStockAreaId = id;
    },
    dialogOpen() {
      this.getList();
    },
    dialogClose() {
      this.list = [];
    },
  },
};
</script>
