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

const columns = [
    {
        title: 'ID',
        dataIndex: 'id',
        width: 120,
    },
    {
        title: '封面',
        dataIndex: 'file',
        slotName: 'file',
    },
    {
        title: '跳转方式',
        dataIndex: 'type_text',
    },
    {
        title: '是否启用',
        dataIndex: 'isStatus',
        slotName: 'isStatus',
        width: 100,
        align: 'center',
    },
    {
        title: '排序',
        dataIndex: 'sort',
        slotName: 'sort',
        width: 140,
    },
    {
        title: '操作',
        dataIndex: 'action',
        slotName: 'action',
        width: 120,
    },
];

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

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

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

<template>
    <EditBanner :position="3" @success="fetchData"></EditBanner>

    <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:file="{record}">
            <a-image height="60" :src="record.file"></a-image>
        </template>
        <template v-slot:isStatus="{record}">
            <x-switch
                v-model:model-value="record.status"
                :id="record.id"
                :api="Api.admin.setADVStatus"
                @change="fetchData">
            </x-switch>
        </template>
        <template v-slot:sort="{record}">
            <SequenceAdjustment
                :id="record.id"
                @success="fetchData"
                :api="Api.admin.setADVWeigh">
            </SequenceAdjustment>
        </template>
        <template v-slot:action="{record}">
            <div class="flex gap-[10px]">
                <EditBanner :position="3" :detail="record" @success="fetchData">
                    <a-link :hoverable="false">编辑</a-link>
                </EditBanner>
                <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>
