<template>
  <div class="pb_content">
    <div class="pb_head">
      <div>
        <span>通知公告</span>
      </div>
    </div>
    <div class="touTop">
      <div class="touRight">
        <div></div>
        <div>
          <el-button @click="openNotice">添加通知</el-button>
        </div>
      </div>
    </div>
    <div class="pb_content_body" style="margin: 0 auto">
      <div class="student_table">
        <el-table
          ref="table"
          :data="tableData"
          border
          :height="tableHeight"
          :fit="true"
          v-loading="isLoading"
          style="width: 100%"
          :header-cell-style="{ background: '#f1f1f1', fontSize: '17px' }"
          :row-class-name="tableRowClassName"
        >
          <el-table-column label="通知标题" min-width="15" align="center">
            <template slot-scope="scope">
              <div>
                {{ scope.row.title }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            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 label="创建者" min-width="15" align="center"
            ><template slot-scope="scope">
              <div>
                {{ scope.row.cName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" min-width="15" align="center"
            ><template slot-scope="scope">
              <div>
                {{ scope.row.cTime }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="20">
            <template slot-scope="scope">
              <div class="tableButton">
                <el-button @click="getNewDetail(scope.row.id)"
                  >查看通知</el-button
                >
              </div>
            </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="look_notice"
    >
      <div>
        <div class="pb_head">
          <span style="font-size: 20px">{{ res.title }}</span>
          <span style="font-size: 15px; line-height: 35px">{{
            res.cName
          }}</span>
        </div>
        <div class="notice_content cont" v-html="res.content"></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="close" @click="dialogVisible = false" type="primary"
          >关闭</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="添加通知"
      :visible.sync="dialogVisible1"
      :append-to-body="true"
      width="600px"
      :before-close="handleClose"
      :close-on-click-modal="false"
      class="dialog_diy"
    >
      <div>
        <div class="nTitle">
          <div>通知标题：</div>
          <el-input v-model="nTitle" placeholder="请输入通知标题"></el-input>
        </div>
        <div>
          <div style="margin: 10px 0">通知内容：</div>
          <el-input
            type="textarea"
            placeholder="请输入通知内容"
            v-model="nText"
            maxlength="200"
            show-word-limit
            rows="8"
            class="tAreaCss"
            cols
          >
          </el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer flex">
        <el-button class="left" @click="dialogVisible1 = false">取消</el-button>
        <el-button class="right" @click="addNotice" v-if="isAddOrUp == false"
          >确认</el-button
        >
        <el-button class="right" @click="addNotice" v-if="isAddOrUp == true"
          >修改</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      tableHeight: "500px",
      isLoading: false,
      formLabelWidth: "100px",
      page: 1,
      total: 0,
      dialogVisible: false,
      dialogVisible1: false,
      isAddOrUp: false,
      nTitle: "",
      nText: "",
      res: [],
    };
  },
  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, "");
    },
    handleClose(done) {
      done();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getNotice();
    },
    search() {
      this.page = 1;
      this.getNotice();
    },
    getNotice() {
      let params = {
        page: this.page,
      };
      this.ajax
        .get(this.$store.state.api + "selectRaceNotice", params)
        .then((res) => {
          this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
          this.tableData = res.data[0];
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getNewDetail(id) {
      this.dialogVisible = true;
      let params = { nid: id };
      this.ajax
        .get(this.$store.state.api + "getRaceNoticeDetail", params)
        .then((res) => {
          this.dialogVisible = true;
          this.res = res.data[0][0];
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    openNotice() {
      this.nTitle = "";
      this.nText = "";
      this.isAddOrUp = false;
      this.dialogVisible1 = true;
    },
    addNotice() {
      let params = {
        t: this.nTitle,
        c: this.nText,
        uid: this.$store.state.userInfo.userid,
      };
      this.ajax
        .get(this.$store.state.api + "insertRaceNote", params)
        .then((res) => {
          this.dialogVisible1 = false;
          this.getNotice();
          this.$message({
            message: "添加成功",
            type: "success",
          });
        })
        .catch((err) => {
          this.loading = false;
        });
    },
  },
  created() {
    this.getNotice();
  },
};
</script>

<style scoped>
.pb_head {
  display: flex;
  justify-content: space-between;
}
.dialog_diy >>> .el-dialog__header,
.dialog_diy1 >>> .el-dialog__header {
  background: #3d67bd !important;
  padding: 15px 20px;
}
.dialog_diy1 >>> .el-dialog__header {
  text-align: center;
}
.dialog_diy >>> .el-dialog__title,
.dialog_diy1 >>> .el-dialog__title {
  color: #fff;
}

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

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

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

.dialog_diy >>> .el-dialog__body,
.dialog_diy >>> .el-dialog__footer,
.dialog_diy1 >>> .el-dialog__body,
.dialog_diy1 >>> .el-dialog__footer {
  background: #fafafa;
}
/* .inputClass.is-active >>> .el-input__inner,
.inputClass >>> .el-input__inner:focus {
  border-color: #5c549f;
}

.inputClass >>> .el-select .el-input.is-focus .el-input__inner {
  border-color: #5c549f;
}
.inputClass >>> .el-select .el-input__inner:focus {
  border-color: #5c549f;
} */
.left {
  background: #fff !important;
  color: #000 !important;
}
.right,
.left {
  width: 80px;
  color: #fff;
  background: #409efe;
  margin-bottom: 20px;
}
.touTop {
  margin: 15px auto;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  width: 95%;
}

.touRight > div:nth-child(2) > .el-button {
  background: #2268bd;
  color: #fff;
}
.student_table >>> .el-table--border td {
  border-right: 0px !important;
}
.student_table >>> .el-table,
.student_table >>> .el-table__body-wrapper {
  height: auto !important;
}
.student_page {
  margin-top: 10px;
}
.tableButton {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}
.tableButton > .el-button {
  background: #409efe;
  color: #fff;
  cursor: pointer;
}
.customWidth >>> .el-dialog {
  min-width: 500px !important;
}
.people {
  border: 1px solid rgb(229 229 229);
  height: 495px;
  border-radius: 5px;
  width: 100%;
  overflow: auto;
  background: #fff;
  box-shadow: 0px 0px 10px 8px #ededed;
}
.people::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 6px;
  /*高宽分别对应横竖滚动条的尺寸*/
  height: 6px;
}

/*定义滚动条轨道 内阴影+圆角*/
.people::-webkit-scrollbar-track {
  border-radius: 10px;
  background-color: #eee;
}

/*定义滑块 内阴影+圆角*/
.people::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  background-color: rgba(0, 0, 0, 0.1);
}
.people_top {
  display: flex;
  width: 100%;
  flex-direction: column;
  padding: 10px 25px 10px;
  box-sizing: border-box;
  border-bottom: 1px solid #f4f4f7;
}

.people_top_right {
  height: 40px;
  margin-bottom: 10px;
}

.people_search {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}
.people_search > div:nth-child(1) {
  font-size: 20px;
  margin-right: 10px;
}
.t_j_box {
  display: flex;
}
.t_j_box span:nth-child(1) {
  width: 15%;
  overflow: hidden;
  margin-right: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.t_j_box span:nth-child(2) {
  width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 10px;
}
.t_j_box span:nth-child(3) {
  width: calc(55% - 20px);
  overflow: hidden;
  text-overflow: ellipsis;
}
.people_name {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-items: flex-start;
  padding: 15px 0 0 15px;
}
.people_name >>> .el-checkbox {
  margin-bottom: 10px;
}
.logoImg {
  width: 30px;
}
.logoImg > img {
  width: 100%;
  height: 100%;
}
.title_add_student {
  margin: 0 auto;
  color: #fff;
}
.look_notice >>> .el-dialog__header {
  padding: 20px 20px 10px;
  text-align: center;
  background: #32455b;
}
.look_notice >>> .el-dialog__title {
  font-size: 14px !important;
  color: #fff !important;
}
.look_notice >>> .el-dialog__headerbtn {
  font-size: 20px !important;
}
.look_notice >>> .el-form-item__label {
  margin-left: 65px;
}
.look_notice >>> .el-form-item {
  display: flex;
}
.look_notice >>> .el-form-item__content {
  margin: 0 !important;
}
.look_notice >>> .el-dialog__footer {
  text-align: center !important;
}
.look_notice >>> .el-dialog {
  min-width: 450px;
}
.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;
}
.tAreaCss >>> .el-textarea__inner {
  resize: none;
  font-size: 14px;
}
.tAreaCss >>> .el-textarea__inner {
  font-family: "微软雅黑";
}
.nTitle {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}
.nTitle >>> .el-input {
  width: 80%;
}
</style>