<script type="text/javascript" src="js/tableExport.js"></script>

<script type="text/javascript" src="js/jquery.base64.js"></script>

<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 100% !important;overflow: inherit !important;} 
.dynamic{display:none}
table td {
padding: 6px !important;
}
legend{line-height: 16px;font-weight:100;} 

.outer .MigrateLeftFee{
  background-color: #DADADA !important;
  border: 5px solid #181616 !important;
  min-height: 440px;
  min-width:700px;
}
.st {
	width: 55px;
	font-size:10px;
	display: inline-block;
	color: #021EF0;
}
.size{
	font-size:12px;
}
.recepit {
	width: 717px !important;
	height: 344px !important;
}
.total{
	color: darkgreen;
    font-weight: 800;
}
</style>

<?php
$pgMod="overall_class_report";
$pgAct="view";

if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
if($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("student","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Attendance", "page_id"=>$pgMod)));
}
if(isset($_SESSION['msg']) && $_SESSION['msg']!=""){
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0){
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
	?>
    <form method="get" action="">
    <input type="hidden" name="src" value="Attendance" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
                    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">

                    <div style="float:left;margin:15px;">
                        
                        <input type="date" name="date" value="<?php if($_GET['date']!=""){echo $_GET['date']; } else { echo date("Y-m-d"); } ?>" />

						<button type="submit" name="attendance_CLASS" class="btn-classic margin">SINGLE DAY OVERALL CLASS</button>

                    </div>
                    <br><br><br>

                    </div>
                    
				</div>
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
	
	if(isset($_GET["attendance_CLASS"])){
// 		if(isset($_GET["fyear"]) && trim($_GET["fyear"])!="" && isset($_GET["fmonth"]) && trim($_GET["fmonth"])!="" && isset($_GET["fdate"]) && trim($_GET["fdate"])!="" && isset($_GET["tyear"]) && trim($_GET["tyear"])!="" && isset($_GET["tmonth"]) && trim($_GET["tmonth"])!="" && isset($_GET["tdate"]) && trim($_GET["tdate"])!=""){
// 			$fdate=$_GET["fyear"]."-".$_GET["fmonth"]."-".$_GET["fdate"];
// 			$tdate=$_GET["tyear"]."-".$_GET["tmonth"]."-".$_GET["tdate"];
// 			$where[]=" (date between '$fdate' and '$tdate') ";
// 			$conwhere.="and (r.date between '$fdate' and '$tdate')";
// 		}
		if(isset($_GET["stype"]) && trim($_GET["stype"])!=""){
			$where[]=" s.stype='".$_GET["stype"]."' ";
			$conwhere.=" and student.stype='".$_GET["stype"]."' ";
		}
		if(isset($_GET["medium"]) && trim($_GET["medium"])!=""){
			$where[]=" s.medium='".$_GET["medium"]."' ";
			$conwhere.=" and student.medium='".$_GET["medium"]."' ";
		}
		if(isset($_GET["class"]) && trim($_GET["class"])!=""){
			$where[]=" s.class='".$_GET["class"]."' ";
			$conwhere.=" and student.class='".$_GET["class"]."' ";
		}
		if(isset($_GET["section"]) && $_GET["section"]!=""){
			$where[]=" s.section='".$_GET["section"]."' ";
			$conwhere.=" and student.section='".$_GET["section"]."' ";
		}
		if(isset($_GET["gender"]) && trim($_GET["gender"])!=""){
			$where[]=" s.gender='".$_GET["gender"]."' ";
			$conwhere.=" and student.gender='".$_GET["gender"]."' ";
		}
		if(isset($_GET["cast"]) && trim($_GET["cast"])!=""){
			$where[]=" s.cast='".$_GET["cast"]."' ";
			$conwhere.="   student.cast='".$_GET["cast"]."' ";
		}
		if(isset($_GET["startage"]) && trim($_GET["startage"])!="" && isset($_GET["endage"]) && trim($_GET["endage"])!=""){
			$start = date('Y-m-d', strtotime('-'.$_GET["startage"].' years'));
			$end = date('Y-m-d', strtotime('-'.$_GET["endage"].' years'));
			$where[]=" (s.dob between '".$end."' and '".$start."')";
			$conwhere.=" and (student.dob between '".$end."' and '".$start."') ";
		}
		if(isset($_GET["location"]) && trim($_GET["location"])!=""){
			$where[]=" s.location='".$_GET["location"]."' ";
			$conwhere.=" and student.location='".$_GET["location"]."' ";
		}
 
 
		$where[]=" s.session='".$_SESSION["admin"]["session"]."' and s.status=1 ";
		$table=" student as s";
		//echo "<pre>";print_r($result);echo "</pre>";echo $db->getLastQuery();		echo $db->getErMsg();
		?>
		 <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Detail</h3>
                <div style="float:right; margin-top:5px">
				<!--<input  class="btn btn-primary" type="button"  onclick="$('.table').tableExport({type:'excel',escape:'false'});" id="btnExport" value=" Export To Excel " />-->
                    <button class="btn btn-success" onclick="exportTableToExcel('tblData', 'Attendance Detail')" style="margin-left:35px;">Excel</button>
                    <button type="button" onclick="printDiv('printDiv2')" class="btn btn-primary">Print</button>
                </div>
            </div>
            <div class="well no-padding" id="printDiv2">
                
                <h4 style="text-align:center;color:crimson;">Single Day Overall Class Attendance Report</h4>
                <h5 style="float:right;margin:0px">Date: <?php echo date('d-M-Y', strtotime($_GET['date'])); ?></h5>

            	<table class="table" id="tblData" border="1" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Class</th>
                             <th>Section</th>
                             <th>Total No of Student</th>
                            <th>Total Absent</th>
                             <th>Total present</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                    	<?php
                       $cla= $db->getRows("select class from class");	

	$i=0;$total_tution=0;$total_student=0;$tf=0;
	foreach($cla as $cl){
	
	    $c= $cl["class"]; 
		 $sec= $db->getRows("select section from student where class='". $c."' and session='".$_SESSION["admin"]["session"]."' group by section");
		 
		 
		 foreach($sec as $sect){$i++;
			
			$section=$sect['section'];
			if(!empty($section)){
			 $where_section=" and section='".$section."' ";

		$total_student= $db->getVal("SELECT count(idno) FROM student WHERE class='".$c."' $where_section AND school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and issue_TC=0 and status=1 ");
		  ?>
                            <tr>
							     <td><?= $i;  ?></td>
							     <td><?=  $c; ?></td>
							     <td><?=  $section; ?></td>
								 <td><?= $total_student;  ?></td>
								  <?php  $stu_sib=$db->getRows("select * from attandenc as r INNER join student as s on s.idno=r.idno where s.class='$c' and s.section='$section' and date='$_GET[date]' $conwhere and r.type='2'   ") ;

                                          $ijk=0;
                                          	foreach($stu_sib as $rows){  $ijk++;
                                             $total+= $ijk;
                                          	}
                                         ?>
                                       <td><?php echo  $ijk; ?></td>   
								  <td><?php echo  $total_student-$ijk;  ?></td>

                                <td>
                                    <?php
                                    $session = $_SESSION["admin"]["session"];
                                    $row_id = $db->getVal("select id from attandenc where class='$c' and section = '$section' and date='$_GET[date]' and type='3' and session = '$session' and status='0' ");
                                    if ($ijk > 0 || $row_id != ''){
                                        echo "Marked";
                                    }else{
                                        echo "Not Marked";
                                    }
                                    ?>
                                </td>
							</tr>
		 <?php
		 $all_total_student+=$total_student; 
			$all_absent+=$ijk;
				$all_present+=$total_student-$ijk;
			    
			    
			}} unset($section);
	unset($where_section);
	
	} ?>
                    </tbody>
                    <tfoot>
                    	<tr>
                        	<th colspan="3">Total</th>
                        	<th col><?php echo  $all_total_student;  ?></th>
                        		<th ><?php echo  $all_absent;  ?></th>
                        			<th ><?php echo  $all_present;  ?></th>
                        </tr>
                    </tfoot>
                 </table>
              </div>
          </div>
          </div>
		<?php
	}

?>

	<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
		
		$("#present").html(<?php echo $pra; ?>);
		$("#absent").html(<?php echo $pr; ?>);
	});
	function activeNowMe(id){
		$.ajax({
			url: "<?php echo URL_ROOT ?>Promote/ajax.php?type=activeMe&id="+id
		}).done(function(data) {
			$("#row"+id).hide("slow");
		});
	}
	function getStudentPopup(){
		var url="";
		$(".checkboxes").each(function(){
			if($(this).prop('checked')==true){
				var idno=$(this).val();
				var name=$(".name"+idno).html()
				var left=$(".left"+idno).html()
				url+="&"+name+"`"+idno+"="+left;
			}
		})
		if(url!=""){
			getPopup("<?php echo URL_ROOT ?>Promote/?page_id=MigrateLeftFee&class=<?php echo $_GET["class"] ?>"+url,"1","MigrateLeftFee");
			$(".close").click(function() {
				$(this).parent().remove();
			});
			$(".outer").click(function() {
				$(this).remove();
			});

		}else{
			alert("select atleast one student to mgrate");
		}
	}
	
	    function exportTableToExcel(tableID, filename = '',e){
    
    var tableType = document.getElementById('tblData').getAttribute("data-tblname");

    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    e.preventDefault();
}

    $("#btn_excel").click(function(e) {
            var filename = $("#filename").val();
            $(".newtable").hide();
        let file = new Blob([$('.divclass').html()], {type:"application/vnd.ms-excel"});
        let url = URL.createObjectURL(file);
        let a = $("<a />", {
        href: url,
        download: filename+".xls"}).appendTo("body").get(0).click();
        e.preventDefault();
    });
    
	</script>
    <style>
    tfoot{display: table-footer-group !important;}
	.MigrateLeftFee{width:650px !important}
    </style>