<style>
.txtAtt{width:60px;}
</style>
<?php
$pgMod="BulkAttendance";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
if($_POST){
	
  if(isset($_POST["save"])){
		$term2_att=$_POST["student1"];
	if(is_array($_POST["student"]) && count($_POST['student'])>0){ $i=0;
		foreach($_POST["student"] as $idno=>$att){$i++;
			 $array=array("idno"=>$idno,
							"term1_att"=>$att,
							"term2_att"=>$term2_att[$idno],
							"school_id"=>$_SESSION["admin"]["school_id"],
							"added_by"=>$_SESSION["admin"]["uid"],
							"session"=>$_SESSION["admin"]["session"]);
			 $any=$db->getRow("Select * from allattandence where idno='".$idno."' and session='".$_SESSION["admin"]["session"]."'");				
			if($any==NULL){
				//$db->delete("allattandence","where idno='".$idno."'");			
				$db->insertAry("allattandence",$array);
				//echo $db->getLastQuery();exit;
			  }
			else{
				$db->updateAry("allattandence",$array,"where idno='".$idno."' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");	
			   }
		  }
			$_SESSION["msg"]="record saved successfully";
			redirect();
			//echo "error:".$db->getErMsg().$db->getLastQuery();
	 }
  }
  elseif(isset($_POST["CreateExelFile"])){
	  
		
		$whereC=array();
		if(isset($_POST["class"]) && $_POST["class"]!="")
			$whereC[]=" class='".$_POST["class"]."' ";
		
		/*if(isset($_GET["medium"]) && $_GET["medium"]!="")
			$whereC[]=" medium='".$_GET["medium"]."' ";
		*/	
		$whereM="";$whereM1="";
		if(is_array($whereC) && count($whereC)>0)
			$whereM1=" where ".implode(" and ",$whereC);
		
		if(isset($_POST["section"]) && $_POST["section"]!="")
			$whereC[]=" section='".$_POST["section"]."' ";
		if(is_array($whereC) && count($whereC)>0)
			$whereM=" where ".implode(" and ",$whereC);
			
		
		$allstudent=$db->getRows("select * from student $whereM and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and issue_TC=0 and status=1 order by name ");
		
		//echo $db->getLastQuery();
		if(is_array($allstudent) && count($allstudent)>0){
			define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
					/** Include PHPExcel */
			require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
			$objPHPExcel = new PHPExcel();
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue('A1', 'idno')
						->setCellValue('B1', 'name')
						->setCellValue('C1', 'fname')
						->setCellValue('D1', 'class')
						->setCellValue('E1', 'section')
						->setCellValue('F1', 'term1_att')
						->setCellValue('G1', 'term2_att');
			$i=1;
			foreach($allstudent as $student){$i++;
				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.$i.'', ''.$student["idno"].'')
							->setCellValue('B'.$i.'', ''.$student["name"].'')
							->setCellValue('C'.$i.'', ''.$student["fname"].'')
							->setCellValue('D'.$i.'', ''.$student["class"].'')
							->setCellValue('E'.$i.'', ''.$student["section"].'');
							
			}
			//$objPHPExcel->getActiveSheet()->setTitle('marks123');
			$objPHPExcel->setActiveSheetIndex(0);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			//echo $root=getcwd()."\\files\marks_".$_GET['class']."_".$_GET['section']."_".$_GET['term'].".xls";
			$root="D:\marks_excel\att_".$_POST['class']."_".$_POST['section'].".xls";
			$objWriter->save($root);
			
			echo 'Files have been created in ' , $root , EOL;
			/*$callEndTime = microtime(true);
			$callTime = $callEndTime - $callStartTime;
			
			echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
			echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
			// Echo memory usage
			echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
			
			
			// Echo memory peak usage
			echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
			
			// Echo done
			echo date('H:i:s') , " Done writing files" , EOL;
			echo __FILE__ , EOL;*/
			

			/*header("Content-Type: application/vnd.ms-excel");
			$filename=$_GET['class']."marks_file.xlsx";
			header("Content-disposition: attachment; filename=".$filename."");
			
			echo 'idno' . "\t" . 'name' . "\t" . 'fname' . "\n";
			foreach($allstudent as $student)
			echo ''.$student["idno"].'' . "\t" . ''.$student["name"].'' . "\t" . ''.$student["fname"].'' . "\n";*/
			
		}
	
  }
}
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"]);
}

?>
<!-- Forms --> 
<form method="post" action="">
<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: 96.7% !important;overflow: inherit !important;} 
</style>
<!-- / Forms -->
    <script>
    </script>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <input type="submit" value="save" name="save" class="btn btn-default pull-right" >
    <input type="submit" value="CreateExelFile" name="CreateExelFile" class="btn btn-primary pull-right" >
    </div>
    <div class="well no-padding" style="  min-height: 86px;">
	<?php
	$aryPgAct=array("src"=>"Attandence", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select idno from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by name");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <div style="position: absolute;width:300px;z-index: 99999;margin: 30px;">
        	<select onchange="fetchByClass()" class="span6" name="class" id="class" >
                <option value="">Class</option>
                <?php 
                    $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."'");
                    foreach($class as $cla){
                        ?>
                        <option value="<?php echo $cla["class"] ?>">
                            <?php echo $cla["class"] ?>
                        </option>
                        <?php
            
                    }
                ?>
            </select>
            <select onchange="fetchByClass()" class="span6" name="section" id="section" >
                <option value="">Section</option>
                <?php 
                    $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                    foreach($class as $cla){
                        ?>
                        <option value="<?php echo $cla["name"] ?>">
                            <?php echo $cla["name"] ?>
                        </option>
                        <?php
            
                    }
                ?>
            </select>
         </div>
        <input type="hidden" id="tabletype" value="admission_data" />
        <table class="table" id="ajaxtable" style="display:none">
            <thead>
                <tr>
                    <th width="4%">S.No</th>
                    <th width="16%">Name</th>
                    <th width="10%">Class</th>
                    <th width="8%">Medium</th>
                    <th width="10%">Admission no</th>
                    <th width="10%">Bus Root</th>
                    <th width="16%">Father</th>
                    <th width="10%">Mobile</th>
                    <th width="8%">Term-1 Att</th>
                    <th width="8%">Term-2 Att</th>                    
                </tr>
            </thead>
            <tfoot>
            </tfoot>
            <tbody>
           
            </tbody>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
</form>
<!-- / Forms -->
<script type="text/javascript">
/*$(document).ready(function(e) {
    var oTable = $('#ajaxtable').dataTable( {
		"bProcessing": true,
		'sPaginationType': 'full_numbers',
		"sAjaxSource": 'Attandence/ajax.php?type='+$('#tabletype').val(),
		'iDisplayLength': 285,
		"oLanguage": {
			"sSearch": "Search by any columns:"
		}
	});
});*/
function fetchByClass(){
	$("#ajaxtable").show();
	var name=$("#class").val();
	var section=$("#section").val();
	if(name!=""){
		var url='Attandence/ajax.php?class='+name+'&type='+$('#tabletype').val();
		if(section!=""){
			url+='&section='+section
		}
		 oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": url,
			'iDisplayLength': 285,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}else{
		var oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": 'Attandence/ajax.php?type='+$('#tabletype').val(),
			'iDisplayLength': 285,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}
}
</script>