<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============

$medium_id;
$atd_status_id;

extract($_GET);
$today = date('Y-m-d');
$atd_session;
?>

<style type="text/css">
	.input-append, .input-prepend {
		margin-bottom: 0px !important;
	}
	.table td {
		padding: 3px !important;
	}
</style>


<?php
$det_in = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='$section_id'");

$tch_id = $obj->Details_By_Cond("_r_class_routine","_section_id='$section_id' AND _teacher_type='CT' AND _teacher_id!='0' GROUP BY _teacher_type");
$teacher_id =  $tch_id['_teacher_id'];

$tch_det = $obj->Details_By_Cond("_pf_emp_personal_info","_id='$teacher_id'");
?>

<div class="row-fluid">
<legend>
	Daily Class Wise Attendance Sheet for (<?php echo 'Class: '.$det_in['class'].', Department: '.$det_in['department'].', Student Type: '.$det_in['student_type'].', Section: '.$det_in['section']; ?>).
</legend>
</div>


<!-- ==================================== -->
<form class="form-vertical" id="attendance_sheet">

<div class="row-fluid">	
	<input type="hidden" name="module_id" id="module_id" value="<?php echo $module_id; ?>">
	<input type="hidden" name="atd_status_id" value="<?php echo $atd_status_id; ?>">
	<input type="hidden" name="shift_id" value="<?php echo $shift_id; ?>">
    <input type="hidden" name="medium_id" value="<?php echo $medium_id; ?>">
	<input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
    <input type="hidden" name="shipt_name" value="<?php echo $shipt_name; ?>">	
    <input type="hidden" name="medium_name" value="<?php echo $medium_name; ?>">						
    <input type="hidden" name="atd_session" value="<?php echo $atd_session; ?>">						
	
    
	<table class="table table-striped table-bordered table-hover table-final-view">
		<thead>
			<th>Sl</th>
			<th>Student ID</th>
			<th>Name</th>
			<th>Roll</th>
			<th style="text-align: center;">Present</th>
			<th style="text-align: center;">Absent</th>
			<th style="text-align: center;">Late</th>
			<th style="text-align: center;">Late Time</th>
			<th style="text-align: center;">Leave</th>
			<th style="text-align: center;">Comments</th>
			<th>Contact No</th>
		</thead>
		<tbody>
			<?php
			$i=0;
			foreach($obj->View_Student_List("sc._id='$section_id' AND pf_std._status='A' AND bsf._session='$atd_session' ORDER BY _section_roll ASC") as $value){
			extract($value);
			$i++;	
			
			
			$student_id = $_id;
			 $where_con = '_section_id='.$section_id.' AND _std_id='.$student_id.' AND DATE(_entry_date)="'.$today.'"';
			 
			 $std_class = $obj->Details_By_Cond("_atd_daily_by_class",$where_con);
			 
			$_late_time = 0;
			$_comments = '';
			$present_status = '';
			$absent_status = '';
			$late_status = '';
			$leave_status = '';
			if(!empty($std_class)){
				
				$_comments = $std_class['_comments'];
				$_late_time = $std_class['_late_time'] ;	
				if($_late_time ==0 || $_late_time ==''){
					 $_late_time = 0;
					}else{
						$_late_time = $std_class['_late_time'];
						
						}
				
				 $_atd_status = $std_class['_atd_status'];		
				$present_status = $_atd_status=='P' ? 'checked' : '';
				$absent_status = $_atd_status=='A' ? 'checked' : '';
				$late_status = $_atd_status=='LA' ? 'checked' : '';
				$leave_status = $_atd_status=='LE' ? 'checked' : '';
				
				}
			
			
			
			?>	
			<tr class="std_row">
				<td class="td_pad">
					<input type="hidden" name="std_id_<?php echo $i; ?>" value="<?php echo $value['_id']; ?>">
                     <input type="hidden" class="late-time-hidden" value="<?php echo $_late_time ?>" name="late-time-hidden" />
					<?php echo $i; ?>
				</td>
				<td class="td_pad"><?php echo $value['_uniq_id']; ?></td>
				<td class="td_pad"><?php echo $value['_full_name']; if(!empty($value['_nick_name'])){echo ' ('.$value['_nick_name'].')'; } ?></td>
				<td class="td_pad"><?php echo $value['_section_roll']; ?></td>
					<td class="td_pad" style="text-align: center;"><label><input type="radio" id="atd_status_<?php echo $i; ?>" name="atd_status_<?php echo $i; ?>" value="P" <?php echo $present_status; ?>   /><span class="lbl"></span></label></td>
				<td class="td_pad" style="text-align: center;"><label><input type="radio" id="atd_status_<?php echo $i; ?>" name="atd_status_<?php echo $i; ?>" value="A" <?php echo $absent_status; ?>  /><span class="lbl"></span></label> </td>
				<td class="td_pad" style="text-align: center;"><label><input type="radio" id="atd_status_<?php echo $i; ?>" name="atd_status_<?php echo $i; ?>" value="LA" <?php echo $late_status; ?>  /><span class="lbl"></span></label></td>
				<td class="td_pad" style="text-align: center;">
					<input name="late_time_<?php echo $i; ?>" type="text" class="input-mini spinner1 late_time" value="0" />
                    
				</td>
				<td class="td_pad" style="text-align: center;"><label><input type="radio" name="atd_status_<?php echo $i; ?>" value="LE" <?php echo $leave_status; ?>   /> <span class="lbl"></span></label></td>
				<td class="td_pad" style="text-align: center;"><input name="comments_<?php echo $i; ?>" class="input-small" type="text" placeholder="Comments" value="<?php echo $_comments ; ?>" /> </td>
				<td class="td_pad"><?php echo $value['_contact_mobile']; ?></td>
			</tr>	
			<?php
			}
			$total_row = $obj->View_Student_List_rows("sc._id='$section_id' AND pf_std._status='A' AND bsf._session='$atd_session'");
			?>
			
		</tbody>	

	</table>
    <input type="hidden" name="total_row" value="<?php echo $total_row; ?>">
