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

const columns = [
    {
        title: 'ID',
        dataIndex: 'key',
        width: 120,
    },
    {
        title: '标题',
        dataIndex: 'title',
    },
    {
        title: '简介',
        dataIndex: 'content',
    },
    {
        title: '跳转方式',
        dataIndex: 'type_text',
    },
    {
        title: '发布时间',
        dataIndex: 'createtime',
    },
    {
        title: '是否启用',
        dataIndex: 'status',
        slotName: 'isStatus',
        width: 100,
        align: 'center',
    },
    {
        title: '是否置顶',
        dataIndex: 'is_top',
        slotName: 'isTop',
        width: 100,
        align: 'center',
    },
    {
        title: '排序',
        dataIndex: 'sort',
        slotName: 'sort',
        width: 140,
    },
    {
        title: '操作',
        dataIndex: 'action',
        slotName: 'action',
        width: 120,
    },
];

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

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

const delNotice = async (id) => {
    const {code, msg} = await Api.admin.delNotice(id);
    if (code === 1) Message.success(msg);
    await fetchData();
}
</script>

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

    <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:isStatus="{record}">
            <x-switch
                v-model:model-value="record.status"
                :id="record.id"
                :api="Api.admin.setNoticeStatus"
                @change="fetchData">
            </x-switch>
        </template>
        <template v-slot:isTop="{record}">
            <x-switch
                v-model:model-value="record.is_top"
                :id="record.id"
                :api="Api.admin.setNoticeTop"
                @change="fetchData">
            </x-switch>
        </template>
        <template v-slot:sort="{record}">
            <SequenceAdjustment
                :id="record.id"
                @success="fetchData"
                :api="Api.admin.setNotcieWeigh">
            </SequenceAdjustment>
        </template>
        <template v-slot:action="{record}">
            <div class="flex gap-[20px]">
                <EditPlatformNews @success="fetchData" :detail="record"></EditPlatformNews>
                <a-popconfirm content="确认删除吗?" @ok="delNotice(record.id)">
                    <a-link :hoverable="false" status="danger">删除</a-link>
                </a-popconfirm>
            </div>
        </template>
    </a-table>
</template>

<style scoped lang="scss">

</style>
