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

if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	$subject=explode(",",$_POST["name"]);
	$marks=explode(",",$_POST["marks"]);
	if(!isset($_POST['name']) || trim($_POST['name'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please Enter name.");
	}elseif(!isset($_POST['type']) || trim($_POST['type'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please Select type.");
	}elseif(!isset($_POST['marks']) || trim($_POST['marks'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please Enter marks.");
	}
	elseif(count($subject) != count($marks))
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please Enter same Quantity of marks as per name.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$array=array("type"=>$_POST["type"],
					"class"=>$_POST["class"],
					"underof"=>$_POST["underof"],
					"school_id"=>$_SESSION["admin"]["school_id"],
					"added_by"=>$_SESSION["admin"]["uid"],
					"session"=>$_SESSION["admin"]["session"]);
		
		if(is_array($subject) && count($subject)>0){
			for($i=0; $i<count($subject);$i++){
				$array["name"]=$subject[$i];
				$array["marks"]=$marks[$i];
				$flgIn=$db->insertAry("naz_type",$array);
			}
			if($db->getErMsg()!=""){
				array_push($alert_err,"Error :".$db->getErMsg());
			}else{
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
			}
		}
		array_push($alert_err,$db->getErMsg());
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$array=array("name"=>$_POST["name"],
					"type"=>$_POST["type"],
					"underof"=>$_POST["underof"],
					"school_id"=>$_SESSION["admin"]["school_id"],
					"added_by"=>$_SESSION["admin"]["added_by"],
					"session"=>$_SESSION["admin"]["session"],
					"marks"=>$_POST["marks"]);
		$flgUp=$db->updateAry("naz_type",$_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 subject WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
	<script>
    	function checktype(id,div){
			var type=$(id).val();
			var cla=$("#class").val();
			$("#subjetsL").html("");$("#criteriasL").html("");
			$.get("ExamCBSE/ajax.php?type=naz_type&myyupe="+type+"&selected=<?php echo $aryForm["underof"] ?>&cla="+cla,function(data){
				$("#"+div).html(data);
			})
		}
		function checktypeNew(id,div,under){
			var type=$(id).val();
			var cla=$("#class").val();
			var underof=$(under).val();
			$.get("ExamCBSE/ajax.php?type=naz_type&myyupe=mybubnews&unserof="+underof+"&cla="+cla,function(data){
				$("#"+div).html(data);
			})
		}
    </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 Head</h3>
				</div>
				<!-- / Forms: Top Bar -->
				<input type="hidden" name="session" value="<?php echo $_SESSION["admin"]["session"] ?>">
				<!-- Forms: Content -->
				<div class="well no-padding">
						<div class="control-group">
							<label class="control-label" for="name">Name</label>
							<div class="controls">
								<input name="name" type="text" id="name" 	class="input-block-level" value="<?php if($_POST)echo $_POST["name"]; else echo $aryForm["name"] ?>">
                                use comma seprate for add multiple
							</div>
						</div>
                        <?php
						$alltype=$db->getRows("SELECT * FROM class where school_id='".$_SESSION["admin"]["school_id"]."'");
						?><div class="control-group">
							<label class="control-label" for="class">Select Class</label>
							<div class="controls">
								<select name="class" id="class">
								<option value="">Select class</option><?php
								foreach($alltype as $atype){
									?><option <?php if($aryForm["class"]==$atype["class"])echo 'selected';?> value="<?php echo $atype["class"]; ?>"><?php echo $atype["class"] ?></option><?php
								}
								?></select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="class">Select Type</label>
							<div class="controls">
								<select name="type" onChange="checktype(this,'subjetsL')" id="type">
                                    <option value="">Select</option>
                                    <option <?php if("subject"==$aryForm["type"])echo 'selected';?> value="subject">Subject</option>
                                    <option <?php if("criteria"==$aryForm["type"])echo 'selected';?> value="criteria">Criteria</option>
                                    <option <?php if("subcriteria"==$aryForm["type"])echo 'selected';?> value="subcriteria">Sub Criteria</option>
                                </select>
							</div>
						</div>
                        <div id="subjetsL">
						</div>
                        <div id="criteriasL">
						</div>
                        <div class="control-group">
							<label class="control-label" for="marks">Marks</label>
							<div class="controls">
								<input name="marks" type="text" id="marks" class="input-block-level" value="<?php if($_POST)echo $_POST["marks"]; else echo $aryForm["marks"] ?>">
							</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{
	?>
    <script>
    	function rediectMeNow(id){
			var no=$(id).val();
			window.location='./?src=ExamCBSE&page_id=naz_subject&typem='+no;
		}
    </script>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> Subject Criteria</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 Subject Criteria</button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$where=" and type='subject'";
	if(isset($_GET["typem"]) && $_GET["typem"]!="")
		$where=" and type='".$_GET["typem"]."'";
	$aryList=$db->getRows("SELECT * FROM naz_type where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $where"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <div style="margin:20px;float: left;position: absolute;z-index: 10;">
        <select name="typem" onchange="rediectMeNow(this)">
        	<option value="subject">Subject</option>
            <option value="criteria">Criteria</option>
            <option value="subcriteria">Sub Criteria</option>
        </select>
        </div>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Class</th>
                    <th>Marks</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 $clist["name"] ?></td>
                    <td><?php echo $clist["type"] ?></td>
                    <td><?php echo $clist["class"] ?></td>
                    <td><?php echo $clist["marks"] ?></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 colspan="3"></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 -->