
<?php include("includes/top-header.php"); ?>
 <style>
 
    body {
        font-family: Arial, sans-serif;
        background-color: #f2f2f2;
    }
    
    .loan-form {
        background-color: #ffffff;
        padding: 20px;
        border: 1px solid #cccccc;
        border-radius: 10px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        width: 80%;
        margin: 40px auto;
    }
    
    .loan-form h2 {
        color: #fc671c;
        text-align: center;
    }
    
    .form-group {
        margin-bottom: 20px;
    }
    
    .form-group label {
        display: block;
        margin-bottom: 10px;
        color: #666666;
    }
    
    .form-group input[type="text"], .form-group input[type="number"], .form-group textarea {
        width: 100%;
        padding: 10px;
        border: 1px solid #cccccc;
        border-radius: 5px;
    }
    
    .form-group textarea {
        height: 100px;
    }
    
    .btn {
        background-color: #fc671c;
        color: #ffffff;
        padding: 10px 20px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
    }
    
    .btn:hover {
        background-color: #2ecc71;
    }
    
    .success-message {
        background-color: #dff0d8;
        padding: 10px;
        border: 1px solid #cccccc;
        border-radius: 5px;
        color: #3c763d;
    }
    
    
    .section {
    background-color: #f2f2f2;
    padding: 20px;
    border: 1px solid #cccccc;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 10px;
    color: #666666;
}

.form-control {
    width: 100%;
    padding: 10px;
    border: 1px solid #cccccc;
    border-radius: 5px;
}

