<template>
  <Container>
    <div slot="left">
      <el-select
        v-model="areaId"
        filterable
        style="width: 150px; margin-right: 10px"
        placeholder="所属库区"
        clearable
        @change="pageChange(1)"
      >
        <el-option
          v-for="item in area_list"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
      <el-select
        v-model="enableStatus"
        filterable
        style="width: 150px; margin-right: 10px"
        placeholder="状态"
        clearable
        @change="pageChange(1)"
      >
        <el-option
          v-for="item in enableStatus_list"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        v-if="$accessCheck($Access.warehouseManageGroupAddLocationManagement)"
        size="small"
        type="primary"
        @click="openAdd()"
      >
        新增库位
      </el-button>
    </div>
    <vxe-table :data="tableData" border="inner">
      <vxe-table-column field="id" title="ID"></vxe-table-column>
      <vxe-table-column field="code" title="库位编码"></vxe-table-column>
      <vxe-table-column field="name" title="库位名称"></vxe-table-column>
      <vxe-table-column field="areaName" title="所属库区"></vxe-table-column>
      <vxe-table-column title="状态">
        <template #default="{ row }">
          <el-switch
            v-if="
              $accessCheck($Access.warehouseManageGroupAuditLocationManagement)
            "
            v-model="row.enableStatus"
            class="switchStyle"
            active-color="#36B365"
            :active-value="5"
            :inactive-value="4"
            active-text="启用"
            inactive-text="禁用"
            @change="enableStorageLocation($event, row)"
          ></el-switch>
          <div v-else>
            <span v-if="row.enableStatus === 5" class="success-status">
              启用
            </span>
            <span v-else class="info-status">禁用</span>
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column fixed="right" width="120px" title="操作">
        <template #default="{ row }">
          <el-button
            v-if="
              $accessCheck($Access.warehouseManageGroupEditLocationManagement)
            "
            type="text"
            @click="openAdd(row.id)"
          >
            编辑
          </el-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
    <AddLocationManagement
      v-if="show_add"
      :is-show="show_add"
      :form-id="form_id"
      @confirm="addConfirm"
      @cancel="show_add = false"
    />
  </Container>
</template>
<script>
  import {
    getAllStorageLocation,
    enableStorageLocation,
    getListReservoir,
  } from "@/api/Stock";
  import AddLocationManagement from "@/views/stock/WarehouseManagement/AddLocationManagement";
  export default {
    components: {
      AddLocationManagement,
    },
    data() {
      return {
        tableData: [],
        page: 1,
        pageSize: 10,
        total: 0,
        form_id: "",
        show_add: false,
        areaId: "",
        area_list: [],
        enableStatus: "",
        enableStatus_list: [
          { value: 5, label: "启用" },
          { value: 4, label: "禁用" },
        ],
      };
    },
    created() {
      this.getAllStorageLocation();
      this.getListReservoir();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getAllStorageLocation();
    },
    methods: {
      //库位
      async getAllStorageLocation() {
        const { data, pageTotal } = await getAllStorageLocation({
          areaId: this.areaId,
          enableStatus: this.enableStatus,
          page: this.page,
          pageSize: this.pageSize,
        });
        this.tableData = data;
        this.total = pageTotal;
      },
      pageChange(val) {
        this.page = val;
        this.getAllStorageLocation();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      openAdd(id) {
        this.show_add = true;
        this.form_id = id || "";
      },
      addConfirm() {
        if (this.form_id) {
          this.getAllStorageLocation();
        } else {
          this.pageChange(1);
        }
      },
      async enableStorageLocation(val, row) {
        try {
          const data = await enableStorageLocation(row.id, {
            enableStatus: val,
          });
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        } catch (e) {
          await this.getAllStorageLocation();
        }
      },
      //  请求库区列表
      async getListReservoir() {
        const data = await getListReservoir();
        this.area_list = data.data;
      },
    },
  };
</script>
<style></style>
