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

			$saveAry =array("idno"              =>  $stu, 
							"present"           =>  $_POST["present"][$i],
							"remark"            =>  $_POST["remark"][$i],
							"working_days"      =>  $_POST["working_days"],
							"view_attendance"   =>  $_POST["view_attendance"],
							"view_remark"       =>  $_POST["view_remark"],
							"class"             =>  $_GET["class"],
							"section"           =>  $_GET["section"],
							"type"              =>  $_GET["term"],
							"school_id"         =>  $_SESSION["admin"]["school_id"],
							"added_by"          =>  $_SESSION["admin"]["uid"],
							"session"           =>  $_SESSION["admin"]["session"]
			);
			
			$check = $db->getVal("select id from attendance_remark where idno='$stu' and class='$_GET[class]' and type='$_GET[term]' and session='".$_SESSION["admin"]["session"]."' ");
			
			if($check > 0){
			        $updateAry = array( "present"       =>  $_POST["present"][$i],
			                            "remark"        =>  $_POST["remark"][$i],
			                            "working_days"  =>  $_POST["working_days"],
			                            "view_attendance"  =>  $_POST["view_attendance"],
			                            "view_remark"  =>  $_POST["view_remark"],
			                            );
			    $flg = $db->updateAry("attendance_remark", $updateAry ,"where idno='$stu' and class='$_GET[class]' and type='$_GET[term]' and session='".$_SESSION["admin"]["session"]."'");
			} else {
    			$db->insertAry("attendance_remark",$saveAry); 
            }

			$i++;
		}
	}

	redirect();
}
?>

<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="term_attendance_remark";
$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>Attendance/Remarks</h3>
                    <div>
                        <button type="submit" name="fee" class="btn btn-primary"> Add Attendance/Remarks</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="">--Select Term--</option>
                                    <option value="Term1">Term1</option>
                                    <option value="Term2">Term2</option>
                                    <!--<option value="Final">Final</option>-->
                                </select>
                                <?php
									//echo $db->getLastQuery();
								?>
                            </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 Record</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 school_id='".$_SESSION["admin"]["school_id"]."' 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 school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' order by id");

		if(is_array($allstudent) && count($allstudent)>0){
		    
		    $data = $db->getRow("select * from attendance_remark where class='$_GET[class]' and section='$_GET[section]' and type='$_GET[term]' and session='".$_SESSION["admin"]["session"]."' ");
		    
			?>
            <div id="printableDiv">
            <br>
            <table class="table" style="width:100%;text-align:center;">
                <tr>
                    <th>Total Working Days</th>
                    <th>Attendance in Result</th>
                    <th>Remarks in Result</th>
                </tr>
                <tr>
                    <td><input type="text" name="working_days" value="<?php echo $data['working_days']; ?>" /></td>
                    <td>
                        Yes <input type="radio" name="view_attendance" value="1" <?php if($data['view_attendance']==1)echo "checked"; ?> />
                        No  <input type="radio" name="view_attendance" value="0" <?php if($data['view_attendance']==0)echo "checked"; ?> />
                    </td>
                    <td>
                        Yes <input type="radio" name="view_remark" value="1" <?php if($data['view_remark']==1)echo "checked"; ?> />
                        No  <input type="radio" name="view_remark" value="0" <?php if($data['view_remark']==0)echo "checked"; ?> />
                    </td>
                </tr>
            </table><br>
            <table class="table" style="width:100%;">

                <thead>
    			<tr>
                    <th>SNo.</th>
                    <th>Student</th>
                    <th>Present Days</th>
                    <th>Remarks</th>
                </tr>
                </thead>
                <tbody>
			<?php $i=0;
			foreach($allstudent as $stu){$i++;
			    $record = $db->getRow("select * from attendance_remark where idno='$stu[idno]' and class='$_GET[class]' and type='$_GET[term]' and session='".$_SESSION["admin"]["session"]."' ");
				?>
				<tr> 
                	<td><?php echo $i; ?> <input type="hidden" name="idno[]" value="<?php echo $stu["idno"]; ?>" /> </td>	
                	<td><?php echo $stu["name"]."(".$stu["fname"].")" ?></td>
                    <td>
                        <input type="text" name="present[]" value="<?php echo $record['present']; ?>"  placeholder="Total Present" />
                    </td>
                    <td>
                        <input type="text" name="remark[]" value="<?php echo $record['remark']; ?>"  placeholder="Enter Remark" />
                    </td>
                </tr>
				<?php
			}
			?>
			</tbody>
			 
			</table>
			</div>
			<?php
		}
		?></div></div></div></div></form><?php
	}
	
	echo $db->getErMsg();
?>
<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(c);
//	var r=$(this).val();
if(c!='AB' && isNaN(v))
{
alert("Please enter only AB");
a.value='';	
}
else if(v>t){
		alert("Please enter correct marks");
		//return false;
		a.value='';
	}
	//else if(c!='A'){
		
		//a.value='';
	//}
	
		
	//alert(r);
}

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


</script>