-- 添加 AMT 设备管理菜单
-- 先检查是否已存在，避免重复插入

-- 插入目录菜单
INSERT INTO Menus (Id, ParentId, Name, Path, Component, Title, Icon, Sort, Roles, IsHide, KeepAlive, IsSystem, CreatedAt, UpdatedAt)
SELECT 5, NULL, 'AmtManage', '/amt', '/index/index', '设备管理', 'ri:computer-line', 2, 'R_SUPER,R_ADMIN', 0, 0, 1, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM Menus WHERE Id = 5 OR Name = 'AmtManage');

-- 插入子菜单：网络扫描
INSERT INTO Menus (Id, ParentId, Name, Path, Component, Title, Icon, Sort, Roles, IsHide, KeepAlive, IsSystem, CreatedAt, UpdatedAt)
SELECT 6, 5, 'AmtScan', 'scan', '/amt/scan', '网络扫描', NULL, 1, 'R_SUPER,R_ADMIN', 0, 1, 1, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM Menus WHERE Id = 6 OR Name = 'AmtScan');

-- 插入子菜单：设备列表
INSERT INTO Menus (Id, ParentId, Name, Path, Component, Title, Icon, Sort, Roles, IsHide, KeepAlive, IsSystem, CreatedAt, UpdatedAt)
SELECT 7, 5, 'AmtDevices', 'devices', '/amt/devices', '设备列表', NULL, 2, 'R_SUPER,R_ADMIN', 0, 1, 1, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM Menus WHERE Id = 7 OR Name = 'AmtDevices');

-- 插入子菜单：AMT凭据
INSERT INTO Menus (Id, ParentId, Name, Path, Component, Title, Icon, Sort, Roles, IsHide, KeepAlive, IsSystem, CreatedAt, UpdatedAt)
SELECT 8, 5, 'AmtCredentials', 'credentials', '/amt/credentials', 'AMT凭据', NULL, 3, 'R_SUPER,R_ADMIN', 0, 1, 1, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM Menus WHERE Id = 8 OR Name = 'AmtCredentials');

-- 插入子菜单：Windows凭据
INSERT INTO Menus (Id, ParentId, Name, Path, Component, Title, Icon, Sort, Roles, IsHide, KeepAlive, IsSystem, CreatedAt, UpdatedAt)
SELECT 9, 5, 'WindowsCredentials', 'windows-credentials', '/amt/windows-credentials', 'Windows凭据', NULL, 4, 'R_SUPER,R_ADMIN', 0, 1, 1, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM Menus WHERE Id = 9 OR Name = 'WindowsCredentials');

-- 为超级管理员和管理员分配新菜单权限
-- 获取角色ID
SET @superRoleId = (SELECT Id FROM Roles WHERE RoleCode = 'R_SUPER' LIMIT 1);
SET @adminRoleId = (SELECT Id FROM Roles WHERE RoleCode = 'R_ADMIN' LIMIT 1);

-- 为超级管理员分配菜单
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@superRoleId, 5);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@superRoleId, 6);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@superRoleId, 7);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@superRoleId, 8);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@superRoleId, 9);

-- 为管理员分配菜单
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@adminRoleId, 5);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@adminRoleId, 6);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@adminRoleId, 7);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@adminRoleId, 8);
INSERT IGNORE INTO RoleMenus (RoleId, MenuId) VALUES (@adminRoleId, 9);

SELECT '✅ AMT菜单已添加完成' AS Result;
