<?php
include('../common/db_connection.php');
$Mainclass=new DB_Controller();
$Mainclass->Employee_COOKIE_Check();
$changeable_worktime=12;/*Only in Hour*/
$check_attendance=$Mainclass->Num_rows_Check('attendance_master','Attendance_Date',date('Y-m-d'));
if($check_attendance=="")
{
?>
<script>
//window.location.href="attendance.php";
</script>
<?php
}
?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
<title>Apply For Leave</title>


<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="Admindek Bootstrap admin template made using Bootstrap 4 and it has huge amount of ready made feature, UI components, pages which completely fulfills any dashboard needs." />
<meta name="keywords" content="bootstrap, bootstrap admin template, admin theme, admin dashboard, dashboard template, admin template, responsive" />
<meta name="author" content="colorlib" />

<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet"><link href="https://fonts.googleapis.com/css?family=Quicksand:500,700" rel="stylesheet">

<link rel="stylesheet" type="text/css" href="files/bower_components/bootstrap/css/bootstrap.min.css">

<link rel="stylesheet" href="files/assets/pages/waves/css/waves.min.css" type="text/css" media="all">

<link rel="stylesheet" type="text/css" href="files/assets/icon/feather/css/feather.css">

<link rel="stylesheet" type="text/css" href="files/assets/icon/themify-icons/themify-icons.css">

<link rel="stylesheet" type="text/css" href="files/assets/icon/icofont/css/icofont.css">

<link rel="stylesheet" type="text/css" href="files/assets/icon/font-awesome/css/font-awesome.min.css">

<link rel="stylesheet" type="text/css" href="files/assets/css/style.css">
<link rel="stylesheet" type="text/css" href="files/assets/css/pages.css">


<!--<link rel="stylesheet" href="files/assets/css/jquery-ui.css">-->

<link rel="stylesheet" type="text/css" href="files/assets/css/jquery.datetimepicker.css"/>
<link href="files/assets/css/jquerysctipttop.css" rel="stylesheet" type="text/css">

<link rel="stylesheet" href="files/assets/css/jquery-ui.css">


 <style type="text/css">
#tooltip
{
text-align: center;
color: #fff;
background: #111;
position: absolute;
z-index: 100;
padding: 5px 10px;
font-size:10px !important;
line-height:12px !important;
}

#tooltip:after /* triangle decoration */
{
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid #111;
content: '';
position: absolute;
left: 50%;
bottom: -10px;
margin-left: -10px;
}

#tooltip.top:after
{
border-top-color: transparent;
border-bottom: 10px solid #111;
top: -20px;
bottom: auto;
}

#tooltip.left:after
{
left: 10px;
margin: 0;
}

#tooltip.right:after
{
right: 10px;
left: auto;
margin: 0;
}

.message_maindiv{
float:left;
height:auto;
padding:15px;
background-color:#fff8e6;
text-align:left;
font-size:12px;
color:#503a27;
}

.time_border{
 border-right:1px solid #263544;
}


@media only screen and (max-width:480px){
.time_border{
 border-right:0px solid #0066FF;
}
}
.resize_text{height:55px !important;}


</style>
        
<script>
function isNumberKey(evt) 
{
	var charCode = (evt.which) ? evt.which : event.keyCode; 
	if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) 
	{
		return false;
	} 
	else 
	{
		return true;
	} 
} 

</script> 
       
 <script>
