<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'ships.php');

function _get_ship_owner($ships_id){
	if($ships_id > 0){
		$companies_id = yes_get_ship_company_id($ships_id);
		return yes_get_company_name($companies_id);
	}
}

$ships = new ships();
switch($_GET['action']){
	case 'overview':
		include('ships_overview.php');
		exit;
		break;
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$ship = $ships->get_ship($id);
			$name = yes_get_ships_name($id);
			$ships->delete( $id );
			$YES_HISTORY->add('system',sprintf(HISTORY_SHIP_DELETED,$name));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_SHIPS));
		break;
}

$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 s.ships_id,sn.name,s.ships_id AS sidowner, s.date_added FROM %s s LEFT JOIN %s sn USING(ships_id)",$ships->get_tablename(),TABLE_SHIP_NAMES ));
if(!xtc_not_null($_REQUEST['search'])){
	$pager->whereConditions = array(
		"sn.is_default=1"
	);
}else{
	$pager->groupbyConditions = array(
		"s.ships_id"
	);
}
$pager->orderbyDefault = array(
	"sn.name"
);

$pager->index_field = $ships->get_index();

$pager->get_row_selector = 'cID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_NAME,TABLE_HEADING_OWNER,TABLE_HEADING_ADDED);
$pager->orderbyDefault = array('name');
$pager->sortOrders = array(
	'ships_id'=>TABLE_HEADING_ID,
	'name'=>TABLE_HEADING_NAME,
	'date_added'=>TABLE_HEADING_ADDED
);
$pager->searchFields = array(
	'name'
);

$pager->gridHelper = array(
	'sidowner'=>'_get_ship_owner'
);

include(DIR_WS_INCLUDES.'header.php');

$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());
$smarty->assign('SEARCH',$pager->getSearchInput());

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

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


$smarty->assign('NEW_ENTRY_BUTTON',yes_draw_href_button(BUTTON_NEW_SHIP,xtc_href_link(FILENAME_SHIPS_EDIT),'ships_edit'));



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

include('includes/footer.php');
