-- ========================================
-- 创建独立的礼物打赏管理菜单
-- ========================================

-- 1. 先检查是否已存在礼物打赏顶级菜单
SELECT id, pid, name, component, is_show, sort
FROM eb_system_menu 
WHERE name = '礼物打赏' AND pid = 0;

-- 2. 如果不存在，创建礼物打赏顶级菜单
-- 注意：sort值设置为99，放在直播管理(98)之后
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte)
SELECT 0, '礼物打赏', 'el-icon-present', '', '/gift', 'M', 99, 1, 0
WHERE NOT EXISTS (
    SELECT 1 FROM eb_system_menu WHERE name = '礼物打赏' AND pid = 0
);

-- 3. 获取新创建的礼物打赏菜单ID
SET @gift_menu_id = (SELECT id FROM eb_system_menu WHERE name = '礼物打赏' AND pid = 0 LIMIT 1);

-- 4. 检查是否已有打赏记录子菜单
SELECT id, pid, name, component, is_show
FROM eb_system_menu 
WHERE name = '打赏记录' AND component = '/gift/records/index';

-- 5. 如果不存在，创建打赏记录子菜单
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte)
SELECT @gift_menu_id, '打赏记录', '', '', '/gift/records/index', 'C', 1, 1, 0
WHERE NOT EXISTS (
    SELECT 1 FROM eb_system_menu WHERE name = '打赏记录' AND component = '/gift/records/index'
);

-- 6. 验证创建结果
SELECT 
    '=== 礼物打赏菜单结构 ===' as section,
    m1.id as parent_id,
    m1.name as parent_name,
    m1.component as parent_component,
    m2.id as child_id,
    m2.name as child_name,
    m2.component as child_component,
    CASE WHEN m1.is_show = 1 AND m2.is_show = 1 THEN '✓ 都显示' 
         WHEN m1.is_show = 1 THEN '父显示，子隐藏'
         ELSE '✗ 父隐藏' 
    END as status
FROM eb_system_menu m1
LEFT JOIN eb_system_menu m2 ON m2.pid = m1.id
WHERE m1.name = '礼物打赏' AND m1.pid = 0
ORDER BY m2.sort;

-- 7. 显示所有礼物相关菜单
SELECT 
    '=== 所有礼物相关菜单 ===' as section,
    id,
    pid,
    name,
    component,
    is_show,
    sort,
    CASE WHEN is_show = 1 THEN '✓ 显示' ELSE '✗ 隐藏' END as status
FROM eb_system_menu 
WHERE name LIKE '%礼物%' OR name LIKE '%打赏%' OR component LIKE '%gift%'
ORDER BY pid, sort;
