<template>
  <div class="pb_content" style="      overflow: auto;
      margin: 0px;
      box-sizing: border-box;
      width: 100%;
      height: 100%;">
    <div class="pb_content_body" style="
         background: #fff;
          padding: 0px 25px;
          box-sizing: border-box;
          margin: 10px auto 0;
      ">
      <div class="pb_head">
        <span>通知公告</span>
        <div class="student_button">
          <el-button type="primary" class="bgColor btnClassGM" @click="dialogVisibleAdd = true"
            v-if="role == '1'">新建通知</el-button>
        </div>
      </div>
    </div>
    <div class="pb_content_body" style="margin: 0 auto 10px;">
      <div class="student_table">
        <el-table ref="table" :data="tableData" border :height="tableHeight" :fit="true" style="width: 100%"
          :header-cell-style="{ background: '#f1f1f1' }" :row-class-name="tableRowClassName" v-loading="loading">
          <el-table-column prop="title" label="通知标题" min-width="15" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="content" label="通知内容" min-width="35" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <div v-html="snippet(scope.row.content)"></div>
            </template>
          </el-table-column>
          <el-table-column prop="creatMen" label="创建者" min-width="25" align="center">
            管理员
          </el-table-column>
          <el-table-column prop="creatTime" label="创建时间" min-width="25" align="center">
          </el-table-column>
          <el-table-column label="操作" min-width="20">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="getNewDetail(scope.row)"
                class="btnClassGM">查看通知</el-button>
              <el-button type="primary" size="small" @click="deleteNotice(scope.row.id)" v-if="role == '1'"
                class="btnClassGM">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="student_page">
        <el-pagination background layout="prev, pager, next" :page-size="10" :total="total" v-if="page"
          @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="查看通知" :visible.sync="dialogVisible" :append-to-body="true" width="50%" :before-close="handleClose"
      class="dialog_diy">
      <div>
        <div class="pb_head">
          <span style="font-size: 20px">{{ res.title }}</span>
          <span style="font-size: 15px; line-height: 35px">管理员</span>
        </div>
        <div class="notice_content cont" v-html="res.content"></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="close btnClassGM" @click="dialogVisible = false" type="primary">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog title="添加全站通知" :visible.sync="dialogVisibleAdd" width="550px" :before-close="handleClose"
      class="dialog_diy" center>
      <div>
        <div class="tian1">
          <span>通知标题</span>
          <el-input v-model="noticeTitle" style="width: 250px; margin: 15px 0px" placeholder="请输入通知标题"></el-input>
        </div>
        <div class="tian1">
          <span>通知内容</span>
          <editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addNotice">确认通知</el-button>
        <el-button @click="dialogVisibleAdd = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import EditorBar from "../../../components/tools/wangEnduit";

export default {
  components: {
    EditorBar,
  },
  data() {
    return {
      tableHeight: "500px",
      page: 1,
      total: 0,
      isLoading: false,
      formLabelWidth: "100px",
      dialogVisible: false,
      dialogVisibleAdd: false,
      title: "",
      tableData: [],
      res: [],
      userid: this.$route.query.userid,
      oid: this.$route.query.oid,
      org: this.$route.query.org,
      role: this.$route.query.role,
      noticeTitle: "",
      detail: "",
      now:"",
    };
  },
  mounted() {
    this.$nextTick(function () {
      this.tableHeight =
        window.innerHeight - this.$refs.table.$el.offsetTop - 200;
      if (this.tableHeight <= 530) {
        this.tableHeight = 530;
      }
      // 监听窗口大小变化
      let self = this;
      window.onresize = function () {
        self.tableHeight =
          window.innerHeight - self.$refs.table.$el.offsetTop - 200;
        if (self.tableHeight <= 530) {
          self.tableHeight = 530;
        }
      };
    });
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if ((rowIndex + 1) % 2 === 0) {
        return "even_row";
      } else {
        return "";
      }
    },
    snippet(value) {
      return value.replace(/<[^>]*>/g, "");
    },
    tableRowClassName({ row, rowIndex }) {
      if ((rowIndex + 1) % 2 === 0) {
        return "even_row";
      } else {
        return "";
      }
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getNews();
    },
    handleClose(done) {
      done();
    },
    getNewDetail(res) {
      this.dialogVisible = true;
      this.res = res;
    },
    change(val){
      console.log(val);
    },
    getNews() {
      this.loading = true;
      let params = { uid: this.userid, oid: this.oid, org: this.org, page: this.page };
      this.ajax
        .get(this.$store.state.api + "selectNotice", params)
        .then((res) => {
          this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
          this.loading = false;
          this.tableData = res.data[0];
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    time() {
      if (!this.now) {
        this.now = new Date().getTime();
        return true;
      } else {
        let time = new Date().getTime();
        if (time - this.now > 3000) {
          this.now = time;
          return true;
        } else {
          return false;
        }
      }
    },
    //添加通知
    addNotice() {
      if (this.noticeTitle === "") {
        this.$message.error("请输入文章标题");
        return;
      } else if (this.detail === "") {
        this.$message.error("请输入文章内容");
        return;
      }
      if (this.time()) {
        let params = [
          {
            t: this.noticeTitle,
            nc: this.detail.replace(/%/g, "%25"),
            oid: this.oid,
            org: this.org,
          },
        ];
        this.ajax
          .post(this.$store.state.api + "addNotice", params)
          .then((res) => {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.noticeTitle = ""
            this.detail = ""
            this.getNews();
            this.dialogVisibleAdd = false;
          })
          .catch((err) => {
            this.$message.error("添加失败");
            console.error(err);
          });
      }
    },
    deleteNotice(id) {
      this.$confirm(
        "确定删除此公告吗？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let params = [
            {
              id: id
            },
          ];
          this.ajax
            .post(this.$store.state.api + "deleteNotice", params)
            .then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.getNews();
            })
            .catch((err) => {
              this.$message.error("删除失败");
              console.error(err);
            });
        })
        .catch(() => { });
    }
  },
  created() {
    this.getNews();
  },
};
</script>

