<?php
// Database connection
include("include/connection.php");
$conn = new mysqli($host, $user, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$message = "Loan has been Registed Susscessful.";
$error_message = "Loan failer to register";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $memberID = $_POST['memberID'];
    $amount = $_POST['amount'];
	$formFee = $_POST['form_fee'];
	$interest = $_POST['interest'];
	$totalLoan = $_POST['TotalLoan'];
	$LoanBalance = $totalLoan; 
    $loanDate = $_POST['loanDate'];

    // Insert loan data into the loan table
    $query = "INSERT INTO loan (memberNo, amount,form_fee,interest,TotalLoan, LoanBalance, date) VALUES (?, ?, ? , ?, ? , ?, ?)";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("iiiiiis", $memberID, $amount, $formFee, $interest, $totalLoan, $LoanBalance,  $loanDate);

    if ($stmt->execute()) {
		 $message;
		 
    } else {
       // echo "Error registering loan: " . $conn->error;

		$error_message;
		
	}

    $stmt->close();
    $conn->close();
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loan Application Status</title>
    <!-- Bootstrap CSS for responsive design -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-5">
    <!-- Display success or error message -->
    <?php if ($message): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo $message; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <!-- Button to redirect to the main page -->
    <a href="index.html" class="btn btn-secondary mt-3">Go to Main Page</a>
</div>

<!-- Bootstrap JS for responsiveness and alerts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>