<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_JOB_ACTIVITY';
$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc"); 
include_once($path_to_root . "/services/includes/ui/job_activities_ui.inc"); 
include_once($path_to_root . "/services/includes/db/job_activities_db.inc"); 
include_once($path_to_root . "/services/includes/ui/job_order_entry_ui.inc"); 
include_once($path_to_root . "/services/includes/db/job_orders_db.inc"); 
include_once($path_to_root . "/assets/includes/db/assets_db.inc");   
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");  
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 

$js = "";
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

page(_($help_context = "Job Activities"), false, false, "", $js);

simple_page_mode(true);

global $Ajax, $systypes_array;

$state = true;

if (isset($_GET['jo_id'])) {
    $jo_id = $_GET['jo_id'];
} elseif (isset($_POST['jo_id'])) {
    $jo_id = $_POST['jo_id'];
} else {
    $jo_id  = -1;
}

if (isset($_POST['ADD_JO_ACTIVITY']) || isset($_POST['UPDATE_JO_ACTIVITY'])) {
    if (isset($_POST['ADD_JO_ACTIVITY'])) {
        $selected_id = -1;
    }
    
    $msg = handle_save_job_activity($jo_id, $selected_id);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
            $state = false;
        } else {
            clear_job_order_inputs($selected_id);
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($Mode) && $Mode == 'Delete') {
    $msg = handle_delete_job_activity($jo_id, $selected_id);
        if ($msg['success'] === false) {
            display_error($msg['message']);
        } else {
            clear_job_order_inputs($selected_id);
            display_notification($msg['message']);
        }
}

if (isset($_POST['CLEAR_DATA']) || isset($_POST['Refresh']) || $Mode == 'RESET') {
    clear_job_order_inputs($selected_id);
    $selected_id = -1;
    unset($_POST['selected_id']);
    $Mode = 'RESET';
}

start_form();

    if ($jo_id > 0) {
        hidden('jo_id', $jo_id);
    }
    
    if ($selected_id > 0) {
        hidden('selected_id', $selected_id);
    }
    
    $Ajax->activate('job_activity_details');
    div_start('job_activity_details');
    
        $job_row = get_job_order_record($jo_id);
            if (! is_array($job_row)) {
                display_error(("Could not find Job Crad #$jo_id."));
                display_footer_exit();
            }
            
        $pro_msg = get_department_approval_project($job_row['department_id']);
            
        $user_id = $_SESSION['wa_current_user']->user;
        $cap = can_access_project($pro_msg['project_id'], $user_id);
            if ($cap['success'] !== true) {
                display_error($cap["message"]);
                display_footer_exit();
                exit;
            }
    
        display_heading($systypes_array[ST_JOB_ORDER]." #$jo_id Header Details");        br();
        view_job_order_header($jo_id);
        
        display_job_activity_details($jo_id, $selected_id, $Mode, $state);
    div_end();
    
end_form(2);
end_page();

?>