<style scoped>
.pb_head {
  display: flex;
  justify-content: space-between;
}

.student_head {
  margin-bottom: 20px;
}

.student_search {
  display: flex;
}

.student_search>div:nth-child(1) {
  line-height: 35px;
  font-size: 14px;
}

.student_search>>>.el-input__inner {
  width: 190px;
  height: 35px;
  margin-left: 10px;
}

.student_table>>>.el-table--border td {
  border-right: 0px !important;
}

.header-title {
  display: flex;
}

.logoImg {
  width: 30px;
}

.logoImg>img {
  width: 100%;
  height: 100%;
}

.title_add_student {
  margin: 0 auto;
  color: #fff;
}

.dialog_diy>>>.el-dialog__header {
  background: #454545 !important;
  padding: 15px 20px;
}

.dialog_diy>>>.el-dialog__title {
  color: #fff;
}

.student_table>>>.el-table--border td {
  border-right: 0px !important;
}

.dialog_diy>>>.el-dialog__headerbtn {
  top: 19px;
}

.dialog_diy>>>.el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}

.dialog_diy>>>.el-dialog__headerbtn .el-dialog__close:hover {
  color: #fff;
}

.notice_content {
  width: 100%;
  word-wrap: break-word;
  word-break: break-all;
  overflow: hidden;
  font-size: 18px;
  line-height: 35px;
  text-indent: 35px;
  min-width: 385px;
}

.close {
  width: 320px;
  height: 30px;
  line-height: 30px;
  font-size: 14px;
  background: #0e72e6;
  padding: 0 !important;
}

/* table 样式 */
.cont>>>table {
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
}

.cont>>>table td,
.cont>>>table th {
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  /* padding: 20px 5px; */
  padding: 5px 10px;
  max-width: 0px;
  height: 30px;
  vertical-align: baseline;
}

.cont>>>table th {
  border-bottom: 2px solid #ccc;
  text-align: center;
}

/* blockquote 样式 */
.cont>>>blockquote {
  display: block;
  border-left: 8px solid #d0e5f2;
  padding: 5px 10px;
  margin: 10px 0;
  line-height: 1.4;
  font-size: 100%;
  background-color: #f1f1f1;
}

/* code 样式 */
.cont>>>code {
  display: inline-block;
  *display: inline;
  *zoom: 1;
  background-color: #f1f1f1;
  border-radius: 3px;
  padding: 3px 5px;
  margin: 0 3px;
}

.cont>>>pre code {
  display: block;
}

/* ul ol 样式 */
.cont>>>ul,
ol {
  margin: 10px 0 10px 20px;
}

.el-table>>>.even_row {
  background-color: #f1f1f1;
}


.student_page {
  margin-top: 10px;
}

.pb_head {
  margin: 0 !important;
  width: 100% !important;
}

.student_table>>>.el-table,
.student_table>>>.el-table__body-wrapper {
  height: auto !important;
}

.student_page>>>.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #5c549f;
  color: #fff !important;
}

.student_page>>>.el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #5c549f;
}

.tian1 {
  margin-top: 15px;
}

.tian1 span {
  display: block;
}
</style>
