<?php 
// $monthsList1 = 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'
// );
$allmonths = $db->getRows("select * from months");

$monthsList1 = array();
    foreach($allmonths as $my){
        $monthsList1[$my['month']] = $my['name'];
    }
$m=$_GET["month"];
// if($month<4){
// 			$m=($month+9);
// 		  }
// 		  else{
// 			$m=$month-3;
// 		  } 
?>
<style>
.popup-form{
	margin:6px;
	border:2px solid #600;
	padding:6px;
}
.btn-new{
  background-color: #05967A;
  color: #fff;
  border: 2px outset #05967A;
  padding: 4px 15px;
  font-weight: 700;
  border-radius: 5px;
  box-shadow: 0px 2px #352E2E;
}
.btn-delete{
  background-color: #CA1A1A;
  border: 2px outset #CA1A1A;
  box-shadow: 1px 3px #322C2C;
  color: #fff;
  padding: 4px 15px;
  font-weight: 700;
  border-radius: 5px;
}
.select-tab{
	padding: 3px;
  border: 2px solid #747171;
  color: #380303;
  font-weight: 600;
}
.level, .btn-new, .btn-delete{
	margin-left: 10px;
  margin-right: 5px;
}
.textbox{
	width:100px;
	border:2px inset red;
}
</style>
<?php		
if($_POST){
	if($_POST['migrate']){ //print_r($_POST);exit;
	 if(is_array($_POST["idno"]) && count($_POST["idno"])>0){
		if(isset($_POST["fine"]) && $_POST["fine"]>0){$c=0;
		for($i=0; $i<count($_POST["idno"]);$i++){
					

					$res=$db->getVal("Select id from otherfee_structure where session ='".$_SESSION["admin"]["session"]."' and class ='".$_POST['class']."' and name = 'latefine_".$monthsList1[$m]."' and otype='".$_POST["idno"][$i]."'");

					//echo $db->getLastQuery();
					if($res == ""){
						$saveAry=array( "session"=>$_SESSION["admin"]["session"],
									"school_id"=>$_SESSION["admin"]["school_id"],
									"added_by"=>$_SESSION["admin"]["uid"],
									"class"=>$_POST["class"][$i],
									"otype"=>$_POST["idno"][$i],
									"month"=>$m,
									"fee"=>$_POST["fine"]
									);
						$saveAry["name"]="Latefine_".$monthsList1[$m];
					//print_r($saveAry);
										
						$flgIn=$db->insertAry("otherfee_structure",$saveAry);
						echo "Rs ".$_POST["fine"]." has been successfully added as fine for student <strong>".$_POST["name"][$i]."</strong><br />";
			        }else{
						echo "Fine for student <strong>".$_POST["name"][$i]." already added of month ".$monthsList1[$m].".</strong><br />";
					}
			}
	 	}
		else echo "please enter fine amount";
		}	
	}
	
	elseif($_POST['delete']){
		/*$qry=("Delete from otherfee_structure where session ='".$_POST['session']."' and class ='".$_POST['class']."' and name ='Back_Dues'");
		echo $qry;exit;
		$flgIn1=mysql_query($qry);*/
		for($i=0; $i<count($_POST["idno"]);$i++){
			$res=$db->delete("otherfee_structure","where session ='".$_SESSION["admin"]["session"]."' and class ='".$_POST['class']."' and name = 'latefine_".$monthsList1[$m]."' and otype='".$_POST["idno"][$i]."'");
		}
			if(!is_null($res)) { echo "All data deleted Successfully"; }
			else { array_push($alert_err,$db->getErMsg()); }
		}
}else{
	
		
?>
<form method="post" action="">
<div class="popup-form">
  
<strong class="level">Class</strong>
<input type="text" class="textbox" name="class" id="class" readonly="readonly" value="<?php echo $_GET["class"]?>" />
<strong class="level">Month</strong>
<input type="text" class="textbox" name="month" id="month" readonly="readonly" value="<?php echo $monthsList1[$m]?>" />
<strong class="level">Fine Amount</strong>
<input type="text" class="textbox" name="fine" id="fine" value="" />       
<input type="submit" class="btn-new" name="migrate" value="Update" />
<input type="submit" class="btn-new" name="delete" value="Delete" />
</div> 
<table class="table" width="100%" border="1" cellpadding="0" cellspacing="0" style="text-align:center;color:#0F05AD;">
<tr>
	<th>No.</th>
    <th>Class</th>
    <th>Name</th>
    <th>Migrate Fee</th>
</tr>
<?php $i=0;

foreach($_GET as $key=>$val){
	$name=explode("`",$key);
	$class = $db->getVal("select class from student where idno=$name[1] ");
	if(is_numeric($val) && $key!="class" && $key!="month" && $val!=0){$i++;
	
	?><tr>
    
    <td><?php echo $i; ?></td>
    <td><?php echo $class;//$_GET["class"]; ?></td>
    <td>
        <input type="hidden" name="class[]" value="<?php echo $class ?>">
        <input type="hidden" name="idno[]" value="<?php echo $name[1] ?>">
    	<input type="hidden" name="name[]" value="<?php echo $name[0] ?>"><?php echo $name[0] ?></td>
    <td><input type="hidden" name="left[]" value="<?php echo $val ?>"><?php echo $val ?></td>
    </tr><?php
	}
}
?>
</table>
</form>
<?php
}
?>