<?php






ini_set('session.gc_maxlifetime', 30*60);

session_start();

include '../../../includes/config.php';



$D_ID = $_SESSION['D_Log'];




if (!$_SESSION['D_Log'])
echo '<script language="JavaScript">
 document.location="../../Drivers-Sign-In.php";
</script>';







$sql3 = mysqli_query($dbConn,"select * from drivers where ID='$D_ID'");
					$row3 = mysqli_fetch_array($sql3);
$Name=$row3['Name'];
$Phone_Number=$row3['Phone_Number'];
$Gender=$row3['Gender'];
$Photo=$row3['Photo'];
$lon=$row3['Longitude'];
$lat=$row3['Latitude'];
$Total_Rates=$row3['Total_Rates'];
$Car_Type=$row3['Car_Type'];
$Car_Color=$row3['Car_Color'];
$Penalty_Balance=$row3['Penalty_Balance'];
$Credit_Balance=$row3['Credit_Balance'];
$Total_Need_Balance=$row3['Total_Need_Balance'];
$Avalability_Status=$row3['Avalability_Status'];





	$TP_ID = $_GET['TP_ID'];
	$User_ID = $_GET['User_ID'];
	$Driver_ID = $_GET['Driver_ID'];
	

$sql = mysqli_query($dbConn,"select * from trips_privates where ID='$TP_ID'");
$row = mysqli_fetch_array($sql);


$Gender = $row['Gender'];
$Total_KM_Distance = $row['Total_KM_Distance'];
$Date = $row['Date'];
$From_Location = $row['From_Location'];
$To_Location = $row['To_Location'];
$lng1 = $row['From_Longitude'];
$lat1 = $row['From_Latitude'];
$lng2 = $row['To_Longitude'];
$lat2 = $row['To_Latitude'];
$Driver_ID = $row['Driver_ID'];
$Payment_Type = $row['Payment_Type'];
$Total_Riders = $row['Total_Riders'];
$Total_Price = $row['Total_Price'];



$sql3 = mysqli_query($dbConn,"select * from users where ID='$User_ID'");
$row3 = mysqli_fetch_array($sql3);
$U_Name = $row3['Name'];
$U_Photo = $row3['Photo'];
$U_Phone_Number = $row3['Phone_Number'];



if(isset($_POST['Submit']))
{
	
	
	
	
	
	
	
	
	
	$TP_ID = $_POST['TP_ID'];
	$User_ID = $_POST['User_ID'];
	$Driver_ID = $_POST['Driver_ID'];
	
	$sql1 = mysqli_query($dbConn,"update trips_privates set Status='Dropoff' WHERE ID='$TP_ID'");

	$sql11 = mysqli_query($dbConn,"select * from trips_privates where ID='$TP_ID'");
	$row11 = mysqli_fetch_array($sql11);
	$From_Time = $row11['From_Time'];
	
	

	
	$To_Time = date("H:i");
	
	
	$start = strtotime($From_Time);
	$end = strtotime($To_Time);
	$mins = ($end - $start) / 60;




	
	$Estimation_Time = $mins;
	
	
	
	
	
$Total_Price = $row11['Total_Price'];
$SHEELD_Credit = $Total_Price*0.10;




$sql4 = mysqli_query($dbConn,"select * from drivers where ID='$D_ID'");
$row4 = mysqli_fetch_array($sql4);
$Credit_Balance = $row4['Credit_Balance'];
$New_Credit_Balance = $Credit_Balance - $SHEELD_Credit;
$sql5 = mysqli_query($dbConn,"update drivers set Credit_Balance='$New_Credit_Balance' WHERE ID='$D_ID'");



	
	
	
	
	
	
	
	
	
	
$sql3 = mysqli_query($dbConn,"update trips_privates set To_Time='$To_Time', Estimation_Time='$Estimation_Time', Status='Dropoff' WHERE ID='$TP_ID'");
	
	
	


	echo '<script language="JavaScript">
 document.location="private-same-gender-4.php?TP_ID='.$TP_ID.'&User_ID='.$User_ID.'&Driver_ID='.$Driver_ID.'";
</script>';
	
	
	
}

	
	

?>

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../../../css/bootstrap.min.css">
    <!-- FontAwesome -->
    <link rel="stylesheet" href="../../../css/fontawesome.css">
    <!-- Main Style -->
    <link rel="stylesheet" href="../../../css/styles.css">
		    <link rel="shortcut icon" href="../../../images/icon.ico"/>

    <title>رحلتك الخاصة الحالية</title>
	
	    <link rel="shortcut icon" href="../../../images/icon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=0"/>


    

		<style>
@font-face {
   font-family: myFirstFont;
   src: url(../../../fonts/GE-SS-Two-Bold.otf);
   font-size:8px;
}

body {
   font-family: myFirstFont !important;
}


@font-face {
   font-family: myFirstFont2;
   src: url(../../../fonts/GE-SS-Two-Bold.otf);
   font-size:8px;
}





