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

extract($_GET);

$medium_id;
$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>

<div id="view-part">

    <div class="row-fluid">
        <legend class="final-legend">
            <strong>
            <!-- <button class="btn btn-mini btn-danger" id="back_a">
				<i class="icon-reply icon-only" id="back_a"></i>
				  Back
			</button> -->
			<a href="#" id="back_a" >
			<img src="asset/custom/back.png" id="back" >
			</a>
            View and Final Approve (Shift: <?php  echo $shipt_name; ?>, Medium: <?php  echo $medium_name; ?>) </strong>
        </legend>
    	<input type="hidden" id="module_id" name="module_id" value="<?php echo $module_id;?>"/>
        <input type="hidden" id="medium_id" name="medium_id" value="<?php echo $medium_id;?>"/>
        <input type="hidden" id="shift_id" name="shift_id" value="<?php echo $shift_id;?>"/>
    </div>

<?php 
$all_section = '';
$branch_id = isset($_SESSION['LOGIN_BRANCH']) ? $_SESSION['LOGIN_BRANCH']: '';	
$table_name=" _int_institute_setup_vw";
$column="*";
$where_cond='branch_id ='.$branch_id .' AND shift_id ='.$shift_id .' AND medium_id ='.$medium_id ;
$all_sec=$obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);
//	print_r($all_sec);
if(count($all_sec)>0){
	foreach($all_sec as $sc ){
		if(isset($sc['section_id'])){
			$all_section .= ', '. $sc['section_id'];
		}
	}
}

if($all_section){
		$all_section = trim($all_section, ',');
	}


	$table_name=" _atd_daily_status_class";
	$column="*";
	$where_cond='_atd_status = 1 && _section_id IN ('.$all_section.') && DATE(_entry_date) = "'.$today.'" ';
	if($all_section)
	$all_section_id=$obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);
	
	$j = 1;
	$total_student = 0;
	$for_total_absent = 0;
	$for_total_late = 0;
	$for_total_leave = 0;
	$k = 0;
	$show_approve_all = 0;
