///
import request from "../modules/request.js";
let editor = null;
export default {
    el: "#app", // 设置挂载对象
    data() {
        return {
            list: [],
            showDialog: false,
            form: {
                category: "",
                comment_count: 0,
                content: "",
                cover: "",
                description: "",
                id: 0,
                publish_time: null,
                status: 1,
                title: "",
                view_count: 0,
            },
        };
    },
    mounted() {
        this.loadArticleList();
    },
    methods: {
        // 加载所有的文章列表
        async loadArticleList() {
            const ret = await request("./api/article/admin/list", {
                size: 10000,
            });
            this.list = ret.list;
        },
        showAdd(){

            // 不能直接赋值一个对象
            this.form = { 
                category: "",
                comment_count: 0,
                content: "",
                cover: "",
                description: "",
                id: 0,
                publish_time: null,
                status: 1,
                title: "",
                view_count: 0,
            };
            this.showDialog = true;
            this.showEditor('')
        },
        showEditor(content){
            if (editor == null) {
                // 将回调延迟到下次 DOM 更新循环之后执行
                this.$nextTick(() => {
                    // 声明全局变量 表示编辑器是否已经初始化
                    if (editor == null) {
                        // // 初始化编辑器
                        editor = KindEditor.create("#editor_id");
                    }
                });
            } else {
                // 给编辑器设置内容
                editor.html(content);
            }
        },
        showEdit(a) {
            // 不能直接赋值一个对象
            this.form = { ...a };
            this.showDialog = true;
            this.showEditor(a.content)
           
        },
        saveArticle() {
            this.form.content = editor.html(); // 获取编辑器的内容
            request("./api/article/admin/save", this.form, "POST").then(
                (ret) => {
                    if (ret.status) {
                        this.$message("更新成功");
                        this.showDialog = false; // 隐藏对话框
                        this.loadArticleList();
                    } else {
                        this.$message("更新出错了");
                    }
                }
            );
        },
        async deleteArticlebyId(id) {
            // status为0表示文章被删除
            const ret = await request("/api/article/admin/update", {
                id,
                status: 0,
            });
            this.loadArticleList(); // 重新加载文章列表
        },
        deleteArticle(art) {
            // 原生的confirm
            // if(confirm('此操作将永久删除该文章数据,是否继续?')){
            //      this.deleteArticlebyId(art.id)
            // }
            // this.$confirm("此操作将永久删除该文件, 是否继续?", "提示")
            //     .then(() => { // 点击了确定按钮
            //         this.deleteArticlebyId(art.id)
            //     })
            //     .catch(() => {});
        },
        async updateArticleStatus(art){
            await request("/api/article/admin/update", {
                id:art.id,
                status: art.status == 1?2:1
            });
            this.loadArticleList(); // 重新加载文章列表
        }
    },
};
