<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_USERS';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/manufacturing.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/school/includes/db/contacts_db.inc");
//include_once($path_to_root . "/school/includes/db/students_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
//include_once($path_to_root . "/admin/includes/ui/kpi_data_inquiry_ui.inc");
include_once($path_to_root . "/admin/db/users_db.inc");


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

page($help_context = "System Users Inquiry", false, false, "", $js);

//----------------------------------------------------------------------------------------

simple_page_mode(true);

//if(isset($_GET['level_1']) && $_GET['level_1'] > 0){
//    $_POST['level_1'] = $_GET['level_1'];
//}
//
//if(isset($_GET['level_2']) && $_GET['level_2'] > 0){
//    $_POST['level_2'] = $_GET['level_2'];
//}
//
//if(isset($_GET['month_id']) && $_GET['month_id'] > 0){
//    $_POST['month_id'] = $_GET['month_id'];
//}
//
//if(isset($_GET['status_id']) && ($_GET['status_id']) != null){
//    $_POST['status_id'] = $_GET['status_id'];
//}

//function build_user_array()
//{
//
//    $result = get_users();
//    
//    while ($user = db_fetch($result)) {
////            $user_line = array("id" => $user['id'], "login_id" => $user['user_id'], "real_name" => $user['real_name']);
//            $user_folders_array[$user['id']] = $user['real_name']." [".$user['user_id']."]";
////            $user_folders_array[] = $user_line;
//        
//    }
//    
//    return $user_folders_array;
//}

function display_users_inquiry_filtering_controls()
{
    start_table(TABLESTYLE_NOBORDER,"width=60%");
//        start_row();
//            echo '<td align="center" colspan=5>&nbsp;</td>';
//        end_row();

        start_row();
            text_cells_ex(_('Login Id:'), 'user_id', 10);
            text_cells_ex(_('Name:'), 'name', 35, 200);
            
        end_row();
        
//        start_row();
//            global $system_user_types, $system_access_types;
////            custom_values_list_cells("User Type:", 'user_type', $system_user_types, null, "", true);
//            
//            custom_values_list_cells("Access Type:", 'access_type', $system_access_types, null, "", true);
//            
//        end_row();
        
        start_row();
            security_roles_list_cells(_("Access Level:"), 'role_id', null, false, true, false, '');
//            submit_cells('SearchOrders', _("Search"), '', _('Search'));
        end_row();
        
        start_row();
//        three_value_list_cells('Blocked', 'blocked', null, '', -1, "No", 0, "Yes", 1, true);
//        three_value_list_cells('Inactive', 'inactive', null, '', -1, "No", 0, "Yes", 1, true);
            submit_cells('SearchOrders', _("Search"), '', _('Search'));
        end_row();
        
    end_table(1);
}

function display_user_login_log_data()
{
    global $system_user_types, $system_access_types, $SysPrefs;
    
    $user_id = $_POST["user_id"];
    $name = $_POST["name"];
//    $user_type = $_POST["user_type"];
//    $access_type = $_POST["access_type"];
    $role_id = $_POST["role_id"];
//    $blocked = $_POST["blocked"]; 

    $result = get_users(true, $user_id, $name, $role_id);
    
    start_table(TABLESTYLE, "width=98%");
    $th = array("Id","Login Id", "Name", "Role", "Last Login Date", 
                    "Employee", "Theme", "Inactive");
    table_header($th);
    
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);
        label_cell($row["id"], 'align=center'); 
        label_cell($row["user_id"]);           
                  
        label_cell($row["real_name"]);  
        label_cell($row["role"]);         
//        label_cell($row["position"]); 
//        label_cell($row["phone"]); 
//        label_cell($row["email"]); 
        label_cell(sql2date($row["last_visit_date"]), 'align=center');  
        label_cell($row["theme"], 'align=center'); 
        
//        label_cell($SysPrefs->dateformats[$row["date_format"]], 'align=center'); 
//        label_cell($row["date_format"], 'align=center'); 
//        label_cell($row["company_code"], 'align=center'); 
        
//        label_cell($row["user_type"] > 0 ? $system_user_types[$row["user_type"]] : "", 'align=center'); 
        
        
//        label_cell($row["employee_code"], 'align=center'); 
        
        if($row["employee_code"] == "-1" || strlen($row["employee_code"]) == 0){
            label_cell('');
        }
        else{
            switch ($row['user_type']) {
                case SUT_CONTACT:
                    $name = get_contact_formatted_name($row["employee_code"]);
                    label_cell($name);
                    break;
                case SUT_STUDENT:
                    $name = get_student_formatted_name($row["employee_code"]);
                    label_cell($name);
                    break;
                case SUT_USER:         
                case SUT_ADMIN: 
                    $name = get_employee_formatted_short_name($row["employee_code"]);
                    label_cell($name);
                    break;
                default :
                    label_cell('');
                    break;
            }
        }
        
//        label_cell($row["access_type"] > 0 ? $system_access_types[$row["access_type"]] : "", 'align=center'); 
        
//        label_cell(get_yes_no_name($row["blocked"]), 'align=center'); 
        label_cell(get_yes_no_name($row["inactive"]), 'align=center'); 
        
        end_row();
    }
    
    end_table();
}



$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------


start_form();

display_users_inquiry_filtering_controls();

br();

display_user_login_log_data();

br(1);

end_form();
end_page();


?>