<template>
  <div class="divBox">
    <!--主要内容-->
    <el-card class="box-card">
      <!--顶部搜索栏-->
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline size="small" label-width="80px">
            <el-form-item label="">
              <el-input v-model="tableFrom.title" placeholder="请输入活动名称">
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)"/>
              </el-input>
            </el-form-item>
          </el-form>
          <el-button size="small" type="success" @click="editCate">添加活动</el-button>
        </div>
      </div>
      <!--表格信息-->
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini">
        <el-table-column label="ID" prop="id" min-width="80" align="center"/>
        <el-table-column label="分类名称" prop="title" min-width="100" align="center"/>
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editCate({id: scope.row.id})">编辑</el-button>
            <el-button type="text" size="small" @click="delCate(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleUserSizeChange"
          @current-change="pageUserChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import {activityDel, activityEditFrom, activityList} from "@/api/marketing";

export default {
  name: "preSaleProductList",
  components: {},
  data() {
    return {
      // 列表
      listLoading: false,
      tableFrom: {
        page: 1,
        limit: 20,
        title: '',
      },
      tableData: {
        data: [],
        total: 0,
      },
    };
  },
  watch: {},
  mounted() {
    this.getList();
  },
  methods: {
    // 列表
    getList(num = ''){
      let _this = this;
      _this.listLoading = true;
      _this.tableFrom.page = num ? num : _this.tableFrom.page;
      activityList(_this.tableFrom).then((res) => {
        _this.tableData.data = res.data.list;
        _this.tableData.total = res.data.count;
        _this.listLoading = false;
      }).catch((res) => {
        _this.listLoading = false;
        _this.$message.error(res.message);
      });
    },
    pageUserChange(page) {
      this.tableFrom.page = page;
      this.getList('');
    },
    handleUserSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList('');
    },
    // 添加 || 编辑
    editCate(data = {}){
      this.$modalForm(activityEditFrom(data)).then(() => this.getList(''));
    },
    // 删除
    delCate(id){
      let _this = this;
      this.$confirm('删除后不可恢复，您确认删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '不删除',
        type: 'warning'
      }).then(() => {
        activityDel({ id: id}).then((res) => {
          _this.getList('')
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        }).catch((res) => {
          console.log('错误',res)
        });
      }).catch(action => {})
    },

  },
};
</script>

<style scoped lang="scss">

</style>
