<template>
    <el-dialog draggable class="cn-operate-dialog" v-model="dialogVisible" width="930px" :title="title" @close="cancel">
        <div style="display: flex">
            <el-form :inline="false" :model="form" label-width="auto" :rules="rules" ref="formRef" style="flex: 1">
                <el-form-item class="top" label="组件名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入组件名称"></el-input>
                </el-form-item>
                <el-form-item class="top" label="父组件节点" prop="system">
                    <el-cascader
                        v-model="form.system"
                        :options="customDeptOption"
                        :props="props"
                        placeholder="请选择父组件节点"
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="组件图标" prop="icon">
                    <IconSelector v-model="form.icon" placeholder="请选择图标" />
                </el-form-item>
                <el-form-item class="top" label="组件标识" prop="code">
                    <el-input v-model="form.code" placeholder="请输入组件菜单选取"></el-input>
                </el-form-item>
                <el-form-item class="top" label="组件路径" prop="path">
                    <el-input v-model="form.path" placeholder="请输入组件路径"></el-input>
                </el-form-item>
                <el-form-item class="top" label="组件查询时间">
                    <el-radio-group v-model="form.timeKey" style="width: 100%">
                        <el-radio-button label="年" value="1" />
                        <el-radio-button label="季" value="2" />
                        <el-radio-button label="月" value="3" />
                        <el-radio-button label="周" value="4" />
                        <el-radio-button label="日" value="5" />
                    </el-radio-group>
                </el-form-item>
                <el-form-item class="top" label="组件排序" prop="sort">
                    <el-input v-model="form.sort" placeholder="请输入组件排序"></el-input>
                </el-form-item>
            </el-form>
            <div style="width: 550px; height: 370px; overflow: hidden">
                <div class="ml10" style="font-weight: 600">组件展示</div>
                <component
                    :is="registerComponent(form.path)"
                    v-if="registerComponent(form.path)"
                    class="pd10 GridLayout"
                    :key="form.path"
                    :height="'350px'"
                    :width="'533px'"
                    :timeKey="form.timeKey"
                />
                <!-- <div  class="pd10">组件加载失败...</div> -->
                <el-empty v-else description="未查询到组件" style="height: 350px; width: 533px" />
            </div>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="submit">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject, onMounted, type Component } from 'vue'
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useDictData } from '@/stores/dictData'
import TableStore from '@/utils/tableStore' // 若不是列表页面弹框可删除
import { getFatherComponent, componentAdd, componentEdit } from '@/api/user-boot/dept'
import IconSelector from '@/components/baInput/components/iconSelector.vue'
import html2canvas from 'html2canvas'
const dictData = useDictData()
const dialogVisible = ref(false)
const title = ref('')
const tableStore = inject('tableStore') as TableStore
const formRef = ref()
// 注意不要和表单ref的命名冲突
const form = ref<anyObj>({
    name: '',
    sort: 100,
    system: [],
    timeKey: '3',
    code: '',
    path: ''
})
const props = { label: 'name', value: 'id' }
const rules = {
    code: [{ required: true, message: '请输入组件标识', trigger: 'blur' }],
    name: [{ required: true, message: '请输输入组件名称', trigger: 'blur' }],
    system: [{ required: true, message: '请先择父组件节点', trigger: 'change' }],
    icon: [{ required: true, message: '请先择组件图标', trigger: 'change' }],
    path: [{ required: true, message: '请输入组件路径', trigger: 'blur' }],
    sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
}
const customDeptOption: any = ref([])
onMounted(() => {
    customDeptOption.value = dictData.getBasicData('System_Type')
    customDeptOption.value.forEach((item: any) => {
        getFatherComponent({ systemType: item.id }).then(res => {
            item.children = res.data
        })
    })
})

const open = (text: string, data?: anyObj) => {

    title.value = text
    dialogVisible.value = true
    if (data) {
        let Data = JSON.parse(JSON.stringify(data))
        form.value = Data
        form.value.system = [Data.systemType, Data.pid]
    }
}
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            let url = ''
            await html2canvas(document.querySelector('.GridLayout'), {
                scale: 2
            }).then(canvas => {
                url = canvas.toDataURL('image/png')
               // console.log('🚀 ~ html2canvas ~ url:', url)
            })
            if (title.value == '新增组件') {
                await componentAdd({
                    ...form.value,
                    systemType: form.value.system[0],
                    pid: form.value.system[1],
                    image: url
                }).then(res => {
                    ElMessage.success('新增成功!')
                    cancel()
                })
            } else {
                await componentEdit({
                    ...form.value,
                    systemType: form.value.system[0],
                    pid: form.value.system[1],
                    image: url
                }).then(res => {
                    ElMessage.success('修改成功!')
                    cancel()
                })
            }
        }
    })
}
// 组件映射
const componentMap = reactive(new Map<string, Component | string>())
// 动态注册组件
const registerComponent = (path: string): Component | string | null => {
    if (!path) return null
    if (path.slice(-4) != '.vue') return null

    const cacheKey = path

    // 使用缓存的组件
    if (componentMap.has(cacheKey)) {
        return componentMap.get(cacheKey)!
    }

    try {
        // 动态导入组件
        const modules = import.meta.glob('@/views/**/*.vue')
        if (!modules[path]) {
            console.error(`组件加载失败: ${path}`)
            return null
        }

        const AsyncComponent = defineAsyncComponent({
            loader: modules[path],
            loadingComponent: () => h('div', '加载中...'),
            errorComponent: ({ error }) => h('div', `加载错误: ${error.message}`),
            delay: 200,
            timeout: 10000
        })

        // 保存到映射中
        componentMap.set(cacheKey, markRaw(AsyncComponent))
        return AsyncComponent
    } catch (error) {
        console.error('注册组件失败:', error)
        return null
    }
}
const cancel = () => {
    tableStore.index()
    dialogVisible.value = false
}
defineExpose({ open })
</script>
