<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="活动标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入活动标题" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in statusOptions" :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:storeActivity: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:storeActivity: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:storeActivity: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">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="活动ID" align="center" prop="activityId" width="80" />
      <el-table-column label="活动标题" align="center" prop="title" width="180" show-overflow-tooltip />
      <el-table-column label="活动描述" align="center" prop="descs" min-width="200" show-overflow-tooltip />
      <el-table-column label="活动状态" align="center" prop="status" width="90">
        <template slot-scope="scope"><dict-tag :options="statusOptions" :value="scope.row.status" /></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:storeActivity:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:storeActivity: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 } from "@/api/common";

export default {
  name: "StoreActivity",
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      multiple: true,
      showSearch: true,
      total: 0,
      list: [],
      statusOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getDicts("sys_normal_disable").then(res => { this.statusOptions = res.data; });
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRequest("/store/storeActivity/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.activityId); this.multiple = !selection.length; },
    handleAdd() { this.$router.push({ path: "/storeActivityEdit" }); },
    handleUpdate(row) { this.$router.push({ path: "/storeActivityEdit", query: { activityId: row.activityId } }); },
    handleDelete(row) {
      const ids = row.activityId || this.ids;
      this.$modal.confirm('确认删除活动"' + (row.title || ids) + '"吗？').then(function() {
        return delRequest("/store/storeActivity/" + ids);
      }).then(() => { this.getList(); this.$modal.msgSuccess("删除成功"); }).catch(() => {});
    },
    handleExport() {
      this.$modal.confirm("确认导出所有活动数据吗？").then(() => {
        this.exportLoading = true;
        getRequest("/store/storeActivity/export", this.queryParams).then(res => { this.download(res.msg); this.exportLoading = false; });
      }).catch(() => {});
    }
  }
};
</script>
