<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="class_wise_report";
$pgAct="view";

$monthsList = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'July ',
    'August',
    'September',
    'October',
    'November',
    'December',
);
$monthsList1 = array(
    1=>'Apr',
    2=>'May',
    3=>'Jul ',
    4=>'Aug',
    5=>'Sep',
    6=>'Oct',
    7=>'Nov',
    8=>'Dec',
	9=>'Jan',
    10=>'Feb',
    11=>'Mar'
);





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"); } ?>" />
                        
                        <select name="class" >
                            <option value="">--Select class--</option>
                            <?php 
                                $classes = $db->getRows("Select class from class where status=1 and school_id=1");
                               foreach($classes as $cls){
                                   $selected="";
                                   if($_GET["class"]==$cls['class']){	 
									    $selected=" selected ";
									}
                                echo "<option $selected>$cls[class]</option>";
                               }
                            ?>
                        </select>

                        <select name="section" >
                            <option value="">--Select Section--</option>
                            <?php
                                $section = $db->getRows("Select name from section where school_id=1");
                               foreach($section as $sec){
                                   $selected="";
                                   if($_GET["section"]==$sec['name']){	 
									    $selected=" selected ";
									}
                                echo "<option $selected>$sec[name]</option>";
                               }
                            ?>
                        </select>

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

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

                    </div>
                    
				</div>
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
	
	if(isset($_GET["attendance"])){

	    if(isset($_GET["class"]) && trim($_GET["class"])!=""){
			$where[]=" class='".$_GET["class"]."' ";
			$conwhere.=" where class='".$_GET["class"]."' ";
		}

		if(isset($_GET["section"]) && trim($_GET["section"])!=""){
			$where[]=" section='".$_GET["section"]."' ";
			$conwhere.=" AND section='".$_GET["section"]."' ";
		}

        //section
		$where[]=" s.session='".$_SESSION["admin"]["session"]."' and s.status=1 ";

		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') ";
		}

		$table=" student as s";

		?>
		 <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">
                    <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 Class Wise Attendance Report</h4>
                <h5 style="float:right;margin:0px">Date: <?php echo date('d-M-Y', strtotime($_GET['date'])); ?></h5>
                <h5 style="float:left;margin:0px">
                    Class: <?php echo $_GET['class'];
                    if($_GET['section']!=""){ echo " - ".$_GET['section']; } ?>
                </h5>
                <h5 style="float:left;width:100%;margin:5px 0px;">
                    Total Present : <span id="present"></span> &emsp;
                    Total Absent : <span id="absent"></span> &emsp;
                </h5>
            	<table class="table" id="tblData" border="1" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Name</th>
                            <th>Father Name</th>
                            <th>Mobile</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                    	<?php
  	                    $result=$db->getRows("Select idno, name, fname, mobile  from student $conwhere and session='".$_SESSION["admin"]["session"]."'  and status='1'  order by name");
    					$i=0;  $pr=0;
							foreach($result as $detail){$i++;
								$total=0;
								?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo ucfirst($detail["name"]); ?></td>
                                    <td><?php echo ucfirst($detail["fname"]); ?></td>
                                    <td><?php echo $detail["mobile"]; ?></td>
                                        <?php  $stu_sib=$db->getVal("select count(id) from attandenc where idno='".$detail["idno"]."' and type='2' and date='$_GET[date]' ");

                                        if($stu_sib===1){
                                            $result_type=1;
                                            $result_type_a=0;
                                            $pr+=$result_type;
                                        } else {
                                            $result_type=0;
                                            $result_type_a=1;
                                            $pra+=$result_type_a;
                                        }

                                        $ijk=0;
                                        ?>
                                    <td><?php if($stu_sib === 1) { echo '<span style="color:red;">Absent</span>'; } else  { echo 'Present'; }  ?></td>
                                </tr>
								<?php
								 $total=0;
							}
						?>
                    </tbody>
                 </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>