function page_submit()
{
	var leave_type=$("#leave_type").val();
	var start_date=$("#start_date").val();
	var start_date_pl('#start_date_pl').val();
	var end_date=$("#end_date").val();
	var end_date_pl=$("#end_date_pl").val();
	
	var Error=0;
	
	if(leave_type=='')
	{
		$("#leave_type").css('border-bottom','1px solid red');
		Error=1;
		$('#pass').show();
	}
	if(leave_type=='CL')
	{
	if(start_date=='')
	{
		$("#start_date").css('border-bottom','1px solid red');
		Error=1;
		$('#pass1').show();
	}
	if(end_date <= start_date)
	{
		$("#end_date").css('border-bottom','1px solid red');
		Error=1;
	}
	if(end_date=='')
	{
		$("#end_date").css('border-bottom','1px solid red');
		Error=1;
	}
	
	}
	if(leave_type=='PL')
	{
		if(start_date_pl=='')
	{
		$("#start_date_pl").css('border-bottom','1px solid red');
		Error=1;
		$('#pass2').show();
	}
	if(end_date_pl=='')
	{
		$("#end_date_pl").css('border-bottom','1px solid red');
		Error=1;
	}
	}
	if(Error==1)
	{
		return false;
	}
	
}
</script>
<script>
function dateformat_pl()
{
	var leave_type=$('#leave_type').val();
	if(leave_type=="PL")
	{
		$('#forcl').hide();
		$('#tocl').hide();
		$('#forpl').show();
		$('#topl').show();
	}
	else
	{
		$('#forcl').show();
		$('#tocl').show();
		$('#forpl').hide();
		$('#topl').hide();
	}
}
</script>
<script>
function avail_leave()
{
	var leave_type=$('#leave_type').val();
	
	$.ajax({
				url:'ajx_check_leave_type.php',
				type:'POST',
				data:'leave_type='+leave_type,
				success:function(f)
				{
					//alert(f);
					if(f==1)
					{
						alert('You do not have any Casual Leave');
					}
					if(f==2)
					{
						alert('You do not have any Paid Leave');
					}
				}	
		
		});
}
</script>   
<script>
function date_chk()
{	
		var start_date=$('#start_date').val();
		var end_date=$('#end_date').val();
		//alert(end_date);
		if(start_date > end_date)
		{
			if(end_date!="")
			{
			alert(Please give after start date);
			$('#end_date').val('');
			}
		}
		else
		{
			$('#end_date').val();
		}
}
</script>
</head>
<body>



<div id="pcoded" class="pcoded">
<div class="pcoded-overlay-box"></div>
<div class="pcoded-container navbar-wrapper">

<?php include('register-top-header.php'); ?>





<div class="pcoded-main-container">
<div class="pcoded-wrapper">

<?php include('register-side-menu.php'); ?>



<div class="pcoded-content">

<div class="page-header card">
<div class="row align-items-end">
<div class="col-lg-8">
<div class="page-header-title">

<div class="d-inline">
<h5 style="text-transform:uppercase; font-size:25px; font-weight:600;">apply for leave</h5>

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

</div>
</div>

<div class="pcoded-inner-content">
<div class="main-body">
<div class="page-wrapper">

<div class="page-body">
<div class="row">
<div class="col-sm-12">

<div class="card">

