<!--产品分类 -->
<template>
    <a-card class="product-category">
        <Search :columns="query.columns" target="category" />
        <JTable
            ref="tableRef"
            :columns="table.columns"
            :request="queryTree"
            model="TABLE"
            :params="query.params"
        >
            <template #headerTitle>
                <a-button type="primary" @click="add"
                    ><plus-outlined />新增</a-button
                >
            </template>
            <template #action="slotProps">
                <a-space :size="16">
                    <a-tooltip
                        v-for="i in getActions(slotProps, 'table')"
                        :key="i.key"
                        v-bind="i.tooltip"
                    >
                        <a-popconfirm
                            v-if="i.popConfirm"
                            v-bind="i.popConfirm"
                            :disabled="i.disabled"
                        >
                            <a-button
                                :disabled="i.disabled"
                                style="padding: 0"
                                type="link"
                                ><AIcon :type="i.icon"
                            /></a-button>
                        </a-popconfirm>
                        <a-button
                            style="padding: 0"
                            type="link"
                            v-else
                            @click="i.onClick && i.onClick(slotProps)"
                        >
                            <a-button
                                :disabled="i.disabled"
                                style="padding: 0"
                                type="link"
                                ><AIcon :type="i.icon"
                            /></a-button>
                        </a-button>
                    </a-tooltip>
                </a-space>
            </template>
        </JTable>
        <!-- 新增和编辑弹窗 -->
        <ModifyModal
            ref="modifyRef"
            :formData="currentForm"
            :title="title"
            :isAdd="isAdd"
            :isChild="isChild"
            @refresh="() => modifyRef.value?.reload()"
        />
    </a-card>
</template>
<script lang="ts" name="Category" setup>
import { queryTree, deleteTree } from '@/api/device/category';
import type { ActionsType } from '@/components/Table/index.vue';
import ModifyModal from './components/modifyModal/index.vue';
import type { TableColumnType, TableProps } from 'ant-design-vue';
import { message } from 'ant-design-vue';
const tableRef = ref<Record<string, any>>({});
const modifyRef = ref();
const dataSource = ref([]);
const currentForm = ref({});
const title = ref('');
const isAdd = ref(0);
const isChild = ref(false);
// 筛选
const query = reactive({
    columns: [
        {
            title: '名称',
            dataIndex: 'name',
            ellipsis: true,
        },
        {
            title: '排序',
            dataIndex: 'sortIndex',
            valueType: 'digit',
            sorter: true,
        },
        {
            title: '描述',
            key: 'description',
            ellipsis: true,
            dataIndex: 'description',
            filters: true,
            onFilter: true,
        },
        {
            title: '操作',
            valueType: 'option',
            width: 200,
            fixed: 'right',
        },
    ],
    params: {
        paging: false,
        sorts: [
            { name: 'sortIndex', order: 'asc' },
            {
                name: 'createTime',
                order: 'desc',
            },
        ],
    },
});

/**
 * 操作栏按钮
 */
const getActions = (
    data: Partial<Record<string, any>>,
    type: 'table',
): ActionsType[] => {
    if (!data) return [];
    const actions = [
        {
            key: 'edit',
            text: '编辑',
            tooltip: {
                title: '编辑',
            },
            icon: 'EditOutlined',
            onClick: async () => {
                title.value = '编辑分类';
                isAdd.value = 2;
                currentForm.value = data;
                nextTick(() => {
                    modifyRef.value.show(data);
                });
            },
        },
        {
            key: 'add',
            text: '添加子分类',
            tooltip: {
                title: '添加子分类',
            },
            icon: 'PlusCircleOutlined',
            onClick: () => {
                title.value = '新增子分类';
                isAdd.value = 0;
                isChild.value = true;
                currentForm.value = {};
                nextTick(() => {
                    modifyRef.value.show(data);
                });
            },
        },
        {
            key: 'delete',
            text: '删除',
            popConfirm: {
                title: '确认删除?',
                okText: ' 确定',
                cancelText: '取消',
                onConfirm: async () => {
                    const resp = await deleteTree(data.id);
                    if (resp.status === 200) {
                        message.success('操作成功！');
                        tableRef.value?.reload();
                    } else {
                        message.error('操作失败！');
                    }
                },
            },
            icon: 'DeleteOutlined',
        },
    ];
    return actions;
};

const table = reactive({
    columns: [
        { title: '名称', dataIndex: 'name', key: 'name' },
        {
            title: '排序',
            dataIndex: 'sortIndex',
            key: 'sortIndex',
        },
        {
            title: '说明',
            dataIndex: 'describe',
            key: 'describe',
        },
        {
            title: '操作',
            key: 'action',
            fixed: 'right',
            width: 250,
            scopedSlots: true,
        },
    ],
    /**
     * 添加产品分类
     */
    add: async () => {
        title.value = '新增分类';
        isAdd.value = 0;
        isChild.value = false;
        nextTick(() => {
            modifyRef.value.show(currentForm.value);
        });
    },
});
const { add, columns } = toRefs(table);
/**
 * 初始化
 */
</script>
<style scoped lang="less"></style>
