<?php
date_default_timezone_set('Asia/Kolkata'); 
//echo $date = date("Y-m-d", time());
include ('../config.php');
@session_start();
@$user_id1=$_SESSION['user_id1'];
@$idvfied1=$_SESSION['idverified1'];
@$namevfied1=$_SESSION['namevfied1'];
if(!isset($_SESSION['user_id1'])){
	header("Location: index.php");
}

$timeout = 60 * 60 * 24 * 365;
$s_name = session_name();
if(isset( $_COOKIE[ $s_name ] )) {
  setcookie( $s_name, $_COOKIE[ $s_name ], time() + $timeout, '/' );
}

$sel1=mysqli_query($conn,"select * from hr_dump where empid='".@$user_id1."'");
$fetch1=mysqli_fetch_array($sel1);

$queryFR=mysqli_query($conn,"select * from tracker where Functional_Reporting='$fetch1[Employee_Number]'");
$dataFR=mysqli_fetch_array($queryFR);

$queryFR=mysqli_query($conn,"select * from emp_leave where Functional_Reporting='$fetch1[Employee_Number]'");
$dataFR=mysqli_fetch_array($queryFR);

$IClv=mysqli_query($conn,"select * from  ic_leave_balance where empid='".@$user_id1."'");
$dataIClv=mysqli_fetch_array($IClv);
?>

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>CL Leave Request</title>
	
	<?php	include ('header.php');	?>
	
	<div id="appCapsule">
        <div class="section wallet-card-section pt-1 mb-4">
            <div class="wallet-card">
				<!--<div class="balance">
                    <div class="left">
                        <span class="title"></?php echo $fetch1['Employee_Full_Name']; ?></span>
                        <h4></?php echo $fetch1['Team_Name']; ?></h4>
                        <h6></?php echo $date = date("Y-m-d", time()); ?></h6>
                    </div>
                    <div class="right">
                        <a href="#"><img src="../assets/img/noimage.png" alt="logo" class="logo-profile"></a>
                    </div>
                </div>-->
				
				<?php	include ('include/wallet-balance.php');	?>
