<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" size="small" label-width="90px">
      <el-form-item label="仓库编号" prop="warehouseNo">
        <el-input v-model="queryParams.warehouseNo" placeholder="请输入仓库编号" clearable style="width: 220px" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input v-model="queryParams.warehouseName" placeholder="请输入仓库名称" clearable style="width: 220px" />
      </el-form-item>
      <el-form-item label="是否总仓" prop="isCentral">
        <el-select v-model="queryParams.isCentral" placeholder="请选择" clearable style="width: 140px">
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </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="['erp:stock:warehouse: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="['erp:stock:warehouse: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="['erp:stock:warehouse:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" align="center" width="80" />
      <el-table-column label="仓库编号" prop="warehouseNo" align="center" min-width="140" />
      <el-table-column label="仓库名称" prop="warehouseName" align="center" min-width="160" />
      <el-table-column label="英文名称" prop="engName" align="center" min-width="130" />
      <el-table-column label="仓库地址" prop="warehouseAddress" align="center" min-width="180" />
      <el-table-column label="是否总仓" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ Number(scope.row.isCentral) === 1 ? "是" : "否" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleUpdate(scope.row)" v-hasPermi="['erp:stock:warehouse:edit']">修改</el-button>
          <el-button type="text" size="mini" @click="handleDelete(scope.row)" v-hasPermi="['erp:stock:warehouse: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="620px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="仓库编号" prop="warehouseNo">
          <el-input v-model="form.warehouseNo" placeholder="请输入仓库编号" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="engName">
          <el-input v-model="form.engName" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="仓库地址" prop="warehouseAddress">
          <el-input v-model="form.warehouseAddress" placeholder="请输入仓库地址" />
        </el-form-item>
        <el-form-item label="是否总仓" prop="isCentral">
          <el-radio-group v-model="form.isCentral">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" />
        </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 { addWarehouse, delWarehouse, getWarehouse, listWarehouse, updateWarehouse } from "@/api/erp/stock";

export default {
  name: "StockWarehouse",
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      list: [],
      title: "",
      open: false,
      ids: [],
      single: true,
      multiple: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseNo: undefined,
        warehouseName: undefined,
        isCentral: undefined
      },
      form: {},
      rules: {
        warehouseNo: [
          { required: true, message: "仓库编号不能为空", trigger: "blur" },
          { pattern: /^0*[1-9]\d*$/, message: "仓库编号必须为大于0的数字，可使用0001格式", trigger: "blur" }
        ],
        warehouseName: [{ required: true, message: "仓库名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then((res) => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
        warehouseNo: "",
        warehouseName: "",
        engName: "",
        warehouseAddress: "",
        isCentral: 0,
        remark: ""
      };
      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) {
      const id = row.id || this.ids[0];
      this.reset();
      getWarehouse(id).then((res) => {
        this.form = res.data || {};
        this.form.isCentral = Number(this.form.isCentral || 0);
        this.open = true;
        this.title = "修改仓库";
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        const req = this.form.id ? updateWarehouse(this.form) : addWarehouse(this.form);
        req.then(() => {
          this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids.join(",");
      this.$modal.confirm("确认删除选中的仓库数据吗？").then(() => delWarehouse(ids)).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => {});
    }
  }
};
</script>
