<?php
require_once 'functions.php';
requireAuth();

$user = getCurrentUser();

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_loan'])) {
    $loanAmount = floatval($_POST['loan_amount'] ?? 0);
    $loanPurpose = trim($_POST['loan_purpose'] ?? '');
    $loanTerm = intval($_POST['loan_term'] ?? 0);
    
    if ($loanAmount <= 0 || empty($loanPurpose) || $loanTerm <= 0) {
        $error = 'Please fill in all loan application fields.';
    } elseif ($loanAmount > $user['annual_income'] * 5 && $user['annual_income'] > 0) {
        $error = 'Loan amount cannot exceed 5x your annual income.';
    } else {
        // In a real system, this would create a loan application record
        $success = 'Loan application submitted successfully! Our team will review your application and get back to you within 3-5 business days.';
        createNotification($user['id'], 'Loan Application Submitted', 'Your loan application for ' . formatCurrency($loanAmount) . ' has been received and is under review.', 'info');
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loans & Credit - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php include 'includes/client-sidebar.php'; ?>
    
    <div class="main-content">
        <?php include 'includes/client-header.php'; ?>
        
        <div class="page-content">
            <div class="page-header">
                <h2>Loans & Credit</h2>
                <p>Apply for financing options tailored to your needs</p>
            </div>
            
            <?php if ($error): ?>
                <div class="alert alert-danger fade-in"><span>⚠</span> <?php echo e($error); ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success fade-in"><span>✓</span> <?php echo e($success); ?></div>
            <?php endif; ?>
            
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 24px;">
                <!-- Loan Calculator -->
                <div class="card">
                    <div class="card-header">
                        <span class="card-title">Loan Calculator</span>
                    </div>
                    <div class="card-body">
                        <div class="loan-calculator">
                            <div class="form-group">
                                <label class="form-label">Loan Amount ($)</label>
                                <input type="number" id="loan-amount" class="form-control" value="10000" min="1000" step="1000" oninput="calculateLoan()">
                            </div>
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
                                <div class="form-group">
                                    <label class="form-label">Interest Rate (%)</label>
                                    <input type="number" id="loan-rate" class="form-control" value="8.5" min="1" max="30" step="0.1" oninput="calculateLoan()">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Term (Months)</label>
                                    <input type="number" id="loan-months" class="form-control" value="36" min="6" max="360" step="6" oninput="calculateLoan()">
                                </div>
                            </div>
                        </div>
                        
                        <div class="loan-result" style="margin-top: 20px;">
                            <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px;">
                                <div style="text-align: center;">
                                    <div class="loan-result-value" id="loan-monthly" style="font-size: 20px;">$315.77</div>
                                    <div class="loan-result-label">Monthly Payment</div>
                                </div>
                                <div style="text-align: center;">
                                    <div class="loan-result-value" id="loan-total" style="font-size: 20px;">$11,367.72</div>
                                    <div class="loan-result-label">Total Payment</div>
                                </div>
                                <div style="text-align: center;">
                                    <div class="loan-result-value" id="loan-interest" style="font-size: 20px;">$1,367.72</div>
                                    <div class="loan-result-label">Total Interest</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Loan Application -->
                <div class="card">
                    <div class="card-header">
                        <span class="card-title">Apply for a Loan</span>
                    </div>
                    <div class="card-body">
                        <form method="POST" action="">
                            <input type="hidden" name="apply_loan" value="1">
                            
                            <div class="form-group">
                                <label class="form-label">Loan Amount ($) *</label>
                                <input type="number" name="loan_amount" class="form-control" min="1000" step="500" required placeholder="Enter amount">
                            </div>
                            
                            <div class="form-group">
                                <label class="form-label">Loan Purpose *</label>
                                <select name="loan_purpose" class="form-control" required>
                                    <option value="">Select Purpose</option>
                                    <option value="personal">Personal</option>
                                    <option value="business">Business</option>
                                    <option value="education">Education</option>
                                    <option value="home">Home Improvement</option>
                                    <option value="auto">Auto/Vehicle</option>
                                    <option value="debt">Debt Consolidation</option>
                                    <option value="medical">Medical</option>
                                    <option value="other">Other</option>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <label class="form-label">Loan Term (Months) *</label>
                                <select name="loan_term" class="form-control" required>
                                    <option value="12">12 months</option>
                                    <option value="24">24 months</option>
                                    <option value="36">36 months</option>
                                    <option value="48">48 months</option>
                                    <option value="60">60 months</option>
                                    <option value="120">120 months</option>
                                </select>
                            </div>
                            
                            <button type="submit" class="btn btn-primary w-100">Submit Application</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script src="assets/js/main.js"></script>
</body>
</html>
