<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'awb_numbers.php');
require_once(DIR_WS_CLASSES.'airlines.php');

function _get_booking_hint($value){
	if(!empty($value)){
		return substr($value,0,25);
	}
	return '&nbsp;';
}
$awb_numbers = new awb_numbers();
$airlines = new airlines;

$aID = ($_GET['aID']) ? $_GET['aID'] : $_POST['airlines_id'];
 
switch($_GET['action']){
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = (int) xtc_db_input($_GET['id']);
			$awb_number = $awb_numbers->get_awb_number($id);
			$awb_numbers->delete( $id );
			$YES_HISTORY->add('airlines',sprintf(HISTORY_AWB_NUMBER_DELETED,$awb_number->data->awb_number),$awb_number->data->airlines_id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_AWB_NUMBERS,'aID='.$awb_number->data->airlines_id));
		break;
	case 'update':
		if(xtc_not_null($_GET['id'])){
			$id = (int) xtc_db_input($_GET['id']);
			$booking_hint = xtc_db_input($_REQUEST['booking_hint']);
			$awb_number = $awb_numbers->get_awb_number($id);
			$awb_numbers->update( $id,$booking_hint );
			$YES_HISTORY->add('airlines',sprintf(HISTORY_AWB_NUMBER_UPDATED,$awb_number->data->awb_number,$booking_hint),$awb_number->data->airlines_id);
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_AWB_NUMBERS,'aID='.$awb_number->data->airlines_id));
		break;
}
$airline = $airlines->get_airline(xtc_db_input($_GET['aID']));

$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';


include(DIR_WS_CLASSES.'class.adodb-pager.php');

$pager = new ADODB_Pager($conn,sprintf("SELECT awb_numbers_id,awb_number,booking_hint,awb_number_check_digit,date_receipt,awb_status FROM %s",$awb_numbers->get_tablename() ));
$pager->whereConditions = array(
	sprintf("airlines_id='%s'",$aID)
);
$pager->index_field = $awb_numbers->get_index();

$pager->get_row_selector = 'anID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_AWB_NUMBER,TABLE_HEADING_BOOKING_HINT,TABLE_HEADING_AWB_NUMBER_CHECK_DIGIT,TABLE_HEADING_DATE_RECEIPT,TABLE_HEADING_AWB_STATUS);
$pager->gridHelper = array(
	'awb_status'=>'yes_get_awb_status_name',
	'booking_hint'=>'_get_booking_hint'
);
$pager->sortOrders = array(
	'awb_numbers_id'=>TABLE_HEADING_ID,
	'date_receipt'=>TABLE_HEADING_DATE_RECEIPT
);

$heading_title_param = $airline->data->name;

include(DIR_WS_INCLUDES.'header.php');

$pager->set_additional_get_params( array(0=>'aID='.$aID) );
$pager->Render($rows_per_page=MAX_DISPLAY_SEARCH_RESULTS_ADMIN);

$selected_item = $pager->selected_row;

$grid_smarty = $smarty;
$smarty->assign('pager_tab_file',$pager->pager_tab_file);
$smarty->assign('HEADER',$pager->getHeader());

$smarty->assign('SORTING',$pager->getSortingInput());

$grid_smarty->assign('grid',$pager->getGrid());
$grid = $grid_smarty->fetch(DIR_FS_ADMIN.'templates/pager_grid.tpl');

$menu = yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_AIRLINES,'id='.$_GET['aID']),$access='airlines');
$smarty->assign('PAGE_MENU',$menu);

$smarty->assign('GRID',$grid);
$smarty->assign('FOOTER',$pager->getFooter());

$smarty->assign('NEW_ENTRY_BUTTON',yes_draw_href_button(BUTTON_NEW_AWB_NUMBER,xtc_href_link(FILENAME_AWB_NUMBERS_EDIT,'aID='.$_GET['aID']),'awb_numbers_edit'));


echo $smarty->fetch(DIR_FS_ADMIN.'templates/pager.tpl');

include('includes/footer.php');
