<template>
  <div class="blogs-page">
    <div class="title">
      <div>评论管理</div>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="id" label="评论id" />
      <el-table-column fixed prop="content" label="评论内容">
        <template slot-scope="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>
      <el-table-column fixed prop="time" label="发布时间" />
      <el-table-column fixed prop="updateTime" label="修改时间" />
      <el-table-column label="操作">
        <!--        <template slot-scope="scope">-->
        <template slot-scope="scope">
          <!--          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>-->
          <el-button type="text" @click="editComments(scope.row)" size="small"
            >编辑</el-button
          >
          <el-button type="text" @click="deleteTag(scope.row)" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="size"
      :total="total"
      @current-change="sizeChange"
      :pager-count="this.pageSize"
    >
    </el-pagination>

    <el-dialog
      :title="form.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose"
    >
      <div>
        <vue2-tinymce-editor
          :options="options"
          ref="tinymce"
          v-model="form.content"
        ></vue2-tinymce-editor>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="pushCommentFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getComments, delComments, pushComment } from "@/api";
import loginCom from "@/components/login.vue";
import { Vue2TinymceEditor } from "vue2-tinymce-editor";

export default {
  name: " blogs",
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 15,
      size: 1,
      dialogVisible: false,
      form: {
        content: "",
      },
      options: {
        language_url: "./tinymce/lang/zh-Hans.js",
        language: "zh-Hans", // 注意大小写
      },
      tinymceKey: "12131",
    };
  },
  components: { Vue2TinymceEditor, loginCom },
  props: {
    userInfo: {
      type: Object,
      default: () => {},
    },
  },
  mounted() {
    this.pageInit();
  },
  methods: {
    async pageInit() {
      if (!this.userInfo?.id) return;
      const res = await getComments({
        userId: this.userInfo.id,
        size: this.size,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    deleteTag(row) {
      this.$confirm("此操作将永久删除该评论, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delComments({ id: row.id });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.pageSize = 15;
          this.size = 1;
          this.pageInit();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    editComments(row) {
      this.dialogVisible = true;
      this.form = {
        ...row,
      };
    },
    openAddTag() {
      this.dialogVisible = true;
      this.form = {};
    },
    async pushCommentFn() {
      if (this.form?.id) {
        const res = await pushComment({
          ...this.form,
          userId: this.userInfo.id,
          token: window.localStorage.getItem("token"),
        });
        console.log(res);
        this.dialogVisible = false;
        this.$message({
          type: "success",
          message: "编辑成功!",
        });
      }
      this.pageSize = 15;
      this.size = 1;
      this.pageInit();
    },
    sizeChange(size) {
      this.size = size;
      this.pageInit();
    },
    handleClose() {},
  },
};
</script>

<style lang="less" scoped>
.blogs-page {
  //margin-left: 0;
  //width: 100vw;
  background-color: #f8f9fb;
  padding: 24px;
  //margin-top: 350px;
  .title {
    text-align: left;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