<div class="card-block">
<?php
$fetch_reporting_manager=$Mainclass->Fetch_needed_datas('employee_master',"*","Emp_Temp_Id",$_COOKIE['Payroll_Emp_Attendance']);
$fetch_name=$Mainclass->Fetch_needed_datas('employee_master',"Emp_First_Name,Emp_Last_Name,Work_Email","Emp_Temp_Id",$fetch_reporting_manager['Reporting_Manager']);
$manager=$fetch_name['Emp_First_Name']." ".$fetch_name['Emp_Last_Name'];
$check=$Mainclass->Fetch_needed_datas('working_hours_master',"Available_PL","Emp_Id",$_COOKIE['Payroll_Emp_Attendance']);
$check2=$Mainclass->Fetch_needed_datas('working_hours_master',"Available_CL","Emp_Id",$_COOKIE['Payroll_Emp_Attendance']);
if(isset($_POST['apply']))
{
	if($_POST['leave_type']!="")
	{	
	if($_POST['leave_type']=="CL")
	{	
	$start_date="";
    $end_date="";
    if($_POST['start_date']!="")
    {
    $start_date=date('Y-m-d',strtotime($_POST['start_date']));
    }
    if($_POST['end_date']!="")
    {
    $end_date=date('Y-m-d',strtotime($_POST['end_date']));
    }
    $diff=strtotime($start_date) - strtotime($end_date);
    $diff_date=abs(round($diff/ 86400))+1;
	}
	else	
	{		
	$start_date="";
    $end_date="";
    if($_POST['start_date']!="")
    {
    $start_date=date('Y-m-d',strtotime($_POST['start_date_pl']));
    }
    if($_POST['end_date']!="")
    {
    $end_date=date('Y-m-d',strtotime($_POST['end_date_pl']));
    }
    $diff=strtotime($start_date) - strtotime($end_date);
    $diff_date=abs(round($diff/ 86400))+1;	
	}
    if($diff_date>0)
    {
	if($_POST['leave_type']=="CL")
	{
	if($check2>0)
	{
    $Mainclass->Insert_Data("leave_apply_master","Company_Id,Emp_Id,Applied_CL,Leave_From,Leave_Till,Leave_Total_Days,Applied_Date,Employee_Remarks,Admin_Id","'".$User_details['Company_Id']."','".$User_details['Emp_Temp_Id']."','".$diff_date."','".date('Y-m-d',strtotime($_POST['start_date']))."','".date('Y-m-d',strtotime($_POST['end_date']))."','".$diff_date."','".date('Y-m-d H:i:s')."','".$Mainclass->real_escapes($_POST['reason'])."','".$fetch_reporting_manager['Reporting_Manager']."'");
	}
	else
	{
		$emtymsg="You don't have any CASUAL Leave";
	}
	}
	else if($_POST['leave_type']=="PL")
	{
	if($check>0)
	{		
	$Mainclass->Insert_Data("leave_apply_master","Company_Id,Emp_Id,Applied_PL,Leave_From,Leave_Till,Leave_Total_Days,Applied_Date,Employee_Remarks,Admin_Id","'".$User_details['Company_Id']."','".$User_details['Emp_Temp_Id']."','".$diff_date."','".date('Y-m-d',strtotime($_POST['start_date_pl']))."','".date('Y-m-d',strtotime($_POST['end_date_pl']))."','".$diff_date."','".date('Y-m-d H:i:s')."','".$Mainclass->real_escapes($_POST['reason'])."','".$fetch_reporting_manager['Reporting_Manager']."'");	
	}
	else
	{
		$emtymsg="You don't have any PAID Leave";
	}
	}
	
	if($_POST['leave_type']=="CL")
	{
$newmsg="Dear Mr./Mrs.".ucwords($manager).", <br/><br/>
	Mr./Mrs.".ucwords($fetch_reporting_manager['Emp_First_Name'])." ".$fetch_reporting_manager['Emp_Last_Name']." has applied leave from ".$_POST['start_date']." to ".$_POST['end_date'].". 

<br/>You are requested to update status in the portal";
	}
	else if($_POST['leave_type']=="PL")
	{
$newmsg="Dear Mr./Mrs.".ucwords($manager).", <br/><br/>
	Mr./Mrs.".ucwords($fetch_reporting_manager['Emp_First_Name'])." ".$fetch_reporting_manager['Emp_Last_Name']." has applied leave from ".$_POST['start_date_pl']." to ".$_POST['end_date_pl'].". 

<br/>You are requested to update status in the portal";
	}	
		
		$subject1="Application for leave from Mr./Mrs.".ucwords($fetch_reporting_manager['Emp_First_Name'])." ".$fetch_reporting_manager['Emp_Last_Name']."";





$url = 'https://api.sendgrid.com/';

$user = 'buurtz';

$pass = 'buurtz_123';

$json_string = array(



  'to' => array(

  $fetch_name['Work_Email']

   

  ),

  'category' => 'Release Order'

);





$params = array(

    'api_user'  => $user,

    'api_key'   => $pass,

    'x-smtpapi' => json_encode($json_string),

    'to'        => 'care@buurtzorgindia.com',

    'subject'   => $subject1,

    'html'      => $newmsg,

    'text'      => $newmsg,

    'from'      => 'care@buurtzorgindia.com',

  );





$request =  $url.'api/mail.send.json';



// Generate curl request

$session = curl_init($request);

// Tell curl to use HTTP POST

curl_setopt ($session, CURLOPT_POST, true);

// Tell curl that this is the body of the POST

curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

// Tell curl not to return headers, but do return the response

curl_setopt($session, CURLOPT_HEADER, false);

// Tell PHP not to use SSLv3 (instead opting for TLS)

curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);

curl_setopt($session, CURLOPT_RETURNTRANSFER, true);



// obtain response

$response = curl_exec($session);

curl_close($session);
    ?>
    <script>alert("Leave Applied Successfully");</script>
    <?php
	}
	}
	else
	{
	?>
    <script>alert("Please fill up required field");</script>
    <?php	
	}
}
?>

