<script setup>
import {reactive} from "vue";
import useTableQuery from "../../../../../hooks/useTableQuery.js";
import Api from "../../../../../api/index.js";
import AddRichTextContent from "./AddRichTextContent.vue";
import {Message} from "@arco-design/web-vue";

const columns = [
    {
        title: 'ID',
        dataIndex: 'id',
        width: 120,
    },
    {
        title: '标题',
        dataIndex: 'title',
    },
    {
        title: '预览',
        dataIndex: 'preview',
        slotName: 'preview',
        width: 100,
    },
    {
        title: '操作',
        dataIndex: 'action',
        slotName: 'action',
        width: 120,
    },
];

const po = reactive({});
const vo = reactive({
    page: '',
    rows: [],
    total: 0,
});

const {loading, pagination, fetchData} = useTableQuery({
    parameter: po,
    api: Api.admin.getSingleList,
    callback: (data) => {
        Object.assign(vo, data);
    }
});

const del = async (id) => {
    const {msg} = await Api.admin.delSingle(id);
    Message.success(msg);
    await fetchData();
}
</script>

<template>
    <AddRichTextContent @success="fetchData">
        <a-button type="primary">
            <template #icon>
                <icon-plus/>
            </template>
            新建
        </a-button>
    </AddRichTextContent>

    <a-table
        :loading="loading"
        @page-change="(e) => pagination.current = e"
        :pagination="pagination"
        :data="vo.rows"
        :columns="columns"
        class="flex-grow mt-[20px] w-full">
        <template v-slot:preview="{record}">
            <AddRichTextContent @success="fetchData" :detail="record" :preview="true">
                <a-link :hoverable="false">
                    <icon-eye/>
                    预览
                </a-link>
            </AddRichTextContent>
        </template>
        <template v-slot:action="{record}">
            <div class="flex gap-[20px]">
                <AddRichTextContent @success="fetchData" :detail="record">
                    <a-link :hoverable="false">编辑</a-link>
                </AddRichTextContent>
                <a-popconfirm content="确定删除吗?" @ok="del(record.id)">
                    <a-link :hoverable="false" status="danger">删除</a-link>
                </a-popconfirm>
            </div>
        </template>
    </a-table>
</template>

<style scoped lang="scss">

</style>
