<?php
session_start();
date_default_timezone_set("Asia/Colombo");
include 'load.php';
$branch_id = $_SESSION['branch_id'];

$user_id = $_SESSION['user_id'];
$today = date('Y-m-d H:i:s');
$todaydate = date('Y-m-d');
if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$user_id' AND branch_id='$branch_id' AND in_or_out='1' AND DATE(date_time)='$todaydate'")) > 0 ){
    header("refresh:1;home.php");
}


$userinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE branch_id='$branch_id' AND user_id ='$user_id'"));

if(isset($_POST['save'])){
    $latitude = $_POST['latitude'];
    $longitude = $_POST['longitude'];


    $sql = "INSERT INTO in_out_time (in_or_out, user_id, date_time,latitude,longitude,branch_id)VALUES ('1', '$user_id', '$today','$latitude','$longitude','$branch_id')";
    
    if ($conn->query($sql) === TRUE) {
        $successMSG = "Successfully! Add Your In Time....";
        header("refresh:2;home.php"); 
    }else{

            $errMSG = "error while inserting....";

    }


}









?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard
">
<meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard
">
<meta name="author" content="Payroll | Pos & Invetory System | Dashboard
">
<meta name="robots" content="noindex, nofollow">

<title>Add User Out Time | Payroll | Pos & Invetory System | Dashboard
</title>


<?php
require_once 'headercss.php';
?>
<script>
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition, showError);
        } else {
            document.getElementById("location").innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function showPosition(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        document.getElementById("latitude").value = latitude;
        document.getElementById("longitude").value = longitude;
        

        
        //var xhr = new XMLHttpRequest();
      //  xhr.open("POST", "save_location.php", true);
       // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
       // xhr.send("latitude=" + latitude + "&longitude=" + longitude);
    }

    function showError(error) {
        switch(error.code) {
            case error.PERMISSION_DENIED:
                document.getElementById("location").innerHTML = "User denied the request for Geolocation.";
                break;
            case error.POSITION_UNAVAILABLE:
                document.getElementById("location").innerHTML = "Location information is unavailable.";
                break;
            case error.TIMEOUT:
                document.getElementById("location").innerHTML = "The request to get user location timed out.";
                break;
            case error.UNKNOWN_ERROR:
                document.getElementById("location").innerHTML = "An unknown error occurred.";
                break;
        }
    }

    getLocation();
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.1.0-rc.0/js/select2.min.js"></script>

</head>
<body>
<div id="global-loader">
<div class="whirly-loader"> </div>
</div>

<div class="main-wrapper">
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>

<div class="page-wrapper">
<div class="content">
<div class="page-header">
<div class="page-title">
<h4>Out Time</h4>
<h6>Add/Update In Time</h6>
</div>
</div>

<div class="card">
<div class="card-body">
<form method="POST" enctype="multipart/form-data">
									<?php

									if(isset($errMSG)){

									?>
			
									<div class="alert alert-danger alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Error!</strong> <?php echo $errMSG; ?>
									</div>

									<?php

									}
	
									else if(isset($successMSG)){

									?>
			
									<div class="alert alert-success alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Success!</strong> <?php echo $successMSG; ?>.
									</div>

									<?php

									}

									?>
<div class="row">
<div class="col-lg-4 col-sm-6 col-12">
<div class="form-group">
<label for="fullName">Name</label>
<input type="text" class="form-control" name="username" id="username" value="<?php echo $userinfo['fullname']; ?>" readonly> 
</div>
</div>


			<div class="col-lg-4 col-sm-6 col-12">
					<div class="form-group">
						<label style="font-weight:bold;color:#000000;">Date Time</label>
						<input type="text" class="form-control" name="datetime" id="datetime" value="<?php echo date('Y-m-d H:i:s'); ?>" readonly>
					</div>
			</div>
            <div class="col-lg-4 col-sm-6 col-12">
					<div class="form-group">
						<label style="font-weight:bold;color:#000000;">Latitude</label>
						<input type="text" class="form-control" name="latitude" id="latitude" value="" readonly required>
					</div>
			</div>
            <div class="col-lg-4 col-sm-6 col-12">
					<div class="form-group">
						<label style="font-weight:bold;color:#000000;">longitude</label>
						<input type="text" class="form-control" name="longitude" id="longitude" value="" readonly required>
					</div>
			</div>
           <!-- <div class="col-lg-4 col-sm-6 col-12">
            <iframe 
                width="600" 
                height="450" 
                style="border:0" 
                loading="lazy" 
                allowfullscreen 
                referrerpolicy="no-referrer-when-downgrade" 
                src="https://www.google.com/maps/embed/v1/view?key=AIzaSyCAIwTPctnSM2PWcbK6cMdlZaSgEYIKp5U&center=40.748817,-73.985428&zoom=15&maptype=roadmap">
                </iframe>
                                    
            </div>-->



<div class="col-lg-12">

<button type="submit" name="save" class="btn btn-submit me-2">Save</button>
</div>
</div>
</form>
</div>
</div>

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

	<script src="assets/js/jquery-3.6.0.min.js"></script>

	<script src="assets/js/feather.min.js"></script>

	<script src="assets/js/jquery.slimscroll.min.js"></script>

	<script src="assets/js/jquery.dataTables.min.js"></script>
	<script src="assets/js/dataTables.bootstrap4.min.js"></script>

	<script src="assets/js/bootstrap.bundle.min.js"></script>

	<script src="assets/plugins/select2/js/select2.min.js"></script>

	<script src="assets/js/moment.min.js"></script>
	<script src="assets/js/bootstrap-datetimepicker.min.js"></script>

	<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
	<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>
	<script src="assets/alertify/alertify.js"></script>
		<script src="assets/alertify/alertify.min.js"></script>

	<script src="assets/js/script.js"></script>
	<!-- <script src="process.js"></script> -->
	<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
	








</body>
</html>