<template>
    <div class="sidebar-layout">
        <!-- 侧边栏 -->
        <aside class="sidebar" :class="{ collapsed: collapsed }">
            <div class="sidebar-header">
                <div class="logo">
                    <img src="/logo.jpg" alt="Logo" class="logo-img" />
                    <span v-if="!collapsed" class="logo-text">城市生命线</span>
                </div>
                <div class="collapse-btn" @click="toggleSidebar">
                    <el-icon>
                        <DArrowLeft v-if="!collapsed" />
                        <DArrowRight v-else />
                    </el-icon>
                </div>
            </div>

            <nav class="nav-menu">
                <div class="nav-section">
                    <template v-for="item in menuItems" :key="item.key">
                        <!-- 父菜单 -->
                        <div
                            class="nav-item"
                            :class="{ active: activeMenu === item.key, 'has-children': item.children }"
                            @click="item.children ? toggleMenu(item) : handleMenuClick(item)"
                        >
                            <el-icon><component :is="item.icon" /></el-icon>
                            <span v-if="!collapsed">{{ item.label }}</span>
                            <!-- 展开/折叠图标 -->
                            <el-icon v-if="item.children && !collapsed" class="expand-icon">
                                <ArrowDown v-if="item.expanded" />
                                <ArrowRight v-else />
                            </el-icon>
                        </div>
                        <!-- 子菜单 -->
                        <template v-if="item.children && item.expanded && !collapsed">
                            <div
                                v-for="child in item.children"
                                :key="child.key"
                                class="nav-item nav-child-item"
                                :class="{ active: activeMenu === child.key }"
                                @click="handleMenuClick(child)"
                            >
                                <el-icon><component :is="child.icon" /></el-icon>
                                <span>{{ child.label }}</span>
                            </div>
                        </template>
                    </template>
                </div>
            </nav>

        </aside>

        <!-- 主内容区 -->
        <main class="main-content">
            <!-- iframe 模式 -->
            <div v-if="currentIframeUrl" class="iframe-container">
                <div class="iframe-header">
                    <span class="iframe-title">{{ currentMenuItem?.label }}</span>
                    <el-button 
                        text 
                        @click="handleRefreshIframe"
                        :icon="Refresh"
                    >
                        刷新
                    </el-button>
                </div>
                <iframe
                    ref="iframeRef"
                    :src="currentIframeUrl"
                    class="content-iframe"
                    frameborder="0"
                    @load="handleIframeLoad"
                />
                <div v-if="iframeLoading" class="iframe-loading">
                    <el-icon class="is-loading"><Loading /></el-icon>
                    <span>加载中...</span>
                </div>
            </div>

            <!-- 路由模式 -->
            <router-view v-else />
        </main>
    </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
    ChatDotRound,
    Grid,
    Connection,
    Document,
    Service,
    DArrowLeft,
    DArrowRight,
    User,
    Setting,
    SwitchButton,
    Refresh,
    Loading,
    ArrowDown,
    ArrowRight
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

interface Props {
    service?: string  // 服务名称：platform, bidding, workcase
}

interface MenuItem {
    key: string
    label: string
    icon: string
    url?: string
    type: 'route' | 'iframe'
    children?: MenuItem[]  // 子菜单
    expanded?: boolean     // 是否展开
}

const props = withDefaults(defineProps<Props>(), {
    service: undefined  // 不设默认值，从路由自动检测
})

const router = useRouter()
const route = useRoute()

// 自动检测当前服务
const currentService = computed(() => {
    // 优先使用 props
    if (props.service) {
        return props.service
    }
    // 从 route.meta 获取
    const meta = route.meta as any
    if (meta?.service) {
        return meta.service
    }
    // 从 URL 路径推断服务（最可靠的方式）
    const hostname = window.location.hostname
    const pathname = window.location.pathname
    
    // 根据 URL 路径判断服务
    // localhost/workcase/... -> workcase
    // localhost/platform/... -> platform
    // localhost/bidding/... -> bidding
    if (pathname.includes('/workcase/')) {
        return 'workcase'
    }
    if (pathname.includes('/platform/')) {
        return 'platform'
    }
    if (pathname.includes('/bidding/')) {
        return 'bidding'
    }
    
    // 从 localStorage 的 loginDomain 中推断（基于当前路由匹配的视图）
    try {
        const loginDomainStr = localStorage.getItem('loginDomain')
        if (loginDomainStr) {
            const loginDomain = JSON.parse(loginDomainStr)
            const userViews = loginDomain.userViews || []
            // 找到当前路由对应的视图
            const currentView = userViews.find((v: any) => 
                v.layout === 'AdminSidebarLayout' && 
                v.url === route.path
            )
            if (currentView?.service) {
                return currentView.service
            }
            // 如果没找到精确匹配，尝试前缀匹配
            const matchedView = userViews.find((v: any) => 
                v.layout === 'AdminSidebarLayout' && 
                route.path.startsWith(v.url)
            )
            if (matchedView?.service) {
                return matchedView.service
            }
        }
    } catch (error) {
        console.error('自动检测服务失败:', error)
    }
    // 默认返回 workcase
    return 'workcase'
})

// 状态管理
const collapsed = ref(false)
const activeMenu = ref('home')
const iframeLoading = ref(false)
const iframeRef = ref<HTMLIFrameElement>()

