<template>
  <div>
    <!-- 标题 -->
    <el-row class="tag">公告记录</el-row>

    <!-- 分割线 -->
    <hr style="background-color: #cccccc; height: 1px; border: 0" />
    <div style="margin: 20px 0"></div>

    <!-- 时间筛选框 -->
    <el-row>
      <el-col :span="6">
        选择查询日期：
        <el-date-picker
          v-model="time"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          @change="changeTime"
        >
        </el-date-picker>
      </el-col>
    </el-row>

    <div style="margin: 20px 0"></div>
    <hr style="background-color: #cccccc; height: 1px; border: 0" />

    <!-- 表格区域 -->
    <el-table
      :data="tableData"
      highlight-current-row
      height="618"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
    >
      <el-table-column prop="noticeTime" label="发布时间" align="center">
      </el-table-column>
      <el-table-column
        prop="noticeContent"
        label="公告内容"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="repairsAdmin.adminName"
        label="发布人员"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            @click.native.prevent="deleteRow(row.noticeId)"
            type="danger"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 55px 0"></div>

    <!-- 分页器 -->
    <el-row>
      <div class="pagination_smoke">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :total="totalCount"
          :page-size="pageSize"
          :current-page.sync="pageNum"
          @current-change="changeCurPage"
        >
        </el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false, //加载效果
      time: null, //查询时间
      totalCount: null, //总条数
      pageSize: 10, //每页多少条
      pageNum: 1, //当前页
      tableData: [],
    };
  },
  created() {
    this.getNoticeRecoder();
  },
  methods: {
    //获取公告记录数据
    async getNoticeRecoder() {
      this.loading = true;
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        time: this.time,
      };
      let res = await this.$API.repair.reqGetNoticeRecord(params);
      //  console.log(res);
      if (res.status == 200) {
        this.tableData = res.data.list;
        this.totalCount = res.data.total;
      } else {
        console.log("获取公告记录数据请求失败");
        this.$message.error(res.errmsg);
      }
      this.loading = false;
    },

    // 删除公告请求
    deleteRow(noticeId) {
      this.$confirm(`确定删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let params = {
            noticeId: noticeId,
          };
          let res = await this.$API.repair.reqDeleteNotice(params);
          // console.log(res);
          if (res.status == 200) {
            this.$message.success("删除成功");
            let totalPage = Math.ceil((this.totalCount - 1) / this.pageSize);
            let currentPage =
              this.pageNum > totalPage ? totalPage : this.pageNum;
            this.pageNum = currentPage < 1 ? 1 : currentPage;
            this.getNoticeRecoder();
          } else {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },

    // 时间选择框数值改变回调
    changeTime() {
      this.pageNum = 1;
      this.getNoticeRecoder();
    },

    // 翻页回调
    changeCurPage(val) {
      this.pageNum = val;
      this.getNoticeRecoder();
    },
  },
};
</script>

<style scoped>
.tag {
  width: 108px;
  height: 50px;
  font-size: 24px;
  font-family: Microsoft YaHei-3970(82674968);
  font-weight: bold;
  line-height: 60px;
}
.pagination_smoke {
  text-align: center;
}
</style>