if(!empty($all_section_id)){
		foreach($all_section_id as $value ){
			
		
		$section_id = $value['_section_id'];
		$status_comments = $value['_comments'];
		$_atd_time = $value['_atd_time'];
		$approved_status = $value['_approve_satus'];
		$atd_status_id = $value['_id'];
		
		if($approved_status==0){
			if($k){
				$show_approve_all = 1;	
			}
			$k++ ;
		}
			 				 
		$table_name="_int_institute_setup_vw iv LEFT JOIN _atd_daily_status_class hs ON hs._section_id = iv.section_id";
		$column="iv.section,iv.section,iv.student_type,iv.class,iv.department";
		$where_cond='_atd_status = 1 && iv.section_id = '.$section_id.' && DATE(_entry_date) = "'.$today.'" order by TIME(_entry_date) ASC limit 1 ';
		 
		$list = $obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);
		
		
				$table_name1 = '_pf_std_basic_info';
				$where_cond1 = '_section_id='.$section_id." AND _session='".$atd_session."'";
				$total_row = $obj->View_All_By_Cond($table_name1,$where_cond1 ); 
?>

 <div class="row-fluid homework_conformation" id="attendance_sheet" >   
		 <form class="form-vertical " id="" >

<div class="span 12 alert alert-success inline no-margin">

<b><?php echo 'Class: '?></b><?php echo $list[0]['class'].', ';?>
<b><?php echo 'Department: '?></b><?php echo $list[0]['department'].', ';?>
<b><?php echo 'Student Type: '?></b><?php echo $list[0]['student_type'].', ';?>
<b><?php echo 'Section: '?></b><?php echo $list[0]['section'];?>

</div>


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


<div class="row-fluid">	
					
	
	<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;">Status</th>
             <th style="text-align: center;">Late Time</th>
			<th style="text-align: center;">Comments</th>
           
			<th>Contact No</th>
		</thead>
		<tbody>
			<?php
			$i=0;
			$p = 0 ;
			$ab= 0;
			$la = 0;
			$le= 0;
			
			$where_cond = 'sc._id='.$section_id.' AND pf_std._status!="D" &&  DATE(hmc._entry_date) ="'.$today.'" AND bsf._session="'.$atd_session.'"  ORDER BY hmc._atd_status,pf_std._section_roll  ASC';
	
			foreach($obj->View_Student_List_atttd_with_comment($where_cond) as $value){
			
			extract($value);
			
			 $_late_time = $value['_late_time'] ;
			 
			 
			if($_atd_status=='P'){
				$atd_status = 'Present';
				
			}
			elseif( $_atd_status=='A')
			{
				$atd_status = 'Absent';
				$ab = $ab +1 ;
				
				
			}elseif($_atd_status=='LA'){
				$atd_status = 'Late';
				$la = $la +1 ;
				
			
			}elseif($_atd_status=='LE'){
				$atd_status = 'Leave';
				$le = $le +1 ;
				
			}
			
			 
			
			$i++;	
			?>	
			<tr>
				<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" />
					<?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;"> <?php echo $atd_status; ?> </td>
				<td class="td_pad" style="text-align: center;">	 <?php if($atd_status=='Late') echo $_late_time ; else echo 0 ; ?> </td>
				
				<td class="td_pad" style="text-align: center;"><?php echo $_comments ; ?></td>
				<td class="td_pad"><?php echo $value['_contact_mobile']; ?></td>
			</tr>	
			<?php
			}
			$selected_student = $i;
			$total_row = $obj->View_Student_List_rows("sc._id='$section_id' AND pf_std._status='A' AND bsf._session='$atd_session'");
			
			$present = $total_row-($ab+$la +$le) ;
			$prsent_persentage = round(($present * 100)/$total_row, 1);
			
			
			$sing_ab_persentage = round(($ab * 100)/$total_row, 1);
			$sing_la_persentage = round(($la * 100)/$total_row, 1);
			$single_le_persentage = round(($le * 100)/$total_row, 1);
			
			
			/// count at every loop, this is for counting total info of all
			$for_total_absent = $for_total_absent + $ab  ;
			$for_total_late = $for_total_late + $la  ;
			$for_total_leave = $for_total_leave + $le  ;
			$total_student = $total_student + $total_row ;
			
			?>
            	
                
                 <tr>
                      
                        <td colspan="8" rowspan="1" style="text-align:center; vertical-align:middle; padding-bottom:5px !important; ">
                         <b>Total Stuednt: </b> <?php echo $total_row.", "; ?>   <b> Preseant:</b>  <?php echo $total_row-($ab+$la +$le)." ( ".$prsent_persentage."%), " ; ?> &nbsp; &nbsp;  <b>Absent: </b><?php echo $ab." ( ".$sing_ab_persentage."%), "; ?>    &nbsp; &nbsp; <b> Late: </b> <?php echo $la." ( ".$sing_la_persentage."%), "; ?>  &nbsp; &nbsp;  <b>Leave: </b> <?php echo $le." ( ".$single_le_persentage."%) " ; ?>
                          
                          
                          
                            
                      </td>
                      
                          
                </tr>
                
                        
			
		</tbody>	

	</table>
</div>

<!-- ========================== -->
	<?php 
			
			$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">	
				<div class="span3"><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="span2"><b>Mobile No:</b> <?php echo $tch_det['_contact_mobile_no']; ?></div>
                <div class="span3"><b>Attendence Time :</b> <?php  echo date("H:i", strtotime($_atd_time)); ?> </div>
             
                <div class="span4"><b>Comments: </b>
                        <input name="remarks" class="input-large" type="text" placeholder="Comments" style="display:none" value="<?php  echo $status_comments;  ?>" />
                        <?php  echo $status_comments;  ?>
                        
                    
                </div>
                 
                
                
                
			</div>


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

<div class="row-fluid">									
				<div class="span12">			
					<div class="control-group">
						<label class="control-label" for="form-field-1">&nbsp;</label>
						<?php 
						$section = 'section_id'.$j;
						$medium = 'medium_id'.$j;
						?>
                        
                        <input type="hidden" class="medium_id <?php echo $medium ; ?> " name="medium_id" value="<?php echo $medium_id; ?>">
						<input type="hidden" class="section_id <?php echo $section ; ?>" name="section_id" value="<?php echo $section_id; ?>">		
                        
						<div class="controls">
							
							
							
                            
			
							&nbsp; &nbsp; &nbsp;
                           <?php 
						   if($_SESSION['edit']=='Y'){
						   
								   if($approved_status !=1){ ?>  
										<button class="btn btn-small btn-success btn_save_final_view apr" type="button" >
											<i class="icon-ok bigger-100"></i>
												Send SMs
										</button> 
										<button class="btn btn-small btn-success btn_final_save_view sve" type="button" >
											<i class="icon-ok bigger-100"></i>
												Save
										</button> 
										<button class="btn btn-small btn-warning btn_reset " type="reset" onclick="attendence_edit_comment('<?php echo $module_id; ?>','<?php echo $shift_id; ?>','<?php echo $section_id; ?>','<?php echo $medium_id; ?>','<?php echo $medium_name; ?>','<?php echo $shipt_name; ?>','<?php echo $atd_status_id; ?>','<?php echo $atd_session;?>'  )" >
											<i class="icon-edit bigger-100"></i>
											Edit
										</button>
								   <?php }else{ ?>
											<button class="btn btn-small btn-success" type="button" >
											<i class="icon-ok bigger-100"></i>
												SMS Sended
											</button>
								   
								   <?php } 
						   
						   }
						   ?>
						</div>
					</div>
				</div>
				
			</div>


</form>


<?php 
		 }
		echo '</div>'; 
	}
  
  
