<?php
	include ('includes/bootstrap.inc.php');
	
	if(empty(Data::getSession('uid'))){
		header('Location: login.php?op=e');
		die();
	}
	
	$op 	= Data::getGet ( 'op' );
	
	// create a new user object
	$bs = new Bootstrap();

	$con = new Connection();
	$sql = "SELECT 
				v.vid, 
				vs.status,
				vt.type,
				v.teaser, 
				p.pid, 
				p.name, 
				o.firstname, 
				o.lastname,
				from_unixtime(v.created, '" . _DEFAULT_MYSQL_DATE_FORMAT . "') as created,
				from_unixtime(v.modified, '" . _DEFAULT_MYSQL_DATE_FORMAT . "') as modified
			FROM visit v, pets p, owners o, visit_status vs, visit_type vt
			WHERE v.pid = p.pid
			AND v.oid = o.oid
			AND v.status_id = vs.status_id
			AND v.type_id = vt.type_id
			ORDER BY v.status_id ASC, v.type_id";

	$visits = $con->execute( $sql );

	unset($con);
	unset($sql);

	// create a Datatable object and populate with data taken from User table
	$DT = new Datatable('visits');

	// populate datatable
	$DT->addRows($visits);

	/**
	 * Check user permission to create table action.
	 */
	if( Security::checkByUid( Security::ROLE_USER, $_SESSION['uid'] ) == true ){
		$DT->addAction( _( 'view' ), 'visit.php?vid=%s', array('vid'));
		$DT->addAction( _( 'new visit' ), 'visit.php?pid=%s', array('pid'));
	}

	// render the Datatable object and put the HTML output into a variable
	$output = $DT->output();

	
	// create the search input using Typeahead suggestion library
	$search_logic = '<script type="text/javascript" src="assets/js/search.logic.js"></script>';

	$bso = new BootstrapInput( 'input-search', '' );
	$bso->placeholder = _('Search...');
	$search_input = $bso->output();

	$layout_visit_history	= new Template( _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/generic_content.tpl" );
	$layout_visit_history->set ( "generic-content", $output);

	
	$layout->set ( 'section-name', _( "Visit history" ) );
	$layout->set ( "search", $search_logic . $search_input );
	$layout->set ( "content", $layout_visit_history->output() );
	
	/**
	 * Outputs the page with the user's profile.
	 */
	echo template_common_header();
	echo $layout->output();
	echo template_common_footer();
?>
