<?php
session_start();
header('Content-Type: text/html; charset=utf-8');

include_once("../scripts/ClassLibs/ClassReport.php");

	$role = $_SESSION['role'];
	$id_user = $_SESSION['id'];
	$instance_id = $_SESSION['client_id'];
	$GMT = $_SESSION['gmt'];
	
	$dbset = json_decode($_SESSION['settingsApp'],true);
	$db = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);
	$db2 = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);
		
	$db->query("SET CHARSET SET utf8");
	$db->query("SET NAMES 'utf8'");
	if (mysqli_connect_errno()) {
		echo "Error connecting to DB";
		exit;
	}
	
	$db2->query("SET CHARSET SET utf8");
	$db2->query("SET NAMES 'utf8'");
	if (mysqli_connect_errno()) {
		echo "Error connecting to DB";
		exit;
	}

	
	
if(isset($_REQUEST['load'])){
	
	$total = 0;
	
	$start = isset($_REQUEST['start']) ? abs($_REQUEST['start']) : 0;
	$limit = isset($_REQUEST['limit']) ? abs($_REQUEST['limit']) : 30;
	
	//$startdt = isset($_REQUEST['reportQues_startdt']) ? $_REQUEST['reportQues_startdt'] : date('Y-m-d');
	//$enddt = isset($_REQUEST['reportQues_enddt']) ? $_REQUEST['reportQues_enddt'] : date('Y-m-d');
	
	
	$query = "SELECT COUNT(*) FROM `tms_editor_trip` WHERE `instanceID` = ".$instance_id.";";
	if ($stmt = $db->prepare($query)) 
	{
        $stmt->execute();
        $stmt->bind_result($total);
        $stmt->fetch();
        $stmt->close();
	}
	
	$query = "SELECT t.`Trip_ID`,t.`Name`,t.`TripPrice`,t.`Distance`,t.`Time`,t.`Comments`,DATE_FORMAT(t.`StartTime`,'%H:%i') AS `StartTime`,DATE_FORMAT(t.`EndTime`,'%H:%i') AS `EndTime`,COUNT(tp.`TripPoint_ID`) AS `OrderCount`
FROM `tms_editor_trip` t
LEFT JOIN `tms_editor_trippoint` tp ON t.`Trip_ID` = tp.`Trip_ID`
WHERE t.`instanceID` = ".$instance_id." GROUP BY t.`Trip_ID`;";
	if(!$stmt = $db->query($query)){
			echo '{"total":"0","rows":[]}';
	}else{
			for ($result = array(); $tmp = $stmt->fetch_array(MYSQLI_ASSOC);)
			{
				$result[] = $tmp;
			}
			
			$stmt->close();
			echo '{"total":"'.$total.'","rows":'.json_encode($result).'}';
	}
	$db->close();
	exit;
	
}


if(isset($_REQUEST['trippoint'])){
	
	$total = 0;
	
	//$query = "SELECT `TripPoint_ID`,`Name`,`Address`,DATE_FORMAT(`EstimateTime`,'%H:%i') AS `EstimateTime`,`DurationInPoint`,`EstimateOutTime`,`OrderInTrip`,`Route`,`Distance`,`Time`,`GeoX`,`GeoY` FROM tms_editor_trippoint WHERE `Trip_ID` = ".$_REQUEST['Trip_ID'].";";
	$query = "SELECT t.`Name` AS RouteName, DATE_FORMAT(t.`StartTime`,'%H:%i') AS RouteStartTime, DATE_FORMAT(t.`EndTime`,'%H:%i') AS RouteEndTime, tp.`TripPoint_ID`,tp.`Name`,tp.`Address`,DATE_FORMAT(tp.`EstimateTime`,'%H:%i') AS `EstimateTime`,tp.`DurationInPoint`,tp.`EstimateOutTime`,tp.`OrderInTrip`,tp.`Route`,tp.`Distance`,tp.`Time`,tp.`GeoX`,tp.`GeoY`
FROM tms_editor_trippoint tp
LEFT JOIN `tms_editor_trip` t ON tp.`Trip_ID` = t.`Trip_ID` WHERE t.`Trip_ID` = ".$_REQUEST['Trip_ID'].";";
	if(!$stmt = $db->query($query)){
			echo '{"total":"0","rows":[]}';
	}else{
			for ($result = array(); $tmp = $stmt->fetch_array(MYSQLI_ASSOC);)
			{
				$result[] = $tmp;
			}
			
			$stmt->close();
			echo '{"total":"'.$total.'","rows":'.json_encode($result).'}';
	}
	$db->close();
	exit;
	
}

?>