<template>
    <div class="sidebar-layout">
        <!-- 侧边栏 -->
        <aside class="sidebar" :class="{ collapsed: collapsed }">
            <div class="sidebar-header">
                <div class="logo" v-if="!collapsed">
                    <img src="/logo.jpg" alt="Logo" class="logo-img" />
                    <!-- <span v-if="!collapsed" class="logo-text">城市生命线</span> -->
                </div>
                <button 
                    class="collapse-btn"
                    @click="toggleSidebar"
                    :title="collapsed ? '展开侧边栏' : '收起侧边栏'"
                >
                    <!-- 收起图标 PanelLeftClose -->
                    <PanelLeftClose v-if="!collapsed"/>
                    <!-- 展开图标 PanelLeftOpen -->
                    <img v-else src="/logo.jpg" alt="Logo" class="logo-img" />
                </button>
            </div>

            <nav class="nav-menu">
                <div class="nav-section">
                    <div
                        v-for="item in menuItems"
                        :key="item.key"
                        class="nav-item"
                        :class="{ active: activeMenu === item.key }"
                        @click="handleMenuClick(item)"
                    >
                        <component :is="item.icon" :size="18" />
                        <span v-if="!collapsed">{{ item.label }}</span>
                        <!-- 折叠时的 Tooltip -->
                        <div v-if="collapsed" class="nav-tooltip">
                            {{ item.label }}
                            <div class="tooltip-arrow"></div>
                        </div>
                    </div>
                </div>
            </nav>

            <!-- 用户信息 -->
            <el-dropdown class="user-section" trigger="click" @command="handleUserCommand">
                <div class="user-info-wrapper">
                    <div class="user-avatar">
                        <el-avatar :size="36" src="/avatar.svg" @error="handleAvatarError" />
                    </div>
                    <span v-if="!collapsed" class="user-name">{{ userName }}</span>
                </div>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="profile">
                            <User :size="16" />
                            个人中心
                        </el-dropdown-item>
                        <el-dropdown-item v-if="hasAdmin" command="settings" divided>
                            <Settings :size="16" />
                            管理后台
                        </el-dropdown-item>
                        <el-dropdown-item command="logout" divided>
                            <LogOut :size="16" />
                            退出登录
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </aside>

        <!-- 主内容区 -->
        <main class="main-content">
            <!-- iframe 模式 -->
            <IframeView
                v-if="currentIframeUrl"
                :url="currentIframeUrl"
                :title="currentMenuItem?.label"
                :show-header="false"
            />

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

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
    PanelLeftClose,
    MessageCircle,
    LayoutGrid,
    Workflow,
    FileText,
    Headphones,
    User,
    Settings,
    LogOut
} from 'lucide-vue-next'
import { IframeView } from 'shared/components'
import { ElMessage } from 'element-plus'
import type { MenuItem } from 'shared/types'

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

// 状态管理
const collapsed = ref(false)
const activeMenu = ref('home')
const hasAdmin = ref(false)

// 从 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) {
    }
    return '管理员'
}

const userName = ref(getUserName())

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

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


        // 过滤出 SidebarLayout 的顶级菜单（没有 parentId，且属于 platform 服务，且不是admin路由）
        const sidebarViews = userViews.filter((view: any) => 
            view.layout === 'SidebarLayout' && 
            !view.parentId &&
            view.type === 1 &&  // type 1 是侧边栏菜单
            view.service === 'platform' &&  // 只显示 platform 服务的视图
            !view.url?.startsWith('/admin')  // 排除 admin 路由（由 AdminSidebar 管理）
        )
        hasAdmin.value = userViews.filter((view: any) => 
            view.layout === 'AdminSidebarLayout' && 
            !view.parentId &&
            view.type === 1 &&  // type 1 是侧边栏菜单
            view.service === 'platform' &&  // 只显示 platform 服务的视图
            view.url?.startsWith('/admin')  // 排除 admin 路由（由 AdminSidebar 管理）
        ).length>0

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

        // 转换为 MenuItem 格式
        const menuItems: MenuItem[] = sidebarViews.map((view: any) => {
            // 根据 viewType 或 iframeUrl 判断是 route 还是 iframe
            const isIframe = view.viewType === 'iframe' || !!view.iframeUrl
            
            // 确定菜单的路由路径
            let menuUrl = view.url
            if (isIframe && view.url && (view.url.startsWith('http://') || view.url.startsWith('https://'))) {
                // iframe 类型且 url 是外部链接，使用 viewId 作为路由路径
                menuUrl = `/${view.viewId}`
            }
            
            return {
                key: view.viewId || view.name,
                label: view.name,
                icon: view.icon || 'Grid',
                url: menuUrl,
                type: isIframe ? 'iframe' : 'route'
            }
        })

        return menuItems
    } catch (error) {
        return []
    }
}

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

// 当前菜单项
const currentMenuItem = computed(() => {
    return menuItems.value.find((item: MenuItem) => 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 handleMenuClick = (item: MenuItem) => {
    activeMenu.value = item.key
    
    // 所有菜单都通过路由跳转
    if (item.url) {
        router.push(item.url)
    }
}

// 用户头像加载错误
const handleAvatarError = () => {
    return true
}

// 用户操作
const handleUserCommand = (command: string) => {
    switch (command) {
        case 'profile':
            router.push('/profile')
            break
        case 'settings':
            // 跳转到管理后台（SubSidebarLayout）
            // 查找第一个 admin 路由
            const loginDomainStr = localStorage.getItem('loginDomain')
            if (loginDomainStr) {
                const loginDomain = JSON.parse(loginDomainStr)
                const userViews = loginDomain.userViews || []
                const adminViews = userViews.filter((view: any) => 
                    view.service === 'platform' && view.url?.startsWith('/admin') && view.viewType === 'iframe'
                )
                if (adminViews.length > 0) {
                    // 按 orderNum 排序，跳转到第一个
                    adminViews.sort((a: any, b: any) => (a.orderNum || 0) - (b.orderNum || 0))
                    router.push(adminViews[0].url)
                }
            }
            break
        case 'logout':
            localStorage.clear()
            ElMessage.success('退出成功')
            router.push('/platform/login')
            break
    }
}

// 监听路由变化，同步激活菜单
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("./SidebarLayout.scss");
</style>