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

if(isset($_POST['psign'])){
  
	if($pgAct=="add_principal")
	{
        if(isset($_FILES["sign"]["name"]) && !empty($_FILES["sign"]["name"])){
                $questionimage = array();
            		$lfilename = basename($_FILES["sign"]["name"]);
            		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
            		$lnewfile=md5(microtime()).".".$lext; 
            		if(move_uploaded_file($_FILES["sign"]["tmp_name"],"uploads/sign/".$lnewfile))
                		{
                		    $keytermArray = array(
                		        "class"     => '',
                		        "section"   => '',
                		        "sign"      => $lnewfile,
                		        "type"      => "Principal",
                 		        'added_by'  => $_SESSION['admin']['uid'],
                				'session'   => $_SESSION['admin']['session'],
                		        );
                		      $flgIn =  $db->insertAry("sign", $keytermArray);
                		}
    	}

		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());
			
		}
	}
    
}

if($_POST)
{
	$flgEr=FALSE; 
	if(!isset($_POST['class']) || trim($_POST['class'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select class.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
        if(isset($_FILES["sign"]["name"]) && !empty($_FILES["sign"]["name"])){
                $questionimage = array();
            		$lfilename = basename($_FILES["sign"]["name"]);
            		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
            		$lnewfile=md5(microtime()).".".$lext; 
            		if(move_uploaded_file($_FILES["sign"]["tmp_name"],"uploads/sign/".$lnewfile))
                		{
                		    $keytermArray = array(
                		        "class"     => $_POST['class'],
                		        "section"   => $_POST['section'],
                		        "sign"      => $lnewfile,
                		        "type"      => "Class Teacher",
                 		        'added_by'  => $_SESSION['admin']['uid'],
                				'session'   => $_SESSION['admin']['session'],
                		        );
                		      $flgIn =  $db->insertAry("sign", $keytermArray);
                		}
    	}

		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'])!='')
// 	{
// 		$flgUp=$db->updateAry("sign",$_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'])!='')
{
    $file = $db->getRow("select * from sign where id='".$_GET['id']."' ");
    
    unlink("uploads/sign/$file[sign]");
    
	$res=$db->delete("sign","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"=>"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 grade WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
	<div class="row-fluid">
        <form method="post" action="" enctype="multipart/form-data">
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Add Signature</h3>
				</div>
				<!-- / Forms: Top Bar -->
				<input type="hidden" name="session" value="<?php echo $_SESSION["admin"]["session"] ?>">
				<!-- Forms: Content -->
				<div class="well no-padding">
						<?php $typeCriteria=$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" class="span6" id="criteria" style="float: right;">
                                    <option value="">--Select Class--</option>
                                    <?php
                                    if(is_array($typeCriteria) && count($typeCriteria)>0){
                                        foreach($typeCriteria as $tc){
                                            ?><option <?php if($aryForm["class"]==$tc["class"]) echo 'selected="selected"';?> value="<?php echo $tc["class"] ?>" ><?php echo $tc["class"] ?></option><?php	
                                        }
                                    }
                                    ?>
                               </select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="sub_criteria">Section</label>
							<div class="controls subc">
							    <?php $typeCriteria=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."' "); ?>
								<select name="section" class="span6" id="sub_criteria" style="float: right;">
                                    <option value="">Select Section</option>
                                    <?php
                                    if(is_array($typeCriteria) && count($typeCriteria)>0){
                                        foreach($typeCriteria as $tc){
                                            ?><option <?php if($aryForm["section"]==$tc["name"]) echo 'selected="selected"';?> value="<?php echo $tc["name"] ?>" ><?php echo $tc["name"] ?></option><?php	
                                        }
                                    }
                                    ?>
                               </select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="class">Upload Sign</label>
							<div class="controls">
								<input type="file" name="sign" class="input-block-level" />
							</div>
						</div>
      <!--                  <div class="control-group">-->
						<!--	<label class="control-label" for="subject">Detail</label>-->
						<!--	<div class="controls">-->
						<!--		<input type="text" id="detail" name="detail" class="input-block-level" value="<?php if($_POST)echo $_POST["detail"]; else echo $aryForm["detail"] ?>" />-->
						<!--	</div>-->
						<!--</div>-->
      <!--                  <div class="control-group">-->
						<!--	<label class="control-label" for="subject">Colour Code</label>-->
						<!--	<div class="controls">-->
						<!--		<input type="text" 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
}
elseif($pgAct=="add_principal" || ($pgAct=="edit_principal" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
	<div class="row-fluid">
        <form method="post" action="" enctype="multipart/form-data">
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Add Signature</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="class">Upload Principal <br> Sign</label>
							<div class="controls">
								<input type="file" name="sign" class="input-block-level" />
							</div>
						</div>

						<div class="form-actions">
							<button type="submit" name="psign" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>

			</div>

        </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_principal'" class="btn" type="button">Add Principal Sign</button>
    <button style="float:right; margin-top:5px" onClick="window.location='./?src=ExamCBSE&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add Teacher Sign</button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM sign order by type DESC "); $i=0;
	if(is_array($aryList) && count($aryList)>0) 
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Class</th>
                    <th>Section</th>
                    <th>Type</th>
                    <th>Sign</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["class"] ?></td>
                    <td><?php echo $clist["section"] ?></td>
                    <td><?php echo $clist["type"] ?></td>
                    <td> 
                        <img src="../uploads/sign/<?php echo $clist['sign']; ?>" style="width:80px;height:40px;" />
                    </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
}
?>
<script type="text/javascript">
function fcriteria(){
	var subc=$("#criteria").val();
	$.post( "ExamCBSE/ajax.php", { type: "scriteria" ,subc: subc } , function( data ) {
		//alert(data);
	  	$(".subc").html(data);
	});
}
</script>
<!-- / Forms -->