<template>
  <div class="container">
    <div>
      <el-button
        type="primary"
        @click="
          noticeVisible = true;
          title = '添加公告';
        "
        style="float: right;"
        >添加</el-button
      >
    </div>
    <el-dialog
      title="添加公告"
      :visible.sync="noticeVisible"
      @close="cancel('noticeInfo')"
    >
      <el-form :model="noticeInfo" ref="noticeInfo">
        <div class="line">
          <el-form-item label="内容" prop="noticeContent">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="noticeInfo.noticeContent"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('noticeInfo')">取 消</el-button>
        <el-button type="primary" @click="submitForm('noticeInfo')"
          >确 定</el-button
        >
        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>

    <el-dialog
      title="编辑公告"
      :visible.sync="editNoticeVisible"
      @close="cancel('editNoticeInfo')"
    >
      <el-form :model="editNoticeInfo" ref="editNoticeInfo">
        <div class="line">
          <el-form-item label="内容" prop="noticeContent">
            <el-input
              type="textarea"
              autosize
              v-model="editNoticeInfo.noticeContent"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('editNoticeInfo')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editNoticeInfo')"
          >确 定</el-button
        >
        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>

    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
        @sort-change="changeTableSort"
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>

        <el-table-column label="公告内容" align="center">
          <template slot-scope="scope">{{ scope.row.noticeContent }}</template>
        </el-table-column>

        <el-table-column
          label="发布时间"
          :sortable="'custom'"
          align="center"
          prop="addTime"
        >
          <template slot-scope="scope">{{ scope.row.noticeTime }}</template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click="
                handleupdateNoticeAll(scope.row.id, scope.row.noticeContent)
              "
              size="mini"
              >编辑</el-button
            >
            <el-button @click="handleDel(scope.row.id)" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next,jumper"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  noticeAll,
  addNoticeAll,
  deleteNotice,
  updateNoticeAll
} from "@/api/cso";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "noticeAll",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      formLabelWidth: "120px",
      options: [],
      teacherId: "",
      noticeVisible: false,
      editNoticeVisible: false,
      title: "",
      noticeInfo: {},
      editNoticeInfo: { noticeContent: "" },
      updateId: ""
    };
  },
  methods: {
    getData() {
      noticeAll(this.listQuery).then(res => {
        // console.log(res, "getData");
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        console.log(this.list, "this.list,noticeAll");
        this.$forceUpdate();
      });
    },

    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getData();
    },
    handleSizeChange(val){
      //切换展示条数
       this.listQuery.pageSize = val;
        this.getData();
    },

    handleStuDetail(id) {
      // 测试用的
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },
    changeTableSort(column) {
      console.log("prop", column.prop);
      console.log("order", column.order);
      // console.log("column", column);
      if (column.prop == "addTime") {
        this.listQuery.secondSort = null;
        if (column.order == "ascending") {
          this.listQuery.firstSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.firstSort = 1;
        }
      }
      if (column.prop == "allocateTime") {
        this.listQuery.firstSort = null;
        if (column.order == "ascending") {
          this.listQuery.secondSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.secondSort = 1;
        }
      }
      if (column.order == null) {
        this.listQuery.secondSort = this.listQuery.firstSort = null;
      }
      this.getData();
      console.log("changeTableSort");
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    submitForm(formName) {
      if (this.updateId) {
        this.noticeInfo.id = this.updateId;
      }
      this.$confirm("是否提交", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        addNoticeAll(this.noticeInfo).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.noticeVisible = false;

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    submitEditForm(formName) {
      this.editNoticeInfo.id = this.updateId;

      this.$confirm("是否提交", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        updateNoticeAll(this.editNoticeInfo).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.editNoticeVisible = false;

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    handleDel(id) {
      //   console.log(id, "handleDel");
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteNotice(id).then(response => {
          this.$message({
            type: "success",
            message: "删除成功",
            duration: 1000
          });

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    handleupdateNoticeAll(id, noticeContent) {
      this.editNoticeVisible = true;
      // this.title = "编辑公告";
      this.updateId = id;
      this.editNoticeInfo.noticeContent = noticeContent;
      console.log(this.updateId, noticeContent);
    },
    cancel(formName) {
      this.noticeVisible = false;
      this.editNoticeVisible = false;
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.getData();
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
.handleStuDetail {
  cursor: pointer;
}
</style>