<form method="post" onSubmit="return page_submit()">
<div class="row">
<div class="col-sm-4"><div class="form-group row">
<label class="col-sm-12 col-form-label">LEAVE TYPE <span style="color:#FF0000;" >*</span> </label>
<div class="col-sm-12">
<select class="form-control" id="leave_type" name="leave_type" onchange="dateformat_pl(),avail_leave()">
<option value="">Select Leave Type</option>
<option value="CL">CASUAL LEAVE</option>
<option value="PL">PAID LEAVE</option>
</select>
<span id="pass" style="display:none;color:red;">Please Select any leave type</span>
</div>
</div></div>

<div class="col-sm-4" id="forcl"><div class="form-group row">
<label class="col-sm-12 col-form-label">LEAVE FROM <span style="color:#FF0000;" >*</span></label>
<div class="col-sm-12">
<input type="text" name="start_date" id="start_date" class="form-control" autocomplete="off" placeholder="Select date" >
<span id="pass1" style="display:none;color:red;">Please Select Date</span>
</div>
</div></div>

<div class="col-sm-4" id="forpl" style="display:none;"><div class="form-group row">
<label class="col-sm-12 col-form-label">LEAVE FROM <span style="color:#FF0000;" >*</span></label>
<div class="col-sm-12">
<input type="text" name="start_date_pl" id="start_date_pl" class="form-control" autocomplete="off" placeholder="Select date">
<span id="pass2" style="display:none;color:red;">Please Select Date</span>
</div>
</div></div>

<div class="col-sm-4" id="tocl"><div class="form-group row">
<label class="col-sm-12 col-form-label">LEAVE TO <span style="color:#FF0000;" >*</span></label>
<div class="col-sm-12">
<input type="text" name="end_date" id="end_date" class="form-control" autocomplete="off" placeholder="Select date" onblur="date_chk()">

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

<div class="col-sm-4" id="topl" style="display:none;"><div class="form-group row">
<label class="col-sm-12 col-form-label">LEAVE TO <span style="color:#FF0000;" >*</span></label>
<div class="col-sm-12">
<input type="text" name="end_date_pl" id="end_date_pl" class="form-control" autocomplete="off" placeholder="Select date">

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


<div class="col-sm-12"><div class="form-group row">
<label class="col-sm-12 col-form-label">LEAVE REASON</label>
<div class="col-sm-12">
<textarea class="form-control resize_text" name="reason" id="reason" type="text" placeholder="Enter reason" rows="5"></textarea>

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


<div class="col-sm-12 mt-4">
<input name="apply" type="submit" class="btn btn-primary mt-0" value="Apply">
</div>
<span style="color:red;"><?php echo $emtymsg; ?></span>
</div>
</form>
</div>
</div>










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

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

<div id="styleSelector">
</div>
</div>
</div>
</div>
</div>



<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/jquery/js/jquery.min.js"></script>
<script src="files/assets/js/jquery.js"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/popper.js/js/popper.min.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/bootstrap/js/bootstrap.min.js"></script>

<script src="files/assets/js/jquery.datetimepicker.js"></script>


<script src="../files/assets/pages/waves/js/waves.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/jquery-slimscroll/js/jquery.slimscroll.js"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/modernizr/js/modernizr.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/modernizr/js/css-scrollbars.js"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/assets/pages/form-validation/validate.js"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/assets/pages/form-validation/form-validation.js"></script>
<script src="files/assets/js/pcoded.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>
<script src="files/assets/js/vertical/vertical-layout.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>
<script src="files/assets/js/jquery.mCustomScrollbar.concat.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/assets/js/script.js"></script>


<script src="files/assets/js/rocket-loader.min.js" data-cf-settings="c3ff8485e8526425ac6f5b03-|49" defer=""></script>


<script src="files/assets/js/jquery-ui.js"></script>
  
<script>
$(function() {
$( "#start_date" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});
</script>

<script>
$(function() {
$( "#start_date_pl" ).datepicker({
changeMonth: true,
changeYear: true,
minDate:0,
dateFormat: 'dd-mm-yy'
});
});
</script>

<script>
$(function() {
$( "#end_date" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});
</script>

<script>
$(function() {
$( "#end_date_pl" ).datepicker({
changeMonth: true,
changeYear: true,
minDate:0,
dateFormat: 'dd-mm-yy'
});
});
</script>


</body>
</html>
