<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_DRIVERS';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Drivers"));
      
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/transport/includes/db/drivers_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{	
    $input_error = 0;
    
    if(strlen($_POST['name']) == '')
    {
        $input_error = 1;
        display_error(_("Name can not be empty."));
	set_focus('name');
        return;             
    }    
       
	if ($input_error !=1)
        {
            if($_POST['employee_code'] == 0){
               $_POST['employee_code'] = "";
            }
            write_drivers($selected_id, $_POST['name'], $_POST['contact_nos'], $_POST['dl_no'], 
                          $_POST['nic'], $_POST['employee_code'], $_POST['inactive'] );
            
            if($selected_id != '')
            {
                display_notification('Selected Driver has been updated');
                       
            }
            else
            {
                display_notification(_('New Driver has been inserted'));
            }             
           
	}
         $Mode = 'RESET';
}
//----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
    delete_driver($selected_id);
    display_notification(_('Selected driver has been deleted'));
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}
//----------------------------------------------------------------------------------

$result = get_all_drivers();

start_form();
start_table(TABLESTYLE, "width=70%");
$th = array(_('ID'),_('Driver'),_('Contact No'),_('DL No'),_('NIC'),_('Employee Code'),_("Inactive"), "", "");
//inactive_control_column($th);

table_header($th);
$k = 0; //row colour counter

while ($myrow = db_fetch($result))
{
    alt_table_row_color($k);
   
    label_cell($myrow["id"], "align=right");
    label_cell($myrow["name"]);
    label_cell($myrow["contact_nos"]);
    label_cell($myrow["dl_no"]);
    label_cell($myrow["nic"]);
   
    if($myrow["driver_code"] == ''){
        label_cell("");
    }else{
        label_cell($myrow["driver_code"]." - ".$myrow["initials"]." - ".$myrow["last_name"]." (".$myrow["first_name"].")") ;
    }
     if($myrow["inactive"] == 0){
        label_cell("No", "align=center");
    }else{
        label_cell("Yes", "align=center");
    }
        
    edit_button_cell("Edit".$myrow["id"], _("Edit"));    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}
hidden('selected_id', $selected_id);
//inactive_control_row($th);
end_table(1);
//----------------------------------------------------------------------------------

start_table(TABLESTYLE2);

if ($selected_id != '') 
{
    if ($Mode == 'Edit') {
        //editing an existing item category

        $myrow = get_drivers_for_editing($selected_id);
        
        $_POST['id'] = $myrow["id"];
        $_POST['name'] = $myrow["name"];
        $_POST['contact_nos']  = $myrow["contact_nos"];
        $_POST['dl_no'] = $myrow["dl_no"];
        $_POST['inactive'] = $myrow["inactive"];
        $_POST['nic'] = $myrow["nic"];
        $_POST['employee_code'] = $myrow["driver_code"];       
    }
    hidden('selected_id', $myrow["id"]);
}

text_row(_("Driver Name:"), 'name', null, 30, 200);
text_row(_("Contact No:"), 'contact_nos', null, 20, 50);
text_row(_("DL No:"), 'dl_no', null, 20, 20);
text_row(_("NIC No:"), 'nic', null, 20, 20);
employees_simple_list_row("Employees:", 'employee_code', null, 'Select an employee');
yesno_list_row("Inactive", 'inactive',null);
end_table(1);

submit_add_or_update_center($selected_id == '', '', 'both');

end_form();

end_page();

?>