?>

<?php 
if(!empty($all_section_id)){
?>

<div class="row-fluid">
<legend>
    <strong>Summary  : </strong>
</legend>
</div>
<div class="row-fluid">	
					
	
	<table class="table table-striped table-bordered table-hover table-final-view">
		<thead>
			<th>Total Student </th>
			<th>Preseant</th>
			<th>Absent</th>
			<th>Late</th>
			<th style="text-align: center;">Leave</th>
		</thead>
		<tbody>
			<?php
			
			
			
			
			$total_present = $total_student-($for_total_absent+$for_total_late +$for_total_leave) ;
			$prsent_persentage = round(($total_present * 100)/$total_student, 1);
			$absent_persentage = round(($for_total_absent * 100)/$total_student, 1);
			$late_persentage = round(($for_total_late * 100)/$total_student, 1);
			$leave_persentage = round(($for_total_leave * 100)/$total_student, 1);
			?>	
			<tr>
				
				<td class="td_pad"><?php echo $total_student; ?></td>
				<td class="td_pad"><?php echo $total_present." ( ".$prsent_persentage.'%)'; ?></td>
				<td class="td_pad"><?php echo $for_total_absent." ( ".$absent_persentage.'%)'; ?></td>
			
				<td class="td_pad" style="text-align: center;"> <?php echo $for_total_late." ( ".$late_persentage.'%)'; ?> </td>
				<td class="td_pad" style="text-align: center;">	 <?php echo $for_total_leave." ( ".$leave_persentage.'%)' ?> </td>
				
				
			</tr>
            
		</tbody>	

	</table>
</div>

<?php 
if($_SESSION['edit']=='Y'){
	// if($show_approve_all==1){
?>
<div class="row-fluid">
	<div class="span3">
	</div>
	<div class="span3">			
           <div class="control-group">
                    <div class="controls" style="text-align:center">
                        <button class="btn btn-small btn-success" type="button" id="btn_save_all_value">
                            <i class="icon-ok bigger-100"></i>Send SMS
                        </button>
                	</div>
            </div>
    </div>
	
	<div class="span3">			
			<div class="control-group">
                    <div class="controls" style="text-align:center">
                        <button class="btn btn-small btn-success" type="button" id="btn_save_all">
                            <i class="icon-ok bigger-100"></i>All Save
                        </button>
                	</div>
            </div>
    </div>
	<div class="span3">
	</div>
</div>
<?php 
//	}
}
	}else{
		    echo 'There is no students available at Medium: '.$medium_name.' for sending homewrok' ;
	} 
?>




</div>

<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(){
});
 

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


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

$(".btn_save_final_view").click(function(){
	
	var section_id = $(this).parent().parent().find(".section_id ").val();
	var module_id = $('#module_id').val();
	$this = $(this);
	$this.addClass('disabled');
	$this.html('<i class=" icon-exchange  bigger-100"></i>Processing...'); 
	if(section_id){
		var dtstr = "section_id="+section_id+"&module_id="+module_id ;
		$.ajax({
			   type: "POST",		   
			   url: "module/attendance/attendence_class_approve_single.php?"+dtstr,		   
			   success: function(result){
					$this.parent().find(".btn_reset").remove();
					$this.parent().find(".sve").remove();
					$this.removeClass('disabled');
					$this.html('<i class="icon-ok bigger-100"></i>SMS Sended'); 
					$this.unbind( "click" );
				},
				error : function(result){
			  	
				}			   	
		});
	}

});
//===============
$(".btn_final_save_view").click(function(){
	
	var section_id = $(this).parent().parent().find(".section_id ").val();
	var module_id = $('#module_id').val();
	$this = $(this);
	$this.addClass('disabled');
	$this.html('<i class=" icon-exchange  bigger-100"></i>Processing...'); 
	if(section_id){
		var dtstr = "section_id="+section_id+"&module_id="+module_id ;
		$.ajax({
			   type: "POST",		   
			   url: "module/attendance/save_approved_class.php?"+dtstr,		   
			   success: function(result){
					$this.parent().find(".btn_reset").remove();
					$this.parent().find(".apr").remove();
					$this.removeClass('disabled');
					$this.html('<i class="icon-ok bigger-100"></i>Saved'); 
					$this.unbind( "click" );
				},
				error : function(result){
			  	
				}			   	
		});
	}

});
//===================

