<?php 
include('../include/header.php');


function getaddress($latitude,$longitude)
{
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=false';
$json = @file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK")
return $data->results[0]->formatted_address;
else
return false;
}

        $locations=array();
		if((isset($_POST['Search'])) and ((isset($_POST['Search']))!='0')){
		 $sql = "select *from tbl_loc_dtl where ent_dt = '".$_POST['s_date']."' and emp_id = '".$_POST['employee']."'";
		}else{
		 $sql = "select *from tbl_loc_dtl where ent_dt = '".date('Y-m-d')."'";
		 }
		  $result = $objhr->viewdata($sql);
 if($result->num_rows > 0){
 		 $positions = array();
			while($rows = $result->fetch_assoc()){
			
			$sqlnm = "select *from tbl_emp_master where id = '".$rows['emp_id']."'";
		  $resultnm = $objhr->viewdata($sqlnm);
			$rownm = $resultnm->fetch_assoc();
			$name = $rownm['emp_name'];
			
			$ent_dt = $rows['ent_dt']; 
			$longitude = $rows['lang'];                              
            $latitude = $rows['lat'];
          	$address= getaddress($latitude,$longitude);
			
			 $locations[]=array( 'name'=>$name, 'ent_dt'=>$ent_dt, 'lat'=>$latitude, 'lng'=>$longitude, 'address'=>$address );
}}else
  {
 
 ?>
 <script>
 alert("Location Not Found For This Employee For Selected Date..");

 </script>

 <?php }
?>

<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyDJ0oDYz1cVvaM-fcAmYVsz9uVwnDNwD5g"></script>
    <script type="text/javascript">
    var map;
    var Markers = {};
    var infowindow;
    var locations = [
        <?php for($i=0;$i<sizeof($locations);$i++){ $j=$i+1;?>
        [
            'AMC Service',
            '<p>Date : <?php echo $locations[$i]['ent_dt'];?><br> Employee Name : <?php echo $locations[$i]['name'];?> <br> Address :  <?php echo $locations[$i]['address'];?></p>',
			
            <?php echo $locations[$i]['lat'];?>,
            <?php echo $locations[$i]['lng'];?>,
			
            0
        ]<?php if($j!=sizeof($locations))echo ","; }?>
    ];
    var origin = new google.maps.LatLng(locations[0][2], locations[0][3]);
    function initialize() {
      var mapOptions = {
        zoom: 12,
        center: origin
      };
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        infowindow = new google.maps.InfoWindow();
        for(i=0; i<locations.length; i++) {
            var position = new google.maps.LatLng(locations[i][2], locations[i][3]);
            var marker = new google.maps.Marker({
                position: position,
                map: map,
				//icon : '../images/1.png'
            });
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][1]);
                    infowindow.setOptions({maxWidth: 200});
                    infowindow.open(map, marker);
                }
            }) (marker, i));
            Markers[locations[i][4]] = marker;
        }
        locate(0);
    }
    function locate(marker_id) {
        var myMarker = Markers[marker_id];
        var markerPosition = myMarker.getPosition();
        map.setCenter(markerPosition);
        google.maps.event.trigger(myMarker, 'click');
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>


		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        


<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}
.style2 {
	color: #00CC00;
	font-style: italic;
}
.style3 {color: #00CC00}


-->
</style>
</script>
<!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                        	<div class="card-body" id="printableArea">
                        <h4>Location Detail </h4>
						<hr />
    
<form name="form1" id="form1" method="post">
 <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">

    <tr>
      <td   ><strong>Date </strong></td>
      <td width="1%"><strong>:</strong></td>
      <td ><input name="s_date" type="date" id="s_date" class="form-control" style="width:200px;"  value="<?php if(isset($_POST['s_date'])){ echo $_POST['s_date'];}else{echo date('m/d/Y');}?>" required ></td>
      
	  
	  <td ><strong>Employee</strong></td>
      <td width="1%"><strong>:</strong></td>
      <td ><select name="employee" id="employee" style="width:200px;"  class=" form-control" required>
	  
      <option value="0">All</option>
      <?php $sql_ec = "select *from tbl_emp_master ";
						$result_ec = $objhr->viewdata($sql_ec);
				
					while($row_ec = $result_ec->fetch_assoc()){  ?>
					<option value="<?php echo $row_ec['id']; ?>" <?php if((isset($_POST['employee']))==$row_ec['id']){?> selected="selected" <?php }?> > <?php echo $row_ec['emp_name']; ?> | <?php echo $row_ec['emp_code']; ?></option>
					<?php }?>
    </select></td>
    
      <td ><input type="submit" name="Search" value="Search" class="btn btn-sm btn-info" /></td>
      </tr>
  </table>
</form>
<br />
<p id="p"></p>
<div class="container" id="map-canvas" style="width:100%; height:400px; ">
	
	
	</div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php 
include('../include/footer.php');
?>