<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;} .check{ width: 133px; padding: 5px; display: inline-block; border-right: 1px dotted;}
.check input[type="checkbox"] { margin: 0 6px;}
</style>
<?php
$pgMod="facultySubject";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['idno']) || trim($_POST['idno'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select faculty idno.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$saveAry=array();
		if(is_array($_POST["subject"]) && count($_POST["subject"])>0){
			foreach($_POST["subject"] as $key=>$sub){
				if(is_array($sub) && count($sub)>0){
					foreach($sub as $s){
						$saveAry=array( "idno"=>$_POST["idno"],
										"class"=>$key,
										"school_id"=>$_SESSION["admin"]["school_id"],
										"added_by"=>$_SESSION["admin"]["uid"],
										"session"=>$_SESSION["admin"]["session"],
										"subject"=>$s
						);
						$db->insertAry("facultySubject",$saveAry);
					}
				}
			}
			$_SESSION['msg']='save Successfully';
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,"Please select atleast one subject");
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$saveAry=array();
		if(is_array($_POST["subject"]) && count($_POST["subject"])>0){
			$db->delete("facultySubject","where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$_POST["idno"]);
			foreach($_POST["subject"] as $key=>$sub){
				if(is_array($sub) && count($sub)>0){
					foreach($sub as $s){
						$saveAry=array( "idno"=>$_POST["idno"],
										"class"=>$key,
										
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
										"subject"=>$s
						);
						$db->insertAry("facultySubject",$saveAry);
					}
				}
			}
			$_SESSION['msg']='save Successfully';
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,"Please select atleast one subject");
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("facultySubject","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=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
<!-- 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> Assign subject to faculty</h3>
				</div>
				<!-- / Forms: Top Bar -->

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

						<div class="control-group">
							<label class="control-label" for="idno">Faculty Name</label>
							<div class="controls">
								<select class="span12" name="idno" id="idno">
                                    <?php 
                                        $class=$db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 order by name ");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["idno"]==$_GET["id"])echo 'selected';?> value="<?php echo $cla["idno"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
							</div>
						</div>
                        <?php 
						$class=$db->getRows("select distinct(class) from subject where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");
						if(is_array($class) && count($class)>0){
							foreach($class as $c){
								?>
								<div class="control-group">
                                    <label class="control-label" for="idbo">class <?php echo $c["class"]; ?></label>
                                    <div class="controls">
                                        <?php 
										$subject=$db->getRows("select subject from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$c["class"]."' and session='".$_SESSION["admin"]["session"]."'"); 
										if(is_array($subject) && count($subject)>0){
											foreach($subject as $sub){
												$checked="";
												$is_chk=$db->getVal("select id from facultySubject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$c["class"]."' and session='".$_SESSION["admin"]["session"]."' and idno='".$_GET["id"]."' and subject='".$sub["subject"]."'");
												if($is_chk!="")$checked=" checked ";
												
												?><div class="check"><input <?php echo $checked; ?> type="checkbox" name="subject[<?php echo $c["class"]; ?>][]" value="<?php echo $sub["subject"]; ?>"><?php echo $sub["subject"];?></div><?php
											}
										}
										?>
                                    </div>
                                </div>
								<?php
							}
						}
						?>
						<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 distinct(idno) FROM facultySubject where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" > 
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>subject(class)</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i ?></td>
                    <td><?php echo $db->getVal("select name from staff where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$clist["idno"]); ?></td>
                    <td><?php $subDetail=$db->getRows("select * from facultySubject where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and idno=".$clist["idno"]);
					if(is_array($subDetail) && count($subDetail)>0){
						foreach($subDetail as $subs){
							echo $subs["subject"]."(".$subs["class"].") " ;
						}
					}
					 ?></td>
                    <td>
                    <?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 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 -->