$("#btn_save_all_value").click(function(){
	$this = $(this) ;
	var module_id = $('#module_id').val();
	var data = {"module_id":module_id,"AllSection":[]};	
	var allform = $(".homework_conformation");
	$(allform).each(function(){
		var section_id = $(this).find(".section_id ").val();
		products = {"section_id":section_id};
		data.AllSection.push(products);
	});
	var p = JSON.stringify(data);
	var intIndexOfMatch = p.indexOf( "&" );		
	while (intIndexOfMatch != -1){
		p = p.replace("&", "%26");
		intIndexOfMatch = p.indexOf( "&" );
	}	
	
	$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: "module/attendance/attendence_class_approve_all.php",
		   data: "p="+p,	   
		  success: function(result){
			
				/*$(allform).each(function(){
					$this.parent().find(".btn_reset").remove(); 
					$this.html('<i class="icon-ok bigger-100"></i>Approved'); 
					$this.unbind( "click" );
				});*/
				$(".btn_save_final_view").html('<i class="icon-ok bigger-100"></i>Sms Sended'); 
				$(".btn_final_save_view").html('<i class="icon-ok bigger-100"></i>Saved'); 
				
				$(".btn_save_final_view").unbind( "click" );
				$(".btn_final_save_view").unbind( "click" );
				$("#btn_save_all").unbind( "click" );
				$this.unbind( "click" );$(".btn_reset").remove(); 
				alert(result.msg);
		  },
		  error : function(result){
		  
		  }			   	
	});
});
//==============================
$("#btn_save_all").click(function(){
	$this = $(this) ;
	var module_id = $('#module_id').val();
	var data = {"module_id":module_id,"AllSection":[]};	
	var allform = $(".homework_conformation");
	$(allform).each(function(){
		var section_id = $(this).find(".section_id ").val();
		products = {"section_id":section_id};
		data.AllSection.push(products);
	});
	var p = JSON.stringify(data);
	var intIndexOfMatch = p.indexOf( "&" );		
	while (intIndexOfMatch != -1){
		p = p.replace("&", "%26");
		intIndexOfMatch = p.indexOf( "&" );
	}	
	
	$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: "module/attendance/attendance_save_class_all.php",
		   data: "p="+p,	   
		  success: function(result){
			
				/*$(allform).each(function(){
					$this.parent().find(".btn_reset").remove(); 
					$this.html('<i class="icon-ok bigger-100"></i>Approved'); 
					$this.unbind( "click" );
				});*/
				$(".btn_save_final_view").html('<i class="icon-ok bigger-100"></i>Sms Sended'); 
				$(".btn_final_save_view").html('<i class="icon-ok bigger-100"></i>Saved'); 
				
				$(".btn_save_final_view").unbind( "click" );
				$(".btn_final_save_view").unbind( "click" );
				$("#btn_save_all_value").unbind( "click" );
				$this.unbind( "click" );
				$(".btn_reset").remove(); 
				alert(result.msg);
		  },
		  error : function(result){
		  
		  }			   	
	});
});

///attendance_save_class_all
function attendence_edit_comment(module_id,shift_id,sc_id,me_id,me_name,ship_name,atd_status_id,atd_session){
	var datastr="module_id="+module_id+"&shift_id="+shift_id+"&section_id="+sc_id+"&medium_id="+me_id+"&medium_name="+me_name+"&shipt_name="+ship_name+"&atd_status_id="+atd_status_id+"&atd_session="+atd_session;
	 $("#view-part").load("module/attendance/edit_class_attendence.php",datastr);
	}

</script>

<script type="text/javascript">
    $('#back_a').click(function(){

    $("#main_content_div").load("module/attendance/attendance_approve_choose_medium.php");

    });
</script>



