<template>
  <div style="width: 100%">
    <el-main>
      <div class="tou">模板管理管理</div>
      <div>
        <div style="margin: 15px 0; display: flex; justify-content: flex-end">
          <el-button type="primary" size="small" @click="dialogVisible1 = true"
            >新建文档模板</el-button
          >
        </div>
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :header-cell-style="{ background: '#f1f1f1', 'text-align': 'center' }"
          :cell-style="{ 'text-align': 'center' }"
          style="width: 100%"
          :height="tableHeight"
          v-loading="isLoading"
        >
          <el-table-column prop="title" label="模板标题" min-width="25">
          </el-table-column>
          <el-table-column prop="time" label="创建时间" min-width="10">
          </el-table-column>
          <el-table-column label="操作" min-width="10">
            <template slot-scope="scope">
              <div class="pb_buttonBox">
                <el-button size="mini" type="primary" @click="check(scope.row)"
                  >查看</el-button
                >
                <!-- @click="goTo('/course?index=1&id=' + scope.row.courseId)" -->
                <el-button
                  size="mini"
                  type="primary"
                  @click="deleteTemplate(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="!isListAjax && page"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-main>
    <el-dialog
      title="添加模板文档"
      :visible.sync="dialogVisible1"
      width="550px"
      :before-close="handleClose"
      class="dialog_diy"
      center
    >
      <div>
        <div class="tian1">
          <span>模板文档标题</span>
          <el-input
            v-model="tTitle"
            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="addTemplate">确认添加</el-button>
        <el-button @click="dialogVisible1 = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="查看详情"
      :visible.sync="dialogVisible2"
      width="700px"
      :before-close="handleClose"
      class="dialog_diy dialog_diyA"
      center
    >
      <div class="head">
        <span>{{ templateD.title }}</span>
        <span class="name">{{ templateD.time }}</span>
      </div>
      <div class="cont" v-html="templateD.content"></div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible2 = false"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

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

export default {
  components: { EditorBar },
  data() {
    return {
      tableHeight: 500,
      courseName: "",
      dialogVisible1: false,
      dialogVisible2: false,
      isClear: false,
      tableData: [],
      page: 1,
      total: 0,
      isLoading: false,
      isListAjax: false,
      tTitle: "",
      detail: "",
      templateD: {},
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    change(val) {
      console.log(val);
    },
    //在method里面写方法
    snippet(value) {
      return value.replace(/<[^>]*>/g, "");
    },

    changeHeight() {
      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;
        }
      };
      // this.$refs.table.$el.offsetTop：表格距离浏览器的高度 //200表示你想要调整的表格距离底部的高度（你可以自己随意调整），因为我们一般都有放分页组件的，所以需要给它留一个高度
    },
    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;
        }
      }
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
    },
    init() {
      this.detail = "";
      this.tTitle = "";
    },
    addTemplate() {
      if (this.tTitle === "") {
        this.$message.error("请输入模板文档标题");
        return;
      } else if (this.detail === "") {
        this.$message.error("请输入模板文档内容");
        return;
      }
      console.log(this.detail);
      if (this.time()) {
        let params = [
          {
            t: this.tTitle,
            nc: this.detail.replace(/%/g, "%25"),
            uid: this.$store.state.userInfo.uid,
          },
        ];
        this.ajax
          .post(this.$store.state.api + "addTemplateAdmin", params)
          .then((res) => {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.init();
            this.getTemplate();
            this.dialogVisible1 = false;
          })
          .catch((err) => {
            this.$message.error("添加失败");
            console.error(err);
          });
      }
    },
    getTemplate() {
      this.isLoading = true;
      let params = { page: this.page };
      this.ajax
        .get(this.$store.state.api + "getTemplateAdmin", params)
        .then((res) => {
          this.isLoading = false;
          this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
          this.tableData = res.data[0];
          console.log(this.tableData);
        })
        .catch((err) => {
          this.isLoading = false;
          console.error(err);
        });
    },
     //删除通知
    deleteTemplate(id) {
      let params = [{ nid: id }];
      this.$confirm("确定删除此模板吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.ajax
            .post(this.$store.state.api + "deleteTemplateAdmin", params)
            .then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              if (this.page != 1 && this.tableData.length == 1) {
                this.page--;
              }
              this.getTemplate();
            })
            .catch((err) => {
              this.$message.error("删除失败");
              console.error(err);
            });
        })
        .catch(() => {});
    },
    check(res) {
      this.dialogVisible2 = true;
      this.templateD = res;
    },
  },
  created() {
    this.$nextTick(function () {
      this.page = 1;
      this.changeHeight();
      this.getTemplate();
    });
  },
};
</script>

<style scoped>
.tou {
  border-bottom: 1px solid #c9c9c9;
  height: 50px;
  font-size: 30px;
}
.dialog_diy >>> .el-dialog__header {
  background: #3d67bc !important;
  padding: 15px 20px;
}
.dialog_diy >>> .el-dialog__title {
  color: #fff;
}
.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;
}
.student_page {
  margin-top: 15px;
}
.tian1 {
  margin-top: 15px;
}
.tian1 span {
  display: block;
}
/* 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: 3px 5px;
}
.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;
}
.head {
  margin: 0 0 15px;
  font-size: 20px;
  border-bottom: 1px solid;
  /* height: 35px; */
  display: flex;
  align-items: center;
  padding-bottom: 10px;
  justify-content: space-between;
  align-items: flex-end;
}
.name {
  /* margin-top: -30px; */
  font-size: 14px;
}
</style>
