<?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'];

// Common Information

$page_title="Daily Attendance";
$page="71_employee_04.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_info';	
$unique='emp_code';

// End Common Information

?>

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

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <?php
  do_calander('#att_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 schedule_intime=(document.getElementById('schedule_intime_'+id).value);
var grace_time=(document.getElementById('grace_time_'+id).value);
var late_count=(document.getElementById('late_count_'+id).value);
var hour=(document.getElementById('hour_'+id).value);
var minute=(document.getElementById('minute_'+id).value); 
var duty_type=(document.getElementById('duty_type_'+id).value); 
var day_count=(document.getElementById('day_count_'+id).value);
var remarks=(document.getElementById('remarks_'+id).value);
var flag=(document.getElementById('flag_'+id).value); 

var strURL="71_employee_04_ajax.php?emp_code="+emp_code+"&att_date="+att_date+"&schedule_intime="+schedule_intime+"&grace_time="+grace_time+"&late_count="+late_count+"&hour="+hour+"&minute="+minute+"&duty_type="+duty_type+"&day_count="+day_count+"&remarks="+remarks+"&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">
        <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 align="right">Attendance Date:</td>
          <td align="left">
		  <input type="text" class="form-control" id="att_date" name="att_date" value="<?php echo $_REQUEST['att_date']?>" >	  </td>
          <td align="right">Department:&nbsp;&nbsp;</td>
          <td width="14%">
		  <select id="department" name="department"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'department','id','department',$_POST['department'],'1 order by id');?>
        	</select>		  </td>
          <td width="13%" align="right">Job Status:&nbsp;&nbsp;</td>
          <td width="17%">
		  <select id="job_status" name="job_status"  class="form-select" required>
		 
			<?php foreign_relation($conn,'job_status','id','job_status',$_POST['job_status'],'1 order by id');?>
        	</select></td>
          <td width="15%" rowspan="6" 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> Run Report&nbsp;&nbsp;</button>
          </strong></td>
        </tr>
        <tr height="50">
        <td width="14%" align="right">&nbsp;</td>
        <td width="15%" align="left">&nbsp;</td>
        <td width="12%" align="right">Designation:&nbsp;&nbsp;</td>
        <td>
		<select id="designation" name="designation"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'designation','id','designation',$_POST['designation'],'1 order by id');?>
        	</select>
			</td>
        <td align="right">EMP Name:&nbsp;&nbsp;</td>
        <td>
		<?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']?>" >	
		</td>
        </tr>
    </table>
               
  </form>
  <br>
	
              <!-- Table with stripped rows -->
              <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="5%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;"> Code </span></th>
                    <th width="18%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;"> Employee Name </span></th>
                    <th width="6%" scope="col">Schedule</th>
                    <th width="12%" scope="col">Duty</th>
                    <th width="22%" scope="col">Remarks</th>
                    <th colspan="4" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;"><center>In Time </center></span></th>
                    <th width="7%" scope="col">Count</th>
                    <th width="8%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Action</span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
$employee_name = $_REQUEST['employee_name'];
  $employee=explode(' :: ',$employee_name);
   $employee_code=$employee[0];
  
if($employee_code!=''){
$employee_con=" and emp_code='".$employee_code."' ";
}

if($_POST['department']!=''){
$department_con=" and department='".$_POST['department']."' ";
}

if($_POST['designation']!=''){
$designation_con=" and designation='".$_POST['designation']."' ";
}

if($_POST['job_status']!=''){
$job_status_con=" and job_status='".$_POST['job_status']."' ";
}


$start_date = ''.$_POST['year'].'-'.$_POST['mon'].'-01';
$end_date = date("Y-m-t", strtotime($start_date));

$fdate = strtotime($start_date);
$tdate = strtotime($end_date);
$datediff = $tdate - $fdate;

