-- 正确添加礼物打赏菜单

-- 1. 删除旧的礼物菜单（如果存在）
DELETE FROM eb_system_menu WHERE name LIKE '%礼物%';

-- 2. 添加礼物打赏主菜单（目录类型 M）
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte, create_time, update_time) 
VALUES (0, '礼物打赏', 'el-icon-present', '', '/gift', 'M', 50, 1, 0, NOW(), NOW());

-- 3. 获取刚插入的主菜单ID
SET @gift_pid = LAST_INSERT_ID();

-- 4. 添加子菜单 - 礼物列表（菜单类型 C）
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte, create_time, update_time)
VALUES (@gift_pid, '礼物列表', 'el-icon-goods', 'admin:gift:config', 'gift/config/index', 'C', 1, 1, 0, NOW(), NOW());

-- 5. 添加子菜单 - 打赏记录（菜单类型 C）
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte, create_time, update_time)
VALUES (@gift_pid, '打赏记录', 'el-icon-document', 'admin:gift:records', 'gift/records/index', 'C', 2, 1, 0, NOW(), NOW());

-- 6. 添加子菜单 - 充值套餐（菜单类型 C）
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte, create_time, update_time)
VALUES (@gift_pid, '充值套餐', 'el-icon-coin', 'admin:gift:recharge', 'gift/recharge/index', 'C', 3, 1, 0, NOW(), NOW());

-- 7. 验证插入结果
SELECT 
    CASE WHEN m.pid = 0 THEN '主菜单' ELSE '  └─ 子菜单' END as '层级',
    m.id as 'ID',
    m.name as '菜单名称',
    m.component as '组件路径',
    m.icon as '图标',
    m.menu_type as '类型',
    m.sort as '排序',
    CASE WHEN m.is_show = 1 THEN '显示' ELSE '隐藏' END as '状态'
FROM eb_system_menu m
WHERE m.name = '礼物打赏' OR m.pid = @gift_pid
ORDER BY m.pid, m.sort;

-- 8. 给管理员角色分配权限（假设管理员角色ID为1）
-- 先查看角色
SELECT * FROM eb_system_role LIMIT 5;

-- 获取所有礼物菜单ID
SELECT @gift_main_id := id FROM eb_system_menu WHERE name = '礼物打赏' AND pid = 0 LIMIT 1;
SELECT @gift_config_id := id FROM eb_system_menu WHERE name = '礼物列表' AND pid = @gift_main_id LIMIT 1;
SELECT @gift_records_id := id FROM eb_system_menu WHERE name = '打赏记录' AND pid = @gift_main_id LIMIT 1;
SELECT @gift_recharge_id := id FROM eb_system_menu WHERE name = '充值套餐' AND pid = @gift_main_id LIMIT 1;

-- 给管理员角色（rid=1）分配权限
INSERT IGNORE INTO eb_system_role_menu (rid, menu_id) VALUES 
(1, @gift_main_id),
(1, @gift_config_id),
(1, @gift_records_id),
(1, @gift_recharge_id);

-- 9. 最终验证
SELECT '========== 菜单添加完成 ==========' as '';
SELECT 
    m.id,
    m.pid,
    m.name as '菜单名称',
    m.component as '组件',
    m.icon as '图标',
    m.sort as '排序'
FROM eb_system_menu m
WHERE m.name LIKE '%礼物%' OR m.pid IN (SELECT id FROM eb_system_menu WHERE name = '礼物打赏')
ORDER BY m.pid, m.sort;
