<style>.top-bar+.well, .accordion .accordion-heading+.accordion-body{overflow:inherit !important;}</style>
<?php
$pgMod="otherfee_structure";
$pgAct="view";
$monthsList = array(
    1=>'Apr',
    2=>'May',
    3=>'Jun',
    4=>'Jul ',
    5=>'Aug',
    6=>'Sep',
    7=>'Oct',
    8=>'Nov',
    9=>'Dec',
	10=>'Jan',
    11=>'Feb',
    12=>'Mar'
);
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['name']) || trim($_POST['name'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter fee title.");
	}elseif(!isset($_POST['fee']) || trim($_POST['fee'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Fees.");
	}
	elseif(!isset($_POST['class']) || trim($_POST['class'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select Class.");
	}
	elseif(!isset($_POST['medium']) || trim($_POST['medium'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select Medium.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$saveAry=array( "session"=>$_SESSION["admin"]["session"],
						"school_id"=>$_SESSION["admin"]["school_id"],
						"added_by"=>$_SESSION["admin"]["uid"],
						"class"=>$_POST["class"],
						"otype"=>$_POST["otype"],
						);
		$nameList=explode(",",$_POST["name"]);
		$feeList=explode(",",$_POST["fee"]);
		$month="";
		if(isset($_POST['month']) && count($_POST['month']))
			$month=$_POST['month'];
		//print_r($month);exit;
		if(is_array($nameList) && count($nameList)>0){$i=0;
			foreach($nameList as $name){
				$saveAry["name"]=$name;
				$saveAry["fee"]=$feeList[$i];
					if($month!="")
				$saveAry["month"]=$month[$i];
				$flgIn=$db->insertAry("otherfee_structure",$saveAry);
				$i++;
			}
		}
		
		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'])!='')
	{
		//print_r($_POST);exit;
		extract($_POST);$i=0;
		$saveAry=array( 
						"class"=>$_POST["class"],
						"otype"=>$_POST["otype"],
						);
				$saveAry["name"]=$_POST["name"];
				$saveAry["fee"]=$_POST["fee"];
				$saveAry["month"]=$_POST["month"][$i];
				//print_r($saveAry);exit;			
		$flgUp=$db->updateAry("otherfee_structure",$saveAry,"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());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$os=$db->getRow("select * from otherfee_structure where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET["id"]."'");
	$res=$db->delete("otherfee_structure","where  school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	$insname=$os["class"]."-otherfee";
	$res=$db->delete("installment_fine","where school_id='".$_SESSION["admin"]["school_id"]."' and ins_name='".$insname."' and reasion='".$os["name"]."'");
	$detail="Student Other fee".$os["name"];
	$res=$db->delete("transaction","where school_id='".$_SESSION["admin"]["school_id"]."' and detail='".$detail."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("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 otherfee_structure 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 a New otherfee structure</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">
						<div class="control-group">
							<label class="control-label" for="name">Fees Title</label>
							<div class="controls">
								<input type="text" id="name" name="name" placeholder="tusion fee,computer fee etc." required class="input-block-level" value="<?php if($_POST)echo $_POST["name"]; else echo $aryForm["name"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="class">Select Month</label>
							<div class="controls">
								<select name="month[]" id="month" multiple="multiple">
                                    <option value="">Select some months</option>
                                    <?php 
                                        			   if(is_array($monthsList) && count($monthsList)>0){
													foreach($monthsList as $key=>$value){
														?>
                                            <option <?php if($key==$aryForm["month"])echo 'selected';?> value="<?php echo $key ?>"> 
												<?php echo $value ?>
                                            </option>
                                            <?php
                                        } }
                                    ?>
                                </select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="fee">Fixed Fees</label>
							<div class="controls">
								<input type="text" id="fee" placeholder="150,120 etc." name="fee" required class="input-block-level" value="<?php if($_POST)echo $_POST["fee"]; else echo $aryForm["fee"] ?>">
							</div>
						</div>
                        
						<div class="control-group">
							<label class="control-label" for="class">Class Name</label>
							<div class="controls">
								<select name="class" id="class" onchange="getStudentlist(this.value)">
                                    <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="otype">Offer to</label>
							<div class="controls" id="studentList">
								<select name="otype" id="otype">
                                    <option <?php if(""==$aryForm["otype"])echo 'selected';?> value="">All Student</option>
                                    <option <?php if("new"==$aryForm["otype"])echo 'selected';?> value="new">New Student</option>
                                    <option <?php if("old"==$aryForm["otype"])echo 'selected';?> value="old">Old Student</option>
                                </select>
							</div>
						</div>
                        <div class="control-group hide">
							<label class="control-label" for="medium">Medium</label>
							<div class="controls">
								<select name="medium" id="medium">
                                    <option <?php if("hindi"==$aryForm["sibling"])echo 'selected';?> value="hindi">Hindi</option>
                                    <option <?php if("english"==$aryForm["sibling"])echo 'selected';?> value="english">English</option>
                                </select>
							</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='./?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("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM otherfee_structure  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 Title</th>
                    <th>Month</th>
                    <th>Class</th>
                    <th>Fee</th>
                    <th>Apply To</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 foreach($monthsList as $key=>$value){if($key==$clist['month']) echo $value;} ?></td>
                    <td><?php echo $clist["class"];//."(".$clist["medium"].")" ?></td>
                    <td><?php echo $clist["fee"] ?></td>
                    <td><?php if($clist['otype']=="")echo "All Student"; 
							  elseif($clist['otype']!="new" && $clist['otype']!="old" && $clist['otype']!=""){ 
							  		$name= $db->getVal("Select name from student where idno ='".$clist["otype"]."'"); echo $name;} 
							  else echo ucwords($clist['otype'])."  Student"; ?></td>
                    <td>
                    <?php $aryPgAct['action']="edit";$aryPgAct['page_id']=$pgMod; ?>
                         <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 -->
<script>
<?php 
if(is_array($aryForm) && count($aryForm)>0){?>
$(document).ready(function(e) {
    getStudentlist('<?php echo $aryForm["class"] ?>');
});
<?php }?>
function getStudentlist(id){
	$.post( "ajax.php", { type: "getStudentlist" ,class: id <?php if(is_array($aryForm) && count($aryForm)>0) echo ',otype:"'.$aryForm["otype"].'"'; ?>} , function( data ) {
	  	$("#studentList").html(data);
	});
}
</script>