::-webkit-input-placeholder { /* Edge */
  color: white !important;
}

:-ms-input-placeholder { /* Internet Explorer 10-11 */
  color: white !important;
}

::placeholder {
  color: white !important;
}


</style>










</head>
<body style="height:auto !important">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    var latitude = position.coords.latitude;
                    var longitude = position.coords.longitude;

                    // Send this to your PHP script via AJAX or other means
                    sendDataToServer(latitude, longitude);
                });
            } else {
                alert("Geolocation is not supported by this browser.");
            }
        }

        function sendDataToServer(latitude, longitude) {
            // Using fetch API to send data to PHP
            fetch('update-my-location-1.php?D_ID=<?php echo $D_ID; ?>	', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    latitude: latitude,
                    longitude: longitude
                })
            })
            .then(response => response.json())
            .then(data => {
                console.log('Success:', data);
            })
            .catch((error) => {
                console.error('Error:', error);
            });
        }

        // Keep updating every 5 seconds
        setInterval(getLocation, 5000);
    </script>


<!--Loading Container Start-->
<div id="load" class="loading-overlay display-flex flex-column justify-content-center align-items-center">
    <div class="primary-color font-28 fas fa-spinner fa-spin"></div>
</div>
<!--Loading Container End-->


<div class="row h-100">
    <div class="col-xs-12 col-sm-12 remaining-height">

 <!--Page Title & Icons Start-->
        <div class="header-icons-container text-center">
           
                <span class="float-left menu-open closed">
                    <img src="../../../icons/menu.svg" alt="Menu Hamburger Icon">
            </span>
            <span class="title"><br>رحلتك الخاصة الحالية</span>
            <a href="#">
               	 <span class="float-right" style="margin-top:-27px !important">

                    <img src="../../../images/icon.png" alt="Logo" width="14px" height="20px">
                </span>
            </a>
        </div>
        <!--Page Title & Icons End-->

       
  			<br><br>

     
				
		
		
<form action="private-same-gender-3.php?TP_ID=<?php echo $TP_ID; ?>" method="post">
<input type="hidden" name="TP_ID" value="<?php echo $TP_ID; ?>"/>
<input type="hidden" name="User_ID" value="<?php echo $User_ID; ?>"/>
<input type="hidden" name="Driver_ID" value="<?php echo $Driver_ID; ?>"/>




 <div class="rest-container" style="margin-top:-50px">
            <div class="all-history-items remaining-height">
                <div class="history-items-container trip-history">
                    <div class="history-item">

						
						
						
                        <!--Google Map Start-->
                        <div id="map1" style="height:520px"></div>
                        <!--Google Map End-->

                       
<br>
                       <center> <div class="confirm-ride-btn">
                    <?php
					
					
					 echo "<a href='geo:".$lat1.",".$lng1."?q=".$lat1.",".$lng1."(".$From_Location.")' style='border-style: dashed; border-radius: 50px; color:#011F39; border-color:#B3E207; background-color:#ebebeb' class='btn btn-sm btn-center'>افتح خريطة جوجل (موقع التوصيل)</a>";

?>
<br>			<br>
<button name="Submit" type="submit" style='height:50px; font-size: 16px;  border-style: dashed; border-radius: 50px; color:#011F39; border-color:#B3E207; background-color:#ebebeb'  class="btn btn-sm btn-center">انهاء الرحلة</button>

			
                </div></center>

	
            	  
								
							
                           

                    </div>
                </div>
            </div>
        </div>
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		



					
						
				
				




</form>





		
		
				
						









        
    </div>

    <?php
   include 'menu.php';
   ?>

  <script>
    function initMap() {
      const map = new google.maps.Map(document.getElementById('map1'), {
        center: { lat: <?php echo $lat1; ?>, lng: <?php echo $lng1; ?> }, // Default center (San Francisco)
        zoom: 10,
      });
	  const trafficLayer = new google.maps.TrafficLayer();
			trafficLayer.setMap(map);

      const startPoint = { lat: <?php echo $lat1; ?>, lng: <?php echo $lng1; ?> }; // Replace with your start coordinates
      const endPoint = { lat: <?php echo $lat2; ?>, lng: <?php echo $lng2; ?> };       // Replace with your end coordinates

      const directionsService = new google.maps.DirectionsService();
      const directionsRenderer = new google.maps.DirectionsRenderer({ map: map });

      const request = {
        origin: startPoint,
        destination: endPoint,
        travelMode: google.maps.TravelMode.DRIVING,
      };

      directionsService.route(request, function (result, status) {
        if (status === google.maps.DirectionsStatus.OK) {
          directionsRenderer.setDirections(result);
        }
      });
    }
  </script>


    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDemEmJPVUC9PoCzTLZLZdhwJLt5fu5XUc&callback=initMap" async defer></script>



<div id="load" > </div>

</body>
</html>