<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;} 
.checkbox {margin: 1px 15px;border: 1px solid #ccc;display: inline-block;width: 150px;border-radius: 4px;}
</style>

<?php
$pgMod="timetableSchedule";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if($_POST)
{//echo $pgAct;
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$classAry=$_POST["class"];
		echo "<pre>";
		//print_r($classAry);
		if(is_array($classAry) && count($classAry)>0){
			for($i=0;$i<count($classAry);$i++){
				$flgIn=$db->delete("timetableSchedule"," where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_POST["class"][$i]."'");
				for($j=0;$j<count($_POST["name"]);$j++){
					$saveAry=array("class"=>$_POST["class"][$i],
									"name"=>$_POST["name"][$j],
									"start"=>$_POST["start"][$j],
									"end"=>$_POST["end"][$j],
									"lorder"=>$_POST["lorder"][$j],
									
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
									"session"=>$_SESSION["admin"]["session"]
					);
					$flgIn=$db->insertAry("timetableSchedule",$saveAry);
				}
			}
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,"Please select atleast one class");
			
		}
		exit;
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$classAry=$_POST["class"];unset($_POST["class"]);
		if(is_array($classAry) && count($classAry)>0){
			for($i=0;$i<count($classAry);$I++){
				$saveAry=array("class"=>$_POST["class"][$i],
								"name"=>$_POST["name"][$i],
								"start"=>$_POST["start"][$i],
								"end"=>$_POST["end"][$i],
								
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
								"lorder"=>$_POST["lorder"][$j]
				);
				$flgIn=$db->delete("timetableSchedule"," where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_POST["class"][$i]."'");
				$flgIn=$db->insertAry("timetableSchedule",$saveAry);
				if(!is_null($flgIn))
				{
					$_SESSION['msg']='Saved Successfully';
				}
			}
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "page_id"=>$pgMod)));
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("timetableSchedule","where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "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 $_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=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM timetableSchedule WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
<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="js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript">
	function addTimePicker(){
		$('.timepic').timepicker({
			timeFormat: "hh:mm tt"
		});
	}
	$(function(){
		addTimePicker();
	});
var add_tr='<tr id="row_1"><td ><input type="text" class="st-forminput" style="width:86%;" name="name[]" /></td><td><input type="text" class="st-forminput timepic" style="width:90%;" name="start[]" /></td><td><input type="text" class="st-forminput timepic" style="width:90%;" name="end[]" /></td><td><input type="text" class="st-forminput" style="width:90%;" name="lorder[]" /></td><td><a class="delete" href="javascript:;">X</a></td></tr>';

$(document).ready(function(){

	$("#add").click(function(){

		$("#tbody").append($(add_tr));
		addTimePicker();

	});

	$('#tbody td a.delete').live('click',function() { 

		var rowCount = $('#tbody tr').length;

		if(rowCount<=1){

			alert("This item cannot deleted !!!");

		}else{

			$(this).parent().parent().remove(); 

		}
		addTimePicker();

	});	

});
</script>
<!-- Forms --> 
	<div class="row-fluid">
        <form method="post" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create a New Exam Schedule</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label" for="class">Class</label>
							<div class="controls">
                            		<div style="height: 94px; overflow:auto; background-color:#fff;padding: 2px 2px;margin: 5px;border-radius: 13px;border: 1px solid #ccc;">
                                    <?php 
                                        $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."' ");
                                        foreach($class as $cla){
                                            ?>
                                            <label class="checkbox" for="clas<?php echo $cla["class"] ?>">
                                              <input type="checkbox" id="clas<?php echo $cla["class"] ?>" name="class[]" <?php if($cla["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                                              <?php echo $cla["class"] ?>				
                                            </label>
                                            <?php
                                        }
                                    ?>
							</div>
						</div>
                        <table border="0" cellpadding="0" id="total_rows" cellspacing="0" class=" table table-bordered">

                      <thead>

                      <tr>

						<th class="titleh">Name</th>
                        
                        <th class="titleh">Start Time</th>

                        <th class="titleh" >End Time</th>
                        
                         <th class="titleh" >Order</th>

                        <th style="width:10px"></th>

                      </tr>

                    </thead>

                    <?php if(isset($_POST["name"]) && is_array($_POST["name"]) && count($_POST["name"])>0)

					{

						for($i=0; $i<=count($_POST["name"])-1;$i++)

						{

						?>

                    <tbody id="tbody">

                      <tr id="row_1">
                      
                      	<td ><input type="text" class="st-forminput" style="width:86%;" name="name[]" value="<?php echo $_POST["name"][$i]; ?>"  required /></td>

                        <td ><input type="text" class="st-forminput timepic" style="width:86%;" name="start[]" value="<?php echo $_POST["start"][$i]; ?>"  required /></td>

                        <td><input type="text" class="st-forminput timepic" style="width:90%;" name="end[]" value="<?php echo $_POST["end"][$i]; ?>"  required  /></td>
                        
                        <td><input type="text" class="st-forminput timepic" style="width:90%;" name="lorder[]" value="<?php echo $_POST["lorder"][$i]; ?>"  required  /></td>

                        <td><a class="delete" href="javascript:;"> X </a></td>

                      </tr>

                      </tbody>

                     <?php } 

					 }elseif($_GET["id"]!=''){ 

					 $all_period=$db->getRows("select * from timetableSchedule where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_GET["class"]."'");

					  if(is_array($all_period) && count($all_period))

                      {

                        foreach($all_period as $iPro)

                        {

					 ?>

                     <tbody id="tbody">

                      <tr id="row_1">
                      
                       <td ><input type="text" class="st-forminput" style="width:86%;" name="name[]" value="<?php echo $iPro["name"]; ?>" required /></td>

                        <td ><input type="text" class="st-forminput timepic" style="width:86%;" name="start[]" value="<?php echo $iPro["start"]; ?>" required /></td>

                        <td><input type="text" class="st-forminput timepic" style="width:90%;" name="end[]" value="<?php echo $iPro["end"]; ?>"  required  /></td>
                        
                         <td><input type="text" class="st-forminput" style="width:90%;" name="lorder[]" value="<?php echo $iPro["lorder"][$i]; ?>"  required  /></td>

                        <td><a class="delete" href="javascript:;"> X </a></td>

                      </tr>

                      </tbody>

                     <?php } }else{

					 ?>

                     <tbody id="tbody">

                      <tr id="row_1">

                        <td ><input type="text" class="st-forminput" style="width:86%;" name="name[]"  required /></td>

                        <td ><input type="text" class="st-forminput timepic" style="width:86%;" name="start[]"  required /></td>
                        
                        <td><input type="text" class="st-forminput timepic" style="width:90%;" name="end[]"  required  /></td>
                        
                        <td><input type="text" class="st-forminput" style="width:90%;" name="lorder[]"  required  /></td>

                        <td><a class="delete" href="javascript:;"> X </a></td>

                      </tr>

                      </tbody>

					 <?php					 

					 }}else{?>

                     <tbody id="tbody">

                      <tr id="row_1">

                        <td ><input type="text" class="st-forminput" style="width:86%;" name="name[]"  required /></td>

                        <td ><input type="text" class="st-forminput timepic" style="width:86%;" name="start[]"  required /></td>
                        
                        <td><input type="text" class="st-forminput timepic" style="width:90%;" name="end[]"  required  /></td>
                        
                        <td><input type="text" class="st-forminput" style="width:90%;" name="lorder[]"  required  /></td>

                        <td><a class="delete" href="javascript:;"> X </a></td>

                      </tr>

                      </tbody>

					 <?php } ?>

                      <tfoot>

                      <tr id="add_row">

                        <td colspan="2">

                          <span style="float:left;"><a id="add" style="cursor:pointer;">Add Row</a></span>

                         

                        </td>

                        <td width="15%"></td>

                      </tr>

                    </tfoot>

                  </table>
                        
						<div class="form-actions">
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        </form>
	</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>
    <button style="float:right; margin-top:5px" onclick="window.location='./?src=TimeTable&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"TimeTable", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM timetableSchedule where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' order by class"); $i=0;
	//echo $db->getLastQuery();
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No</th>
                    <th>Class</th>
                    <th>Period Name</th>
                     <th>Start Time</th>
                     <th>End Time</th>
                    <th style="display:none">Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i ?></td>
                    <td><?php echo $clist["class"] ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["start"] ?></td>
                    <td><?php echo $clist["end"] ?></td>
                    <td style="display:none">
                    <?php $aryPgAct['action']="edit"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</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></th><th></th><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>
	<?php
}
?>
<!-- / Forms -->