<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
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:storageArea:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:storageArea:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:storageArea:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="multiple"
            @click="handleEnable"
            v-hasPermi="['system:storageArea:edit']"
            >启用</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDisabled"
            v-hasPermi="['system:storageArea:remove']"
            >禁用</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:storageArea:export']"
            >导出</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-search" size="mini" @click="handleQuery"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-col>
        <right-toolbar ref="toolBarRef" @customCols="customColsHandler" :showSearch.sync="showSearch"
                       showColumnsType="transfer" :columns="columns"></right-toolbar>
      </el-row>
    </div>
    <div class="search-container" ref="searchPanel">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="mini"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="库区编号" prop="storageAreaId">
          <el-input
            v-model="queryParams.storageAreaId"
            placeholder="请输入库区编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="库区名称" prop="storageAreaName">
          <el-input
            v-model="queryParams.storageAreaName"
            placeholder="请输入库区名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="库区简称" prop="storageAreaShortName">
                    <el-input
                        v-model="queryParams.storageAreaShortName"
                        placeholder="请输入库区简称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
        <!-- <el-form-item label="拼音简称" prop="pyName">
                    <el-input
                        v-model="queryParams.pyName"
                        placeholder="请输入拼音简称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
        <el-form-item label="仓库编号" prop="storageId">
          <el-select
            v-model="queryParams.storageId"
            placeholder="请选择仓库"
            clearable
          >
            <el-option
              v-for="dict in storageList"
              :key="dict.storageId"
              :label="dict.storageName"
              :value="dict.storageId"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="排序" prop="sort">
                    <el-input
                        v-model="queryParams.sort"
                        placeholder="请输入排序"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
      </el-form>
    </div>


    <div class="table-container">
      <div ref="mainTablePanel" :style="{ height: mainTableDivHeight }">
        <el-table :key="rndKey" ref="providerTableRef" v-loading="loading" border size="mini" :height="mainTableHeight"
                  :data="storageAreaList" @selection-change="handleSelectionChange" @header-dragend="handleHeaderDragEnd">
          <el-table-column type="selection" width="55" align="center" />

            <el-table-column v-for="item in columns" :key="item.key" :label="item.label" align="center"
                             :prop="item.columnName" v-if="item.visible" :width="item.width">
              <template v-slot="scope">
                <dict-tag  v-if="item.columnName === 'status'"  v-slot="scope" :options="dict.type.common_status" :value="scope.row[item.columnName]"/>
                <span v-else>{{ scope.row[item.columnName] }}</span>
              </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
      id="dialog-container"
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <div class="edit-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="85px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="库区编号" prop="storageAreaId">
                <el-input
                  v-model="form.storageAreaId"
                  placeholder="请输入库区编号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库区名称" prop="storageAreaName">
                <el-input
                  v-model="form.storageAreaName"
                  placeholder="请输入库区名称"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="库区简称" prop="storageAreaShortName">
                <el-input
                  v-model="form.storageAreaShortName"
                  placeholder="请输入库区简称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="拼音简称" prop="pyName">
                <el-input v-model="form.pyName" placeholder="请输入拼音简称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="仓库编号" prop="storageId">
                <el-select
                  v-model="form.storageId"
                  placeholder="请选择仓库"
                  clearable
                >
                  <el-option
                    v-for="dict in storageList"
                    :key="dict.storageId"
                    :label="dict.storageName"
                    :value="dict.storageId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" placeholder="请输入排序" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.common_status"
                    :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="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row> </el-row>
        </el-form>
      </div>
      <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 { listStorage } from "@/api/system/storage";
import {
  addStorageArea,
  changeStatus,
  delStorageArea,
  disabled,
  enabled,
  getStorageArea,
  listStorageArea,
  updateStorageArea,
} from "@/api/system/storageArea";
export default {
  name: "StorageArea",
  dicts: ["common_status"],
  data() {
    return {
      rndKey:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库库区表格数据
      storageAreaList: [],
      //仓库集合
      storageList: [],
      // 弹出层标题
      title: "",
      mainTableHeight: "0px",
      mainTableDivHeight: "0px",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        storageAreaId: null,
        storageAreaName: null,
        storageAreaShortName: null,
        pyName: null,
        storageId: null,
        sort: null,
        status: null,
        selectIds:[]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storageAreaId: [
          { required: true, message: "库区编号不能为空", trigger: "blur" },
        ],
        storageAreaName: [
          { required: true, message: "库区名称不能为空", trigger: "blur" },
        ],
      },
      columns: [],
    };
  },
  created() {
    this.getList();
    this.getStorageList();
  },
  mounted() {
    this.computedTableHeight();
  },
  watch: {
    showSearch: function () {
      this.computedTableHeight();
    },
  },
  methods: {
    handleHeaderDragEnd (newWidth, oldWidth, column, event) {
      // 当列宽发生变化时，将变化值写入到columns变量中。
      this.columns.map((obj) => {
        if (obj.columnName == column.property) obj.width = newWidth;
      });
      this.$refs.toolBarRef.saveConfig(this.columns);

    },
    customColsHandler (columns) {
      this.columns = columns;
      this.rndKey = Math.random();
    },

    computedTableHeight() {
      this.$nextTick(() => {
        var totalHeight = this.$refs.appPanel.clientHeight;
        var toolHeight = this.$refs.toolPanel.clientHeight;
        var searchHeight = this.$refs.searchPanel.clientHeight;

        var tablePanelHeight = totalHeight - toolHeight - searchHeight - 14 - 5;

        this.mainTableDivHeight = tablePanelHeight + "px";
        this.mainTableHeight = tablePanelHeight - 25 + "px";
      });
    },
    /** 查询仓库库区列表 */
    getList() {
      this.loading = true;
      listStorageArea(this.queryParams).then((response) => {
        this.storageAreaList = response.rows;
        this.total = response.total;
        if (this.columns.length == 0) this.columns = response.columns;

        this.loading = false;
      });
    },
    /** 获取仓库列表 */
    getStorageList() {
      listStorage().then((response) => {
        this.storageList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storageAreaId: null,
        storageAreaName: null,
        storageAreaShortName: null,
        pyName: null,
        storageId: null,
        sort: null,
        status: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: 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;
      this.queryParams.selectIds=this.ids;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库库区";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStorageArea(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) {
            updateStorageArea(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorageArea(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 delStorageArea(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 用户状态修改 */
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '"id为"' + row.id + '"数据项？')
        .then(function () {
          return changeStatus(row.id, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 启用按钮操作 */
    handleEnable(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认启用仓库库区编号为"' + ids + '"的数据项？')
        .then(function () {
          return enabled(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        })
        .catch(() => {});
    },
    /** 禁用按钮操作 */
    handleDisabled(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认禁用仓库库区编号为"' + ids + '"的数据项？')
        .then(function () {
          return disabled(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("禁用成功");
        })
        .catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/storageArea/export",
        {
          ...this.queryParams,
        },
        `storageArea_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}
</style>
