<?php 
include("../db.php");
$ch=$_REQUEST["type"];
if($ch=="getTeacher"){
	?>
    <div class="control-group"> 
    	<label class="control-label" for="idbo">Faculty</label>
        <div class="controls">
		    &nbsp;
		</div>
    </div>
	<div class="control-group"> 
 
		<?php
        $faculty=$db->getRows("select * from facultySubject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["cls"]."' and subject='".$_REQUEST["subject"]."' group by idno"); 
		//echo $db->getLastQuery();
        if(is_array($faculty) && count($faculty)>0){
			?><ul><?php
            foreach($faculty as $fac){
                 echo "<li data-id='".$fac["idno"]."' class='fac'>".$db->getVal("select name from staff where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$fac["idno"])."</li>"; 
            }
			?></ul><?php
        }
        ?> 
    </div>
	<?php //echo $db->getErMsg();
}

elseif($ch=="getExamDropdown"){
	?>
    <select name="exam" id="exam" onchange="showExam(this.value)">
    <option>Select exam</option>
		<?php 
       	$class=$db->getRows("select distinct(type) from timetableexam where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");
        if(is_array($class) && count($class)>0){
		    foreach($class as $cla){
                ?>
                <option <?php if($_REQUEST["exam"]==$cla["name"])echo 'selected="selected"';?>  value="<?php echo $cla["name"] ?>">
                    <?php echo $cla["name"] ?>
                </option>
                <?php
            }
		}
        ?>
    </select>
	<?php
}
elseif($ch=="showExamDetail1"){
$Timetable=$db->getRows("select * from examTimeTable where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."' and type='".$_REQUEST["exam"]."'");

		?><table class="table"><?php
		foreach($Timetable as $tt){
			?>
			<tr>
            	<td><input type="text" name="subject[]" required  value="<?php echo $tt["subject"] ?>" readonly="readonly" /></td>
                <td><input type="text" class="datePick" required  value="<?php echo $tt["examDate"] ?>" name="examDate[]" /></td>
				<td>  <select name="seating_plan[]">
                <option <?php if($tt["seating_plan"]=="")echo 'selected';?>  value="">Select Seating </option>
				<option <?php if($tt["seating_plan"]=="1st Seating")echo 'selected';?> value="1st Seating">1st Seating</option>
				<option <?php if($tt["seating_plan"]=="2nd Seating")echo 'selected';?> value="2nd Seating">2nd Seating</option>
            </select> </td>
                <td>
				<!--<select name="schedule[]">
				<?php 	
						$exmSch=$db->getRows("select * from examSchedule where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."'"); 
						if(is_array($exmSch) && count($exmSch)>0){
							foreach($exmSch as $sub){
								$schedule=$sub["start"]." - ".$sub["end"];
								?><option <?php if($schedule==$tt["schedule"]) ?>><?php echo $schedule; ?></option><?php
							}
						}
				?>
                </select>-->
                </td>
            </tr>
			<?php
		}
		?></table>

        <div class="form-actions">
        <input type="submit" name="submit" value="Save" class="btn btn-primary" />
        <button type="reset" class="btn"> Cancel</button>
        </div>
		
		<?php    
    
}

elseif($ch=="showExamDetail"){
// 	$Timetable=$db->getRows("select * from examTimeTable where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."' and type='".$_REQUEST["exam"]."'");
	$Timetable=$db->getRows("select * from subject_mp where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."' ");
	//echo "<pre>";print_r($Timetable);echo "</pre>";
// 	if(is_array($Timetable) && count($Timetable)>0){
		?>

<table class="table">
    		<tr>
    		    <td colspan="3">
    		        1st Seating Timing <input type="text" name="seating1_time" />&ensp;&ensp;&ensp;&ensp;&ensp;
    		        2nd Seating Timing <input type="text" name="seating2_time" />
    		    </td>
    		</tr>
		
		
				<?php
		foreach($Timetable as $tt){
			?>
			<tr>
            	<td><input type="text" name="subject[]" required  value="<?php echo $tt["subject"] ?>" readonly="readonly" /></td>
                <td><input type="text" class="datePick" required  value="<?php echo $tt["examDate"] ?>" name="examDate[]" /></td>
				<td>  <select name="seating_plan[]">
                <option <?php if($tt["seating_plan"]=="")echo 'selected';?>  value="">Select Seating </option>
				<option <?php if($tt["seating_plan"]=="1st Seating")echo 'selected';?> value="1st Seating">1st Seating</option>
				<option <?php if($tt["seating_plan"]=="2nd Seating")echo 'selected';?> value="2nd Seating">2nd Seating</option>
            </select> </td>
                <td>
				<!--<select name="schedule[]">
				<?php 	
						$exmSch=$db->getRows("select * from examSchedule where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."'"); 
						if(is_array($exmSch) && count($exmSch)>0){
							foreach($exmSch as $sub){
								$schedule=$sub["start"]." - ".$sub["end"];
								?><option <?php if($schedule==$tt["schedule"]) ?>><?php echo $schedule; ?></option><?php
							}
						}
				?>
                </select>-->
                </td>
            </tr>
			<?php
		}
		?></table><?php
// 	}
/*	else{
	?><table class="table"><?php
	$SubjectList=$db->getRows("select * from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."' and type='".$_REQUEST["exam"]."'");
	//echo $db->getLastQuery();
	if(is_array($SubjectList) && count($SubjectList)>0){
		foreach($SubjectList as $sub){
			?>
			<tr>
            	<td><input type="text" name="subject[]" required  value="<?php echo $sub["subject"] ?>" readonly="readonly" /></td>
                <td><input type="text" class="datePick" required  value="" name="examDate[]" /></td>
				<td>  <select name="seating_plan[]">
                <option value="">Select Seating </option>
				<option value="1st Seating">1st Seating</option>
				<option value="2nd Seating">2nd Seating</option>
            </select> </td>
                <td>
				<!--<select name="schedule[]">
				<?php 	
						$exmSch=$db->getRows("select * from examSchedule where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["cls"]."'"); 
						if(is_array($exmSch) && count($exmSch)>0){
							foreach($exmSch as $sub){
								?><option><?php echo $sub["start"]." - ".$sub["end"] ?></option><?php
							}
						}
				?>
                </select>-->
                </td>
                
            </tr>
			<?php
		}
	}
	?></table>
	<?php
	}*/
	?>
	<div class="form-actions">
	     <input type="submit" name="submit" value="Save" class="btn btn-primary" />
        <!--<button type="submit" class="btn btn-primary"> Save</button> !-->
        <button type="reset" class="btn"> Cancel</button>
    </div>
	<?php
}
elseif($ch=="addClassTimeTable"){
	$saveAry=array();
	$dtf=explode('-',$_REQUEST['dtf']);
	$saveAry['day']=$dtf[0];
	$saveAry['pid']=$dtf[1];
	$saveAry['fid']=$dtf[2];
	$saveAry['subject']=$_REQUEST['subject'];
	$saveAry['class']=$_REQUEST['cls'];
	$saveAry['session']=$_SESSION["admin"]["session"];
	$checkdata=$db->getVal("select id from classTimeTable where school_id='".$_SESSION["admin"]["school_id"]."' and day='".$saveAry['day']."' and pid='".$saveAry['pid']."'  and class='".$saveAry['class']."' and subject='".$saveAry['subject']."'");
	if($checkdata==""){
		$saveAry["school_id"]=$_SESSION["admin"]["school_id"];
$saveAry["added_by"]=$_SESSION["admin"]["uid"];
		$ins=$db->insertAry("classTimeTable",$saveAry);
		echo $ins;
	}else{
		echo "0";
	}
	
}elseif($ch=="removeClassTimeTable"){
	$ins=$db->delete("classTimeTable","where school_id='".$_SESSION["admin"]["school_id"]."' and id=".$_REQUEST["id"]." and session='".$_SESSION["admin"]["session"]."'");
	echo $ins;
}

elseif($ch=="get_subjects"){
    $idno = $_POST["idno"];
    ?>
<select name="subject" class="input-block-level">
    <option value="">--Select Subject --</option>
        <?php $allstaff = $db->getRows("SELECT * from assign_teacher_subject where  teacher='$idno' ");
            foreach($allstaff as $staff){ 
            $name = $db->getVal("select subject from timetable_subject where id='$staff[subject]' ");
            ?>
            <option value="<?php echo $staff['subject']; ?>"><?php echo $name; ?></option>
        <?php   } ?>
</select>
        
    </select>
    <?php
}

elseif($ch=="findStudent"){
	
	$where="";
	if(isset($_REQUEST["section"]) && $_REQUEST["section"]!="")
			$where=" AND section='".$_REQUEST["section"]."' ";
	$subjects=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["cls"]."' and session='".$_SESSION["admin"]["session"]."' and status='1' $where order by name");
	?>
	<select name="idno" id="idno">
	<?php
	if(is_array($subjects) && count($subjects)>0){
		?><option value="">All Student</option><?php
		foreach($subjects as $cla){
			?>
			<option value="<?php echo $cla["idno"] ?>">
				<?php echo $cla["name"] ?>
			</option>
			<?php
		}
	}
	?></select>
	<?php }

elseif($ch=="getdata"){

    $days = array(
        1 => 'Monday',
        2 => 'Tuesday',
        3 => 'Wednesday',
        4 => 'Thursday',
        5 => 'Friday',
        6 => 'Saturday',
    );

    // echo "select * from assign_class_subject where  class='".$_REQUEST["cls"]."' and section='$_REQUEST[section]' and session='".$_SESSION["admin"]["session"]."' group by subject ";


    $tempary = array();

	$subjects=$db->getRows("select * from assign_class_subject where  class='".$_REQUEST["cls"]."' and section='$_REQUEST[section]' and session='".$_SESSION["admin"]["session"]."' group by subject ");
	foreach($subjects as $det){
	    $periods = $db->getVal("select sum(periods) from assign_class_subject where teacher='$det[teacher]' and subject='$det[subject]' and class='$det[class]' and section='$det[section]' and session='".$_SESSION["admin"]["session"]."' ");
        $name = $db->getVal("select subject from timetable_subject where id='$det[subject]' ");
        $teacher = $db->getVal("select name from staff where idno='$det[teacher]' ");

        for($i = 0; $i < $periods; $i++){
            array_push($tempary,["teacher" => $teacher, "subject" => $name, "period" => $periods]);
        } 

        // print_r($tempary);
        // echo "<br>";
	}

// 	print_r($tempary);

// 	echo count($tempary);

	for ($e = count($tempary)+1; $e <= 48; $e++) {
        // echo $e;
        $tempary[] = $e;
    }
// 	echo count($tempary);
// unset($arr1[1]); 
// $a = array_values($arr1);
// print_r($a);

    ?>  
        <table class="" border="1" cellpadding="8" style="width:100%;">
            <thead>
                <tr>
                    <th class="tablebg">Period/Days</th>
                    <?php foreach($days as $day){ ?> 
                    <th class="tablebg"><?php echo $day; ?></th>
                    <?php } ?>
                </tr>
            </thead>
            <tbody>
                <?php for($i = 1; $i <= 8; $i++){ ?>
                    <tr>
                        <td class="tablebg" style="height:40px;"><?php echo $i; ?></td>
                    <?php $j = 0;
                        foreach($tempary as $temp){
                            ?>
                                <td><?php if($temp['subject']!="") echo $temp['subject']."<br>(".$temp['teacher'].")"; ?></td>
                            <?php
                            unset($tempary[$j]);
                            if($j==5){ 
                                $tempary = array_values($tempary);
                                break;
                            }
                            $j++;
                        }
                    ?>
                    </tr>
                <?php
                    }
                ?>
            </tbody>
        </table>
    <?php 
     
/*
	?>
	<select name="idno" id="idno"> 
    	<?php
        	if(is_array($subjects) && count($subjects)>0){
        		foreach($subjects as $det){
        		    $periods = $db->getVal("select sum(periods) from assign_class_subject where teacher='$det[teacher]' and subject='$det[subject]' and class='$det[class]' and section='$det[section]' and session='".$_SESSION["admin"]["session"]."' ");
                    $name = $db->getVal("select subject from timetable_subject where id='$det[subject]' ");
        			?>
        			<option value="<?php echo $cla["idno"] ?>">
        				<?php echo "$det[class] - $det[section] - $name - $periods"; ?>
        			</option>
        			<?php
        		}
        	} else {
        	    ?>
        		<option value="">--------</option>
        		<?php
        	}
    	?> 
	</select>
<?php */ }


elseif($ch=="dropdowndata"){

	$subjects=$db->getRows("select * from assign_class_subject where  class='".$_REQUEST["cls"]."' and section='$_REQUEST[section]' and session='".$_SESSION["admin"]["session"]."' group by subject ");

    ?>  

	<select name="idno" id="idno"> 
    	<?php
        	if(is_array($subjects) && count($subjects)>0){
        		foreach($subjects as $det){
        		    $periods = $db->getVal("select sum(periods) from assign_class_subject where teacher='$det[teacher]' and subject='$det[subject]' and class='$det[class]' and section='$det[section]' and session='".$_SESSION["admin"]["session"]."' ");
                    $name = $db->getVal("select subject from timetable_subject where id='$det[subject]' ");
        			?>
        			<option value="<?php echo $cla["idno"] ?>">
        				<?php echo "$det[class] - $det[section] - $name - $periods"; ?>
        			</option>
        			<?php
        		}
        	} else {
        	    ?>
        		<option value="">--------</option>
        		<?php
        	}
    	?> 
	</select>
<?php  }


?>




