<template>
	<el-container class="layout">
		<!-- 左侧导航栏 - 嵌入模式时隐藏 -->
		<el-aside v-if="!isEmbedMode" :width="isCollapse ? '64px' : '200px'" class="aside">
			<div class="logo" :class="{ 'logo-collapse': isCollapse }">
				<span v-if="!isCollapse">进销存系统</span>
				<span v-else>销</span>
			</div>
			<el-menu :default-active="activeMenu" class="menu" :collapse="isCollapse" :collapse-transition="false"
				background-color="#304156" text-color="#bfcbd9" active-text-color="#409EFF">
				<!-- 仪表盘 - 所有登录用户可见 -->
				<el-menu-item index="/dashboard" @click="goTo('/dashboard')" v-if="userStore.adminUserInfo">
					<el-icon>
						<Monitor />
					</el-icon>
					<span>仪表盘</span>
				</el-menu-item>

				<!-- 代理管理 - 仅管理员可见 -->
				<el-sub-menu index="employee" v-if="userStore.isAdmin">
					<template #title>
						<el-icon>
							<User />
						</el-icon>
						<span>代理管理</span>
					</template>
					<el-menu-item index="/admin/employees" @click="goTo('/admin/employees')">代理列表</el-menu-item>
					<el-menu-item index="/admin/employees/add" @click="goTo('/admin/employees/add')">添加代理</el-menu-item>
					<el-menu-item idnex="/admin/employee-type" @click="goTo('/admin/employee-type')">账户类型</el-menu-item>
				</el-sub-menu>

				<!-- 以下菜单 - 所有登录用户可见 -->
				<template v-if="userStore.adminUserInfo">
					<el-sub-menu index="supplier">
						<template #title>
							<el-icon>
								<Service />
							</el-icon>
							<span>供应商管理</span>
						</template>
						<el-menu-item index="/supplier" @click="goTo('/supplier')">供应商列表</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="warehouse">
						<template #title>
							<el-icon>
								<MessageBox />
							</el-icon>
							<span>仓库管理</span>
						</template>
						<el-menu-item index="/warehouse" @click="goTo('/warehouse')">
							<el-icon>
								<MessageBox />
							</el-icon>
							<span>仓库列表</span>
						</el-menu-item>
						<el-menu-item index="/car" @click="goTo('/car')">
							<el-icon>
								<Van />
							</el-icon>
							<span>小车管理</span>
						</el-menu-item>
						<el-menu-item index="/shop" @click="goTo('/shop')">
							<el-icon>
								<Shop />
							</el-icon>
							<span>店铺管理</span>
						</el-menu-item>
						<el-menu-item index="/printer-manager" @click="goTo('/printer-manager')">
							<el-icon>
								<Printer />
							</el-icon>
							<span>打印机管理</span>
						</el-menu-item>
						<el-menu-item index="/pdaManage" @click="goTo('/pdaManage')">
							<el-icon>
								<Iphone />
							</el-icon>
							<span>PDA管理</span>
						</el-menu-item>
					</el-sub-menu>

					<el-sub-menu index="/wave">
						<template #title>
							<el-icon>
								<DataLine />
							</el-icon>
							<span>波次管理</span>
						</template>
						<el-menu-item index="/testUrl" @click="goTo('/testUrl')">
							<el-icon>
								<Tools />
							</el-icon>
							<span>核价器配置</span>
						</el-menu-item>
						<el-menu-item index="/wave" @click="goTo('/wave')">
							<el-icon>
								<Expand />
							</el-icon>
							<span>波次创建</span>
						</el-menu-item>
						<el-menu-item index="/wave-task" @click="goTo('/wave-task')">
							<el-icon>
								<Tickets />
							</el-icon>
							<span>波次任务列表</span>
						</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="purchase-order">
						<template #title>
							<el-icon>
								<Document />
							</el-icon>
							<span>采购订单</span>
						</template>
						<el-menu-item index="/purchase-order" @click="goTo('/purchase-order')">采购单列表</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="product">
						<template #title>
							<el-icon>
								<Box />
							</el-icon>
							<span>商品管理</span>
						</template>
						<el-menu-item index="/product" @click="goTo('/product')">
							<el-icon>
								<Tickets />
							</el-icon>
							<span>商品列表</span>
						</el-menu-item>
						<el-menu-item index="/release-record" @click="goTo('/release-record')">
							<el-icon>
								<List />
							</el-icon>
							<span>发布记录</span>
						</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="sales-order">
						<template #title>
							<el-icon>
								<Tickets />
							</el-icon>
							<span>销售订单</span>
						</template>
						<el-menu-item index="/sales-order" @click="goTo('/sales-order')">销售单列表</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="outbound">
						<template #title>
							<el-icon>
								<Upload />
							</el-icon>
							<span>出库管理</span>
						</template>
						<el-menu-item index="/outbound" @click="goTo('/outbound')">出库列表</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="shipping-order">
						<template #title>
							<el-icon>
								<Van />
							</el-icon>
							<span>发货单</span>
						</template>
						<el-menu-item index="/shipping-order" @click="goTo('/shipping-order')">发货单列表</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="inventory">
						<template #title>
							<el-icon>
								<Box />
							</el-icon>
							<span>库存记录</span>
						</template>
						<el-menu-item index="/inventory" @click="goTo('/inventory')">库存查询</el-menu-item>
					</el-sub-menu>
					<el-sub-menu index="stock-check">
						<template #title>
							<el-icon>
								<Finished />
							</el-icon>
							<span>盘库管理</span>
						</template>
						<el-menu-item index="/stock-check" @click="goTo('/stock-check')">盘库单列表</el-menu-item>
					</el-sub-menu>
					<el-menu-item index="/review-illegal-book" @click="goTo('/review-illegal-book')">
						<el-icon>
							<Edit />
						</el-icon>
						<span>异常书目审核</span>
					</el-menu-item>
					<el-menu-item index="/shop-settings" @click="goTo('/shop-settings')" style="display: none">
						<el-icon>
							<Setting />
						</el-icon>
						<span>店铺设置</span>
					</el-menu-item>
					<el-menu-item index="/sorting-settings" @click="goTo('/sorting-settings')" style="display: none">
						<el-icon>
							<Setting />
						</el-icon>
						<span>分拣设置</span>
					</el-menu-item>
				</template>
			</el-menu>
		</el-aside>

		<el-container>
			<!-- 顶部栏 - 嵌入模式时隐藏 -->
			<el-header v-if="!isEmbedMode" class="header">
				<div class="header-left">
					<el-icon @click="toggleCollapse" class="collapse-icon">
						<Fold v-if="!isCollapse" />
						<Expand v-else />
					</el-icon>
				</div>
				<div class="header-right">
					<el-dropdown @command="handleCommand">
						<span class="user-info">
							{{ currentUserInfo?.name || currentUserInfo?.username }}
							<el-icon>
								<ArrowDown />
							</el-icon>
						</span>
						<template #dropdown>
							<el-dropdown-menu>
								<el-dropdown-item command="logout" divided>
									<el-icon>
										<SwitchButton />
									</el-icon>
									退出登录
								</el-dropdown-item>
							</el-dropdown-menu>
						</template>
					</el-dropdown>
					<span class="points-badge" v-if="currentUserInfo && currentUserInfo.role !== 255">
						积分：{{ userStore.points }}
					</span>
				</div>
			</el-header>

			<el-main class="main">
				<router-view />
			</el-main>
		</el-container>
	</el-container>
