<?php
include('../include/header.php');
	
?>
<style>
.tb > tbody > tr > td, .tb > tfoot > tr > td, .tb > tfoot > tr > th, .tb > thead > tr > td, .tb > thead > tr > th { border-top:0px;
}
</style>
 <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary"></h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Grant Leave</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
						
										<?php	
						?>
						<form method="post">
					<?php if(!isset($_REQUEST['empl_code'])){?>	<table  class="table tb" style="width:50%">
						<tbody>
						 <tr><td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Employee Code </label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" <?php if(isset($_POST['emp_code'])){?> value="<?php echo $_POST['emp_code']; ?>" <?php }?> id="emp_code" name="emp_code" required placeholder="eg: EMP001 etc">
                                            </div></div></td>
											<td style="text-align:left">
											
                                                <button type="submit" class="btn btn-primary" name="save">Search</button>
                                          </td></tr>
										  </tbody>
											</table><?php }?>
											</form>
											 <?php 
						 if(isset($_POST['save'])){
$sqlww = "select *from tbl_emp_master where emp_code='".$_POST['emp_code']."'  ";
						$resultww = $objhr->viewdata($sqlww);
				if($resultww->num_rows >0){
					$rowsww = $resultww->fetch_assoc();

 
						 ?>
						<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
						 
						 <tbody>
						
						 <tr>
						<th>Employee Code: </th> <td align="center"><?php echo $rowsww['emp_code']; ?></th>
					<th>Employee Name: </th><td align="center"><?php echo $rowsww['emp_name']; ?></th>
					<th></th>
						 </tr>
						
						
						 </tbody>
						 </table>
						 <br /><br />
						 <form method="post">
						  <table  class="table tb" style="width:80%">
						  <tbody>
						 <tr><td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Leave Detail</label>
                                            <div class="col-lg-6">
                                                  <input type="hidden" class="form-control" id="emp_id" name="emp_id" value="<?php echo $rowsww['id']; ?>" required ><textarea class="form-control" id="leave_detail" style="height:80px;" name="leave_detail" required ></textarea>
                                            </div></div></td>
											
											<td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Leave Type </label>
                                            <div class="col-lg-6">
                                                <select type="text" class="form-control" id="leave_type" name="leave_type" required>
												<option>Select</option>
												<?php
												 $sql2 = "select *from tbl_leave_type ";
						$result2 = $objhr->viewdata($sql2);
							
							while($rows2 = $result2->fetch_assoc()){
												?>
												<option value="<?php echo $rows2['id']; ?>"><?php echo strtoupper($rows2['leave_type']); ?></option>
												<?php } ?>
												</select>
                                            </div></div></td>
											
											</tr>
										  
										  <tr><td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">From</label>
                                            <div class="col-lg-6">
                                                <input type="date" class="form-control"  id="from_date" name="from_date" required>
                                            </div></div></td>
											
											<td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">To </label>
                                            <div class="col-lg-6">
                                                <input type="date" class="form-control" id="to_date" name="to_date" required >
                                            </div></div></td>
											</tr>
											<tr><td style="text-align:left">
											
                                                <button type="submit" class="btn btn-primary" name="grant">Save</button>
                                          </td></tr>
										  </tbody>
											</table>
											</form>
					 <?php }else {echo 'No Data Found.';}} ?>
					 
					 
					 
					 
					 
					 
					 
					 <?php 
						 if(isset($_REQUEST['empl_code'])){
$sqlww = "select *from tbl_emp_master where emp_code='".$_REQUEST['empl_code']."'  ";
						$resultww = $objhr->viewdata($sqlww);
				if($resultww->num_rows >0){
					$rowsww = $resultww->fetch_assoc();

 $sql_lv_dt = "select *from tbl_leave_request where emp_id='".$rowsww['id']."' ";
						$result_lv_dt = $objhr->viewdata($sql_lv_dt);
						$row_lv_dt = $result_lv_dt->fetch_assoc();
						
						 ?>
						<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
						 
						 <tbody>
						
						 <tr>
						<th>Employee Code: </th> <td align="center"><?php echo $rowsww['emp_code']; ?></th>
					<th>Employee Name: </th><td align="center"><?php echo $rowsww['emp_name']; ?></th>
					<th></th>
						 </tr>
						
						
						 </tbody>
						 </table>
						 <br /><br />
						 <form method="post">
						  <table  class="table tb" style="width:80%">
						  <tbody>
						 <tr><td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Leave Detail</label>
                                            <div class="col-lg-6">
											
											<input type="hidden" class="form-control" id="lv_id" name="lv_id" readonly="" value="<?php echo $row_lv_dt['id']; ?>" required >
                                                  <input type="hidden" class="form-control" id="emp_id1" readonly="" name="emp_id1" value="<?php echo $rowsww['id']; ?>" required ><textarea class="form-control" id="leave_detail1" style="height:80px;" readonly="readonly" name="leave_detail1" required ><?php echo $row_lv_dt['leave_detail']; ?></textarea>
                                            </div></div></td>
											
											<td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Leave Type </label>
                                            <div class="col-lg-6">
                                                <select type="text" class="form-control"  id="leave_type1" name="leave_type1" required>
												
												<?php
												 $sql2 = "select *from tbl_leave_type where id =".$row_lv_dt['leave_type']." ";
						$result2 = $objhr->viewdata($sql2);
							
							while($rows2 = $result2->fetch_assoc()){
												?>
												<option value="<?php echo $rows2['id']; ?>" <?php if($row_lv_dt['leave_type']==$rows2['id']){?> selected<?php }?>><?php echo strtoupper($rows2['leave_type']); ?></option>
												<?php } ?>
												</select>
                                            </div></div></td>
											
											</tr>
										  
										  <tr><td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">From</label>
                                            <div class="col-lg-6">
                                                <input type="date" class="form-control" readonly=""  id="from_date1" name="from_date1" value="<?php echo $row_lv_dt['from_date']; ?>" required>
                                            </div></div></td>
											
											<td> <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">To </label>
                                            <div class="col-lg-6">
                                                <input type="date" class="form-control" readonly="" id="to_date1" name="to_date1" value="<?php echo $row_lv_dt['to_date']; ?>" required >
                                            </div></div></td>
											</tr>
											<tr><td style="text-align:left">
											
                                                <button type="submit" class="btn btn-primary" name="grant1">Approved</button>
                                          </td></tr>
										  </tbody>
											</table>
											</form>
					 <?php }else {echo 'No Data Found.';}} ?>
					 
					 
					 
					 
							 </div>
							 
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->

