<?php
//echo "<pre>";print_r($_SERVER);exit;
if($_POST){
	//echo "<pre>";
	if(is_array($_POST["student"]) && count($_POST["student"])>0){ 
		$i=0;
		foreach($_POST["student"] as $stu){
			$detail=json_decode(base64_decode($_POST["detail"][$i]),true);
			$db->delete("exammarks","where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$detail["idno"]." and type='".$detail["term"]."' and subject='".$detail["subject"]."' and session='".$_SESSION["admin"]["session"]."'");
			$newExam=array("obtain"     =>  $stu, 
			                "class"     =>  $_GET["class"],
							"idno"      =>  $detail["idno"],
							"subject"   =>  $detail["subject"],
							"type"      =>  $detail["term"],
							"total"     =>  $detail["total"],
							"school_id" =>  $_SESSION["admin"]["school_id"],
							"added_by"  =>  $_SESSION["admin"]["uid"],
							"session"   =>  $_SESSION["admin"]["session"]
			); 
			$db->insertAry("exammarks",$newExam); 
			$i++;
		}
	}
	//print_r($_POST);echo "</pre><hr/>";
	redirect();
}
?>
<script type="text/javascript" src="https://unpkg.com/xlsx@0.15.1/dist/xlsx.full.min.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;} 
</style>
<?php
$pgMod="cross_list";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

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="ExamCBSE" />
    <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-cog"></i>Search Student Record</h3>
                    <div>
                        <!--<button type="submit" name="percentage" class="btn btn-primary">Percentage List</button>-->
                        <button type="submit" name="marks" class="btn btn-primary">Cross List</button>
						<!--<button type="submit" name="fee" class="btn btn-primary"> Add Marks</button>-->
                        <!--<button type="submit" name="create_file" class="btn btn-primary"> Create Excel file</button>-->
                        
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
                        
                        <div class="control-group span4">
                            <label class="control-label" for="class">Class</label>
                            <div class="controls">  
                                <select 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 <?php if($cla["class"]==$_GET["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                                                <?php echo $cla["class"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span4">
                            <label class="control-label" for="class">Section</label>
                            <div class="controls">
                               <select 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 <?php if($cla["name"]==$_GET["section"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                            <?php echo $cla["name"] ?>
                        </option>
                        <?php
            
                    }
                ?>
            </select>
                            </div>
                        </div>
                        <div class="control-group span4 hide">
                            <label class="control-label" for="medium">Medium</label>
                            <div class="controls">
                                <select name="medium" id="medium">
                                    <option <?php if("hindi"==$aryForm["sibling"])echo 'selected';?> value="hindi">Hindi</option>
                                    <option <?php if("english"==$aryForm["sibling"])echo 'selected';?> value="english">English</option>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span4">
                            <label class="control-label" for="term">Term</label>
                            <div class="controls"> 
                                <select name="term" id="term">
                                    <option value="">Term</option>
                                    <?php 
                                        $class=$db->getRows("select * from examname ");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["name"]==$_GET["term"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                                <?php
									//echo $db->getLastQuery();
								?>
                            </div>
                        </div>
                    </div>
                    <br>
				</div>
				<!-- / Forms: Content -->
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
    if(isset($_GET["marks"])){
		?><form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Add Result of students</h3>
                    <div>
    					<button class="btn btn-danger" onclick="ExportToExcel('xlsx')">Export Excel</button>
                        <button type="button" name="fee" onClick="printDivNoHeader('printableDiv')" class="btn btn-primary"> Print</button>
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
		
		<?php
		$whereC=array();
		if(isset($_GET["class"]) && $_GET["class"]!="")
			$whereC[]=" class='".$_GET["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($_GET["section"]) && $_GET["section"]!="")
			$whereC[]=" section='".$_GET["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 LENGTH(fincome), fincome ");

        $checksection = $db->getVal("select count(section) from subject where class='$_GET[class]' and section!='' and session='".$_SESSION["admin"]["session"]."' ");
        $wheresection = '';
        if($checksection > 0){
            $wheresection = " and section='$_GET[section]' ";
        }
        $allsubject=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_GET["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' order by id");
// 		$allsubject=$db->getRows("select * from subject $whereM1 and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' order by id");
		//echo $db->getLastQuery();
		if(is_array($allstudent) && count($allstudent)>0){
			?>
            <div id="printableDiv">
            
            	<div class="headPrint hide" style="border: none;">
			 
			  
			 <div style="margin-left:55px"> <img src="<?php echo $LinksDetails["logo"] ?>" height="90px" width="110px" style="margin:0px 0px 3px 10px;"></div>
			 <div style="float:left;text-align:center;margin-bottom:18px;"> <h1 style="font-size:30px;margin:5px 0px 0px 0px;color: #EC1E1E;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h1>
			   <span style="font-size:16px;font-weight:800;  color: #186916;"> <?php echo ucfirst(nl2br($LinksDetails["contact_content"])) ?></span>
			 </div> 
		  </div>
		  	<div style="font-size: 18px;clear: both;font-weight: bold;padding: 5px;color: #000;text-align: center;border-right: 1px solid #000;  border-left: 1px solid #000;border-top: 1px solid #000;border-bottom: 1px solid #000;"> CROSS LIST OF CLASS:&nbsp;&nbsp;<?php echo $_GET['class']; ?>-<?php echo $_GET['section']; ?>&nbsp;&nbsp;Exam:&nbsp;&nbsp;<?php echo $_GET['term']; ?></div>

            <table class="table" cellspacing="0" cellpadding="0" border="1" id="tbl_exporttable_to_xls">
                <thead>
        			<tr>
                    <th style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;">SNo.</th>
                    <th style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;">Roll No.</th>
                    <th style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;">Student</th>
                    <?php 
        			if(is_array($allsubject) && count($allsubject)>0){
        				foreach($allsubject as $sub){
        					?><th style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;"><?php echo $sub["subject"] ?></th><?php
        				}
        			}?>
                    </tr>
                </thead>
			<?php $i=0;
			foreach($allstudent as $stu){$i++;
				?>
				<tr> 
                	<td style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;"><?php echo $i; ?></td>
                	<td style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;"><?php echo $stu["fincome"] ?></td>	
                	<td style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;"><?php echo $stu["name"]."(".$stu["fname"].")" ?></td>
                    <?php 
					if(is_array($allsubject) && count($allsubject)>0){
						foreach($allsubject as $sub){
							?><td style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;border-right:2px solid black;">
                            <?php 
							$examResultData=$db->getRow("select * from exammarks where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$stu["idno"]."' and type='".$_GET["term"]."' and subject='".$sub["subject"]."'");
							?>
                          
                             
                             <?php
							 $detail=array("idno"=>$stu["idno"],
							 				"subject"=>$sub["subject"],
											"term"=>$_GET["term"],
											"total"=>$total,
											"class"=>$_GET["class"]
											);
							$data=base64_encode(json_encode($detail));
							 ?>
                             <input type="hidden" name="detail[]" value="<?php echo $data ?>" />
                             
                            </td><?php
							
						}
					}?>
                </tr>
				<?php
			}
			?></table>
			</div>
			<?php
		}
		?></div></div></div></div></form><?php
	}
?>


<script>
    function ExportToExcel(type, fn, dl) {
        var elt = document.getElementById('tbl_exporttable_to_xls');
        var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" });
        return dl ?
            XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }) :
            XLSX.writeFile(wb, fn || ('Cross List.' + (type || 'xlsx')));
    }
</script>


<script type="text/javascript">

$(document).ready(function(){
	/*$("student").change(function() {
		var v=this.val();
		alert(v);
		});*/
});
function validateme(a){
    
	var v=parseInt(a.value);
	var t=$('#total').val();
	var c=(a.value);
// 	alert(v);
//	var r=$(this).val();

if(c==='AB' || c==='NA' || (!isNaN(v) && v<t) ){
    
}
else if(v>t){
	alert("Please enter correct marks");
	a.value='';
}
else {
    alert("Please enter only AB or NA");
    a.value='';
}

// if(c!='AB' && isNaN(v))
// {
// alert("Please enter only AB");
// a.value='';	
// }
// else if(v>t){
// 		alert("Please enter correct marks");
// 		a.value='';
// 	}
	//else if(c!='A'){
		
		//a.value='';
	//}
	
		
	//alert(r);
}

</script>