-- 底注配置菜单与权限
-- 说明：默认挂载在「大富翁」目录（path=/dice）下；若不存在则自动创建目录。

SET @now = NOW();

-- 1) 找到或创建 Dice 顶级目录
SET @dice_root_id = (
  SELECT `id` FROM `sa_system_menu`
  WHERE `path` = '/dice' AND `type` = 1
  ORDER BY `id` ASC LIMIT 1
);

INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`icon`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT 0, '大富翁', 'Dice', NULL, 1, '/dice', NULL, NULL, 'ri:gamepad-line', 100, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE @dice_root_id IS NULL;

SET @dice_root_id = (
  SELECT `id` FROM `sa_system_menu`
  WHERE `path` = '/dice' AND `type` = 1
  ORDER BY `id` ASC LIMIT 1
);

-- 2) 创建底注配置菜单
INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`icon`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT @dice_root_id, '底注配置', 'AnteConfig', NULL, 2, 'ante_config', '/dice/ante_config/index', NULL, 'ri:coins-line', 92, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE NOT EXISTS (
  SELECT 1 FROM `sa_system_menu` WHERE `path` = 'ante_config' AND `component` = '/dice/ante_config/index' AND `type` = 2
);

SET @ante_menu_id = (
  SELECT `id` FROM `sa_system_menu`
  WHERE `path` = 'ante_config' AND `component` = '/dice/ante_config/index' AND `type` = 2
  ORDER BY `id` ASC LIMIT 1
);

-- 3) 创建按钮权限
INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT @ante_menu_id, '数据列表', '', 'dice:ante_config:index:index', 3, '', '', '', 100, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE NOT EXISTS (SELECT 1 FROM `sa_system_menu` WHERE `slug` = 'dice:ante_config:index:index' AND `type` = 3);

INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT @ante_menu_id, '读取', '', 'dice:ante_config:index:read', 3, '', '', '', 100, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE NOT EXISTS (SELECT 1 FROM `sa_system_menu` WHERE `slug` = 'dice:ante_config:index:read' AND `type` = 3);

INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT @ante_menu_id, '添加', '', 'dice:ante_config:index:save', 3, '', '', '', 100, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE NOT EXISTS (SELECT 1 FROM `sa_system_menu` WHERE `slug` = 'dice:ante_config:index:save' AND `type` = 3);

INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT @ante_menu_id, '修改', '', 'dice:ante_config:index:update', 3, '', '', '', 100, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE NOT EXISTS (SELECT 1 FROM `sa_system_menu` WHERE `slug` = 'dice:ante_config:index:update' AND `type` = 3);

INSERT INTO `sa_system_menu`
(`parent_id`,`name`,`code`,`slug`,`type`,`path`,`component`,`method`,`sort`,`is_iframe`,`is_keep_alive`,`is_hidden`,`is_fixed_tab`,`is_full_page`,`generate_id`,`generate_key`,`status`,`create_time`,`update_time`)
SELECT @ante_menu_id, '删除', '', 'dice:ante_config:index:destroy', 3, '', '', '', 100, 2, 2, 2, 2, 2, 0, NULL, 1, @now, @now
WHERE NOT EXISTS (SELECT 1 FROM `sa_system_menu` WHERE `slug` = 'dice:ante_config:index:destroy' AND `type` = 3);
