<?php
	include ('includes/bootstrap.inc.php');
	$op 	= Data::getGet ( 'op' );
	$msg 	= '';
	
	if(empty(Data::getSession('uid'))){
		header('Location: login.php?op=e');
	}
	
	/**
	 * Loads our page template and print in the main layout.
	 */
	$template_content 	= _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/dashboard.tpl";
	$layout_content 	= new Template ( $template_content );
	
	$con = new Connection();
	$sql = "SELECT 
				v.vid, 
				v.teaser,
				p.pid,
				p.name, 
				from_unixtime(v.created, '" . _DEFAULT_MYSQL_DATE_FORMAT . "') as created
			FROM visit v, pets p
			WHERE v.pid = p.pid
			AND v.status_id > 0
			ORDER BY v.created DESC LIMIT 3";

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

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

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

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

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

	// render the Datatable object and put the HTML output into a variable
	$output_latest_visit = $DT->output();
	
	/**
	 * Latest Pets
	 */
	$con = new Connection();
	$sql = "SELECT 
				p.pid as pid, 
				p.name, 
				b.familiar_name as species
			FROM pets p, species b
			WHERE p.tsn = b.tsn 
			ORDER BY created DESC LIMIT 3";

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

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

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

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

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

	// render the Datatable object and put the HTML output into a variable
	$output_latest_pets = $DT->output();
	
	$layout_content->set( 'application-name', _APPLICATION_NAME );
	$layout_content->set( 'application-version', _APPLICATION_VERSION );
	
	$layout_content->set('latest-visit-title', _("Latest Visits"));
	$layout_content->set('latest-visit-data', $output_latest_visit);
	
	$layout_content->set('latest-pet-title', _("Latest Pets"));
	$layout_content->set('latest-pet-data', $output_latest_pets);
	
	/**
	 * Set page layout
	 */
	$layout->set ( "section-name", _( "Dashboard" ) );
	
	// 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->set ( "search", $search_logic . $search_input );

	$layout->set ( "content", $layout_content->output() );
	
	/**
	 * Outputs the page.
	 */
	echo template_common_header();
	echo $layout->output ();
	echo template_common_footer();
?>
