<template>
    <ele-page flex-table>
        <common-table
            ref="pageRef"
            :pageConfig="pageConfig"
            :columns="columns"
            :tools="false"
        >
            <template #toolbar>
                <div class="flex justify-between items-center">
                    <el-button
                        type="primary"
                        plain
                        v-permission="'customer:userTag:add'"
                        @click="handleUpdate()"
                        :icon="PlusOutlined"
                    >
                        新增标签
                    </el-button>

                    <div class="flex items-center">
                        <el-input
                            v-model="queryParams.tagNameLike"
                            class="mr-6"
                            placeholder="请输入标签名称"
                        />
                        <el-button
                            style="width: 80px"
                            type="primary"
                            @click="reload(queryParams)"
                            >查询</el-button
                        >
                        <el-button
                            style="width: 80px"
                            type="info"
                            @click="handleReset"
                            >重置</el-button
                        >
                    </div>
                </div>
            </template>

            <template #action="{ row }">
                <div>
                    <el-button
                        type="primary"
                        link
                        v-permission="'customer:userTag:update'"
                        @click="handleUpdate(row)"
                    >
                        [编辑]
                    </el-button>
                    <el-button
                        type="danger"
                        link
                        v-permission="'customer:userTag:delete'"
                        @click="handleDelete(row)"
                    >
                        [删除]
                    </el-button>
                </div>
            </template>
        </common-table>
        <page-edit ref="pageEditRef" @success="reload()" />
    </ele-page>
</template>

<script setup>
    import { ref, reactive } from 'vue';
    import { PlusOutlined } from '@/components/icons';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import pageEdit from './components/page-edit.vue';
    import request from '@/utils/request';

    defineOptions({ name: 'UserTagList' });

    const queryParams = reactive({ tagNameLike: '' });
    /** 表格列配置 */
    const columns = ref([
        { label: '序号', type: 'index', align: 'center', width: 100 },
        { label: '标签名称', prop: 'tagName', align: 'center' },
        { label: '标签用户数', prop: 'useNum', align: 'center' },
        { columnKey: 'action', label: '操作', align: 'center', slot: 'action' }
    ]);

    /** 页面组件实例 */
    const pageRef = ref(null);

    const pageConfig = reactive({
        pageUrl: '/user/tag/pagelist',
        exportUrl: '/user/tag/export',
        fileName: '用户标签列表',
        cacheKey: 'userTagTable'
    });

    //刷新表格
    function reload(where) {
        pageRef.value?.reload(where);
    }

    //删除
    function handleDelete(row) {
        pageRef.value?.messageBoxConfirm({
            message: '确认删除？',
            fetch: () => request.post('/user/tag/delete', [row.id])
        });
    }

    //编辑
    const pageEditRef = ref(null);
    function handleUpdate(row) {
        pageEditRef.value?.handleOpen(row);
    }

    //重置
    function handleReset() {
        queryParams.tagNameLike = '';
        reload();
    }
</script>
