<?php include('inc/header.php');
$clientObj->select($org_id); 

if(isset($_GET['dept']))
{
	$dept=sanitize($_GET['dept']);
	$mode=sanitize($_GET['mode']);
	$empid=sanitize($_GET['emp_id']);
	$month_year=sanitize($_GET['default_widget']);
	//echo $month_year;
	explode('/',$month_year);
	$month=$month_year[0].$month_year[1];
	$year=$month_year[3].$month_year[4].$month_year[5].$month_year[6];
	$num = cal_days_in_month(CAL_GREGORIAN, $month, $year); // Total number of days in a month
	$_SESSION['dept']=$dept;
	$_SESSION['mode']=$mode;
	$_SESSION['emp_id']=$empid;
	$_SESSION['month']=$month;
	$_SESSION['year']=$year;
}
/*$date = $year.'-'.$month.'-'.$day;;
$weekday = date('l', strtotime($date)); // note: first arg to date() is lower-case L
echo $weekday;*/


if(isset($_POST['submit_attendance']))
{
	$day=sanitize($_POST['del_id']);
	  
	
		$type=sanitize($_POST['leave_type']);
		if(!$type=='')
		{
	
	$fn_an=sanitize($_POST['fn_an']);
	$leave_date=$year.'-'.$month.'-'.$day;
	if($type!='' && $fn_an!='' && $leave_date!='')
	{
	if($fn_an==1)
	{ 
	$fn=1;
	}
	if($fn_an==2)
	{ 
	$an=1;
	}
	if($fn_an==3)
	{ 
	$fn=1;
	$an=1;
	}
	$emp_absentObj->emp_id=sanitize($_POST['emp_id']);
	$emp_absentObj->dept_id=$dept;
	$emp_absentObj->leave_type=$type;
	$emp_absentObj->fn=$fn;
	$emp_absentObj->an=$an;
	$emp_absentObj->leave_date=$leave_date;
	$emp_absentObj->admin_id=$userid;
	$emp_absentObj->org_id=$org_id;
	$emp_absentObj->insert();
	
	$present_days=sanitize($_POST['present_days']);
	$attndsid=sanitize($_POST['attnds_id']);
	$emp_check=$emp_monthlyattendanceObj->check_monthattendence($attndsid,$month,$year);
	if($emp_check==-1)
	{
	$emp_monthlyattendanceObj->attnds_id=$attndsid;
	$emp_monthlyattendanceObj->present=$present_days-1;
	$emp_monthlyattendanceObj->absent='1';
	$emp_monthlyattendanceObj->month=$month;
	$emp_monthlyattendanceObj->year=$year;
	$emp_monthlyattendanceObj->insert();
	$msg=1;
	}
	else
	{
	$emp_monthlyattendanceObj->select($emp_check);
	$present=$emp_monthlyattendanceObj->present-1;	
	$absent=$emp_monthlyattendanceObj->absent+1;	
	$emp_monthlyattendanceObj->attnds_id=$attndsid;
	$emp_monthlyattendanceObj->present=$present;
	$emp_monthlyattendanceObj->absent=$absent;
	$emp_monthlyattendanceObj->month=$month;
	$emp_monthlyattendanceObj->year=$year;
	$emp_monthlyattendanceObj->updateattendance($attndsid,$month,$year);
	$msg=1;
	}
	}
	else
	{
		$error=1;
	}
		}

}

/* Delete  */ 
if(isset($_POST['delsubmit']))
{
	echo $id=sanitize($_POST['del_id']);
	//$emp_positionObj->select($id);
	
	//$emp_positionObj->delete($id);
	
}
 Delete  

?>
<!--Delete Form-->             
<form action="#" name="delete_empabsentform" id="delete_empabsentform" method="POST">

</form>
<!--Delete Form-->

