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


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['type']) || trim($_POST['type'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter fee type.");
	}
	elseif(!isset($_POST['class']) || trim($_POST['class'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select Class.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$_POST["session"]=$_SESSION["admin"]["session"];
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
		$_POST["added_by"]=$_SESSION["admin"]["uid"];
		//echo "<pre>";print_r($_POST);exit;
		$flgIn=$db->insertAry("extrafee_type",$_POST);
		if(is_null($db->getErMsg()))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("extrafee_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("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}
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=="allocate" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM extrafee_type WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="allocate" && 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 a New Extra fee structure</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">
						<div class="control-group">
							<label class="control-label" for="type">Fees Type</label>
							<div class="controls">
								<input type="text" id="type" readonly="readonly" name="type" placeholder="tusion fee or computer fee etc." required class="input-block-level" value="<?php if($_POST)echo $_POST["type"]; else echo $aryForm["type"] ?>">
							</div>
						</div>
                        
						<div class="control-group">
							<label class="control-label" for="class">Class Name </label>
							<div class="controls">
								<select name="class" readonly="readonly" id="class">
                                    <option value="">Class</option>
                                    <?php 
                                        $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                                                <?php echo $cla["class"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="type">Fees Total</label>
							<div class="controls">
								<input type="text" readonly="readonly" id="total" name="total" placeholder="Enter Total fee." required class="input-block-level" onkeyup="devideEqualFeetoall(this)" value="<?php if($_POST)echo $_POST["total"]; else echo $aryForm["total"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="">Student List<input type="checkbox" id="checkboxa" onchange="checkCheckedAll()" /></label>
							<div class="controls">
								<?php
								$stduentLst=$db->getRows("select idno,name from student where class='".$aryForm["class"]."'");
								if(is_array($stduentLst) && count($stduentLst)>0){
									foreach($stduentLst as $sList){
										?><label class="cjkLable" for="scheck<?php echo $sList["idno"] ?>">
                                        <input type="checkbox" class="chk" id="scheck<?php echo $sList["idno"] ?>" value="<?php echo $sList["idno"] ?>" />
                                        <?php echo $sList["name"] ?>
                                        </label> <?php
									}
								}
								?>
							</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> </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM extrafee_type  where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                	<th>Fee Type</th>
                    <th>Class</th>
                    <th>Fee</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["type"] ?></td>
                    <td><?php echo $clist["class"];//."(".$clist["medium"].")" ?></td>
                    <td><?php echo $clist["total"] ?></td>
                    <td>
                    <?php $aryPgAct['action']="allocate";$aryPgAct['page_id']=$pgMod; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Allocate Now</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 -->
<script>
function devideEqualFeetoall(id){
	var total=$(id).val();
	var chked=$('input[type="checkbox"].chk:checked').length;
	if(chked==0){
		alert("please check atleast 1 check box ");
		$(id).val('');$(".monthfeeDetail").val('');
	}else{
		eqial12=total/chked;
		$(".monthfeeDetail").val('');
		$('input[type="checkbox"].chk:checked').each(function(index, element) {
			var name=$(this).val();
			$("#"+name).val(eqial12);
		});
		
	}
	
}
function checkCheckedAll(){
	if($('#checkboxa').prop('checked')) {
		$('.chk').prop('checked', true);
    } else {
        $('.chk').prop('checked', false);
    }
}

$(document).ready(function(e) {
	$('.monthfeeDetail, #total').keydown(function(event) {
	if(event.shiftKey)
	{
		event.preventDefault();
	}
   if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 190 || event.keyCode == 110 )   {
   }
   else {
        if (event.keyCode < 95) {
          if (event.keyCode < 48 || event.keyCode > 57) {
                event.preventDefault();
          }
        }
        else {
              if (event.keyCode < 96 || event.keyCode > 105) {
                  event.preventDefault();
              }
        }
      }
   });
});

</script>