-- SET @admin_id := (SELECT id FROM ohrm_user_role where name = 'Admin');
SET @sysAdminId = (SELECT id FROM `ohrm_user_role` WHERE name = 'ConfigAdmin');
-- SET @sysAdminTypeId = (SELECT id FROM `ohrm_user_role_type` WHERE name = 'ConfigAdmin');
SET @recruitment_module_id := (SELECT `id` FROM ohrm_module WHERE `name` = 'recruitment');

INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('321forms_configuration', 'HR Administration - 321 Forms Configurations', 1, 1, 1, 1);

SET @321forms_data_group_id := (SELECT id from ohrm_data_group WHERE name = '321forms_configuration');

INSERT INTO ohrm_user_role_data_group (user_role_id, data_group_id, can_read, can_create, can_update, can_delete, self) VALUES
(@sysAdminId, @321forms_data_group_id, 1, 1, 1, 1, 0);

INSERT INTO ohrm_screen (name, module_id, action_url) VALUES
    ( '321 Forms Configurations', @recruitment_module_id, 'view321FormsConfiguration');

SET @321forms_config_screen_id =(SELECT id from ohrm_screen WHERE action_url = 'view321FormsConfiguration');

SET @recruitment_menu_id = (SELECT id from ohrm_menu_item WHERE menu_title = 'Recruitment (ATS)' AND level =2);
SET @recruitment_config_menu_id = (SELECT id from ohrm_menu_item WHERE menu_title = 'Configuration' AND level =3 AND parent_id = @recruitment_menu_id);

INSERT INTO `ohrm_menu_item` (`menu_title`, `menu_category`, `screen_id`, `parent_id`,
                              `level`, `order_hint`, `url_extras`, `image_name`, `uisref`, `status`,
                              `include_attachment_section`, `has_multiple_records`)
VALUES ('321 Forms Configurations', NULL, @321forms_config_screen_id, @recruitment_config_menu_id, 4, 700, NULL,
        NULL, 'vueadmin.321configuration', '1', NULL, NULL);

INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES
    (@sysAdminId, @321forms_config_screen_id, 1, 1, 1, 1);

CREATE TABLE ohrm_321forms_sync
(
    `id` INT NOT NULL AUTO_INCREMENT,
    emp_number int(7) NOT NULL,
    synced_time TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY (`emp_number`) REFERENCES `hs_hr_employee` (`emp_number`) ON DELETE CASCADE,
    UNIQUE (emp_number)
) ENGINE = INNODB DEFAULT CHARSET=utf8;
