<template>
    <div>
        <div class="custom-table-header">
            <div class="title">菜单列表</div>
            <el-input maxlength="32" show-word-limit v-model.trim="tableStore.table.params.searchValue"
                style="width: 310px" placeholder="请输入菜单名称" class="ml10" clearable @input="search" />
            <el-button :icon="Plus" type="primary" @click="addMenu" class="ml10">新增</el-button>
        </div>
        <Table @currentChange="currentChange" />
        <popupMenu ref="popupRef" @init="emits('init')"></popupMenu>
    </div>
</template>
<script setup lang="ts">
import { Plus } from '@element-plus/icons-vue'
import { ref, nextTick, inject, provide, watch } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import popupMenu from './popupMenu.vue'
import { delMenu } from '@/api/systerm'

defineOptions({
    name: 'auth/menu/menu'
})
const emits = defineEmits<{
    (e: 'init'): void
    (e: 'select', row: any): void
}>()
const props = withDefaults(
    defineProps<{
        menuData: treeData[]
    }>(),
    {
        menuData: () => {
            return []
        }
    }
)
const popupRef = ref()
const tableStore = new TableStore({
    showPage: false,
    url: '/user-boot/function/functionTree',
    column: [
        { title: '菜单名称', field: 'title', align: 'left', treeNode: true },
        {
            title: '图标',
            field: 'icon',
            align: 'center',
            width: '60',
            render: 'icon'
        },
        {
            title: '操作',
            align: 'center',
            width: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    text: '新增',
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    render: 'basicButton',
                    click: row => {
                        popupRef.value.open('新增菜单', { pid: row.id })
                    }
                },
                {
                    name: 'edit',
                    text: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        popupRef.value.open('编辑菜单', row)
                    }
                },
                {
                    name: 'del',
                    text: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除该菜单吗？'
                    },
                    click: row => {
                        delMenu(row.id).then(() => {
                            emits('init')
                        })
                    }
                }
            ]
        }
    ]
})
tableStore.table.loading = false
watch(
    () => props.menuData,
    () => {
        search()
    }
)
provide('tableStore', tableStore)

const addMenu = () => {
    popupRef.value.open('新增菜单', {})
}
const currentChange = (newValue: any) => {
    emits('select', newValue.row.id)
}
const search = () => {
    tableStore.table.data = filterData(JSON.parse(JSON.stringify(props.menuData)))
    if (tableStore.table.params.searchValue) {
        nextTick(() => {
            tableStore.table.ref?.setAllTreeExpand(true)
        })
    }
}

// 过滤
const filterData = (arr: treeData[]): treeData[] => {
    if (!tableStore.table.params.searchValue) {
        return arr
    }
    return arr.filter((item: treeData) => {
        if (item.title.includes(tableStore.table.params.searchValue)) {
            return true
        } else if (item.children?.length > 0) {
            item.children = filterData(item.children)
            return item.children.length > 0
        } else {
            return false
        }
    })
}
</script>
