<style>
.popup-form{
	margin:0px;
	border:2px solid #600;
	padding:5px;
}
.title{
  text-align: center;
  color: #fff;
  background-color: #1443BD;
  padding: 4px;
  border: 1px inset #1443BD;
  box-shadow: 0px 0px 10px #050101;
  border-radius: 5px;
 
}
.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-text{
	padding: 3px;
  border: 2px solid #747171;
  color: #380303;
  font-weight: 600;
}
.level, .btn-new, .btn-delete{
	margin-left: 10px;
  margin-right: 5px;
}

.st {
	width: 55px;
	display: inline-block;
	color: #021EF0;
}
.recepit {
	width: 500px !important;
	height: 344px !important;
}
.half {
	width: 90%;
	display: inline-block;
	vertical-align: top;
	border-left: 5px solid #f00;
border-right: 5px solid #f00;
}
.half .input-block-level {
	width: 80px;
}
.half .table td {
	padding: 3px 5px;
}
.half center {
	font-weight: bold;
	border-top: 1px solid #f00;
	padding: 5px;
	background-color: #A7A1A1;
	color: #000;
}

.reset-btn {
    background-color:teal;
    color:white;
    border:none;
    border-radius:12px;
    padding:5px 10px;
    box-shadow:0px 0px 5px #555;
}

.reset-btn:hover {
    cursor:pointer;
}

</style>
<?php
if($_POST){
echo $_GET['id'];	
}
 
$aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
?>
<link rel="stylesheet" href="<?php echo URL_ROOT; ?>js/jquery-ui.css">
<script src="<?php echo URL_ROOT; ?>js/jquery-1.10.2.js"></script>
<script src="<?php echo URL_ROOT; ?>js/jquery-ui.js"></script>
<script>
var add_tr='<tr id="row_1"><td ><input type="text" class="st-forminput" style="width:86%;" name="attr_month[]" /></td><td ><textarea type="text" class="st-forminput" style="width:86%;" name="attr_name[]" ></textarea></td><td><input type="text" class="st-forminput" style="width:90%;" name="attr_value[]" /></td><td><a class="delete" href="javascript:;">X</a></td></tr>';
$(document).ready(function(){
	$("#add").click(function(){
		$("#tbody").append($(add_tr));
	
		$('#tbody td a.delete').on('click',function() {
			var rowCount = $('#tbody tr').length;
			if(rowCount<=1){
				//alert("This item cannot deleted !!!");
			}else{
				$(this).parent().parent().remove(); 
			}
		});	
	});
});
function fetchDetail(ids){
	var values=$(ids).val();
	 $.getJSON("ajax.php?type=getValByRecepit&id="+values, function(field){
		 //alert(field.month)
		$("#month").val(field.month)
		$("#name").val(field.name)
		$("#value").val(field.value)
    });
}
$(function() {
	$(".datepicker").each(function(index, element) {
        var d = new Date(); // for now
		datetext=d.getHours()+": "+d.getMinutes()+": "+d.getSeconds();
		$( this ).datepicker({
		  changeMonth: true,
		  changeYear: true,
		  showOn: "button",
		  buttonImage: "<?php echo URL_ROOT ?>js/images/calendar.gif",
		  buttonImageOnly: true,
		  buttonText: "Select date",
		  dateFormat: 'yy-mm-dd ',
		  onSelect: function(datetext){
			var d = new Date(); // for now
			datetext=datetext+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
			$(this).val(datetext);
		  }
		});
    });
});

function updateFee(id,oldvalue,total,discount,type){
	var newValue=$("#"+type+id).val();

	//alert(newValue);
	//alert(newValue+oldvalue+type);
	if(newValue==""){
		alert("New value for total cannot blank"); 
	}else if(isNaN(parseInt(newValue))){
		alert("New value for total must numeric");
		
	}/*else if(parseInt(newValue)==parseInt(oldvalue)){
		alert("old total and new total both are same");
	}*//*else if(parseInt(newValue)>parseInt(oldvalue)){
		alert("New value cannot more then old total");
	}*/else{
		$.post( "../Promote/ajax.php", { type: "resetdiscount" ,paytype: type, id: id,  newValue: newValue, idno:"<?php echo $_GET["id"] ?>" } , 

		function( data ) {
			//alert(data);
			window.location.reload();
		});
	}
}


function reset_other(idno, other_id, dd_id,name){

	$.post( "../Promote/ajax.php", { type: "resetOther" ,idno: idno, other_id: other_id, dd_id: dd_id, name: name } , 
		function( data ) {
		  //  alert(data);
			window.location.reload();
		});		
}

