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

$customers = new customers();

switch($_GET['action']){
	case 'delete':
		if( xtc_not_null($_GET['id']) ){
			$id = xtc_db_input($_GET['id']);
			$customer = $customers->get_customer($id);
			$customers->delete( $id );
			$YES_HISTORY->add('system',sprintf(HISTORY_CUSTOMER_DELETED, $customer->getFullName() ));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS));
		break;
}

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

switch($_GET['action']){
	case 'overview':
		include(FILENAME_CUSTOMERS_OVERVIEW);
		break;
	default:
		include(DIR_WS_CLASSES.'class.adodb-pager.php');
		
		$pager = new ADODB_Pager($conn,sprintf("SELECT customers_id,customers_email_address,customers_gender, concat(customers_firstname, ' ',customers_lastname) as name,customers_date_added,customers_status FROM %s",$customers->get_tablename() ));
		$pager->index_field = $customers->get_index();
		$pager->whereConditions = array(
			"customers_id>0"
		);
		$pager->orderbyDefault = array(
			'customers_date_added DESC'
		);
		
		$pager->get_row_selector = 'cID';
		$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_EMAIL,TABLE_HEADING_GENDER,TABLE_HEADING_NAME,TABLE_HEADING_CREATED,TABLE_HEADING_TYPE);
		$pager->gridHelper = array(
			'customers_status'=>'yes_get_customers_status_name',
			'customers_gender'=>'yes_get_customers_gender_name'
		);
		$pager->sortOrders = array(
			'customers_id'=>TABLE_HEADING_ID,
			'name'=>TABLE_HEADING_NAME,
			'customers_date_added'=>TABLE_HEADING_CREATED
		);
		
		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_CUSTOMER,xtc_href_link(FILENAME_CUSTOMERS_EDIT),$access='customers_edit'));


		echo $smarty->fetch(DIR_FS_ADMIN.'templates/pager.tpl');
		
		break;
}
include('includes/footer.php');
