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

include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/projects/includes/db/project_serial_categories_db.inc");

$category_id = 0;

if(isset($_GET['cat_id'])){
     $category_id = $_GET['cat_id'];
}

if($category_id > 0){
    $_POST['cat_id'] = $category_id;
}

if(isset($_POST['cat_id'])){
    $category_id = $_POST['cat_id'];
}

switch ($category_id) {
    case 1:
        page(_($help_context = "Project Serial Project Category"));
        break;
    case 2:
        page(_($help_context = "Project Serial Customer Category"));
        break;
    case 3:
        page(_($help_context = "Project Serial Company Category"));
        break;
    default:
        break;
}


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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;

    if(!($category_id > 0)){
        display_error(_("No category selected."));
        set_focus('category_code');
                return 1;  
    }
    
    if (strlen($_POST['category_code']) == 0)
    {
        //$input_error = 1;
        display_error(_("Code cannot be empty."));
        set_focus('category_code');
                return 1;      
    }
    
	if (strlen($_POST['description']) == 0)
	{
		//$input_error = 1;
		display_error(_("Name cannot be empty."));
		set_focus('description');
                return 1;
	}

	if (strlen($_POST['next_number']) == 0)
	{
		//$input_error = 1;
		display_error(_("The next number cannot be empty."));
		set_focus('next_number');
                return 1;
	}
    
    if (is_numeric($_POST['next_number'])== FALSE){

            //$input_error = 1;
    display_error(_("Next number must be numeric."));
    set_focus('next_number');
            return 1;
    }
    
    if($Mode=='ADD_ITEM'){
        $res_category_code = check_existing_category_code($category_id, $_POST['category_code']);
        if($res_category_code){
            display_error(_("Entered code already available."));
            set_focus('description');
            return 1;
        }
    }


	if ($input_error !=1) {
        write_category($selected_id, $category_id, $_POST['category_code'], $_POST['description'], $_POST['next_number']);
            
		if($selected_id != ''){
			display_notification(_('Selected code has been updated'));
        }
		else{
			display_notification(_('New code has been added'));
        }
        
		$Mode = 'RESET';
	}
}

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

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

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

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

$result = get_categories($category_id);

start_form();
start_table(TABLESTYLE, "width=40%");
$th = array(_('Code'), _('Name'),_('Next Number'), "", "");
//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["category_code"]);
    label_cell($myrow["description"]);
    label_cell($myrow["next_number"], 'align=right');
    
    edit_button_cell("Edit".$myrow["id"], _("Edit"));
    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}

//inactive_control_row($th);
end_table(1);

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

start_table(TABLESTYLE2);

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

        $myrow = get_category_details($selected_id);
        
        $_POST['category_code'] = $myrow["category_code"];
        $_POST['description'] = $myrow["description"];
        $_POST['next_number']  = $myrow["next_number"];
    }
    hidden('selected_id', $myrow["id"]);
    label_row(_("Code:"), $myrow["category_code"]);
    text_row(_("Description:"), 'description', null, 20, 20);
    text_row(_("Next Number:"), 'next_number', null, 20, 20);
    
    hidden('category_code', $myrow["category_code"]);
}

if ($Mode != 'Edit') {
    
    text_row(_("Code:"), 'category_code', null, 20, 20);
    text_row(_("Name:"), 'description', null, 20, 20);
    text_row(_("Next Number:"), 'next_number', null, 20, 20);
}

hidden('cat_id', $category_id);

end_table(1);

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

end_form();

end_page();

?>