</div>

<!-- ========================== -->

<div class="row-fluid">	
	<div class="span5"><b>Class Teacher name:</b> <?php echo $tch_det['_full_name']; if(!empty($tch_det['_nick_name'])){ echo ' ('.$tch_det['_nick_name'].')';} ?></div>

	<div class="span3"><b>Mobile No:</b> <?php echo $tch_det['_contact_mobile_no']; ?></div>
</div>

<!-- ========================= -->
<?php 
$table_name="_atd_daily_status_class";
$where_cond='_section_id = '.$section_id.' && DATE(_entry_date) = "'.$today.'" ';
$com=$obj->Details_By_Cond($table_name,$where_cond);

?>
<div class="row-fluid">	
	<div class="span3">Attendance Time
		<div class="controls">
			<div class="input-append bootstrap-timepicker">
				<input name="atd_time" type="text" class="input-small timepicker1" value="<?php echo $com['_atd_time']; ?>" />
				<span class="add-on">
					<i class="icon-time"></i>
				</span>
			</div>
		</div>
	</div>

	<div class="span3">Comments
		<div class="controls">
			<input name="remarks" class="input-large" type="text" placeholder="Comments" value="<?php echo $com['_comments']; ?>">
		</div>
	</div>
</div>


<!-- =========================== -->

<div class="row-fluid">									
	<div class="span12">			
		<div class="control-group">
			<label class="control-label" for="form-field-1">&nbsp;</label>

			<div class="controls">
				<?php if($total_row){ ?>
				<button class="btn btn-small btn-success" type="button" id="btn_update_atdsheet">
					<i class="icon-ok bigger-100"></i>Save
				</button>
				<?php
				}
				else{
				?>
				<button class="btn btn-small btn-success" type="button" id="btn_update_atdsheet">
					<i class="icon-ok bigger-100"></i>Save
				</button>
				<?php } ?>

				&nbsp; &nbsp; &nbsp;
				<!--<button class="btn btn-small btn-danger" type="reset" id="btn_reset">
					<i class="icon-undo bigger-100"></i>
					Reset
				</button>-->
			</div>
		</div>
	</div>
	
