<?php
$pgMod="school_code";
$pgAct="view";


if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
			unset($_SESSION['form']);
if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;

	if(!isset($_POST['code']) || trim($_POST['code'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter School code.");
	}
    elseif(!isset($_POST['school_url']) || trim($_POST['school_url'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter school URL.");
	}
	    elseif(!isset($_POST['login_session']) || trim($_POST['login_session'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Session is mandatory for students login.");
	}
	
	$code = $_POST['code'];
	$url =  $_POST['school_url'];

// 	$checkexists = $db->getRow("SELECT * from all_school_codes where code='".$code."' or school_url='".$url."' ");
// 	if(count($checkexists)>0){
// 	    $flgEr=TRUE;
// 	    array_push($alert_err, "School CODE or URL already exists.");
// 	}
//     else {
    if($flgEr!=TRUE && $pgAct=="add")
	{
		$aryData=array(
					'code'	=>	$_POST['code'],
					'school_url'	=>	$_POST['school_url'],
					'login_session' => $_POST['login_session'],
			);

	 	    $flgIn=$db->insertAry("all_school_codes",$aryData);	

		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
		}
		else
		{
		array_push($alert_err,$db->getErMsg());
		}

	}
		elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
    		$aryData=array(
					'code'	=>	$_POST['code'],
					'school_url'	=>	$_POST['school_url'],
					'login_session' => $_POST['login_session'],
			);
		$flgUp=$db->updateAry("all_school_codes",$aryData,"where id='".$_GET['id']."'");
	
		if(!is_null($flgUp))
		{ 
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	}
	
//}

// 	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
// 	{
// 		$flgUp=$db->updateAry("student_notice",$aryData,"where added_by='".$_SESSION["admin"]["uid"]."' and id='".$_GET['id']."'");
// 		if(!is_null($flgUp))
// 		{ 
// 			$_SESSION['msg']='update Successfully';
// 			unset($_SESSION['form']);
// 			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));
// 		}
// 		else
// 		{
// 			array_push($alert_err,$db->getErMsg());
// 		}
// 	}
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("all_school_codes","where  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"=>"Promote", "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>complain</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>Student Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM all_school_codes WHERE  id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{

 ?>
<!-- Forms --> 


<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: 96.7% !important;overflow: inherit !important;} 
</style>

	<div class="row-fluid">
        <form method="post" enctype="multipart/form-data" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create a New School Code</h3>
				</div>
				<!-- / Forms: Top Bar -->

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

                        <div class="control-group">
							<label class="control-label" for="Name">School Code</label>
							<div class="controls">
								<input type="text" id="upload_notice" name="code" class="input-block-level" value="<?php echo $aryForm['code']; ?>" required>
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" for="detail">School URL</label>
							<div class="controls">
								<input type="text" id="upload_notice" name="school_url" class="input-block-level" placeholder="bps.savarnacloud.com" value="<?php echo $aryForm['school_url']; ?>" required>
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" for="detail">Login Session</label>
							<div class="controls">
								<input type="text" id="upload_notice" name="login_session" class="input-block-level" placeholder="" value="<?php echo $aryForm['login_session']; ?>" required>
							</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{
?>
<style>
.hoverclass{
hover:hoverclass;
}
</style>    

	<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=Promote&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"=>"Promote", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM all_school_codes"); $i=0;

	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            
            <thead>
                <tr>
                    <th>S. No.</th>
                    <th>Code</th>
                    <th>School Url</th>
                    <th>Login Session</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['code']; ?></td>
              		<td><?php echo $clist['school_url']; ?> </td>
              		<td><?php echo $clist['login_session']; ?> </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></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>complain</b> Record not found </i>
        </div>
        <?php
	} 
	?>
	</div></div></div>
	<?php
}
?>
<!-- / Forms -->