.btn {
    background-color: #fc671c;
    color: #ffffff;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

.result {
    background-color: #ffffff;
    padding: 20px;
    border: 1px solid #cccccc;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    margin-top: 20px;
}

.result h3 {
    margin-top: 0;
}
</style>
<!-- Preloader
<div id="preloader">
  <div data-loader="dual-ring"></div>
</div>
Preloader End -->

<!-- Document Wrapper   
============================================= -->
<div id="main-wrapper"> 
  <!-- Header
  ============================================= -->

  <!-- Header End -->
  
  <!-- Content
  ============================================= -->
  <div id="content"> 

 <?php
// ...


if (isset($_POST['submit'])) {
    // Get user ID from session
    $userId = $_SESSION['uid'];

    // Get loan application details from form
    $loanAmount = $_POST['loanAmount'];
    $loanReason = $_POST['loanReason'];
    $loanTenure = $_POST['loanTenure'];
    $interestRate = 0.01; // 1% interest rate

    // Calculate interest and total repayment amount
    if ($loanTenure == 30) {
        $interest = $loanAmount * $interestRate * (30 / 365);
        $totalRepayment = $loanAmount + $interest;
        $repaymentSchedule = "Monthly repayment of $" . number_format($totalRepayment / 1, 2);
    } elseif ($loanTenure == 90) {
        $interest = $loanAmount * $interestRate * (90 / 365);
        $totalRepayment = $loanAmount + $interest;
        $repaymentSchedule = "Monthly repayment of $" . number_format($totalRepayment / 3, 2);
    } elseif ($loanTenure == 180) {
        $interest = $loanAmount * $interestRate * (180 / 365);
        $totalRepayment = $loanAmount + $interest;
        $repaymentSchedule = "Monthly repayment of $" . number_format($totalRepayment / 6, 2);
    }

    // Retrieve user details from database
    $sql = "SELECT * FROM users WHERE uID = '$userId'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    $userName = $row['fn'] . ' ' . $row['ln'];
    $userEmail = $row['email'];

    // Send email to company
    $to = 'customerservice@newageunb.com'; // Replace with your company email
    $subject = 'New Loan Application from ' . $userName;

    $message = '
        <html>
        <body style="background-color: #f2f2f2; font-family: Arial, sans-serif;">
            <table width="600" border="0" cellpadding="10" cellspacing="0" style="background-color: #ffffff; border: 1px solid #cccccc; border-radius: 10px; margin: 20px auto; padding: 20px;">
                <tr>
                    <td>
                        <h2 style="color: #008000;">New Loan Application</h2>
                        <p>User ID: ' . $userId . '</p>
                        <p>User Name: ' . $userName . '</p>
                        <p>Email: ' . $userEmail . '</p>
                        <p>Loan Amount: $' . $loanAmount . '</p>
                        <p>Reason for Loan: ' . $loanReason . '</p>
                        <p>Loan Tenure: ' . $loanTenure . ' days</p>
                        <p>Interest Rate: 5%</p> 
                        <p>Interest: $' . number_format($interest, 2) . '</p>
                        <p>Total Repayment: $' . number_format($totalRepayment, 2) . '</p>
                        <p>Repayment Schedule: ' . $repaymentSchedule . '</p>
                    </td>
                </tr>
            </table>
        </body> 
        </html>
    ';

    // PHPMailer configuration
    $mail = new PHPMailer(true);

     $mail->isSMTP();
          $mail->SMTPDebug = 0; 
          $mail->Host = "mail.newageunb.com";
          $mail->SMTPAuth = true;
          $mail->SMTPSecure = true;
          $mail->Port = 465; 
          
          $mail->Username = 'no-reply@newageunb.com';
          $mail->Password = 'fe-XB;AYE-$Y';
          
        
            $mail->setFrom($userEmail, $userName);
            $mail->addAddress($to);
            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body = $message;

    try {
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }
    $sql = "INSERT INTO loan_applications (user_id, loan_amount, loan_reason, loan_tenure, interest_rate, interest, total_repayment, repayment_schedule) 
        VALUES ('$userId', '$loanAmount', '$loanReason', '$loanTenure', '$interestRate', '$interest', '$totalRepayment', '$repaymentSchedule')";
$conn->query($sql);

// Display success message
echo '<div class="success-message">Loan application submitted successfully!</div>';

}

?>
   
<div class="loan-form">
    <h2>Loan Application Form</h2>
    <form id="loan-form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div class="form-group"> 
            <label for="loanAmount">Loan Amount:</label>
            <input type="number" class="form-control" id="loanAmount" name="loanAmount" required>
        </div>
        <div class="form-group">
            <label for="loanReason">Reason for Loan:</label>
            <textarea class="form-control" id="loanReason" name="loanReason" required></textarea>
        </div>
        <div class="form-group">
            <label for="loanTenure">Loan Tenure:</label>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="loanTenure" id="30days" value="30" checked>
                <label class="form-check-label" for="30days">30 Days</label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="loanTenure" id="90days" value="90">
                <label class="form-check-label" for="90days">90 Days</label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="loanTenure" id="6months" value="180">
                <label class="form-check-label" for="6months">6 Months</label>
            </div>
        </div>
        <div class="form-group">
            <label for="interestRate">Interest Rate:</label>
            <input type="text" class="form-control" id="interestRate" name="interestRate" value="5%" readonly>
        </div>
        <button type="submit" class="btn" name="submit">Apply for Loan</button> 
    </form>
   
</div>




 
    <!-- Can't find
    ============================================= -->
    <div id="content"> 
    <!-- Can't find ============================================= --> 
 <section class="section py-4 my-4 py-sm-5 my-sm-5" style="min-height: 350px;">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <h2 class="text-center">Loan Calculator</h2>
                <form>
                    <div class="form-group">
                        <label for="calculatorLoanAmount">Loan Amount:</label>
                        <input type="number" class="form-control" id="calculatorLoanAmount" placeholder="Enter loan amount" />
                    </div>
                    <div class="form-group">
                        <label for="calculatorLoanTenure">Loan Tenure:</label>
                        <select class="form-control" id="calculatorLoanTenure">
                            <option value="30">30 Days</option>
                            <option value="90">90 Days</option>
                            <option value="180">6 Months</option>
                        </select>
                    </div>
                    <button type="button" class="btn btn-primary" onclick="calculateLoan()">Calculate</button>
                </form>
                <div class="result">
                    <h3>Result:</h3>
                    <p id="interest"></p>
                    <p id="totalRepayment"></p>
                    <p id="monthlyRepayment"></p>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    function calculateLoan() {
        const loanAmountInput = document.getElementById('calculatorLoanAmount');
        const loanAmount = parseFloat(loanAmountInput.value);

        if (isNaN(loanAmount) || loanAmount <= 0) {
            alert("Please enter a valid loan amount.");
            loanAmountInput.focus();
            return;
        }

        const loanTenure = parseInt(document.getElementById('calculatorLoanTenure').value);
        const interestRate = 0.07; // 1% interest rate

        const interest = loanAmount * interestRate * (loanTenure / 365);
        const totalRepayment = loanAmount + interest;

        let monthlyRepayment;
        switch (loanTenure) {
            case 30:
                monthlyRepayment = totalRepayment / 1;
                break;
            case 90:
                monthlyRepayment = totalRepayment / 3;
                break;
            case 180:
                monthlyRepayment = totalRepayment / 6;
                break;
            default:
                monthlyRepayment = 0;
        }

        document.getElementById('interest').innerHTML = `Interest: $${interest.toFixed(2)}`;
        document.getElementById('totalRepayment').innerHTML = `Total Repayment: $${totalRepayment.toFixed(2)}`;
        document.getElementById('monthlyRepayment').innerHTML = `Monthly Repayment: $${monthlyRepayment.toFixed(2)}`;
    }
</script>


    <!-- Can't find end -->
</div>
<!-- Content end -->
<!-- Footer ============================================= -->
<?php include("includes/footer.php"); ?>
<!-- Footer end -->
</div>
<!-- Document Wrapper end -->
<!-- Back to Top ============================================= -->
<a id="back-to-top" data-bs-toggle="tooltip" title="Back to Top" href="javascript:void(0)"><i class="fa fa-chevron-up"></i></a>
<!-- Script -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Style Switcher -->
<script src="js/switcher.min.js"></script>
<script src="js/theme.js"></script>
</body>
</html>