INSERT INTO `hs_hr_dashboard_panel` (`id`, `group_id`, `panel_name`, `panel_params`) VALUES
(1, 1, 'Pending Leave Requests', '{"width":"300","height":"238","module_name":"dashboard","action_name":"pendingLeaveRequests","top":"13","left":"0"}'),
(2, 1, 'Pending Timesheets', '{"width":"300","height":"237.8","module_name":"dashboard","action_name":"pendingTimesheets","top":"13","left":"6"}'),
(3, 1, 'Scheduled Interviews', '{"width":"300","height":"238.8","module_name":"dashboard","action_name":"scheduledInterviews","top":"11","left":"12"}'),
(4, 3, 'Employee Distribution by Subunit', '{"width":"302","height":"281","module_name":"dashboard","action_name":"employeeDistribution","top":"0","left":"0"}'),
(5, 3, 'Leave Taken from January by Subunit', '{"width":"337","height":"281","module_name":"dashboard","action_name":"leaveGraph","top":"0","left":"6"}'),
(6, 4, 'Quick Launch', '{"width":"auto","height":"90.8","module_name":"dashboard","action_name":"quickLaunchPanelHorizontal","top":"4","left":"5"}'),
(7, 3, 'Legend', '{"width":"257","height":"280","module_name":"dashboard","action_name":"baseLegend","top":"0","left":"13"}');

INSERT INTO `hs_hr_dashboard_group` (`id`, `group_name`, `group_order`, `group_type`, `group_params`) VALUES
(1, 'Task List', 3, 'MISC', '{"title":"Task List","width":"1100","height":"280","permissions":["1","3"]}'),
(3, 'Graphs', 2, 'MISC', '{"title":"","width":"1100","height":"300","permissions":["1"]}'),
(4, 'QuickLaunch', 1, 'MISC', '{"title":"","width":"1100","height":"120","permissions":["1","2","5","6","4","3","7"]}');


SET @admin_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Admin');
SET @ess_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ESS');

INSERT INTO `ohrm_module` (`name`, `status`) VALUES ('dashboard', 1);

SET @module_id := (SELECT LAST_INSERT_ID()); 
INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`) VALUES  
('Dashboard', @module_id, 'index');

SET @dashboard_screen_id := (SELECT LAST_INSERT_ID()); 
SET @ess_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ESS');

INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `status`) VALUES  
('Dashboard', @dashboard_screen_id, NULL, 1, 950, NULL, 1); 

INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES  
(@admin_role_id, @dashboard_screen_id, 1, 0, 0, 0);
INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES  
(@ess_role_id, @dashboard_screen_id, 1, 0, 0, 0);

INSERT INTO ohrm_home_page (`user_role_id`, `action`, `enable_class`, `priority`) VALUES 
(@admin_role_id, 'dashboard/index', NULL, 15),
(@ess_role_id, 'dashboard/index', NULL, 5);
 

DELIMITER $$
DROP FUNCTION IF EXISTS dashboard_get_subunit_parent_id;$$

CREATE FUNCTION  dashboard_get_subunit_parent_id
(
  id INT
)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
SELECT (SELECT t2.id 
               FROM ohrm_subunit t2 
               WHERE t2.lft < t1.lft AND t2.rgt > t1.rgt    
               ORDER BY t2.rgt-t1.rgt ASC LIMIT 1) INTO @parent
FROM ohrm_subunit t1 WHERE t1.id = id;

RETURN @parent;

END;$$

-- grant execute on function dashboard.dashboard_get_subunit_parent_id  to 'dashboard'@'localhost';

DELIMITER ;
