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

page(_($help_context = "Order Category Parameters"));

include_once($path_to_root . "/admin/db/order_category_parameters_db.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/includes/ui.inc");

simple_page_mode(true);
//-----------------------------------------------------------------------------------

global $order_categories_type, $order_category_types, $inventory_movement_type_linked_fields;

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        
        if(!($_POST['order_category_id'] > 0))
        {        
            $input_error = 1;
            display_error(_("Please select an order category."));
            set_focus('order_category_id');
        }

        if(!($_POST['parameter_id'] > 0))
        {
            $input_error = 1;
            display_error(_("Please select an order category parameter."));
            set_focus('parameter_id');
        }
        
       
	if ($input_error != 1) 
	{
            if ($selected_id != -1) 
            {
                update_order_category_parameters($selected_id, $_POST['parameter_id'], $_POST['parameter_value']);
                    display_notification(_('Selected order category parameter has been updated'));
            } 
            else 
            {
                add_order_category_parameters($_POST['order_category_id'], $_POST['parameter_id'], $_POST['parameter_value']);                                  
                    display_notification(_('New order category parameter has been added'));
            }
    	
		$Mode = 'RESET';
	}
} 

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

if ($Mode == 'Delete')
{
    delete_order_category_parameters($selected_id);
    display_notification("Selected order category paramter has been deleted.");
    $Mode = 'RESET';
}

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

start_form();

start_table(TABLESTYLE);
//value_list_row($label, 'list_code', $_POST['list_code'], null, '', true);
//lists_list_row("Select List :", 'list_code', "", null, '', true);
order_categories_list_cells("Order Category:", 'order_category_id', OCT_SALES, null, 'Select an Order Category', true);
end_table();


$result = get_all_order_category_parameters($_POST['order_category_id']);

br();
br();

global $Ajax;
$Ajax->activate('code_table');
$Ajax->activate('edits');

div_start('code_table');
start_table(TABLESTYLE, "width=40%");


$th = array(_("Parameter Type"), _("Parameter Description"),"", "");

table_header($th);
$k = 0;
while ($myrow = db_fetch($result)) 
{	
    alt_table_row_color($k);	

    label_cell($myrow['parameter_name']);
    label_cell($myrow['parameter_value']);    
    	
    edit_button_cell("Edit".$myrow['id'], _("Edit"));
    delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

hidden('selected_id', $selected_id);

end_table(1);
div_end();

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

start_table(TABLESTYLE2);

if ($selected_id != -1) 
{
// 	if ($Mode == 'Edit') {
		//editing an existing status code
               
    $myrow = get_order_category_parameters($selected_id);            

    $_POST['parameter_id']  = $myrow["parameter_id"];
    $_POST['parameter_value']  = $myrow["parameter_value"];  
    
    hidden('selected_id', $selected_id);
//	}
	
} 

//inventory_movement_types_list_row(_("Movement Type:"), 'type');
//if ($selected_id > 0) {
////    inventory_movement_types_list_row(_("Movement Type:"), 'type', $_POST['type']) ;
//    label_row(_("Types:"), $order_category_types[$_POST["type"]]);
////    hidden('type', $_POST['type']);
//}
//else{
    value_list_row("Parameter:", 'parameter_id', 'ORDER_CATEGORY_PARAMETERS', null, 'Please select a Parameter', false);
//}

    text_row("Description:", 'parameter_value', null, 50, 200);


end_table(1);

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

end_form();

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

end_page();



?>
