<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

$data_found=$_REQUEST['emp_code'];
$lv_no=$_REQUEST['lv_no'];
// Common Information

$page_title="Leave Application";
$page="71_employee_leave.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='leave_master';	
$unique='lv_no';

// End Common Information


if(isset($_POST['run_data']))
{
$employee_name = $_REQUEST['employee_name'];
$employee=explode(' :: ',$employee_name);
$emp_code=$employee[0];
?>

<script language="javascript">
window.location.href = "<?php echo $page;?>&emp_code=<?=$emp_code?>";
</script>

<?php }

if(isset($_POST['insert_data']))
{
$entry_by = $_SESSION['user']['id'];
$entry_at = date('Y-m-d H:i:s');
$emp_code = $_POST['emp_code'];
$f_date = $_POST['f_date'];
$t_date = $_POST['t_date'];
$leave_type = $_POST['leave_type'];
$purpose = $_POST['purpose'];

$f_dt = $f_date;	   
$t_dt =  date('Y-m-d', strtotime('+1 day', strtotime($t_date)));

$from_date = strtotime($f_dt);
$to_date = strtotime($t_dt);
$datediff = $to_date - $from_date;
 $count_days = round($datediff / (60 * 60 * 24));

		$YR = date('Y',strtotime($f_date));
 		$year = date('y',strtotime($f_date));
  		$month = date('m',strtotime($f_date));
 		$inv_cy_id = find_a_field($conn,'leave_master','max(lv_id)','year="'.$YR.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
   		$lv_no=''.$year.''.$month.''.$cy_id;
		//$view_lv_no='LV-'.$year.''.$month.''.$cy_id;

$ins_sql_ms = 'INSERT INTO leave_master (lv_no, year, lv_id, emp_code, lv_date, f_date, t_date, leave_type, leave_days, purpose, entry_by, entry_at) VALUES

("'.$lv_no.'", "'.$YR.'", "'.$cy_id.'", "'.$emp_code.'", "'.$f_date.'", "'.$f_date.'", "'.$t_date.'", "'.$leave_type.'", "'.$count_days.'", "'.$purpose.'", "'.$entry_by.'", "'.$entry_at.'")';
	
mysqli_query($conn,$ins_sql_ms);


$s_date= strtotime($f_date);
$e_date= strtotime($t_date);


for($i=$s_date; $i<=$e_date; $i+=86400){

$leave_date=date('Y-m-d',$i);
$leave_days=1;

$sql_del1 = 'delete from employee_daily_attendance where emp_code="'.$emp_code.'" and att_date="'.$leave_date.'"';
mysqli_query($conn,$sql_del1);

$ins_sql_dt = 'INSERT INTO leave_details (emp_code, lv_no, lv_date, leave_type, leave_days, entry_by, entry_at) VALUES

("'.$emp_code.'", "'.$lv_no.'", "'.$leave_date.'", "'.$leave_type.'", "'.$leave_days.'", "'.$entry_by.'", "'.$entry_at.'")';
	
mysqli_query($conn,$ins_sql_dt);

}


//for($i=0;$i<$total_installment;$i++)
//{
//$smon=$mon+$i;
//$syear=$year;
//$current_mon = date('m',mktime(1,1,1,$smon,1,$syear));
//$current_year = date('Y',mktime(1,1,1,$smon,1,$syear));
//$installment_no = $i+1;
//
// $ins_sql = 'INSERT INTO  employee_advance (emp_code, ledger_id, tr_no, tr_date, start_mon, installment_no, mon, year, advance_amt, monthly_amt, entry_at, entry_by) VALUES
//
//("'.$emp_code.'",  "'.$ledger_id.'", "'.$jv_no.'", "'.$tr_date.'", "'.$start_mon.'", "'.$installment_no.'", "'.$current_mon.'", "'.$current_year.'", "'.$advance_amt.'", "'.$monthly_amt.'", "'.$entry_at.'", "'.$entry_by.'")';
//	
//mysqli_query($conn,$ins_sql);
//}




?>

<script language="javascript">
window.location.href = "<?php echo $page;?>&lv_no=<?php echo $lv_no;?>";
</script>

<?php }?>

<!DOCTYPE html>
<html lang="en">

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <?php 
 do_calander('#f_date'); 
 do_calander('#t_date'); ?>
 
 
 <script>



function getXMLHTTP() { //fuction to return the xml http object



		var xmlhttp=false;	



		try{



			xmlhttp=new XMLHttpRequest();



		}



		catch(e)	{		



			try{			



				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch(e){



				try{



				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");



				}



				catch(e1){



					xmlhttp=false;



				}



			}



		}



		 	



		return xmlhttp;



    }



	function update_value(id)
	{

var emp_code=id; // Rent
var att_date=(document.getElementById('att_date').value); 

var hour=(document.getElementById('hour_'+id).value);

var minute=(document.getElementById('minute_'+id).value); 

var flag=(document.getElementById('flag_'+id).value); 

var strURL="71_employee_04_ajax.php?emp_code="+emp_code+"&att_date="+att_date+"&hour="+hour+"&minute="+minute+"&flag="+flag;



		var req = getXMLHTTP();



		if (req) {



			req.onreadystatechange = function() {

				if (req.readyState == 4) {

					// only if "OK"

					if (req.status == 200) {						

						document.getElementById('divi_'+id).style.display='inline';

						document.getElementById('divi_'+id).innerHTML=req.responseText;						

					} else {

						alert("There was a problem while using XMLHTTP:\n" + req.statusText);

					}

				}				

			}

			req.open("GET", strURL, true);

			req.send(null);

		}	



}




</script>

<body>

  <!-- ======= Header ======= -->
  <?php 
  require_once('inc_files/inc_header.php');
  ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">HR Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
<?php if($data_found=="" && $lv_no=="") {?>
<?php 
   $emp_sql="SELECT * FROM employee_info  WHERE emp_code='".$_REQUEST['emp_code']."'";
   $emp_data = find_all_field_sql($conn,$emp_sql);
   ?>
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>

 <form class="row g-3" action="" method="post" name="codz" id="codz">		
	<table width="80%" border="0" align="center">
        <tr height="50">
          <td width="14%" align="right"><span style="color:#FF0000;"><strong>Employee Name:</strong></span></td>
          <td width="15%" align="left">
		  <?php 
		   auto_complete_from_db_sql($conn,'SELECT concat(emp_code," :: ",emp_name), concat(emp_code," :: ",emp_name) FROM employee_info
			WHERE 1 order by emp_code','employee_name');	
			?>
       <input type="text" class="form-control" id="employee_name" name="employee_name" value="<?php echo $_REQUEST['employee_name']?>" style="width:100%" >	  </td>
          <td width="15%" rowspan="5" align="center" ><strong>
            &nbsp;&nbsp;&nbsp;
            <button type="submit" name="run_data" id="run_data" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> View Data&nbsp;&nbsp;</button>
          </strong></td>
        </tr>
    </table>
               
  </form>
  <br>
 

            </div>
          </div>
        </div>
		
		 <?php }?>

     <section class="section profile">
      <div class="row">
	  
	<?php 
    $emp_sql="SELECT * FROM employee_info  WHERE emp_code='".$_REQUEST['emp_code']."'";
   $emp_data = find_all_field_sql($conn,$emp_sql);
   ?>
  <?php if($data_found>0) {?>

        <div class="col-xl-9">

          <div class="card">
            <div class="card-body">
            <br>
              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
			  
			  <div class="col-md-4">
                 <label for="title_name" class="form-label"><strong>EMP Code</strong></label>
                  <input type="text" class="form-control" id="emp_code" name="emp_code" value="<?php echo $emp_data->emp_code; ?>" readonly="" required >
                </div>
                <div class="col-md-8">
                 <label for="first_name" class="form-label"><strong>Employee Name</strong></label>
                  <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo $emp_data->emp_name; ?>" readonly="" required >
                </div>
			
			  
			 	<div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Start Date</strong></label>
                  <input type="text" class="form-control" id="f_date" name="f_date" value="" required >
                </div>
                <div class="col-md-4">
                  <label for="mobile_no" class="form-label"><strong>End Date</strong></label>
                  <input type="text" class="form-control" id="t_date" name="t_date" value="" required >
</div>
				
				<div class="col-md-4">
                 <label for="email" class="form-label"><strong>Leave Type</strong></label>
                  <select id="leave_type" name="leave_type"  class="form-select"   required>
                      <option></option>
					 <?php foreign_relation($conn,'leave_type','id','leave_type',$leave_type, '1');?>
                    </select>
                </div>
				
				
                <div class="col-md-12">
                 <label for="mother_name" class="form-label"><strong>Purpose</strong></label>
             
				<textarea class="form-control" style="height: 60px" id="purpose" name="purpose" ></textarea>
                </div>
				
			
				<br>
			
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                  <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				  <button type="submit" class="btn btn-primary btn-sm" name="insert_data" id="insert_data" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

        </div>
		<?php }?>
		
		
		  <?php if($data_found>0) {?>
        <div class="col-xl-3">

          <div class="card"> 
		  
            <div class="card-body profile-card pt-4 d-flex flex-column align-items-center">
              <img src="uploads/<?php echo $data_found?>.jpg" alt="Profile" class="rounded-circle" style="border:1px solid #999999;">
              
			  
			   <h2><?php echo $emp_data->emp_name?></h2>
              <h3>EMP Code: <?php echo $emp_data->emp_code?></h3>
			 
            </div>
			
			
          </div>
        </div>
		<?php }?>
		
		
      </div>
    </section>
	
	 <?php if($lv_no>0) {?>
	
	<section class="section profile">
      <div class="row">
	  
	<?php 
   $lv_sql="SELECT * FROM leave_master WHERE lv_no='".$lv_no."'";
   $lv_data = find_all_field_sql($conn,$lv_sql);
   $emp_sql="SELECT * FROM employee_info  WHERE emp_code='".$lv_data->emp_code."'";
   $emp_data = find_all_field_sql($conn,$emp_sql);
   ?>
     
        <div class="col-xl-9">

          <div class="card">
            <div class="card-body">
            <br>
              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
			  
			  <div class="col-md-4">
                 <label for="title_name" class="form-label"><strong>EMP Code</strong></label>
                  <input type="text" class="form-control" id="emp_code" name="emp_code" value="<?php echo $lv_data->emp_code; ?>" readonly="" required >
                </div>
                <div class="col-md-8">
                 <label for="first_name" class="form-label"><strong>Employee Name</strong></label>
                  <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo $emp_data->emp_name; ?>" readonly="" required >
                </div>
			 	<div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Start Date</strong></label>
                  <input type="text" class="form-control" id="f_date2" name="f_date2" value="<?php echo $lv_data->f_date; ?>" required readonly="" >
                </div>
                <div class="col-md-4">
                  <label for="mobile_no" class="form-label"><strong>End Date</strong></label>
                  <input type="text" class="form-control" id="t_date2" name="t_date2" value="<?php echo $lv_data->t_date; ?>" required readonly="" >
				</div>
				
				<div class="col-md-4">
                 <label for="email" class="form-label"><strong>Leave Type</strong></label>
                  <select id="leave_type" name="leave_type"  class="form-select"   required>
                     
					 <?php foreign_relation($conn,'leave_type','id','leave_type',$leave_type, 'id="'.$lv_data->leave_type.'"');?>
                    </select>
                </div>
				
				
               <div class="col-md-12">
                 <label for="mother_name" class="form-label"><strong>Purpose</strong></label>
             
				<textarea class="form-control" style="height: 60px" id="purpose" name="purpose" ><?php echo $lv_data->purpose; ?></textarea>
                </div>
				
			
				<br>
			<div class="col-md-12">
			<table width="100%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="17%" scope="col">LV No </th>
                    <th width="21%" scope="col">lv date</th>
                    <th width="20%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">duration</span></th>
                    <th width="21%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;" >LV Type </span></th>
                    </tr>
                </thead>
                <tbody>
		<?php
	

	 
		
		 $sql = "select id, leave_type from leave_type where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $leave_type[$info->id]=$info->leave_type;
		 
		}
		
		
			
					    $sql = 'select * from leave_details where lv_no="'.$lv_no.'" ';
						$query = mysqli_query($conn,$sql);
						while ($inst_data = mysqli_fetch_object($query)) {

					?>
		
      <tr>
      <td><?php echo $inst_data->lv_no;?></td>
      <td><?php echo date('d-m-Y',strtotime($inst_data->lv_date));?></td>
      <td><?php echo $inst_data->leave_days;?></td>
	  <td><?php echo  $leave_type[$inst_data->leave_type];?></td>
      </tr>
				  <?php }?>
                </tbody>
              </table>
			</div>
                
                
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

        </div>
		
		
		
		 
        <div class="col-xl-3">

          <div class="card"> 
		  
            <div class="card-body profile-card pt-4 d-flex flex-column align-items-center">
              <img src="uploads/<?php echo $emp_data->emp_code?>.jpg" alt="Profile" class="rounded-circle" style="border:1px solid #999999;">
              
			  
			   <h2><?php echo $emp_data->emp_name?></h2>
              <h3>EMP Code: <?php echo $emp_data->emp_code?></h3>
			 
            </div>
			
			
          </div>
        </div>
		
		
		
      </div>
    </section>
		<?php }?>
		
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <!-- ======= Footer ======= -->

</body>

</html>