<?php
$getdata_emp = $db->get_row ( "employees", array ("email" => $uemail ) );
if ($_POST ['marked']) {
	
	$currentdate = date ( 'Y-m-d' );
	$total = $db->get_row ( "attendence", array ("date" => $currentdate, "e_id" => $getdata_emp ['e_id'] ) );
	
	if ($total) {
		$update = $db->update ( "attendence", array ("firsthalf" => '0.00', 'secondhalf' => '0.00', 't_leave' => '', 'application' => '' ), array ("e_id" => $getdata_emp ['e_id'], 'date' => date ( 'Y-m-d' ) ) );
	} else {
		$insert = $db->insert ( "attendence", array ("e_id" => $getdata_emp ['e_id'], "firsthalf" => '0.00', 'secondhalf' => '0.00', 't_leave' => '', 'application' => '', 'date' => date ( 'Y-m-d' ) ) );
	}

}
?>
<div id="modal" class="modal hide fade in" style="display: none;">

	<div class="modal-header">
		<a class="close"
			href="<?php echo $link->link('employee_attendence',Admin);?>">x</a>


		<div class="modal-body">
			<form class="form-horizontal" method="post" action=""
				enctype="multipart/form-data">

				<div class="control-group">
					<label class="control-label" for="amount">Employee Name</label>
					<div class="controls">
						<input type="text" name="empname"
							value="<?php echo $getdata_emp['e_name'];?>" readonly>
					</div>
					<!-- /controls -->
				</div>
				<!-- /control-group -->

				<div class="control-group">

					<div class="controls">
						<input type="text" name="avin" value="Leave" disabled>
					</div>
					<!-- /controls -->
				</div>
				<!-- /control-group -->

				<div class="control-group" id="date">
					<label class="control-label" for="amount">Date</label>
					<div class="controls">
						<span> <input class="span1 date-picker" id="id-date-picker-1"
							type="text" data-date-format="dd-mm-yyyy" name="date1" />
						</span> <span> <input class="span1 date-picker"
							id="id-date-picker-1" type="text" data-date-format="dd-mm-yyyy"
							name="date2" />
						</span>
						<!-- <input type="text" name="date" id="id-date-range-picker-2" > -->
					</div>
					<!-- /controls -->
				</div>
				<!-- /control-group -->

				<div class="second">
					<div class="control-group">
						<label class="control-label" for="amount">Type Of Leaves</label>
						<div class="controls">
							<select name="t_leave">
								<option value="" selected="selected" disabled="disabled">Type Of
									Leave</option>
								<option value="casual">Casual Leave</option>
								<option value="sick">Sick Leave</option>
								<option value="maternity">Maternity Leave</option>
								<option value="other">Other Leave</option>
							</select>
						</div>
						<!-- /controls -->
					</div>
					<!-- /control-group -->

					<div class="control-group">
						<label class="control-label" for="amount">Range Of Leaves</label>
						<div class="controls">
							<select name="r_leave">
								<option value="" selected="selected" disabled="disabled">Range
									Of Leaves</option>
								<option value="firsthalf">First Half Leave</option>
								<option value="secondhalf">Second Half Leave</option>
								<option value="fullday">Full Day Leave(s)</option>
							</select>
						</div>
						<!-- /controls -->
					</div>
					<!-- /control-group -->

					<div class="control-group">
						<label class="control-label" for="amount">Application</label>
						<div class="controls">
							<input type="file" name="application">
						</div>
						<!-- /controls -->
					</div>
					<!-- /control-group -->

				</div>
				<!-- .second -->
		
		</div>
		<!-- .modal body -->

		<div class="row-fluid">
			<div class="span5"></div>
			<div class="span4">
				<div class="control-group">
					<div class="controls">
						<span class="span12"> <input type="hidden" name="id" value='+id+'>
							<button type="submit" name="submit"
								class="btn btn-small btn-primary">Save</button>
						</span>
					</div>
					<!-- .control -->
				</div>
				<!-- .control group -->
			</div>
		</div>
		</form>
	</div>

</div>
<!-- .example -->
<div class="page-content">
	<div class="page-header position-relative">
		<h1>
			Attendence <small> <i class="icon-double-angle-right"></i> with
				editable events
			</small>
		</h1>
	</div>
	<!--/.page-header-->
	<script>
