<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="factoryId">
        <el-input
          v-model="queryParams.factoryId"
          placeholder="请输入厂区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入所属部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编号" prop="storageId">
        <el-input
          v-model="queryParams.storageId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库简称" prop="storageShortName">
        <el-input
          v-model="queryParams.storageShortName"
          placeholder="请输入仓库简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="storageName">
        <el-input
          v-model="queryParams.storageName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库地址" prop="storageAddress">
        <el-input
          v-model="queryParams.storageAddress"
          placeholder="请输入仓库地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库联系人名称" prop="storageConcatsName">
        <el-input
          v-model="queryParams.storageConcatsName"
          placeholder="请输入仓库联系人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库联系人方式" prop="storageConcatsMobile">
        <el-input
          v-model="queryParams.storageConcatsMobile"
          placeholder="请输入仓库联系人方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否是AGV库 0：否 1：是" prop="isAgv">
        <el-input
          v-model="queryParams.isAgv"
          placeholder="请输入是否是AGV库 0：否 1：是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['app:storage: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="['app:storage: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="['app:storage: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="['app:storage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="厂区" align="center" prop="factoryId" />
      <el-table-column label="所属部门" align="center" prop="deptId" />
      <el-table-column label="仓库编号" align="center" prop="storageId" />
      <el-table-column label="仓库简称" align="center" prop="storageShortName" />
      <el-table-column label="仓库名称" align="center" prop="storageName" />
      <el-table-column label="仓库地址" align="center" prop="storageAddress" />
      <el-table-column label="仓库联系人名称" align="center" prop="storageConcatsName" />
      <el-table-column label="仓库联系人方式" align="center" prop="storageConcatsMobile" />
      <el-table-column label="是否为立体库1:立体库 0：平库" align="center" prop="autoStatus" />
      <el-table-column label="是否是AGV库 0：否 1：是" align="center" prop="isAgv" />
      <el-table-column label="是否可用" align="center" prop="status" />
      <el-table-column label="仓储类型 1AGV 2堆垛机 3穿梭4 人工库 5 其他" align="center" prop="type" />
      <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="['app:storage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:storage: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="factoryId">
          <el-input v-model="form.factoryId" placeholder="请输入厂区" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属部门" />
        </el-form-item>
        <el-form-item label="仓库编号" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入仓库编号" />
        </el-form-item>
        <el-form-item label="仓库简称" prop="storageShortName">
          <el-input v-model="form.storageShortName" placeholder="请输入仓库简称" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="storageName">
          <el-input v-model="form.storageName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="仓库地址" prop="storageAddress">
          <el-input v-model="form.storageAddress" placeholder="请输入仓库地址" />
        </el-form-item>
        <el-form-item label="仓库联系人名称" prop="storageConcatsName">
          <el-input v-model="form.storageConcatsName" placeholder="请输入仓库联系人名称" />
        </el-form-item>
        <el-form-item label="仓库联系人方式" prop="storageConcatsMobile">
          <el-input v-model="form.storageConcatsMobile" placeholder="请输入仓库联系人方式" />
        </el-form-item>
        <el-form-item label="是否是AGV库 0：否 1：是" prop="isAgv">
          <el-input v-model="form.isAgv" placeholder="请输入是否是AGV库 0：否 1：是" />
        </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 { listStorage, getStorage, delStorage, addStorage, updateStorage } from "@/api/system/storage";

export default {
  name: "Storage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      storageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryId: null,
        deptId: null,
        storageId: null,
        storageShortName: null,
        storageName: null,
        storageAddress: null,
        storageConcatsName: null,
        storageConcatsMobile: null,
        autoStatus: null,
        isAgv: null,
        status: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storageId: [
          { required: true, message: "仓库编号不能为空", trigger: "blur" }
        ],
        storageName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listStorage(this.queryParams).then(response => {
        this.storageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryId: null,
        deptId: null,
        storageId: null,
        storageShortName: null,
        storageName: null,
        storageAddress: null,
        storageConcatsName: null,
        storageConcatsMobile: null,
        autoStatus: null,
        isAgv: null,
        status: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        type: 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
      getStorage(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) {
            updateStorage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorage(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 delStorage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('app/storage/export', {
        ...this.queryParams
      }, `storage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
