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



if(isset($_POST['save']))
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE; 
	if(!isset($_POST['total']) || trim($_POST['total'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter total."); 
	}
	if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("examterm",$_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"=>"Account", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}
if(isset($_POST['save_fine'])){

    $i = 0;
    foreach($_POST['cls'] as $cls){
        $fee    = $_POST['fee'][$i];
        $amount = $_POST['amount'][$i];
        $fdate  = $_POST['fdate'][$i];
        $tdate  = $_POST['tdate'][$i];

            $saveary = array(
                "class"     =>  $cls,
                "fee"       =>  $fee,
                "amount"    =>  $amount,
                "fdate"     =>  $fdate,
                "tdate"     =>  $tdate,
                "time"      =>  $_POST['time'][$i],
                "session"   =>  $_SESSION["admin"]["session"],
                "added_by"  =>  $_SESSION["admin"]["uid"],
                "school_id" =>  $_SESSION["admin"]["school_id"]
            );
            
            $check = $db->getVal("select id from fine_setting where class='$cls' and session='".$_SESSION["admin"]["session"]."' ");

            if($fee!="" && $amount !="" && $fdate!="" && $tdate!=""){
                if($check > 0){
                    $flg = $db->updateAry("fine_setting", array("fee"=>$fee, "amount" => $amount, "fdate"=>$fdate, "tdate" => $tdate, "time"=>$_POST['time'][$i]), "where class='$cls' and session='".$_SESSION["admin"]["session"]."'");
                } else {
                    $flg = $db->insertAry("fine_setting", $saveary);
                }
            }

        $i++;
    }

    redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Account", "page_id"=>$pgMod)));
    
}
if($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("fine_setting","where session='".$_SESSION["admin"]["session"]."' 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"=>"Account", "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
}
		?>
		<form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Add Fine Setting</h3>
                    <div>
						<button type="submit" name="save_fine" class="btn btn-primary"> Save Setting</button> 
					</div>
				</div>
				<div class="well no-padding" style="height:auto;">

            <h3 style="text-align:center;"><?php echo $exam['name']; ?></h3>
            <div id="printableDiv">
            <table class="table">
			<tr>
                <th>SNo.</th>
                <th>Class</th>
                <th>Fee Type</th>
                <th>Amount</th>
                <th>Start Date</th> 
                <th>End Date</th>
                <th>Time</th>
            </tr> 
			<?php $i=0; $j = 0;
			
			$allclass = $db->getRows("SELECT class from class ");
			foreach($allclass as $cls){$i++;
                $ary = explode("-",$cls['newclass']);

                $aryForm=$db->getRow("SELECT * FROM fine_setting WHERE session='".$_SESSION["admin"]["session"]."' and class='".$cls['class']."'");

				?>
				<tr> 
                	<td><?php echo $i; ?></td> 
                	<td><?php echo $cls['class']; ?>
                	<input type="hidden" name="cls[]" value="<?php echo $cls['class']; ?>" />
                	</td> 
                	<td>
                	    <select name="fee[]">
                	        <option value="">--Select Fee Type--</option>
                	        
                	        <?php
                	        
                	        $allfees = $db->getRows("select * from otherfee_structure where session='".$_SESSION['admin']['session']."' AND name LIKE 'Latefine%' group by name ");
                	                foreach($allfees as $fee){ ?>
                	                <option value="<?php echo $fee["name"]; ?>" <?php if($aryForm['fee']==$fee['name']){echo "selected";} ?> ><?php echo $fee["name"]; ?></option>
                	                <?php } ?>
                	    </select>
                	</td>

                	<td><input type="text" name="amount[]" value="<?php echo $aryForm['amount']; ?>" style="width:80px;" /></td>

            	    <td>
            	        <input type="date" name="fdate[]" value="<?php echo $aryForm['fdate']; ?>" style="width:150px;"  />
            	    </td>
            	    <td>
            	        <input type="date" name="tdate[]" value="<?php echo $aryForm['tdate']; ?>" style="width:150px;"  />
            	    </td>
            	    <td>
            	        <input type="time" name="time[]" value="<?php echo $aryForm['time']; ?>" style="width:150px;"  />
            	    </td>
                </tr>
				<?php $j++;
			}
			?></table>
			</div>
		</div></div></div></form>