<?php
if(isset($_POST['submit']))
{
	$DateFrom = $_POST['DateFrom'];
	$query=mysqli_query($conn,"select * from emp_leave where empid='".@$user_id1."' AND DateFrom = '$DateFrom' ORDER BY id ASC ");
	$dataEL=mysqli_fetch_array($query);

	$LeaveCategory=($_POST['LeaveCategory']);
	$LeaveType  = ($_POST['LQTY']."-".$_POST['LeaveCategory']);
	//echo "<pre>"; print_r($_POST); exit;
	
	
	$stmt = $conn->prepare("INSERT INTO emp_leave (empid, Employee_Full_Name, Department, Team_Name, LeaveCategory, lat, longt, LeaveType, LQTY, DateFrom, Remarks, Functional_Reporting, Reporting_Head_Code, Team_Head_Code, Status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

	if (!$stmt) {	die("Error preparing statement: " . $conn->error);	}

	$stmt->bind_param("sssssssssssssss", $_POST['empid'], $_POST['Employee_Full_Name'], $_POST['Department'], $_POST['Team_Name'], $LeaveCategory, $_POST['lat'], $_POST['longt'], $LeaveType, $_POST['LQTY'], $_POST['DateFrom'], $_POST['Remarks'], $_POST['Functional_Reporting'], $_POST['Reporting_Head_Code'], $_POST['Team_Head_Code'], $_POST['Status']);
	
	/* $query=mysqli_query($conn,"insert into emp_leave (empid, Employee_Full_Name, Department, Team_Name, LeaveCategory, lat, longt, LeaveType, LQTY, DateFrom, Remarks, Functional_Reporting, Reporting_Head_Code, Team_Head_Code, Status)
	values ('".$_POST['empid']."', '".$_POST['Employee_Full_Name']."', '".$_POST['Department']."', '".$_POST['Team_Name']."', '".$_POST['LeaveCategory']."', '".$_POST['lat']."', '".$_POST['longt']."', '$LeaveType',  '".$_POST['LQTY']."', '".$_POST['DateFrom']."', '".$_POST['Remarks']."', '".$_POST['Functional_Reporting']."', '".$_POST['Reporting_Head_Code']."', '".$_POST['Team_Head_Code']."', '".$_POST['Status']."' )"); */
	
	$LeaveCategory=($_POST['LeaveCategory']);
	
	if ($LeaveCategory == 'CL') {
		$Lbal=$dataIClv['CL'];	$TakenCL=($_POST['LQTY']);			$CL=($Lbal-$TakenCL);
		$query = mysqli_query($conn,"update ic_leave_balance set CL='$CL' where empid='$fetch1[empid]'" );
		}
	
	if ($stmt->execute()) {
		echo "<script>window.location.href='leave-view-current.php';</script>";	exit;
	} else {
		die("Error executing statement: " . $stmt->error);
	}

	/* if($query==true) {
		echo "<script>window.location.href='leave-view-current.php';</script>"; exit;
		echo "<div class='col-md-12 text-center btn btn-success'>Leave Request Sent! </div>";
		}
	else {	
		echo mysqli_error($conn);
		echo "<div class='col-md-12 text-center btn btn-danger'><strong>Error!</strong>Sorry something went wrong!</div>";	} */
}
?>
				<div class="row">
					<div class="wallet-footer"><h6 class="text-center">Apply CL</h6></div>
					<form class="form-horizontal" name="leavereq" action="" method="post" enctype="multipart/form-data" autocomplete="off">
						<div class="row">
							<h5 class="text-center"><b><?php	echo "(Available CL: ".$dataIClv['CL'].")";	?></b></h5>
							<input class="form-control" type="hidden" name="empid" value="<?php echo $fetch1['empid']; ?>" />
							<input class="form-control" type="hidden" name="Employee_Full_Name" value="<?php echo $fetch1['Employee_Full_Name']; ?>" >
							<input class="form-control" type="hidden" name="Department" value="<?php echo $fetch1['Department']; ?>" >
							<input class="form-control" type="hidden" name="Team_Name" value="<?php echo $fetch1['Team_Name']; ?>" >
							<input class="form-control" type="hidden" name="Role" value="<?php echo $fetch1['Role']; ?>" >
							<input class="form-control" type="hidden" name='LeaveCategory' value='CL'>
							<input class="form-control" type="hidden" name="lat" id="lat" onkeydown="return false;"  placeholder="No Manual Entry">
							<input class="form-control" type="hidden" name="longt" id="longt" onkeydown="return false;" placeholder="No Manual Entry">
							<div class="col-md-3 col-sm-3 cl5"><label>Day Type <span class="red">*</span></label>
								<?php 
								if ($dataIClv['CL'] == 0.5)
									{
									echo '<select name="LQTY" class="form-control" required>
											<option value="0.5">Half Day</option>
										</select>';
									}
								elseif ($dataIClv['CL'] >= 1)
									{
									echo '<select name="LQTY" class="form-control" required>
											<option value="0.5">Half Day</option>
											<option value="1">Full Day</option>
										</select>';
									}
								?>
							</div>
							<div class="col-md-3 col-sm-3 cl5"><label>Date For <span class="red">*</span></label>
								<input class="form-control" type="date" onClick='getMyLocation();' name="DateFrom" required / >
							</div>
						</div>
						<div class="row">
							<div class="col-md-12 col-sm-12"><label>Remarks<span class="red">*</span></label>
								<input class="form-control" type="text" name="Remarks" placeholder="Remarks" maxlength="60" required>
								<input class="form-control" type="hidden" name="Functional_Reporting" value="<?php echo $fetch1['Functional_Reporting']; ?>" >
								<input class="form-control" type="hidden" name="Reporting_Head_Code" value="<?php echo $fetch1['Reporting_Head_Code']; ?>" >
								<input class="form-control" type="hidden" name="Team_Head_Code" value="<?php echo $fetch1['Team_Head_Code']; ?>" >
							</div>
							<div class="col-md-12 col-sm-12 mt5 text-center"><br>
								<input class="form-control" type="hidden" name="Status" value="Pending">
								<?php 
								if ($dataIClv['CL'] == 0.0)
									{	echo '<button class="btn btn-danger disabled">No Balance</button>';	}
								elseif ($dataIClv['CL'] >= 0.5)
									{	echo '<button type="submit" name="submit" class="btn btn-success">Save</button>';	}
								?>
							</div>
						</div>
					</form>
				</div>
            </div>
        </div>
	
	<?php	include ('include/copyright.php');	?>
	</div>
	
	<?php	include ('footer.php');	?>


<script>
	function getMyLocation(){
		if(navigator.geolocation){
			navigator.geolocation.getCurrentPosition(showPosition);
		}
		navigator.geolocation.getCurrentPosition(function(position)
		{
			var coordinates = position.coords;
			document.getElementById('lat').value = coordinates.latitude;
			document.getElementById('longt').value = coordinates.longitude;
		});
	}
</script>

<script type="text/javascript">
    var ab;
    function showPosition(position){
      ab = position.coords.latitude+","+position.coords.longitude;          
    }
    function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
		}
	}
</script>