</template>

<script setup>import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Monitor, Tickets, User, DataLine, List, Fold, Expand, ArrowDown, SwitchButton, Wallet, Money, Avatar, Tools, Upload, Box, Document, Setting, Van, Finished, Shop, Printer, Edit } from '@element-plus/icons-vue'
import { removeAdminToken, removeAdminUserInfo } from '@/utils/auth'
import { useUserStore } from '@/store/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const isCollapse = ref(false)

// 检测嵌入模式（从 localStorage 读取）
const isEmbedMode = ref(localStorage.getItem('embed_mode') === 'true')

const activeMenu = computed(() => route.path)
// 使用管理端用户信息
const currentUserInfo = computed(() => userStore.adminUserInfo)

const toggleCollapse = () => {
	isCollapse.value = !isCollapse.value
}

const goTo = (path) => {
	router.push(path)
}

const handleCommand = (command) => {
	if (command === 'logout') {
		handleLogout()
	}
}

const handleLogout = async () => {
	try {
		await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		})

		removeAdminToken()
		removeAdminUserInfo()
		userStore.clearAdminUserInfo()
		localStorage.clear()
		router.push('/login')

		ElMessage.success('退出成功')
	} catch (error) {
		if (error !== 'cancel') {
			// console.error('退出失败:', error)
		}
	}
}
</script>

<style scoped>
.layout {
	height: 100%;
}

.aside {
	background-color: #304156;
	transition: width 0.3s;
	overflow: hidden;
	display: flex;
	flex-direction: column;
}

.logo {
	height: 60px;
	line-height: 60px;
	text-align: center;
	color: #fff;
	font-size: 18px;
	font-weight: bold;
	background-color: #1f2d3d;
	white-space: nowrap;
	overflow: hidden;
	flex-shrink: 0;
}

.logo-collapse {
	font-size: 20px;
}

.menu {
	border-right: none;
	flex: 1;
	overflow-y: auto;
	overflow-x: hidden;
}

/* 滚动条样式 */
.menu::-webkit-scrollbar {
	width: 6px;
}

.menu::-webkit-scrollbar-track {
	background: #304156;
}

.menu::-webkit-scrollbar-thumb {
	background: #4a5568;
	border-radius: 3px;
}

.menu::-webkit-scrollbar-thumb:hover {
	background: #718096;
}

.header {
	background-color: #fff;
	border-bottom: 1px solid #e6e9f0;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 20px;
}

.header-left {
	display: flex;
	align-items: center;
}

.collapse-icon {
	font-size: 20px;
	cursor: pointer;
	color: #606266;
}

.header-right {
	display: flex;
	align-items: center;
	gap: 20px;
}

.user-info {
	display: flex;
	align-items: center;
	gap: 5px;
	cursor: pointer;
	color: #606266;
	font-size: 14px;
	outline: none;
}

.user-info:focus {
	outline: none;
}

.points-badge {
	background-color: #f56c6c;
	color: #fff;
	padding: 4px 8px;
	border-radius: 4px;
	font-size: 12px;
}

.main {
	background-color: #f0f2f5;
	padding: 10px;
}
</style>