$day_count=$datediff / (60 * 60 * 24)+1;

	 	$sql = "select id, designation from designation where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $designation[$info->id]=$info->designation;
		}	
		
		$sql = "select id, schedule_intime, grace_time, late_count from attendance_schedule where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $schedule_intime[$info->id]=$info->schedule_intime;
		 $grace_time[$info->id]=$info->grace_time;
		 $late_count[$info->id]=$info->late_count;
		}		
		
		  $sql = "select emp_code, id as lv_data from leave_details where lv_date='".$_POST['att_date']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $lv_data[$info->emp_code]=$info->lv_data;
		}
		
		 $sql = "select h_date, id as hd_date from hrm_holiday  where h_date='".$_POST['att_date']."' group by h_date";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $hd_date[$info->h_date]=$info->hd_date;
		}
		
		
		 $sql = "select emp_code, att_hour, att_min, duty_type, remarks from employee_daily_attendance where att_date='".$_POST['att_date']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $emp_code[$info->emp_code]=$info->emp_code;
  		 $att_hour[$info->emp_code]=$info->att_hour;
		 $att_min[$info->emp_code]=$info->att_min;
		 $duty_type[$info->emp_code]=$info->duty_type;
		 $remarks[$info->emp_code]=$info->remarks;
		}
		
		
			
					    $sql = 'select * from '.$table.' where 1 '.$employee_con.$designation_con.$department_con.$job_status_con.' ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
	<?php if(isset($_POST['run_data'])){?>		
      <tr>
      <td><?php echo $data->emp_code;?></td>
      <td><?php echo $data->emp_name;?></td>
      <td><?php echo $schedule_intime[$data->schedule_type];?>
	  <input name="schedule_intime_<?php echo $data->emp_code?>" type="hidden" id="schedule_intime_<?php echo $data->emp_code?>" value="<?php echo $schedule_intime[$data->schedule_type];?>" />
	  <input name="grace_time_<?php echo $data->emp_code?>" type="hidden" id="grace_time_<?php echo $data->emp_code?>" value="<?php echo $grace_time[$data->schedule_type];?>" />
	  <input name="late_count_<?php echo $data->emp_code?>" type="hidden" id="late_count_<?php echo $data->emp_code?>" value="<?php echo $late_count[$data->schedule_type];?>" />	  </td>
      <td>
	  <select name="duty_type_<?php echo $data->emp_code?>" id="duty_type_<?php echo $data->emp_code?>" class="form-select" style="width:100%; ">
					
					<?php foreign_relation($conn,'duty_type','id','duty_type',$duty_type[$data->emp_code],'1 order by id');?>
                </select>	  </td>
      <td><input name="remarks_<?php echo $data->emp_code?>" type="text" id="remarks_<?php echo $data->emp_code?>" value="<?php echo $remarks[$data->emp_code]?>"  class="form-control"/></td>
      <td width="7%">
	 		 <select name="hour_<?php echo $data->emp_code?>" id="hour_<?php echo $data->emp_code?>" class="form-select" style="width:100%; ">
					
					<?php foreign_relation($conn,'hour','hour','hour',$att_hour[$data->emp_code],'1 order by id');?>
                </select>				</td>
      <td width="3%" valign="middle"><strong>Hr</strong></td>
      <td width="7%">
	    <select name="minute_<?php echo $data->emp_code?>" id="minute_<?php echo $data->emp_code?>" class="form-select" style="width:100%;">
					
					<?php foreign_relation($conn,'minute','minute','minute',$att_min[$data->emp_code],'1 order by id');?>
                </select></td>
      <td width="5%" valign="middle"><strong>Min</strong></td>
      <td><input name="day_count_<?php echo $data->emp_code?>" type="text" id="day_count_<?php echo $data->emp_code?>" value="1"  style="width:50px" class="form-control"/></td>
      <td>
	  <?php if($lv_data[$data->emp_code]==0 && $hd_date[$_POST['att_date']]==0) {?>
	  
	  <span id="divi_<?php echo $data->emp_code?>">

            <?php
			  if($emp_code[$data->emp_code]>0)
			  {?>
			 
			  <input name="flag_<?php echo $data->emp_code?>" type="hidden" id="flag_<?php echo $data->emp_code?>" value="1" />
			  <input class="form-control" type="button" name="Button" value="Edit"  onclick="update_value(<?php echo $data->emp_code?>)" style="width:80%; height:30px; background-color:#FF3366; font-weight:700; font-size:12px;"/>
			  
			  <?php
			  }
			  else
			  {
			  ?>
			  <input name="flag_<?php echo $data->emp_code?>" type="hidden" id="flag_<?php echo $data->emp_code?>" value="0" />
			  <input class="form-control" type="button" name="Button" value="Save"  onclick="update_value(<?php echo $data->emp_code?>)" style="width:80%; height:30px;background-color:#66CC66;   font-weight:700; font-size:12px;"/><?php }?>
          </span>	 
		  
		  <?php }?>
		   </td>
      </tr>
				  <?php }}?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

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

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

</body>

</html>