<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>
<link rel="stylesheet" media="all" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="../Faculty/js/jquery-ui-timepicker-addon.js"></script>
<script>
$(document).ready(function(){
	$('.datePick').datepicker();
});
</script>
<script>
var root='<?php echo URL_ROOT; ?>';
function getExam(id,exam){
	$("#exam").html("Loading..");
	$("#ExamDetail").html('');
	$.post( "<?php echo URL_ROOT ?>Faculty/ajax.php", { 
	type: "getExamDropdown", cls:  id, exam:	exam } , 
	function( data ) {
	  	$("#exam").html(data);
		$('.datePick').datepicker();
	});
}
function showExam(exam){
	$("#ExamDetail").html('<center><img src="'+root+'assets/icon/load.gif" /></enter>');
	var cls=$("#cls").val();
	$.post( "<?php echo URL_ROOT ?>Faculty/ajax.php", { 
	type: "showExamDetail", cls: cls, exam: exam } , 
	function( data ) {
	  	$("#ExamDetail").html(data);
		$('.datePick').datepicker();
	});
}
function showSelectedExam(cls,exam){
	$("#ExamDetail").html('<center><img src="'+root+'assets/icon/load.gif" /></enter>');
	$("#cls").val(cls);getExam(cls,exam);
	$.post( "<?php echo URL_ROOT ?>Faculty/ajax.php", { 
	type: "showExamDetail", cls: cls, exam: exam } , 
	function( data ) {
	  	$("#ExamDetail").html(data);
		$('.datePick').datepicker();
	});
}
</script>
<?php
$pgMod="examTimeTable";
$pgAct="view";

if($_POST){
	if(is_array($_POST["subject"]) && count($_POST["subject"])>0)
	$db->delete("examTimeTable","where class='".$_POST["class"]."' and type='".$_POST["exam"]."'");
	for($i=0; $i<count($_POST["subject"]);$i++){
		$saveAry=array(	"class"=>$_POST["class"],
						"type"=>$_POST["exam"],
						"subject"=>$_POST["subject"][$i],
						"examDate"=>$_POST["examDate"][$i],
						
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
						"schedule"=>$_POST["schedule"][$i]
						);
		//echo"<pre>";print_r($saveAry);echo"</pre>";
		$_SESSION["msg"]="time table create successfully";
		$db->insertAry("examTimeTable",$saveAry);
		
	}redirect("?page_id=".$pgMod);
}
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($pgAct=="delete" && $_GET["id"]!=""){
	$db->delete("examTimeTable","where class='".$_GET["class"]."' and type='".$_GET["exam"]."'");
	$_SESSION["msg"]="time table delete successfully";
	redirect("?page_id=".$pgMod);
}elseif($pgAct=="publish" && $_GET["id"]!=""){
	$db->updateAry("examTimeTable",array("publish"=>1),"where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_GET["class"]."' and type='".$_GET["exam"]."'");
	$_SESSION["msg"]="time table Publish successfully";
	redirect("?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 ucfirst($_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
}
if($pgAct=="view" && $_GET["id"]!="")
{   
	$examSchedule=$db->getRows("select * from examSchedule where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_GET["class"]."'");
	$timeTable=$db->getRows("select * from examTimeTable where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_GET["class"]."' and type='".$_GET["exam"]."'");
	?>
	<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">
    	<table class="table">
        <tr>
        <th></th>
    	<?php
		foreach($examSchedule as $sub){
			?><th><?php echo $sub["start"]." - ".$sub["end"] ?></th><?php
		}
		?>
         </tr>
         
         <?php
		 	foreach($timeTable as $table){
				?>
				<tr>
                	<td><?php echo $table["examDate"] ?></td>
                    <?php
					foreach($examSchedule as $sub){
						?><th><?php echo $db->getVal("select subject from examTimeTable where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_GET["class"]."' and type='".$_GET["exam"]."' and  subject='".$table["subject"]."' and schedule='".$sub["start"]." - ".$sub["end"]."'"); ?></th><?php
					}
					?>
                </tr>
				<?php
			}
		 ?>
        </table>
	</div>
	<?php
}
else
{
	?>

	<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>
    <form method="post" action="">
    <div class="well no-padding">
    <div class="control-group">
        <label class="control-label" for="cls">Class</label>
        <div class="controls">
            <select name="class" onchange="getExam(this.value,'')" id="cls" class="span6">
            <option>Select class</option>
                <?php 
                    $class=$db->getRows("select distinct(class) from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and");
                    foreach($class as $cla){
                        ?>
                        <option <?php if($cla["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                            <?php echo $cla["class"] ?>
                        </option>
                        <?php
                    }
                ?>
            </select><span id="exam">
            <select name="class" id="class" class="span6">
                <option>Select Exam</option>
            </select></span>
        </div>
    </div>
    <div id="ExamDetail">
         <div class="row-fluid"><div class="span12"><div class="top-bar">
            <h3><i class="icon-eye-open"></i> Exam Timetable list</h3>
            </div>
                <div class="well no-padding">
                <?php
                $aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
                $aryList=$db->getRows("SELECT * FROM examTimeTable where school_id='".$_SESSION["admin"]["school_id"]."'  group by class, type"); $i=0;
                if(is_array($aryList) && count($aryList)>0)
                {
                    ?>
                    <table class="table" id="example"> 
                        <thead>
                            <tr>
                                <th>Class</th>
                                <th>Type</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach($aryList as $clist){ $i++; 
                        $aryPgAct["id"]=$clist['id'];$aryPgAct['class']=$clist["class"];$aryPgAct['exam']=$clist["type"];
                        ?>
                            <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                                <td><?php echo $clist["class"] ?></td>
                                <td><?php echo $clist["type"] ?></td>
                                <td>
                                <?php 
								if($clist["publish"]==0){ ?>
                                     <a style="float:left" onclick="showSelectedExam('<?php echo $clist["class"] ?>','<?php echo $clist["type"] ?>')" href="javascript:;"><button type="button" class="btn btn-info">Edit</button></a>
                                <?php $aryPgAct['action']="publish"; ?>
                                     <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Publish</button></a>
                                <?php 
								}else{ $aryPgAct['action']="view"; ?>
                                     <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">View</button></a>
								<?php	
								}
									$aryPgAct['action']="delete"; ?>
                                     <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  
                                </td>
                            </tr>
                        <?php } ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th></th>
                                <th colspan="2"></th>
                            </tr>
                        </tfoot>
                    </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>
    </div>
	</div>
    </form>
    </div></div>
	<?php
}
?>