<template>
  <div class="system-notice">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>系统通知</h1></div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <div></div>
        <div class="part-filter-form-action">
          <el-button type="primary" icon="icon icon-add" @click="toAdd"
            >新增</el-button
          >
        </div>
      </div>
    </div>

    <el-table :data="tableData">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="title" label="标题" min-width="120">
      </el-table-column>
      <el-table-column prop="content" label="通知内容" min-width="240">
      </el-table-column>
      <el-table-column prop="enable" label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.enable | booleanEnableDisableFilter }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" fixed="right">
        <div slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            :type="scope.row.enable ? 'danger' : 'primary'"
            plain
            @click="toEnable(scope.row)"
            >{{ scope.row.enable ? "禁用" : "启用" }}</el-button
          >
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <ModifySystemNotice
      ref="ModifySystemNotice"
      :instance="curRow"
      @modified="searchForm"
    />
  </div>
</template>

<script>
import { searchSystemNoice, enableSystemNotice } from "@/api/system";
import ModifySystemNotice from "./ModifySystemNotice.vue";

export default {
  name: "BlackList",
  components: {
    ModifySystemNotice,
  },
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      curRow: {},
    };
  },
  created() {
    this.searchForm();
  },
  methods: {
    async searchForm() {
      const res = await searchSystemNoice({
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifySystemNotice.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifySystemNotice.open();
    },
    async toEnable(row) {
      const action = row.enable ? "禁用" : "启用";
      const result = await this.$confirm(
        `确定要${action}通知【${row.title}】吗?`,
        "操作提醒",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          iconClass: "el-icon-warning",
          customClass: "el-message-box__error",
        }
      ).catch(() => {});
      if (result !== "confirm") return;

      const enable = !row.enable;
      await enableSystemNotice({
        id: row.id,
        enable,
      });
      row.enable = enable;
      this.$message.success("操作成功！");
    },
  },
};
</script>