<?php 
if(isset($_POST["grant"]))
{
	
	$date2 = $_POST["from_date"];
	$date3 = $_POST["to_date"];




for($i=$date2; $i<=$date3; $i++){

$sqlinsert = "INSERT INTO `tbl_leave_detail` (`emp_id`, `leave_type_id`, `leave_detail`, `leave_date`, `user_id`) VALUES ('".$_POST['emp_id']."', '".$_POST['leave_type']."', '".$_POST['leave_detail']."', '$i', '$user_id');";
$resultinsert = $objhr->insertion($sqlinsert);
}
?>
<script>
alert('Success');
location='';
</script>
<?php

}

 ?>	
 
 
 
 <?php 
if(isset($_POST["grant1"]))
{
	
	$date2 = $_POST["from_date1"];
	$date3 = $_POST["to_date1"];




for($i=$date2; $i<=$date3; $i++){

$sqlinsert = "INSERT INTO `tbl_leave_detail` (`emp_id`, `leave_type_id`, `leave_detail`, `leave_date`, `user_id`, `leave_request_id`) VALUES ('".$_POST['emp_id1']."', '".$_POST['leave_type1']."', '".$_POST['leave_detail1']."', '$i', '$user_id', '".$_REQUEST['lv_id']."');";
$resultinsert = $objhr->insertion($sqlinsert);
}

$sqlupt = "UPDATE `tbl_leave_request` SET `leave_status` = '1' WHERE md5(id) = '".$_REQUEST['leave_id']."';";
$resultupt = $objhr->updation($sqlupt);

?>
<script>
alert('Success');
location='view_leave_request.php';
</script>
<?php

}

 ?>	
 
 
 
<?php
include('../include/footer.php');
?>