$(document).ready(function(){
  $("#m_attendence").click(function(){
	 var mark= "attendence";
	 
	 $.ajax({
		    type: "POST", //changed
		    url: "",
		    data: 'marked=' + mark, // changed
		    success: function(){
		    	window.location='';
		        }
		    });
		    
		  
		    return false;
	 
  });
});
</script>


	<div class="row-fluid">

		<div class="span3">
			<?php if(isset($_POST['show'])){?>
				<a class="btn btn-danger btn-small"
				href="<?php echo $link->link('employee_attendence', Admin); ?>">Today</a>
			<?php
			
			} else {
				?>
					<button id="m_attendence" class="btn btn-danger">Mark Attendence</button>
				<?php
			
			}
			?>		
			</div>
		<!-- .span4 -->


		<div class="span7" id="8">
			<form class="form-horizontal" method="post" action="">
				<div class="control-group">
					<label class="control-label">Date Range:</label>
					<div class="controls">
						<div class="row-fluid input-prepend">

							<input class="span6" type="text" name="date[final]"
								id="id-date-range-picker-1"
								placeholder="<?php //echo $date['final']; ?>" /> <span
								class="add-on"> <i class="icon-calendar big"></i>
							</span> <input type="hidden" name="check" value="date">
							<button class="btn btn-small btn-purple" name="show">Show</button>

						</div>
						<!-- .row-fluid input-prepend -->


					</div>
					<!-- /controls -->
				</div>
				<!-- /control-group -->
			</form>
		</div>
		<!-- .span6 -->

		<div class="span2">
			<div class="control-group">

				<div class="controls">
					<p data-toggle="modal" href="#modal" class="btn btn-primary"
						id='edit'>Mark Leave</p>
				</div>
				<!-- /controls -->
			</div>
			<!-- /control-group -->
		</div>
		<!-- .span2 -->

	</div>
	<!-- .rowfluid -->



	<div class="row-fluid">
		<div class="span12">
			<!--PAGE CONTENT BEGINS-->
							<?php
							$query = $db->get_all ( 'employees' );
							if (isset ( $_POST ['show'] )) {
								$date = $_POST ['date'];
								
								$final = explode ( '-', $date ['final'] );
								
								$date ['date1'] = $final [0];
								$date ['date2'] = $final [1];
								
								$date1 = strtotime ( $date ['date1'] );
								
								$date2 = strtotime ( $date ['date2'] );
								
								$time = $db->get_all ( 'attendence', array ("e_id" => $getdata_emp ['e_id'] ) );
								$var = 0;
								foreach ( $time as $row ) {
									$dtime = $row ['date'];
									$datenew = date ( "d-m-Y", strtotime ( $dtime ) );
									$db_time = strtotime ( $datenew );
									
									if ($date1 <= $db_time && $date2 >= $db_time) {
										$select [] = $row;
									} else {
										$warning = "<div class='hide1'>
											<div id='gritter-notice-wrapper'>
											<div id='gritter-item-2' class='gritter-item-wrapper gritter-warning' style=''>
											<div class='gritter-top'></div>
											<div class='gritter-item'>
											<div class='gritter-close' id='gritterremove' style='display: inline;'></div>
											<h5>
											Oops, There Is No Data For Required Date...
											</h5>
											<div style='clear:both'></div>
											</div>
											<div class='gritter-bottom'></div>
											</div>
											</div>
											</div>";
									}
								}
								if ($select == '') {
									echo $warning;
									$query = $db->get_all ( 'employees', array ('e_id' => 0 ) );
								} else {
									$db->order_by = "`id` DESC"; // order by id field
									                             // descending.
									$query = $select;
								
								}
								?>
								
								<table id="example-table1"
				class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th>S.no</th>
						<th>Employee Code</th>
						<th>Department</th>
						<th>Date</th>

						<th>Attendence</th>
						<th>Application</th>
					</tr>
				</thead>

				<tbody>
						<?php
								$num = 1;
								
								foreach ( $query as $row1 ) {
									?>
								<tr>
						<td><?php echo $num;?></td>
						<td><?php echo $getdata_emp['e_code'];?></td>
						<td>
							<?php
									$depart = $db->get_col ( "departments", array ('id' => $getdata_emp ['department'] ), 'name' );
									echo $depart [0];
									?>
						</td>
						<td><?php echo $row1['date'];?></td>
						<td>
										<?php
									if ($row1 ['firsthalf'] == 0.00 && $row1 ['secondhalf'] == 0.00) {
										?>
												<span class="label label-success arrowed">Present</span>
											<?php
									
									} else {
										?>
												<span
							class="label label-large label-pink arrowed-in-right arrowed-in">Absent</span>
											<?php
									
									}
									?>
									</td>
						<td>
						<?php if($row1['application']!=''){?>
					<a
							href="<?php echo  $link->link ( 'downloads', Admin, '?load=' . $row1['application'] . '&path=' . 'uploads/employees/' . $row1['e_id'].'/' );?>">Click
								To See Application</a>
					<?php }else{?>
					<span class="label label-warning">No Application</span>
					<?php }?>
						</td>
					</tr>
							<?php
									$num ++;
								}
								?>	      
				</tbody>
			</table> 
								
								
								
						<?php
							
} else {
								?>
				<br>
			<table id="example"
				class="table table-striped table-bordered table-hover no-margin-bottom no-border-top">
				<thead>
					<tr>
						<th>S.no</th>
						<th>Employee Code</th>
						<th>Department</th>
						<th>Date</th>

						<th>Attendence</th>
					</tr>
				</thead>

				<tbody>
						<?php
								$num = 1;
								$db->order_by = "`id` DESC"; // order by id field
								                             // descending.
								$getattendencedata = $db->get_all ( "attendence", array ("e_id" => $getdata_emp ['e_id'] ) );
								foreach ( $getattendencedata as $row1 ) {
									?>
								<tr>
						<td><?php echo $num;?></td>
						<td><?php echo $getdata_emp['e_code'];?></td>
						<td>
							<?php
									$depart = $db->get_col ( "departments", array ('id' => $getdata_emp ['department'] ), 'name' );
									echo $depart [0];
									?>
						</td>
						<td><?php echo $row1['date'];?></td>
						<td>
										<?php
									if ($row1 ['firsthalf'] == 0.00 && $row1 ['secondhalf'] == 0.00) {
										?>
												<span class="label label-success arrowed">Present</span>
											<?php
									
									} else {
										?>
												<span
							class="label label-large label-pink arrowed-in-right arrowed-in">Absent</span>
											<?php
									
									}
									?>
									</td>
					</tr>
							<?php
									$num ++;
								}
								?>	      
				</tbody>
			</table> 
								<?php
							}
							?>
							<!--PAGE CONTENT ENDS-->
		</div>
		<!--/.span-->
	</div>
	<!--/.row-fluid-->
</div>
<!--/.page-content-->


</div>
<!--/.main-content-->