<?php
//echo "<pre>";print_r($_SERVER);exit;
if($_POST){
	if(is_array($_POST["idno"]) && count($_POST["idno"])>0){
		$i=0;
		foreach($_POST["idno"] as $idno){

            $db->delete("optional_subject", " where session='".$_SESSION["admin"]["session"]."' and idno='".$idno."' ");

            foreach($_POST["subject_$idno"] as $sub){
                
                $ary = array(
                            "class"     =>  $_GET["class"],
                            "section"   =>  $_GET["section"],
                            "idno"      =>  $idno,
                            "subject"   =>  $sub,
                            "added_by"  =>  $_SESSION["admin"]["uid"],
							"session"   =>  $_SESSION["admin"]["session"]
                    );
                
                if($sub!=""){
                    $db->insertAry("optional_subject",$ary); 
                }
                
            }

			$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="optional_subject";
$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 Optional Subject</button>
                        <!--<button type="submit" name="create_file" class="btn btn-primary"> Create Excel file</button>-->
                        <!--<a href="./?src=ExamCBSE&page_id=import_marks" class="btn btn-primary" style="color:white;">Import Marks</a>-->
					</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>
                    <br>
				</div>
				<!-- / Forms: Content -->
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
	if(isset($_GET["fee"])){  
		?><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 type="submit" name="fee" class="btn btn-primary"> Save</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"]."' ";

		$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 session='".$_SESSION["admin"]["session"]."' and issue_TC=0 and status=1 order by name ");

        $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 $whereM1 $wheresection and session='".$_SESSION["admin"]["session"]."' order by id");

		//echo $db->getLastQuery();
		if(is_array($allstudent) && count($allstudent)>0){
			?>
            <div id="printableDiv">
            <table class="table">
			<tr>
            <th>SNo.</th>
            <th>Student</th>
            <th>Admission No.</th>
            <?php 
			if(is_array($allsubject) && count($allsubject)>0){
				foreach($allsubject as $sub){
					?><th><?php echo $sub["subject"] ?></th><?php
				}
			}
			?> 
            </tr>
			<?php $i=0;
			foreach($allstudent as $stu){$i++;
				?>
				<tr> 
    				<input type="hidden" name="idno[]" value="<?php echo $stu['idno']?>" />
                	<td><?php echo $i; ?></td>	
                	<td><?php echo $stu["name"]."(".$stu["fname"].")" ?></td>
                	<td><?php echo $stu["admissionno"]; ?></td>
                    <?php //$total=$db->getVal("select total from examterm where school_id='".$_SESSION["admin"]["school_id"]."' and name='".$_GET["term"]."' and class='$_GET[class]' and section='$_GET[section]' and subject='$sub[subject]' and session='".$_SESSION["admin"]["session"]."' "); ?>

                    <?php
					if(is_array($allsubject) && count($allsubject)>0){
						foreach($allsubject as $sub){
						    $check = $db->getVal("select id from optional_subject where class='$_GET[class]' and section='$_GET[section]' and idno='$stu[idno]' and subject='$sub[subject]' and session='".$_SESSION["admin"]["session"]."' ");
							?>
							<td>
                                <input type="checkbox" name="subject_<?php echo $stu['idno']; ?>[]" value="<?php echo $sub['subject']; ?>" <?php if($check > 0) echo "checked"; ?> style="width:20px;height:20px" />
                            </td>
                            <?php
						}
					}
					?>
                </tr>
				<?php
			}
			?></table>
			</div>
			<?php
		}
		?></div></div></div></div></form><?php
	}

?>
<script type="text/javascript">

$(document).ready(function(){
	/*$("student").change(function() {
		var v=this.val();
		alert(v);
		});*/
});




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

    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 || ('Exam Marks.' + (type || 'xlsx')));
    }

</script>



