<?php

class vehicle_movement {

	private static $vrmpath = 'http://touchpark.azurewebsites.net/api/vehiclemovementid/';

	public static function get($id=false) {
		$result=false;
		if(false !== $id){
			$sql	="SELECT * FROM `vehicle_movement` WHERE `VehicleMovementID`='".(int)$id."' LIMIT 1";
			$issues	=mysql_query($sql);
			if (mysql_num_rows($issues) > 0) {
				while ($row=mysql_fetch_array($issues)) {
					$result=$row;
				}
			}
		}
		return $result;
	}

	/**
	 * Get Most recent ID added
	 * @return integer The maximum ID stored
	 */
	public static function getMostRecentId() {
		$result=false;
		$sql	="SELECT `VehicleMovementID` FROM `vehicle_movement` ORDER BY `VehicleMovementID` DESC LIMIT 1";
		$issues	=mysql_query($sql);
		if (mysql_num_rows($issues) > 0) {
			while ($row=mysql_fetch_array($issues)) {
				$result=$row['VehicleMovementID'];
			}
		}
		return $result;
	}

	public static function addRecent($id, $movements=array()){
		$response = false;
		if(0 < count($movements)){
			$sql = "INSERT INTO `vehicle_movement` (";
			$sql .= "`VehicleMovementID`, ";
			$sql .= "`SiteCode`, ";
			$sql .= "`VRM`, ";
			$sql .= "`EventDate`, ";
			$sql .= "`Direction`, ";
			$sql .= "`Lane`, ";
			$sql .= "`Confidence`, ";
			$sql .= "`MachineName`, ";
			$sql .= "`PlateImage`, ";
			$sql .= "`OverviewImage`, ";
			$sql .= "`InfraredImage` ";
			$sql .= ") ";
			$sql .= "VALUES ";
			$sql .= "(";
			$sql .= "'".(int)$id."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['SiteCode']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['VRM']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['EventDate']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['Direction']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['Lane']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['Confidence']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['MachineName']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['PlateImage']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['OverviewImage']))."', ";
			$sql .= "'".mysql_real_escape_string(trim($movements['InfraredImage']))."' ";
			$sql .= ")";

			mysql_query($sql) or die(mysql_error());

			$response = $id;
		}
		return $response;
	}

	public static function getMovements(){
		$movements=false;
		// Get Last Movement ID
		$movement_id = self::getMostRecentId();

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, self::$vrmpath . (int)$movement_id);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$json = curl_exec($ch);

