<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_SECTIONS';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/dashboard/includes/db/project_sections_db.inc");
$js = '';

if (user_use_date_picker()) {
    
        $js .= get_js_date_picker();
}

page(_($help_context = "Project Sections"),false,false,"",$js);



simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}


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

//set_global_stock_item($_POST['project_id']);
function clear_site_data()
{
	unset($_POST['site_name']);
	unset($_POST['project_site_status_code']);
	unset($_POST['contact_no']);
	unset($_POST['comment']);
	unset($_POST['inactive']);
	
}

function can_process($Mode)
{
    if($Mode=='ADD_ITEM'){
        if(!(get_post('project_id') > 0)){
            display_error("Please select a project.");
            set_focus('project_id');
            return false;
        }
    }
    
    if(!(strlen(get_post('section_name')) > 0)){
        display_error("Name can not be empty.");
        set_focus('section_name');
        return false;
    }
    
    return true;
    
}
//----------------------------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM') 
{
                  
    if(can_process($Mode)){
        add_project_section($_POST['project_id'], $_POST['section_name'],$_POST['description']);            
        $msg = _("The new project section has been added.");
             
//        clear_site_data();

        display_notification($msg);
        $Mode = 'RESET';  
    }
}


if ($Mode=='UPDATE_ITEM') 
{
    if(can_process($Mode)){
        update_project_section($selected_id, $_POST['section_name'], $_POST['description']); 
//        clear_site_data();
        $msg = _("Selected project section has been updated.");
        
        display_notification($msg);
        $Mode = 'RESET';
    }
}
//------------------------------------------------------------------------------------------------------

//if ($Mode == 'Delete')
//{
//	//the link to delete a selected record was clicked
//	delete_site($_POST['project_id'], $selected_id);
//	display_notification(_("The selected task has been deleted."));
//	$Mode = 'RESET';
//}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

//--------------------------------------------------------------------------------------------------------
//$tasks = get_task($_POST['project_id']);

global $Ajax;

start_form();

br(2);    
    
start_table(TABLESTYLE_NOBORDER);
start_row();
project_list_cells(_("Project:"), 'project_id', null,
  _('Select a Project'), true);

$project_id = get_post('project_id');
end_row();
end_table();

br(2);

$Ajax->activate('price_table');

$site_list = get_project_sections($_POST['project_id']);

div_start('price_table');

start_table(TABLESTYLE, "width=60%");

$th = array(_("Id"), "Name", "Description", "");

table_header($th);

$k = 0; //row colour counter

$calculated = false;

while ($myrow = db_fetch($site_list))
{
    alt_table_row_color($k);

    label_cell($myrow["id"]);
    label_cell($myrow["section_name"]);
    label_cell($myrow["description"]);
    
//    label_cell($myrow["inactive"]);
     edit_button_cell("Edit".$myrow['id'], _("Edit"));
   // delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

end_table();

div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_project_section_details($selected_id);
                
        $_POST['section_name'] = $myrow["section_name"];
        $_POST['description'] = $myrow["description"];
}

hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}


div_start('price_details');

    start_table(TABLESTYLE2);

    text_row(_("Name:"), 'section_name', null, 50, 50);
    textarea_row(_("Description:"), 'description', null, 31, 3);

    end_table(1);

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

div_end();

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}	
?>