function reset_hostel_other(idno, other_id, dd_id){

	$.post( "../Promote/ajax.php", { type: "resethostelOther" ,idno: idno, other_id: other_id, dd_id: dd_id } , 
		
		function( data ) {
			window.location.reload();
		});
}

function updateExtraFee(installment, newValue, extra_id){
    	$.post( "../Promote/ajax.php", { type: "resetExtra" ,idno: "<?php echo $_GET["id"] ?>", installment: installment, newValue: newValue, extra_id: extra_id } , 
		
		function( data ) {
			window.location.reload();
		});
}

</script>

       	<div class="half"><center>Tution fee</center>
            	<table class="table">
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th><span class="action">Action</span></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php 

					$aryInstall=$db->getRow("select * from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and session='".$_SESSION["admin"]["session"]."'");

                    $feetotal=$db->getval("select fee from class where school_id='".$_SESSION["admin"]["school_id"]."'  and class='".$_GET['class']."'");
				
                    $feeinstallment=$db->getval("select installment from class where school_id='".$_SESSION["admin"]["school_id"]."'  and class='".$_GET['class']."'");
                    $totalamountpaid=$feetotal/$feeinstallment;

						$feeCount=array();$totalclasspayfee=0;$totalbuspayfee=0;$busFine=0;$schoolFine=0;
				// 		for($i=1;$i<=12;$i++){
        				$allmonths = $db->getRows("select * from months");
                        foreach($allmonths as $mn){
                            $i = $mn['month'];
							$iis="i".$i;
							$iip="ip".$i;
				// 			if($aryInstall[$iis]!="" && $aryInstall[$iis]==0){
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
								$is_rr=false;
								
                                $tution_discount = $db->getVal(" select sum(discount_amount) from discount_detail where idno='".$aryForm["idno"]."' and installment='$iis' and type='tution' ");
								if($tution_discount > 0){
							?>
                            <tr>
                            <td>
                                <?php $ij=$i;
    				// 		    if($ij<=9){
    				// 			$newDate=date("Y")."-".($ij+3)."-1";
    				// 			echo date("F",strtotime($newDate));//."(".($ij+3).")";
    				// 		    }
    				// 		    else{
    				// 			$newDate=date("Y")."-".($ij-9)."-1";
    				// 			echo date("F",strtotime($newDate));//."(".($ij-9).")";
    				// 		    }
    				echo $mn['name'];
    						    ?>
						    </td>
                            <td><input type="text" class="input-block-level" oldValue value="<?php echo $tution_discount; ?>"  >
                            <input type="hidden" id="fee<?php echo $i ?>" class="input-block-level" oldValue value="<?php echo $totalamountpaid; ?>"  name="fee[]">
                            </td>
                            
                            <td><?php if($aryInstall[$iip]>0)echo '<font color="#009900">Paid</font>';else echo '<font color="#FF0000">Discount</font>'; ?></td>
                            <td class="classtd">
								<?php
								if($aryInstall[$iip]>0){}else{
								?><a href="javascript:;" onClick="updateFee('<?php echo $i ?>','<?php echo $iis; ?>','<?php echo $aryInstall["total"];?>','<?php echo $aryInstall[$iis]?>','fee')">
								<button type="button" class="reset-btn">Reset Discount</button></a>
								<?php	
								}
								?>
							</td>
                                <td></td>
                            </tr>
							<?php
							}
						}
					?>
					</tbody>
                </table>
                </div>

                <?php if($aryForm["bus"]!=""){ ?>
                <div  class="half">
                <center>Transport fee</center>
                <table class="table">
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th><span class="action">Action</span></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
					<?php
				
				 $busid=$db->getval("SELECT bus FROM student where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and idno='".$_GET['id']."' ");

					  $bustotal=$db->getval("select fee from bus where school_id='".$_SESSION["admin"]["school_id"]."' and id='$busid' ");
				
				  $businstallment=$db->getval("select installment from bus where school_id='".$_SESSION["admin"]["school_id"]."' and id='$busid'");
                  $totalamountbuspaid=$bustotal/$businstallment;
						$ji=0;
				// 		for($i=1;$i<=12;$i++){
                        foreach($allmonths as $mn){
                            $i = $mn['month'];

							$iis="b".$i;
							$iip="bp".$i;$is_rr=false;
				// 			if($aryInstall[$iis]!="" && $aryInstall[$iis]==0){
								$totalbuspayfee=$totalbuspayfee+$aryInstall[$iip];$ji++;
							$is_rr=false;
							
							$bus_discount = $db->getVal(" select sum(discount_amount) from discount_detail where idno='".$aryForm["idno"]."' and installment='$iis' and type='bus' ");
							if($bus_discount > 0){
							?>
                            <tr <?php if($ji==1)echo 'class="first" style="border-top:2px solid #F00"'; ?>>
                            	<td><?php $ij1=$i;
								// 	  if($ij1<=9){
								// 		$newDate=date("Y")."-".($ij1+3)."-1";
								// 		echo date("F",strtotime($newDate));//."(".($ij+3).")";
								// 	  }
								// 	  else{
								// 		$newDate=date("Y")."-".($ij1-9)."-1";
								// 		echo date("F",strtotime($newDate));//."(".($ij-9).")";
								// 	  }
								echo $mn['name'];
									  ?></td>
                                <td> <input type="text" class="input-block-level" value="<?php echo $bus_discount; ?>"  >
                                <input type="hidden" id="busfee<?php echo $i ?>" class="input-block-level" value="<?php echo $totalamountbuspaid; ?>"  name="busfee[]">
                                </td>
                               
                                <td><?php 
								if($aryInstall[$iip]>0)echo '<font color="#009900">Paid</font>';
								else echo '<font color="#FF0000">Discount</font>'; ?></td>
                                <td class="bustd">
                                <?php
								if($aryInstall[$iip]>0){}else{
								?><a href="javascript:;" onClick="updateFee('<?php echo $i ?>','<?php echo $iis; ?>','<?php echo $aryInstall["bus"]?>','<?php echo $aryInstall["busdiscount"]?>','busfee')">
                                <button type="button" class="reset-btn">Reset Discount</button></a>
								<?php	
								}
								?>
                                </td>
                                <td></td>
                            </tr>
							<?php
							}

						}
					?>
                    </tbody>
                </table>
                </div>
                <?php } ?>	

       	<div class="half"><center>Extra fee</center>
            	<table class="table">
                	
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Amount</th>
                            <th>Fee Name</th>
                            <th>Status</th>
                            <th><span class="action">Action</span></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php 
                    
                    $extrafee_type = $db->getRows("select * from extrafee_type where class='".$_GET['class']."' and session='".$_SESSION["admin"]["session"]."' ");

					$aryInstall=$db->getRow("select * from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and session='".$_SESSION["admin"]["session"]."'");

                    $feetotal=$db->getval("select fee from class where school_id='".$_SESSION["admin"]["school_id"]."'  and class='".$_GET['class']."'");
				
                    $feeinstallment=$db->getval("select installment from class where school_id='".$_SESSION["admin"]["school_id"]."'  and class='".$_GET['class']."'");
                    $totalamountpaid=$feetotal/$feeinstallment;

						$feeCount=array();$totalclasspayfee=0;$totalbuspayfee=0;$busFine=0;$schoolFine=0;
				// 		for($i=1;$i<=12;$i++){
                        foreach($allmonths as $mn){
                            $i = $mn['month'];

							$iis="i".$i;
							$iip="ip".$i;

								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
								$is_rr=false;
								
								foreach($extrafee_type as $extra){

						            $extra_discount = $db->getVal(" select sum(discount_amount) from discount_detail where idno='".$aryForm["idno"]."' and installment='$iis' and type='extra' and other_id='".$extra['id']."' ");
								    if($extra_discount > 0){
								        
								        $paid = $db->getVal("select pay from extrafee where idno='".$aryForm["idno"]."' and type='".$extra['id']."' and pid='p$i' and session='".$_SESSION["admin"]["session"]."' ");
								        ?>
								        <tr>
								            <td>
                                                <?php $ij=$i;
                    				// 		    if($ij<=9){
                    				// 			$newDate=date("Y")."-".($ij+3)."-1";
                    				// 			echo date("F",strtotime($newDate));//."(".($ij+3).")";
                    				// 		    }
                    				// 		    else{
                    				// 			$newDate=date("Y")."-".($ij-9)."-1";
                    				// 			echo date("F",strtotime($newDate));//."(".($ij-9).")";
                    				// 		    }
                    				echo $mn['name'];
                    						    ?>
                						    </td>
			                                <td>
			                                    <input type="text" id="fee<?php echo $i ?>" class="input-block-level" oldValue value="<?php echo $extra_discount; ?>"  name="fee[]">
		                                    </td>
			                                <td><?php echo $extra['type']; ?></td>
			                                <td><?php if($paid > 0)echo '<font color="#009900">Paid</font>';else echo '<font color="#FF0000">Discount</font>'; ?></td>
                                            <td class="classtd">
                								<?php
                								if($paid >0){}else{
                								?><a href="javascript:;" onClick="updateExtraFee('<?php echo $i ?>','<?php echo $extra["p$i"]; ?>','<?php echo $extra["id"];?>')">
                								<button type="button" class="reset-btn">Reset Discount</button></a>
                								<?php
                								}
                								?>
                							</td>
                                            <td></td>

								        </tr>
								        <?php
								    }
								}

						}
					?>
					</tbody>
                </table>
                </div>


				<div  class="half">                
				<center>Other fee</center>                
				   <table class="table">                    
				       <thead>                        
                        <tr>
                            <th>Name</th>
                            <th>Amount</th>
                            <th><span class="action">Action</span></th>
                            <th></th>
                        </tr>
				</thead><?php $other_disc= $db->getRows("select otherfee_discount.id, otherfee_discount.other_id, otherfee_discount.discount, otherfee_structure.name from otherfee_structure left join otherfee_discount  on otherfee_discount.other_id=otherfee_structure.id where otherfee_discount.idno='".$_GET['id']."'");
			foreach($other_disc as $other_disc1){
			 //   print_r($other_disc1);

			    $discount_detail_id = $db->getVal("select id from discount_detail where idno='".$_GET['id']."' and discount_amount='$other_disc1[discount]' and type='otherfee' and other_id='$other_disc1[other_id]' ");

				?>
				<tbody>			
				<tr>
				<td><?= $other_disc1['name']?></td>
				<td><?= $other_disc1['discount'] ?></td>
				<td class="bustd"><a href="javascript:;" onClick="reset_other('<?php echo $_GET['id'] ?>', '<?php echo $other_disc1['id'] ?>', '<?php echo $discount_detail_id;?> ', '<?php echo $other_disc1['name'] ?>')"> 
				<button type="button" class="reset-btn">Reset Discount </button></a>
				</td>
				<td></td>
				</tr>
				</tbody>
			<?php } ?>
				</table>
				</div>

                <?php
					$checkhostelFee=$db->getVal("select id from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");
					if($checkhostelFee){
						?>
                        <div  class="half">
                        <center>Hostel fee</center>
                        <table class="table">
                	
                    <thead>
                        
                        <tr>
                            <th>Month</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th><span class="action">Action</span></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
		$hostelid=$db->getval("SELECT hid FROM hostelinstallment where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'  and idno='".$_GET['id']."' ");
	
               $hosteltotal=$db->getval("select fee from hostel where school_id='".$_SESSION["admin"]["school_id"]."' and id='$hostelid' ");
				
				 $hostelinstallment=$db->getval("select installment from hostel where school_id='".$_SESSION["admin"]["school_id"]."' and id='$hostelid'");
                  $totalamounthospaid=$hosteltotal/$hostelinstallment;

					
						$aryHostelInstall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and session='".$_SESSION["admin"]["session"]."'");

						$hostelfeeCount=array();$totalhostelpayfee=0;$hostelFine=0;
				// 		for($i=1;$i<=12;$i++){
                        foreach($allmonths as $mn){
                            $i = $mn['month'];

							$iis="i".$i;
							$iip="ip".$i;$is_rr=false;

				// 			if($aryHostelInstall[$iis]!="" && $aryHostelInstall[$iis]==0){
								$totalhostelpayfee=$totalhostelpayfee+$aryHostelInstall[$iip];$ji++;
								$is_rr=false;
								
                             $hostel_discount = $db->getVal(" select sum(discount_amount) from discount_detail where idno='".$aryForm["idno"]."' and installment='$iis' and type='hostel' ");
								if($hostel_discount > 0){
							?>
                            <tr <?php if($ji==1)echo 'class="first" style="border-top:2px solid #F00"'; ?>>
                            	<td><?php $ij1=$i;
								// 	  if($ij1<=9){
								// 		$newDate=date("Y")."-".($ij1+3)."-1";
								// 		echo date("F",strtotime($newDate));//."(".($ij+3).")";
								// 	  }
								// 	  else{
								// 		$newDate=date("Y")."-".($ij1-9)."-1";
								// 		echo date("F",strtotime($newDate));//."(".($ij-9).")";
								// 	  }
								echo $mn['name'];
									  ?></td>
                                <td><input type="text" class="input-block-level" value="<?php echo $hostel_discount; ?>" >
                                <input type="hidden" id="hostelfee<?php echo $i ?>" class="input-block-level" value="<?php echo $totalamounthospaid; ?>"  name="hostelfee[]"></td>

                                <td><?php 
								if($aryHostelInstall[$iip]>0)echo '<font color="#009900">Paid</font>';
								else echo '<font color="#FF0000">Discount</font>'; ?></td>
                                <td class="hosteltd"><?php
								if($aryHostelInstall[$iip]>0){}else{
								?><a href="javascript:;" onClick="updateFee('<?php echo $i ?>','<?php echo $iis; ?>','<?php echo $aryHostelInstall["total"]?>','<?php echo $aryHostelInstall["discount"]?>','hostelfee')">
                                <button type="button" class="reset-btn">Reset Discount</button></a>
                                <?php	
								}
								?></td>
                                <td></td>
                            </tr>
							<?php
							}
						}
					?>
					</tbody>
                </table>
                        </div>
                		<?php
					}
				?>

			<div  class="half">                
				<center>Hostel Other fee</center>                
				   <table class="table">                    
				       <thead>                        
                        <tr>
                            <th>Name</th>
                            <th>Amount</th>
                            <th><span class="action">Action</span></th>
                            <th></th>
                        </tr>
				</thead>
				<?php $other_disc= $db->getRows("select hostelotherfee_discount.id, hostelotherfee_discount.other_id, hostelotherfee_discount.discount, hostel_otherfee_structure.name from hostel_otherfee_structure left join hostelotherfee_discount  on hostelotherfee_discount.other_id=hostel_otherfee_structure.id where hostelotherfee_discount.idno='".$_GET['id']."'");
			foreach($other_disc as $other_disc1){

    		    $discount_detail_id = $db->getVal("select id from discount_detail where idno='".$_GET['id']."' and discount_amount='$other_disc1[discount]' and type='hostelother' and other_id='$other_disc1[other_id]' ");
				?>
				<tbody>			
				<tr>
				<td><?= $other_disc1['name']?></td>
				<td><?= $other_disc1['discount'] ?></td>
				<td class="bustd"><a href="javascript:;" onClick="reset_hostel_other('<?php echo $_GET['id'] ?>', '<?php echo $other_disc1['id'] ?>', '<?php echo $discount_detail_id; ?>')"> 
				<button type="button" class="reset-btn">Reset Discount </button></a>
				</td>
				<td></td>
				</tr>
				</tbody>
			<?php } ?>
				</table>
				</div>



  <div class="half"><center> School fee Discription</center>
            	<table class="table">
                	 <thead>
                        <tr>
                            <th>Class  Amount</th>
                            <th>Action</th>
                           
                        </tr>
                    </thead>
                    <tbody>
		 	     <tr>
		 	          <td ><?php echo $classfee=$db->getval("select total from installment where idno='".$_GET['id']."'"); ?></td>
		 	         <td class="classtd">
								
					<a href="javascript:;" onClick="resetclassfee('<?php echo $i ?>','<?php echo $classfee ?>','classfee')"><button type="button" class="reset-btn">Reset fee</button></a>
							
								</td>
                </tr>     
                </table>
     <table class="table">
                	 <thead>
                        <tr>
                            <th>Bus  Amount</th>
                            <th>Action</th>
                           
                        </tr>
                    </thead>
                    <tbody>
		 	     <tr>
		 	          <td ><?php echo $busfee=$db->getval("select bus from installment where idno='".$_GET['id']."'"); ?></td>
		 	         <td class="classtd">
								
					<a href="javascript:;" onClick="resetclassfee('<?php echo $i ?>','<?php echo $busfee ?>','busfee')"><button type="button" class="reset-btn">Reset fee</button></a>
							
								</td>
                </tr>     
                </table>           
                
         <table class="table">
                	 <thead>
                        <tr>
                            <th>Hostel  Amount</th>
                            <th>Action</th>
                           
                        </tr>
                    </thead>
                    <tbody>
		 	     <tr>
		 	          <td ><?php echo $hostelfee=$db->getval("select total from hostelinstallment where idno='".$_GET['id']."'"); ?></td>
		 	         <td class="classtd">
								
					<a href="javascript:;" onClick="resetclassfee('<?php echo $i ?>','<?php echo $hostelfee ?>','hostel')"><button type="button" class="reset-btn">Reset fee</button></a>
							
								</td>
                </tr>     
                </table>                
                
                
          </div>
         </div>
    </div>
    
    <script>
    
    function resetclassfee(id,oldvalue,type){

	var newValue=prompt("Please Enter Default Fee",0);
	
	if (newValue === null) {
        return; //break out of the function early
    }
    
	$.post( "../Promote/ajax.php", { type: "updateclassfee" ,paytype: type, oldvalue: oldvalue, id: id, newValue: newValue, idno:"<?php echo $_GET["id"] ?>" } , function( data ) {
			//alert(data);
		window.location.reload();
	});
}
</script>