		if($json){
			$data = json_decode($json);
			$movements = array();
			foreach($data as $index=>$movement){
				$movements[(int)$movement->VehicleMovementID] = array(
					'SiteCode'		=> (int)$movement->SiteCode,
					'VRM'			=> $movement->VRM,
					'EventDate'		=> str_replace('T',' ',$movement->EventDate),
					'Direction'		=> (int)$movement->Direction,
					'Lane'			=> $movement->Lane,
					'Confidence'	=> (int)$movement->Confidence,
					'MachineName'	=> $movement->MachineName,
					'PlateImage'	=> $movement->PlateImage,
					'OverviewImage'	=> $movement->OverviewImage,
					'InfraredImage'	=> $movement->InfraredImage
				);
			}
		}
		return $movements;
	}

	public static function getBySearch($search='', $last=false, $limit=10){
	    $result=false;
		if('' !== $search){
			$sql ="SELECT * FROM `vehicle_movement` ";
			$sql .="WHERE `VRM` LIKE '%".mysql_real_escape_string($search)."%' ";
			$sql .="OR `EventDate` LIKE '%".mysql_real_escape_string($search)."%' ";
			$sql .="OR `SiteCode` LIKE '%".mysql_real_escape_string($search)."%' ";
			$sql .="OR IF(`Direction` = 1, 'left', 'arrived') LIKE '%".mysql_real_escape_string($search)."%' ";
			if($last > 0){
				$sql .="AND `VehicleMovementID` < '".(int)$last."' ";
			}
			$sql .="ORDER BY `VehicleMovementID` DESC LIMIT ".$limit;

			$issues	=mysql_query($sql) or die(mysql_error());
			if (mysql_num_rows($issues) > 0) {
				while ($row=mysql_fetch_assoc($issues)) {
					$result[$row['VehicleMovementID']]=$row;
				}
			}
		}
		return $result;
	}

	public static function getLatest($limit=10) {
		$result=false;
		if(false !== $id){
			$sql="SELECT * FROM `vehicle_movement` ORDER BY `VehicleMovementID` DESC LIMIT ".$limit;
			$issues	=mysql_query($sql);
			if (mysql_num_rows($issues) > 0) {
				while ($row=mysql_fetch_assoc($issues)) {
					$result[$row['VehicleMovementID']]=$row;
				}
			}
		}
		return $result;
	}

	public static function getAfterIDs($id=false, $limit=10) {
		$result=false;
		if(false !== $id){
			$sql="SELECT * FROM `vehicle_movement` WHERE `VehicleMovementID` < '".(int)$id."' ORDER BY `VehicleMovementID` DESC LIMIT ".$limit;
			$issues	=mysql_query($sql);
			if (mysql_num_rows($issues) > 0) {
				while ($row=mysql_fetch_array($issues)) {
					$result[$row['VehicleMovementID']]=$row;
				}
			}
		}
		return $result;
	}

	public static function makeList($arrVehicle=array()) {
		$result='';
		if(count($arrVehicle) > 0){
			$now = date("Y-m-d");
			$arrData = array();
			$today = false;
			foreach($arrVehicle as $id => $vehile){
				$date = date("Y-m-d", strtotime($vehile['EventDate']));

				$arrData[$date][$id] = $vehile;
				if($date == $now){
					$today = true;
				}
			}
			
			foreach($arrData as $date => $data){

				$result .= '<div class="vrmsearch-timeline">';
					

					if($now == $date){
						$result .= '<a href="#" class="vrmsearch-timeline-header">TODAY</a>';
						$result .= '<div class="vrmsearch-timeline-items-new">';
							$result .= '<a href="#" class="vrmsearch-timeline-items-new-button">0 NEW</a>';
						$result .= '</div>';
					} else{
						$result .= '<a href="#" class="vrmsearch-timeline-header">'.date("l jS F Y", strtotime($date)).'</a>';
					}
					
				$result .= '<div class="vrmsearch-timeline-items-block">';
				foreach($data as $id => $vehicle){	
					$result .= self::getItem($vehicle);	
				}
			$result .= '</div></div>';
			}
		}
		return $result;
	}


	public function getItem($vehicle= array()){
		$result = '';

		if($vehicle['Direction'] == 1){
			$diraction_class = "item-juftleft";
			$diraction_icon  = "icon-park-out";
			$summary_title   = "JUST LEFT";
			$summary_content = "just left at <b>STORE CODE ".$vehicle['SiteCode']."</b> Car park";
		} else {
			$diraction_class = "item-juftarrived";
			$diraction_icon  = "icon-park-in";
			$summary_title   = "JUST ARRIVED";
			$summary_content = "just arrived at <b>STORE CODE ".$vehicle['SiteCode']."</b> Car park";
		}

		$result .= '<div class="vrmsearch-timeline-items '.$diraction_class.'" data-id="'.$vehicle['VehicleMovementID'].'">
						<div class="vrmsearch-timeline-item">
							<div class="vrmsearch-timeline-img">
								<span class="'.$diraction_icon.'"></span>
							</div>
							<div class="vrmsearch-timeline-summary">
								<p class="vrmsearch-timeline-summary-title"><span>'.$summary_title.'</span> <span class="vrmsearch-timeline-summary-numberplate">'.$vehicle['VRM'].'</span></p>
								<p class="vrmsearch-timeline-summary-content">'.$summary_content.'</p>
							</div>
							<div class="vrmsearch-timeline-result">
								<span class="vrmsearch-timeline-result-time">'.date("H:i", strtotime($vehicle['EventDate'])).'</span>
							</div>
							<span title="Open action block" class="icon-dropdown vrmsearch-timeline-item-open"></span>
						</div>
						<div class="vrmsearch-timeline-action">
							<div class="vrmsearch-timeline-action-block">
								<a href="#" class="vrmsearch-timeline-action-button u-Action-See">SEE</a>
								<a href="#" class="vrmsearch-timeline-action-button u-Action-Flag">FLAG</a>
								<a href="#" class="vrmsearch-timeline-action-button u-Action-Keep">KEEP</a>
							</div>
						</div>
					</div>';

		return $result;			
	}
}

?>