<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="Trans_New">
    	<div class="form-group">
      	<input class="form-control" type="text" placeholder="input A/C no and enter" name="no">
    	</div>
  </form>
</div>

<?php
    if(isset($_GET["no"]) && isset($_GET["type"])&&$_GET["type"]=="Loan"){
        echo '
        <div class="container" style="margin-top: 20px;"> <h5>New Transaction : '.$GLOBALS['post_info'].' </h5><hr></div>
        <div class="container">
        <table class="table table-striped table-bordered table-hover table-responsive">
          	<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"];}
              	//$ID=$row["GC_ID"];
          		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 && $_GET['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>
											<input type="number" id="fine" name="fine_amount" value="'.$fine_amount.'" min="0" style="width:50px;border:1px solid red" onchange="calculateFine()">
											<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>';
    }

  	if(isset($_GET["no"]) && isset($_GET["type"])&&$_GET["type"]=="Recurring"){
    	echo '
    		<div class="container" style="margin-top: 10px;"> <h5>New Transaction : '.$GLOBALS['post_info'].' </h5><hr></div>
    		<div class="container table-responsive">
    			<table class="table table-striped table-bordered table-hover">
      				<tr>
						<th>Name</th>
						<th>Phone</th>
						<th>AC No</th>
						<th>Total Deposit</th>
						<th>Installment</th>
						<th>Receive Amount</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"];}
            //$ID=$row["GC_ID"];
      	echo "
      		<tr>
				<td>".$row["AA_NAME"].    "</td>
				<td>".$row["AA_PHONE"].  "</td>
				<td>".$row["AA_ACNO"]."</td>
				<td>".$row["AA_BAL"]. "</td>
				<td>".$row["AA_INSTALLMENT"].  '</td>
        		<td>
        			<form method="post" enctype="multipart/form-data" id="submitInstallment">
          				<input type="hidden" name="FORM_NAME" value="add_installment">';
          				if ($due_i > 0 && $_GET['type'] == "Recurring") {
                  			$due_amount = $due_i * $row["AA_INSTALLMENT"];
                  			$due_amount = intval($due_amount);
							$fine_amount = ($due_amount * 20) / 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>
										<input type="number" id="fine" name="fine_amount" value="'.$fine_amount.'" min="0" style="width:50px;border:1px solid red" onchange="calculateFine()">
										<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>';
}
?>

<div class="container" style="margin-top: 70px;">
  	<div class="row">
    	<div class="col-md-2">
      		<h5>Past Transactions::::</h5>
    	</div>
    	<div class="col-md-2">
      		<button class="btn btn-success" onclick="window.location.reload()">Refresh</button>
    	</div>
  	</div><hr>
</div>

<?php

if(isset($_GET["no"])){
  	echo '
  	<div class="container table-responsive">
    	<table class="table table-striped table-bordered table-hover">
      	<tr>
			<th>SL</th>
			<th>Tr No</th>
			<th>TimeStamp</th>
			<th>User</th>
			<th>A/C No</th>
			<th>Amount</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_tran']."` WHERE  `AT_ACID` =  '".$_GET['no']."'";
    $sql = "SELECT * FROM `".$GLOBALS['arif_tran']."` WHERE  `AT_ACID` =  '".$_GET['no']."'  ORDER BY `".$GLOBALS['arif_tran']."`.`AT_ID` DESC";
    $result = $conn->query($sql);
    $rowcount=mysqli_num_rows($result);//$rowcount++;
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            //$ID=$row["GC_ID"];
            echo "
            <tr>
				<td>".$rowcount.    "</td>
				<td>".$row["AT_ID"].    "</td>
				<td>".$row["AT_TIMESTAMP"].  "</td>
				<td>".$row["AT_ADMIN"]. "</td>
				<td>".$row["AT_ACID"].   "</td>
				<td>".$row["AT_AMOUNT"].   "</td>
            </tr>";
            $rowcount--;
        }
    } else echo "No Past record Found";
    mysqli_free_result($result);$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);
		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>
