<?php include "../../includes/start_ajax.php";

?>

<?php


//device library:
$query_device="select * from t_".$key_table_prefix."_device ";
$result_device = mysqli_query($dbconn,$query_device);
while($line_device = mysqli_fetch_array($result_device,MYSQL_ASSOC))
{
	
	$device_data[$line_device["dev_android_id"]]=$line_device["dev_name"];
}



if($_REQUEST["location_date"]=="")
{
	$_REQUEST["location_date"]=date("Y-m-d");
}


include "../map_menu.php";



echo "<form name='frm_transaction_location' >";
echo "<br><hr>";
    
    
//onclick=\"mon_send_get_marker('mod/location_transaction/marker.php','');\"

echo "<br><font size=2><b>Document Location:</b></font><br>";

echo "<table bgcolor=#ECF0F1 width=100% cellpadding=3 style='border-radius:5px 5px;' >";
	echo "<tr>";
		echo "<td>";
			echo "<span class='fa fa-calendar-o fa-lg'></span> Date ";
		echo "</td>";
		
		echo "<td>";
		
			
			echo ": <input type=text class='textbox textbox-s' name='location_date' value='".$_REQUEST["location_date"]."'>";
			
			echo "<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop)gfPop.fPopCalendar(document.frm_transaction_location.location_date);"."return false;\" HIDEFOCUS>";
			echo " <span class='fa fa-calendar fa-lg' style='color:#000;'></span>";
			echo "</a>";
			
		echo "</td>";
		
	echo "</tr>";
	
	echo "<tr>";
		echo "<td>";
			
		echo "</td>";
		
		echo "<td>";
			echo "<input type=button class='button button-blue' value='Show' onclick=\"mon_send_get_lookup('map/location_transaction/index.php','?location_date='+document.frm_transaction_location.location_date.value,'div_control_panel');\">";
		echo "</td>";
		
	echo "</tr>";
	
	/*
	echo "<tr>";
		echo "<td>";
			echo "Hour";
		echo "</td>";
		
		echo "<td>";
			echo ": <input name='location_hour' type='range'  min='8' max='18' step=1 value=8 onchange=\"document.getElementById('range').innerHTML=this.value;\"  />";
			echo "<span id='range'>0</span>";
		echo "</td>";
		
	echo "</tr>";
	
	
	echo "<tr>";
		echo "<td>";
			
		echo "</td>";
		
		echo "<td>";
			echo "<input type=button class='button_blue' value='Show Location' onclick=\"mon_send_get_marker('mod/location_transaction/marker.php?date='+document.frm_transaction_location.location_date.value+'&hour='+document.frm_transaction_location.location_hour.value,'');\">";
		echo "</td>";
		
	echo "</tr>";
	*/
	
echo "</table>";


    echo "<br><font size=2><b>Recent Location(s):</b>";
	echo "<span class='fa fa-refresh fa-1x' style='float:right;cursor:pointer;color:#3498DB;' onclick=\"mon_send_get_lookup('map/location_transaction/index.php','','div_control_panel');\"></span>";
	echo "</font>";
	
echo "<br>";

echo "<div style='height:250px;overflow:scroll;border:1px solid #ECF0F1;'>";


    
    echo "<table cellpadding=3 cellspacing=2 width=100% >";
            $query_marker = "
			
			
						select
						count(ivh_id) as total_entry,
						count( distinct device_id) as total_person,
						GROUP_CONCAT(distinct(device_id)  SEPARATOR ',') as list_device,
						
						
						date_format(created_date,'%Y-%m-%d')   as location_date,
						date_format(created_date,'%d %M %Y')   as location_date_f,
						date_format(created_date,'%H')   as location_hour,
						date_format(created_date,'%l %p')   as location_hour_f
						
						
						
						from t_".$key_table_prefix."_invoice_header
						where date_format(created_date,'%Y-%m-%d') <= '".$_REQUEST["location_date"]."'
						and (not ifnull(ivh_latitude,'') = '' and not ifnull(ivh_latitude,'') = '0.0')
						
						group by location_date, location_date_f, location_hour,location_hour_f 
						order by location_date desc , location_hour desc 
						limit 0, 40 ; ";
						
		
		
									   				
			/*
			$query_marker2 = "select distinct
						loc_person_code,
						date_format(loc_datetime,'%Y-%m-%d')   as location_date,
						date_format(loc_datetime,'%d %M %Y')   as location_date_f,
						date_format(loc_datetime,'%H')   as location_hour
						from t_".$key_table_prefix."_location_submission
						order by loc_datetime desc
						limit 0, 40 ; ";
			*/
			
			$result_marker = mysqli_query($dbconn,$query_marker);
            echo mysqli_error($dbconn);
            while($line_marker = mysqli_fetch_array($result_marker,MYSQL_ASSOC))
            {
                
                if($temp_date!=$line_marker["location_date"])
                {
                    echo "<tr>";
                    echo "<td align=center >";
                        echo "<br>";
                        echo "<font color=#BDC3C7 >".$line_marker["location_date_f"]."</font>";
                    echo "</td>";
                echo "</tr>";
                }
                
                echo "<tr>";
                    echo "<td style='border-radius:2px;background:#ECF0F1;cursor:pointer;' onclick=\"mon_send_get_marker('map/location_transaction/marker.php?date=".$line_marker["location_date"]."&hour=".$line_marker["location_hour"]."','');\">";
						echo "<div style='float:right;text-align:right;'>";
							
							///echo "".$line_marker["total_person"]."";
							
							
							//echo "<br>";
							
							$device_array = explode(",",$line_marker["list_device"]);
							foreach($device_array as $device_id)
							{
								echo " <span class='fa fa-tablet fa-1x '></span> ";
								echo "<font color=#2980B9 >".$device_data[trim($device_id)]."</font><br>";
								
								
								
							}
							
							//echo "<font color=#2980B9 >".$device_data[$line_marker["list_device"]]."</font><br>";
							//echo "<font color=#BDC3C7 >".$line_marker["list_device"]."</font>";
						echo "</div>";
						
						echo "<span class='fa fa-map-marker fa-lg'></span> ";
						
                        		//echo $line_marker["location_date"];
                        		echo " ";
                        		echo $line_marker["location_hour_f"]."<br>";
						echo "<font size=1 color=#BDC3C7 >".$line_marker["total_entry"]." GPS data</font>";
                    echo "</td>";
                echo "</tr>";
                
                $temp_date=$line_marker["location_date"];
                
            }
    echo "</table>";     


echo "</div>";
    




echo "</form>";

		    
?>


	
<?php include "../../includes/end_ajax.php";

?>