<!-- mobile navigation -->

                        
			<nav id="mobile_navigation"></nav>
			
			<section id="breadcrumbs">
				<div class="container">
					<ul>
						<li><a href="#">Web school </a></li>
                        <li><a href="#">Human Resources</a></li>
                         <li><a href="#">Attendance Management</a></li>
						<li><span>Employee Attendance Report</span></li>						
					</ul>
				</div>
			</section>
            
			<section class="container clearfix main_section">
				<div id="main_content_outer" class="clearfix">
					<div id="main_content">
						<div class="alert alert-info"><strong>Employee Attendance Report</strong> </div>
                         <?php 
						if($setting_error==1)
						{
						?>
                        <div class="warning">Hello ! Please <a href="general_setting.php" />Click Here</a> Complete your settings page. It is very important to fill up your general settings before you start.</div>
                        <?php
						}?>
                        
                        
						<!-- main content -->
                        <?php $emp_departmentlist=$emp_departmentObj->selectorgall();
						if(count($emp_departmentlist)>0)
						{ ?>
        
     
                        
                        <form action="#" method="GET" id="getattendancereport" name="getattendancereport" enctype="multipart/form-data">
                          
						<div class="row">
							<div class="col-sm-6">
								<div class="panel panel-default">
									<!--<div class="panel-heading">
										<h4 class="panel-title">Simple Validation</h4>
									</div>-->
									<div class="panel-body">
                                    
                                    <div class="form_sep">
												<label for="reg_input_name" class="req">Department Name</label>
												<select id="dept" name="dept" class="form-control" data-required="true" >
                                                <option value="" >Select Department</option>
                                                <?php 
												foreach($emp_departmentlist as $deptlist)
												{?>
                                                <option value="<?php echo $deptlist->id ?>" <?php if($deptlist->id==$dept) { ?> selected="selected" <?php } ?> ><?php echo $deptlist->department; ?></option>
                                                <?php } ?>
                                                </select>
											</div> 
											
                                    <div class="form_sep">
												<label for="reg_input_name" class="req">Mode</label>
												<select id="mode" name="mode" onchange="get_individualabsentlist(this.value)" class="form-control" data-required="true" >
                                                <option value="" >Select Mode</option>
                                               
                                                
                                                 <option value="1" <?php if(1==$mode) { ?> selected="selected" <?php } ?>>Yearly</option>
                                                  <option value="2"<?php if(2==$mode) { ?> selected="selected" <?php } ?> >Monthly</option>
                                                   <option value="3"<?php if(3==$mode) { ?> selected="selected" <?php } ?> >Individual</option>
                                               
                                                </select>
											</div> 
									
								</div>
                                
								</div>
							</div>
                           
							<div class="col-sm-6">
								<div class="panel panel-default">
									<!--<div class="panel-heading">
										<h4 class="panel-title">Simple Validation</h4>
									</div>-->
									<div class="panel-body">
                                   
									<div class="form_sep">
                                            <!--<label for="startDate">Date :</label>
                                            <input type="text" name="default_widget" id="default_widget" class="form-control input_validate" />-->
    
												<label for="reg_input_name" class="req">Month & year</label>
												<div class="input-group">
													<input  name="default_widget" id="default_widget" class="form-control" type="text" data-required="true"  value="<?php if($month_year!=''){ echo $month_year; } ?>">
													<span class="input-group-addon"><i class="icon-calendar"></i></span>
												</div>
											</div>  
									<div class="form_sep">
                                    <div id="employee_div">
                                   </div>
                                    </div>
								</div>
                                
								</div>
							</div>		
									
                            
						</div>
						<div class="row">
							<div class="col-sm-4">
                            <div class="">
							<div class="panel-body">
							<div class="form_sep">
							<button class="btn btn-success" type="submit" name="att_submit" id="att_submit" >Go</button>
							</div>
                            </div>
                            </div>
                            </div>
                            <div class="col-sm-4">
                            
							<div class="panel-body">
							<div class="form_sep">
						
							</div>
                            </div>
                          
                            </div>
                            <div class="col-sm-4">
                            <div class="">
							<div class="panel-body">
							<div class="form_sep">
							</div>
                            </div>
                            </div>
                            </div>   
                            
						</div>
						</form>
                        <?php }
						else
						{
						?>
                        <div class="warning">You should add department before adding attendance.Please <a href="emp_department.php">Click Here</a> to add employee leave</div>
                        <?php
						}?>	
						
						
						
						
						<?php $emp_leavelist=$emp_leaveObj->selectorgall(); 
							if(count($emp_leavelist)>0)
							{?>
							<?php 
							if(!$dept=='')
								{
								$emplist=$emp_regObj->selectemployee($dept);
								
								
							 
							if(count($emplist)>0)
							{?>
                            
                        <div style="margin-top:20px;">
                        
						<div class="row">
							<div class="col-sm-12"> 
							<div class="panel panel-default">
                            	
                                
                            		<div class="panel-heading">
										<h4 class="panel-title"> Employee Leave List </h4>
									</div>
							<div class="panel_controls">
										<div class="row">
											<div class="col-sm-4">
												<label for="table_search">Search:</label>
												<input type="text" id="table_search" class="form-control">
											</div>
											
												<div class="col-sm-3 col-xs-6">
												
											</div>
											
											<div class="col-sm-3 col-xs-6">
												Generate Report <a href="examples/exemple07.php" target="_blank" ><img src="<?php echo SITE_URL; ?>img/pdf.png" width="64" height="64" /></a>
											</div>
										</div>
									</div>
                                    <?php if(!$dept=='' && $mode==1)
								{ ?>
                           <table id="resp_table" class="table toggle-square" data-filter="#table_search" data-page-size="40">
										<thead>
											<tr>
												<th data-toggle="true">Employee</th>
                                                <th data-hide="phone,tablet">Register No</th>
                                                <th data-hide="phone,tablet">Job Title</th>
                                                <th >Leave</th>
                                                
											</tr>
										</thead>
										<tbody>
                                        
                                        <?php foreach($emplist as $list)
										{?>
											<tr>
                                            <td><?php echo $list->first_name.' '.$list->mid_name.' '.$list->last_name; ?></td>
                                            <td data-value="<?php //echo $list->department.'_D'; ?>"><?php echo $list->emp_no; ?></td>
                                            <td><?php echo $list->job_title; ?></td>
                                            <td><?php $count=$emp_absentObj->yearlyabsentcount($list->id,$year); echo count($count); ?></td>
                                            
                                            </tr>
                                        <?php } ?>
											
										</tbody>
										<tfoot class="hide-if-no-paging">
											<tr>
												<td colspan="6" class="text-center">
													<ul class="pagination pagination-sm"></ul>
												</td>
											</tr>
										</tfoot>
									</table>
                           <?php } ?>
                              <?php if(!$dept=='' && $mode==2)
								{ ?>
                           <table id="resp_table" class="table toggle-square" data-filter="#table_search" data-page-size="40">
										<thead>
											<tr>
												<th data-toggle="true">Employee</th>
                                                <th data-hide="phone,tablet">Register No</th>
                                                <th data-hide="phone,tablet">Job Title</th>
                                                <th >Leave</th>
                                                
											</tr>
										</thead>
										<tbody>
                                        
                                        <?php foreach($emplist as $list)
										{?>
											<tr>
                                            <td><?php echo $list->first_name.' '.$list->mid_name.' '.$list->last_name; ?></td>
                                            <td data-value="<?php //echo $list->department.'_D'; ?>"><?php echo $list->emp_no; ?></td>
                                            <td><?php echo $list->job_title; ?></td>
                                            <td><?php $count=$emp_absentObj->absentcount($list->id,$month,$year); echo count($count); ?></td>
                                            
                                            </tr>
                                        <?php } ?>
											
										</tbody>
										<tfoot class="hide-if-no-paging">
											<tr>
												<td colspan="6" class="text-center">
													<ul class="pagination pagination-sm"></ul>
												</td>
											</tr>
										</tfoot>
									</table>
                           <?php } ?>
                              <?php if(!$dept=='' && $mode==3)
								{ ?>
                           <table id="resp_table" class="table toggle-square" data-filter="#table_search" data-page-size="40">
										<thead>
											<tr>
												<th data-toggle="true">Employee</th>
                                                <th data-hide="phone,tablet">Register No</th>
                                                <th data-hide="phone,tablet">Job Title</th>
                                                <th data-hide="phone,tablet">Reason</th>
                                                <th data-hide="phone,tablet">Leave Date</th>
                                                <th >FN/AN</th>
                                                 
											</tr>
										</thead>
										<tbody>
                                        
                                        <?php
                                        $leavecountlist=$emp_absentObj->individualabsentcount($empid,$month,$year); 
										foreach($leavecountlist as $list)
										{
											 
										?>
											<tr>
                                            <td><?php  $emp_regObj->select($list->emp_id);  echo $emp_regObj->first_name.' '.$emp_regObj->mid_name.' '.$emp_regObj->last_name; ?></td>
                                            <td data-value="<?php //echo $list->department.'_D'; ?>"><?php $emp_regObj->select($list->emp_id); echo $emp_regObj->emp_no; ?></td>
                                            <td><?php $emp_regObj->select($list->emp_id); echo $emp_regObj->job_title; ?></td>
                                            <td><?php  $emp_leaveObj->select($list->leave_type); echo $emp_leaveObj->leavetype; ?></td>
                                            <td><?php echo date('d-M-Y',strtotime($list->leave_date)); ?></td>
                                           <td><?php if($list->fn==1 && $list->an==0){ echo 'FN';}
										   if($list->fn==0 && $list->an==1){ echo 'AN';}
										   if($list->fn==1 && $list->an==1){ echo 'FN and AN';} ?></td>
                                            
                                            </tr>
                                        <?php } ?>
											
										</tbody>
										<tfoot class="hide-if-no-paging">
											<tr>
												<td colspan="6" class="text-center">
													<ul class="pagination pagination-sm"></ul>
												</td>
											</tr>
										</tfoot>
									</table>
                           <?php } ?>
							</div>
                            </div>
						</div>
						</div>
                        
                        <?php }
						else
						{
						?>
                        <div class="warning">You should add employee before adding attendance.Please <a href="emp_register.php">Click Here</a> to add employees.</div>
                        <?php
						}
								}?>	
						<?php }
						else
						{
						?>
                        <div class="warning">You should add leave before adding attendance.Please <a href="emp_leave.php">Click Here</a> to add employee leave</div>
                        <?php
						}?>	
						</div>
					</div>
				
			</section>
			<div id="footer_space"></div>
		</div>
	<?php include('inc/footer.php'); ?>