// 从 LocalStorage 获取用户名
function getUserName(): string {
    try {
        const loginDomainStr = localStorage.getItem('loginDomain')
        if (loginDomainStr) {
            const loginDomain = JSON.parse(loginDomainStr)
            return loginDomain.user?.username || loginDomain.userInfo?.username || '管理员'
        }
    } catch (error) {
        console.error('❌ 获取用户名失败:', error)
    }
    return '管理员'
}

const userName = ref(getUserName())

/**
 * 从 LocalStorage 加载菜单
 */
function loadMenuFromStorage(): MenuItem[] {
    try {
        const loginDomainStr = localStorage.getItem('loginDomain')
        if (!loginDomainStr) {
            console.warn('⚠️ 未找到 loginDomain')
            return []
        }

        const loginDomain = JSON.parse(loginDomainStr)
        const userViews = loginDomain.userViews || []

        const service = currentService.value
        console.log(`📋 [${service}] 加载用户视图:`, userViews)

        // 过滤出 AdminSidebarLayout 的菜单（使用当前服务动态过滤）
        const allSidebarViews = userViews.filter((view: any) => 
            view.layout === 'AdminSidebarLayout' && 
            (view.type === 0 || view.type === 1) &&  // type 0=目录 1=菜单
            view.service === service &&  // 动态匹配服务
            view.url?.startsWith('/admin')  // 只留 admin 路由
        )

        // 分离顶级菜单和子菜单
        const topLevelViews = allSidebarViews.filter((view: any) => !view.parentId)
        const childViews = allSidebarViews.filter((view: any) => view.parentId)

        console.log(`🔍 [${service}] 顶级视图:`, topLevelViews)
        console.log(`🔍 [${service}] 子视图:`, childViews)

        // 按 orderNum 排序
        topLevelViews.sort((a: any, b: any) => (a.orderNum || 0) - (b.orderNum || 0))

        // 转换为 MenuItem 格式并构建树形结构
        const menuItems: MenuItem[] = topLevelViews.map((view: any) => {
            const isIframe = view.viewType === 'iframe' || !!view.iframeUrl
            
            let menuUrl = view.url
            if (isIframe && view.url && (view.url.startsWith('http://') || view.url.startsWith('https://'))) {
                menuUrl = `/${view.viewId}`
            }
            
            // 查找子菜单
            const children = childViews
                .filter((child: any) => child.parentId === view.viewId)
                .sort((a: any, b: any) => (a.orderNum || 0) - (b.orderNum || 0))
                .map((child: any) => ({
                    key: child.viewId || child.name,
                    label: child.name,
                    icon: child.icon || 'Document',
                    url: child.url,
                    type: (child.viewType === 'iframe' || !!child.iframeUrl) ? 'iframe' : 'route' as 'iframe' | 'route'
                }))
            
            return {
                key: view.viewId || view.name,
                label: view.name,
                icon: view.icon || 'Grid',
                url: menuUrl,
                type: isIframe ? 'iframe' : 'route',
                children: children.length > 0 ? children : undefined,
                expanded: false  // 默认折叠
            }
        })

        console.log('✅ 侧边栏菜单:', menuItems)
        return menuItems
    } catch (error) {
        console.error('❌ 加载菜单失败:', error)
        return []
    }
}

// 菜单配置（从 LocalStorage 加载）
const menuItems = ref<MenuItem[]>(loadMenuFromStorage())

// 当前菜单项
const currentMenuItem = computed(() => {
    return menuItems.value.find(item => item.key === activeMenu.value)
})

// 当前 iframe URL（从路由 meta 读取）
const currentIframeUrl = computed(() => {
    const meta = route.meta as any
    return meta?.iframeUrl || null
})

// 切换侧边栏
const toggleSidebar = () => {
    collapsed.value = !collapsed.value
}

// 切换菜单展开/折叠
const toggleMenu = (item: MenuItem) => {
    if (item.children) {
        item.expanded = !item.expanded
    }
}

// 处理菜单点击
const handleMenuClick = (item: MenuItem) => {
    activeMenu.value = item.key
    
    // 所有菜单都通过路由跳转
    if (item.url) {
        router.push(item.url)
        if (item.type === 'iframe') {
            iframeLoading.value = true
        }
    }
}

// iframe 加载完成
const handleIframeLoad = () => {
    iframeLoading.value = false
}

// 刷新 iframe
const handleRefreshIframe = () => {
    if (iframeRef.value) {
        iframeLoading.value = true
        iframeRef.value.src = iframeRef.value.src
    }
}

// 监听服务变化，重新加载菜单
watch(
    currentService,
    () => {
        console.log(`🔄 服务切换到: ${currentService.value}，重新加载菜单`)
        menuItems.value = loadMenuFromStorage()
    }
)

// 监听路由变化，同步激活菜单
watch(
    () => route.path,
    (newPath) => {
        // 查找匹配的菜单项（route 或 iframe 类型）
        const menuItem = menuItems.value.find((item: MenuItem) => item.url === newPath)
        if (menuItem) {
            activeMenu.value = menuItem.key
        } else {
            // 如果路径不匹配，尝试通过 route.name 匹配 viewId
            const menuByName = menuItems.value.find((item: MenuItem) => item.key === route.name)
            if (menuByName) {
                activeMenu.value = menuByName.key
            }
        }
    },
    { immediate: true }
)
</script>

<style lang="scss" scoped>
    @import url("./AdminSidebarLayout.scss");
</style>