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


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['grade']) || trim($_POST['grade'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please Enter Grade.");
	}elseif(!isset($_POST['minrange']) || trim($_POST['minrange'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Minimum marks.");
	}elseif(!isset($_POST['maxrange']) || trim($_POST['maxrange'])=='')
	{
		$flgEr=TRUE; 
		array_push($alert_err,"Please enter Maximum marks.");
	}elseif(!isset($_POST['point']) || trim($_POST['point'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Point.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
        $_POST["added_by"]=$_SESSION["admin"]["uid"];

		$flgIn=$db->insertAry("examgrade",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
	    $_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgUp=$db->updateAry("examgrade",$_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());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("subject","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"=>"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 examgrade 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> Create Grading System</h3>
				</div>
				<!-- Forms: Content -->
				<div class="well no-padding">
                        <div class="control-group">
							<label class="control-label" for="grade">Grade</label>
							<div class="controls">
								<input type="text" placeholder="eg. A or B or C" id="grade" name="grade" class="input-block-level" value="<?php if($_POST)echo $_POST["grade"]; else echo $aryForm["grade"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="range">Grade Range</label>
							<div class="controls">
								<input type="number" id="minrange" placeholder="Minimum Marks" name="minrange" class="input-block-level span6" value="<?php if($_POST)echo $_POST["minrange"]; else echo $aryForm["minrange"] ?>">
                                <input type="number" id="maxrange" placeholder="Maximum Marks" name="maxrange" class="input-block-level span6" value="<?php if($_POST)echo $_POST["maxrange"]; else echo $aryForm["maxrange"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="grade">Point</label>
							<div class="controls">
								<input type="text" id="point" placeholder="eg. 10.0 or 9.0" name="point" class="input-block-level" value="<?php if($_POST)echo $_POST["point"]; else echo $aryForm["point"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="subject">Colour Code</label>
							<div class="controls">
								<input type="text" placeholder="eg. #ff0066" name="colour" class="input-block-level" value="<?php if($_POST)echo $_POST["colour"]; else echo $aryForm["colour"] ?>" />
							</div>
						</div>
						<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=ExamCBSE&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"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM examgrade  where school_id='".$_SESSION["admin"]["school_id"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S. No.</th>
                    <th>Grade</th>
                    <th>Range</th>
                    <th>Point</th>
                    <th>Colour Code</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["grade"] ?></td>
                    <td><?php echo $clist["minrange"]."-".$clist["maxrange"] ?></td>
                    <td><?php echo $clist["point"] ?></td>
                    <td> 
                        <div style="width:50px;height:20px;background:<?php echo $clist["colour"] ?>;"></div>
                        (<?php echo $clist["colour"]; ?>)
                    </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>
        </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 -->