</div>


</form>


<script type="text/javascript">
$('.spinner1').ace_spinner({value:0,min:0,max:80,step:1, btn_up_class:'btn-info' , btn_down_class:'btn-info'})
.on('change', function(){
});



$('.timepicker1').timepicker({
	minuteStep: 1,
	showSeconds: false,
	showMeridian: false
})

$(".table-final-view tbody tr").each(function(){
		 $this = $(this); 
		 var check = $this.find(".late-time-hidden").length;
		 if (check > 0) {
				var late_time_hidden = $(this).find(".late-time-hidden").val();
			 	
		 		$this.find(".late_time").val(late_time_hidden); 
			 }
		 
	});


$("#btn_update_atdsheet").click(function(){
	var module_id = $('#module_id').val();
	var atd_status_id = $('input[name="atd_status_id"]').val();	
	var atd_session = $('input[name="atd_session"]').val();	
	var shift_id = $('input[name="shift_id"]').val();
 	var medium_id = $('input[name="medium_id"]').val();
	var section_id = $('input[name="section_id"]').val();
	var shipt_name = $('input[name="shipt_name"]').val();
	var medium_name = $('input[name="medium_name"]').val();
	var remarks = $('input[name="remarks"]').val();
	
	
	var atd_time = $('input[name="atd_time"]').val();
	var total_row = $('input[name="total_row"]').val();
	var operation = 'save_atd';
	var operation2 = 'up_status';

	//var rs = false;
    
	
	
	for (i = 1; i <= total_row; i++) {

		var std_id = $('input[name="std_id_'+i+'"]').val();
		var atd_status = $('input:radio[name="atd_status_'+i+'"]:checked').val();
		var late_time = $('input[name="late_time_'+i+'"]').val();
		
		var comments = $('input[name="comments_'+i+'"]').val();

		var dataStr="operation="+operation+"&section_id="+section_id+"&std_id="+std_id+"&atd_status="+atd_status+"&late_time="+late_time+"&comments="+comments;

		$.ajax({
			type:"post",
			url:"module/attendance/update_atd_data_class.php",
			data:dataStr ,
			success:function(result){}
		});
	}
    
	var dataStr2="operation2="+operation2+"&atd_status_id="+atd_status_id+"&atd_time="+atd_time+"&remarks="+remarks;
	var dataStr3="module_id="+module_id+"&shift_id="+shift_id+"&medium_id="+medium_id+"&atd_status_id="+atd_status_id+"&shipt_name="+shipt_name+"&medium_name="+medium_name+"&atd_session="+atd_session;
	
	$.ajax({
		type:"post",
		url:"module/attendance/update_atd_data_class.php",
		data:dataStr2 ,
		success:function(st){
			$(".data_to_show_atd").load("module/attendance/attendence_final_view.php",dataStr3);

			$('#first_part').remove();
			$('#second_part').css("display","block");
		}
	});
	
	
			
   
			

});



// =================================



$("#btn_save_atd").click(function(){

	var atd_status_id = $('input[name="atd_status_id"]').val();
	
	var medium_id = $('input[name="medium_id"]').val();
	var remarks = $('input[name="remarks"]').val();
	var atd_time = $('input[name="atd_time"]').val();
	var shipt_name = $('input[name="shipt_name"]').val();
	var medium_name = $('input[name="medium_name"]').val();
	var operation2 = 'up_status';

	var dataStr4="operation2="+operation2+"&atd_status_id="+atd_status_id+"&atd_time="+atd_time+"&remarks="+remarks;

	var dataStr5="medium_id="+medium_id;


	$.ajax({
		type:"post",
		url:"module/attendance/update_atd_data_class.php",
		data:dataStr4 ,
		success:function(st){
			//alert(st);
		//	$(".data_to_show_atd").load("module/attendance/daily_attendance_class_status_view.php",dataStr5);

			$('#first_part').remove();
			$('#second_part').css("display","block");
		}
	});

});
</script>