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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.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 . "/projects/includes/db/project_sites_db.inc");
$js = '';
$js .= get_js_date_picker();

if (!@$_GET['popup'])
        page(_($help_context = "Project Sites"),false,false,"",$js);

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

check_db_has_projects(_("There are no projects defined in the system."));

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('site_name')) > 0)){
        display_error("Site name can not be empty.");
        set_focus('site_name');
        return false;
    }
    
    return true;
    
}
//----------------------------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM') 
{
                  
    if(can_process($Mode)){
        add_site(get_post('project_id'), $_POST['site_name'],$_POST['project_site_status_code'],$_POST['contact_no'],$_POST['comment'],$_POST['inactive']);            
        $msg = _("The new site has been added.");
             
        clear_site_data();
        $msg = _("Site has been added.");

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


if ($Mode=='UPDATE_ITEM') 
{
    if(can_process($Mode)){
        update_site($selected_id, $_POST['site_name'],
                $_POST['project_site_status_code'],$_POST['contact_no'],
                $_POST['comment'],$_POST['inactive']); 
        clear_site_data();
        $msg = _("Site details 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']);

if (!@$_GET['popup'])
	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();

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

div_start('price_table');

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

$th = array(_("ID"), "Description","Status","Contact Nos","Comments"," ");

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["description"]);
    label_cell($myrow["value_name"]);
    label_cell($myrow["contact_nos"]);
    label_cell($myrow["comments"]);
    
//    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_site_details($selected_id);
                
        $_POST['site_name'] = $myrow["description"];
        $_POST['project_site_status_code'] = $myrow["status_code"];
        $_POST['contact_no'] = $myrow["contact_nos"];
        $_POST['comment'] = $myrow["comments"];
         $_POST['inactive'] = $myrow["inactive"];
}

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(_("Site name:"), 'site_name', null, 50, 50);
    text_row(_("Contact Nos:"), 'contact_no', null, 50, 50);
    textarea_row(_("Comment:"), 'comment', null, 31, 3);
    value_list_code_row(_("Status:"), 'project_site_status_code', 'PROJ_SITE_STATUS');
    
    record_status_list_row(_("Inactive:"),'inactive');

    end_table(1);

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

div_end();

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