<script>
function calculateFine() {
	var due_amount = document.getElementById('due_amount'),
		inst_no = document.getElementById('inst_no').value,
		inst_amount = document.getElementById('inst_amount').value,
		total_rec = document.getElementById('total_rec').value,
		// rec_amount = document.getElementById('rec_amount'),
		fine = document.getElementById('fine');
		due_amount = parseInt(due_amount.value);
		fine = parseInt(fine.value);
	// document.getElementById('due_amount').value = due_amount;
	// document.getElementById('rec_amount').value = due_amount + fine;
	document.getElementById('total_amount').value = inst_amount * inst_no + fine;
	document.getElementById('hidden_total_rec').value =  inst_amount * inst_no;
	document.getElementById('total_rec').value = inst_amount * inst_no;
}

function calculateAmount() {
  	var inst_amount = document.getElementById('inst_amount').value,
      	// rec_amount = document.getElementById('rec_amount').value,
      	inst_no = document.getElementById('inst_no').value;
      	show_amount = document.getElementById('show_amount').value;
  	// document.getElementById('rec_amount').value = inst_amount * inst_no;
  	// document.getElementById('show_amount').value = inst_amount * inst_no;
  	document.getElementById('total_amount').value = inst_amount * inst_no;
  	document.getElementById('hidden_total_amount').value = inst_amount * inst_no;
}
</script>

<div class="container" style="margin-top: 20px;margin-bottom:20px;">
  	<form method="get" action="Agent_Trans_New">
    	<div class="form-group">
      		<input class="form-control" type="text" placeholder="Input A/C no and press enter" name="no">
    	</div>
  </form>
</div>

<?php
	if(isset($_GET["no"])) {
		echo '
        <div class="container" style="margin-top: 20px;"> <h3>New Transaction</h3><hr></div>
        <div class="container">
        <table class="table table-striped table-bordered table-hover">
          	<tr>
				<th>Name</th>
				<th>Phone</th>
				<th>AC No</th>
				<th>Remaining Amount</th>
				<th>Installment</th>
          	</tr>';
        $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $sql = "SELECT * FROM `".$GLOBALS['arif_ac']."` WHERE  `AA_ACNO` = '".$_GET["no"]."' ";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              	$date1 = date_create($row["AA_DATE"]);
              	$date2 = date_create(date("Y/m/d"));
              	$diff = date_diff($date1, $date2);
              	if ($row["AA_ACTYPE"] == 'D'){
					  $diff = $diff->format("%a"); 
					  $diff = (int)$diff;
					  $due_i = $diff - $row["AA_NO_OF_PAYPAID"];
				} else {
					$diff = $diff->format("%m");
					$diff = (int)$diff;
					$due_i = $diff - $row["AA_NO_OF_PAYPAID"];
				}
          		echo "
				<tr>
					<td>".$row["AA_NAME"]."</td>
					<td>".$row["AA_PHONE"]."</td>
					<td>".$row["AA_ACNO"]."</td>
					<td>".$row["AA_BAL"]. '</td>
					<td>
					<form method="post" enctype="multipart/form-data" id="submitInstallment">
							<input type="hidden" name="FORM_NAME" value="add_installment">';						
							if ($due_i > 0 && $row["AA_TYPE"] == "Loan") {
								$due_amount = $due_i * $row["AA_INSTALLMENT"];
								$due_amount = intval($due_amount);
								$fine_amount = ($due_amount * 40) / 100;
								$total_amount = $row["AA_INSTALLMENT"] + $fine_amount;
								$total_rec = $row["AA_INSTALLMENT"] * 1;
								echo '
								<table class="table table-bordered">
									<tr>
										<td>
											<input type="hidden" name="AA_ACNO" value="'.$row['AA_ACNO'].'">
											<input type="hidden" name="ins_no" value="'.$due_i. '">
											<small>Due Amount:</small>
											<input type="text" id="due_amount" value="'.$due_amount. '" disabled style="width:50px;border:1px solid red">
											<small>Installment Amount:</small>
											<input type="text" id="inst_amount" value="' . $row['AA_INSTALLMENT'] . '" style="width:50px;">
											<small>No:</small>
											<input type="number" id="inst_no" value="1" min="1" style="width:50px;" onchange="calculateFine()">
											<small>Total received:</small>
											<input type="number" id="total_rec" value="'.$total_rec.'" style="width:50px;" onchange="calculateFine()" disabled>
										</td>
									</tr>
									<tr>
										<td>
											<input type="hidden" id="hidden_total_rec" value="'.$total_rec.'" name="add_i" style="width:50px;" onchange="calculateFine()">
											<small>Fine:</small>
											<!-- changed the line to disable fine change for agents <input type="number" id="fine" name="fine_amount" value="'.$fine_amount.'" min="0" style="width:50px;border:1px solid red" onchange="calculateFine()"> -->
											<input type="number" id="fine" name="fine_amount" value="'.$fine_amount.'" min="0" style="width:50px;border:1px solid red" disabled">
											<small>Total:</small>
											<input type="text" id="total_amount" value="'.$total_amount. '" disabled style="width:50px;border:1px solid red">
											<input type="submit" class="btn-info" value="Receive" onclick="sendData(event)">
										</td>
									</tr>
								</table>';
							} else 
							echo'
							<input type="hidden" id="inst_amount" value="'.$row['AA_INSTALLMENT']. '">
							<small>Amount:</small>
							<input type="text" id="show_amount" value="'.$row['AA_INSTALLMENT']. '" disabled style="width:50px;">
							<small>No:</small>
							<input type="number" id="inst_no" value="1" min="1" style="width:50px;" onchange="calculateAmount()">
							<input type="hidden" name="AA_ACNO" value="'.$row['AA_ACNO']. '">
							<small>Total:</small>
							<input type="text" id="total_amount" value="'.$row['AA_INSTALLMENT'].'" disabled style="width:50px;">
							<input type="hidden" id="hidden_total_amount" name="add_i" value="'.$row['AA_INSTALLMENT'].'">
							<input type="submit" class="btn-info" value="Receive" onclick="sendData(event)">';
							echo '</form></td></tr>';
            }
        } else {echo "Check A/C no.";};
        	$conn->close();
        echo '</table></div>';
	}
?>

<script>
var submitInstallment = document.getElementById("submitInstallment");
function sendData(event) {
  	event.preventDefault();
  	var XHR = new XMLHttpRequest();
  	var FD = new FormData(submitInstallment);

 	 XHR.addEventListener("load", function (event) {
      	var obj = JSON.parse(event.target.responseText);
      	// console.log(obj);
		alert(obj.statusmsg);
		// alert(obj.status);
		window.location.reload(true);
  	});

  	XHR.addEventListener("error", function () {
		alert('Error', 'Ooops!! Something went wrong.');
  	});
  	// console.log(FD);

  	XHR.open("POST", "/exe/receive_amount/");
  	XHR.send(FD);
}
</script>