<template>
    <div class="default-main">
        <TableHeader :showSearch="false">
            <template v-slot:operation>
                <el-button type="primary" @click="add" icon="el-icon-Plus">新增</el-button>
            </template>
        </TableHeader>
        <Table
            ref="tableRef"
            :tree-config="{ transform: true, parentField: 'uPid', rowField: 'uId' }"
            :scroll-y="{ enabled: true }"
        />
        <Add ref="addRef" v-if="addFlag" @onSubmit="tableStore.index()" />
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import { useDictData } from '@/stores/dictData'
import { ElMessage } from 'element-plus'
import TableHeader from '@/components/table/header/index.vue'
import Add from './add.vue'
import { deleteSubassembly } from '@/api/user-boot/dept'
defineOptions({
    name: 'component'
})
const addRef = ref()
const addFlag = ref(false)
const tableRef = ref()
const treeData: any = ref([])
const tableStore = new TableStore({
    url: '/user-boot/component/componentTree',
    method: 'GET',
    showPage: false,
    column: [
        { field: 'name', title: '功能组件名称', align: 'left', treeNode: true },
        {
            title: '组件图标',
            field: 'icon',
            align: 'center',
            width: '80',
            render: 'icon'
        },
        { field: 'code', title: '组件标识' },
        { field: 'path', title: '组件路径' },
        { field: 'image', title: '组件展示', render: 'image' },
        {
            title: '操作',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '修改',
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    render: 'basicButton',
                    disabled: row => {
                        return row.path == '' || row.path == null
                    },
                    click: row => {
                        addFlag.value = true
                        setTimeout(() => {
                            addRef.value.open('修改组件', row)
                        }, 100)
                    }
                },
                {
                    name: 'del',
                    text: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    disabled: row => {
                        return row.path == '' || row.path == null
                    },
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除？'
                    },
                    click: row => {
                        deleteSubassembly({ id: row.id }).then(() => {
                            ElMessage.success('删除成功!')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    loadCallback: () => {
        addFlag.value = false
        setTimeout(() => {
            tableRef.value.getRef().setAllTreeExpand(true)
        }, 1000)
        tableStore.table.data.forEach((item:any) => {
            item.state = 0
        })
        treeData.value = tree2List(tableStore.table.data, Math.random() * 1000)
        tableStore.table.data = treeData.value
    }
})
const tree2List = (list: any, id: any) => {
    //存储结果的数组
    let arr: any = []
    // 遍历 tree 数组
    list.forEach((item: any) => {
        item.uPid = id
        item.uId = Math.random() * 1000
        // 判断item是否存在children
        if (!item.children) return arr.push(item)
        // 函数递归，对children数组进行tree2List的转换
        const children = tree2List(item.children, item.uId)
        // 删除item的children属性
        delete item.children
        // 把item和children数组添加至结果数组
        //..children: 意思是把children数组展开
        arr.push(item, ...children)
    })
    // 返回结果数组
    return arr
}
// 新增
const add = () => {
    addFlag.value = true
    setTimeout(() => {
        addRef.value.open('新增组件')
    }, 100)
}
provide('tableStore', tableStore)
onMounted(() => {
    tableStore.index()
})
</script>
