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


if(isset($_POST['save']))
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE; 
	if(!isset($_POST['total']) || trim($_POST['total'])=='')
	{
		$flgEr=TRUE; 
		array_push($alert_err,"Please enter total.");
	}
	if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("examterm",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}

if(isset($_POST['save_term'])){

    $db->delete("term_permission","where term='$_POST[term]' and class='$_GET[class]' ");

    $i = 0;
    foreach($_POST['exams'] as $ex){
        $check = $_POST["exam_$ex"]; 
        $exam  = $_POST["exam_name$ex"];
        if($check){
            
            $saveary = array(
                "term"      =>  $_POST['term'],
                "exam"      =>  $exam,
                "class"     =>  $_GET['class'],
                "ordering"  =>  $_POST['order'][$i],
                "co_scholastic"  =>  $_POST['co_scholastic'],
                "start_date"  =>  $_POST["start_date$ex"],
                "end_date"  =>  $_POST["end_date$ex"],
                "result_view" => $_POST['result_view'],
                "co_scholastic_view" => $_POST['co_scholastic_view'],
                "added_by"  =>  $_SESSION["admin"]["uid"],
                "session"   =>  $_SESSION["admin"]["session"],
                "school_id" =>  $_SESSION["admin"]["school_id"],
            );

            $flg = $db->insertAry("term_permission", $saveary);
            
        }
        $i++;

    }
// die();

    redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "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 examterm WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
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> Terms</h3>
				</div>
				<!-- Forms: Content -->
				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label" for="class">Term</label>
							<div class="controls">
								<?php echo $aryForm["full_name"] ?>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="total">Total</label>
							<div class="controls">
								<input type="text" id="total" name="total" class="input-block-level" value="<?php if($_POST)echo $_POST["total"]; else echo $aryForm["total"] ?>">
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary" name="save"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

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

        </form>
	</div>
<?php   
}
elseif(isset($_GET["add_term"])){
    $data = $db->getRow("select * from term_permission where term='$_GET[term]' and class='$_GET[class]' ");
		?>
		<form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Create Term</h3>
                    <div>
						<button type="submit" name="save_term" class="btn btn-primary"> Save Term</button> 
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">

    		<input type="hidden" name="term" value="<?php echo $_GET['term']; ?>" />

            <h4 style="text-align:center;">Please select the checkbox for Adding Exams in  : <?php echo $_GET['term']; ?></h4>
            <div id="printableDiv">
                
                <br>
            <table class="table" style="width:100%;text-align:center;">
                <tr>
                    <th>Result View</th>
                    <th>Co Scholastic View</th>
                </tr>
                <tr>
                    <td>
                        Yes <input type="radio" name="result_view" value="1" <?php if($data['result_view']==1)echo "checked"; ?> />
                        No  <input type="radio" name="result_view" value="0" <?php if($data['result_view']==0)echo "checked"; ?> />
                    </td>
                    <td>
                        Yes <input type="radio" name="co_scholastic_view" value="1" <?php if($data['co_scholastic_view']==1)echo "checked"; ?> />
                        No  <input type="radio" name="co_scholastic_view" value="0" <?php if($data['co_scholastic_view']==0)echo "checked"; ?> />
                    </td>
                </tr>
            </table><br>
                
            <table class="table">
			<tr>
                <th>SNo.</th>
                <th>Class</th>
                <th>Exam</th>
                <th>Tick</th>
                <th>Order</th>
                <th>Co-scholastic</th>
                <th>Start Time</th>
                <th>End Time</th>
            </tr>
			<?php $i=0; $j = 0;
			$allexams = $db->getRows("SELECT * from examname ");
			foreach($allexams as $exam){$i++;

                $data = $db->getRow("select * from term_permission where term='$_GET[term]' and exam='$exam[name]' and class='$_GET[class]' ");
                $start = $data['start_date'];
                $start = str_replace(" ", "T", $start);

                $end = $data['end_date'];
                $end = str_replace(" ", "T", $end);
				?>
				<tr> 
    				<input type="hidden" name="exams[]" value="<?php echo $exam['id']; ?>" />
    				<input type="hidden" name="exam_name<?php echo $exam['id']; ?>" value="<?php echo $exam['name']; ?>" />
                	<td><?php echo $i; ?></td>
                	<td><?php echo $_GET['class']; ?></td>
                	<td><?php echo $exam['name']; ?></td>
                	<td><input type="checkbox" name="exam_<?php echo $exam['id']; ?>" <?php if(count($data)>1){echo "checked";} ?> /></td>
                	<td><input type="text" name="order[]" value="<?php echo $data['ordering']; ?>" /></td>
                	
                    <td>
                        <input type="radio" name="co_scholastic" value="<?php echo $exam['id']; ?>" <?php if($data['co_scholastic']==$exam['id'])echo "checked"; ?> />
                    </td>
                    <td><input type="datetime-local" name="start_date<?php echo $exam['id']; ?>" value="<?php echo $start; ?>" style="width:150px;"  /></td>
                    <td><input type="datetime-local" name="end_date<?php echo $exam['id']; ?>" value="<?php echo $end; ?>" style="width:150px;"  /></td>
                    
                </tr>
				<?php $j++;
			}
			?></table>
			</div>
		</div></div></div></div></form><?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>
    <div>
        <form method="get" action="">

        <input type="hidden" name="src" value="ExamCBSE" />
        <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />

            <div class="control-group" style="display:inline-block;">
                <label class="control-label" style="color:white;">Select Term</label>
                <div  class="controls">
                    <select name="term">
                        <option value=""> -- Select -- </option>
                        <option value="Term1"> Term1 </option>
                        <option value="Term2"> Term2 </option>
                        <!--<option value="Final"> Final </option>-->
                    </select>
                </div>
            </div>

            <div class="control-group" style="display:inline-block;">
                <label class="control-label" style="color:white;">Class</label>
                <div  class="controls">
                    <select name="class">
                        <option value=""> -- Select Class -- </option>
                        <?php
                            $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."' ");
                            foreach($class as $cla){
                                ?>
                                <option value="<?php echo $cla["class"] ?>">
                                    <?php echo $cla["class"] ?>
                                </option>
                                <?php
                            }
                        ?>
                    </select>
                </div>
            </div>

            <button type="submit" name="add_term" class="btn btn-primary"> Add Term</button>
        </form>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM term_permission "); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Term</th>
                    <th>Exam</th>
                    <th>Class</th>
                    <th>Order</th>

                    <!--<th>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["term"] ?></td>
                    <td><?php echo $clist["exam"]; ?></td>
                    <td><?php echo $clist["class"]; ?></td>
                    <td><?php echo $clist["ordering"] ?></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> -->
                    <!--</td>-->
                </tr>
            <?php } ?>
            </tbody>

        </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 -->