<template>
  <div class="blogs-page">
    <div class="title">个人博客管理</div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="id" label="博客id" />
      <el-table-column fixed prop="name" label="博客名称">
        <template slot-scope="scope">
          <el-button type="text" @click="showBlog(scope.row)" size="small">{{
            scope.row.name
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column fixed prop="time" label="发布时间" />
      <el-table-column fixed prop="updateTime" label="修改时间" />
      <el-table-column fixed prop="likes" 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="editBlog(scope.row)" size="small"
            >编辑</el-button
          >
          <el-button type="text" @click="deleteBlog(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>
  </div>
</template>

<script>
import { getBlogs, delBlog } from "@/api";

export default {
  name: " blogs",
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 15,
      size: 1,
    };
  },
  props: {
    userInfo: {
      type: Object,
      default: () => {},
    },
  },
  mounted() {
    this.pageInit();
  },
  methods: {
    async pageInit() {
      if (!this.userInfo?.id) return;
      const res = await getBlogs({
        userId: this.userInfo.id,
        size: this.size,
        pageSize: this.pageSize,
      });
      console.log(48, res);
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    deleteBlog(row) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delBlog({ id: row.id });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    editBlog(row) {
      this.$router.push({
        path: "blog",
        query: {
          id: row.id,
        },
      });
    },
    sizeChange(size) {
      this.size = size;
      this.pageInit();
    },
    showBlog(row) {
      this.$router.push({
        path: "/blogDetails",
        query: {
          id: row.id,
        },
      });
    },
  },
};
</script>

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