<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="分类名称" prop="cateName">
        <el-input v-model="queryParams.cateName" placeholder="请输入分类名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否推荐" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in showOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </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="['store:storeProductCategory:add']">新增</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="['store:storeProductCategory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['store:storeProductCategory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange" row-key="cateId">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="分类ID" align="center" prop="cateId" width="80" />
      <el-table-column label="分类名称" align="center" prop="cateName" width="160" show-overflow-tooltip />
      <el-table-column label="父级ID" align="center" prop="pid" width="80" />
      <el-table-column label="排序" align="center" prop="sort" width="70" />
      <el-table-column label="推荐" align="center" prop="isShow" width="70">
        <template slot-scope="scope"><dict-tag :options="showOptions" :value="scope.row.isShow" /></template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['store:storeProductCategory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:storeProductCategory: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" />
  </div>
</template>

<script>
import { getRequest, delRequest, postRequest, putRequest } from "@/api/common";

export default {
  name: "StoreProductCategory",
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      multiple: true,
      showSearch: true,
      total: 0,
      list: [],
      showOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cateName: undefined,
        isShow: undefined
      }
    };
  },
  created() {
    this.getDicts("sys_show_hide").then(res => { this.showOptions = res.data; });
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRequest("/store/storeProductCategory/list", this.queryParams).then(res => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      }).catch(() => {
        this.list = [];
        this.total = 0;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cateId);
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.$router.push({ path: "/storeProductCategoryEdit" });
    },
    handleUpdate(row) {
      this.$router.push({ path: "/storeProductCategoryEdit", query: { cateId: row.cateId } });
    },
    handleDelete(row) {
      const ids = row.cateId || this.ids;
      this.$modal.confirm('确认删除分类"' + (row.cateName || ids) + '"吗？').then(function() {
        return delRequest("/store/storeProductCategory/" + ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.$modal.confirm("确认导出所有分类数据吗？").then(() => {
        this.exportLoading = true;
        getRequest("/store/storeProductCategory/export", this.queryParams).then(res => {
          this.download(res.msg);
          this.exportLoading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>
