<?php
// Include necessary files and check if the user is an admin
require_once '../includes/db.php';
require_once '../includes/functions.php';

// Ensure the user is an admin
redirectIfNotAdmin();

// Handle adding a new investment plan
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_plan'])) {
    // Sanitize user input
    $plan_name = htmlspecialchars($_POST['plan_name']);
    $interest_rate = (float) $_POST['interest_rate'];
    $min_amount = (float) $_POST['min_amount'];
    $max_amount = (float) $_POST['max_amount'];
    $duration_months = (int) $_POST['duration_months'];
    $risk_level = htmlspecialchars($_POST['risk_level']);

    // Insert into database
    $stmt = $pdo->prepare("INSERT INTO investment_plans (plan_name, interest_rate, min_amount, max_amount, duration_months, risk_level) 
                           VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->execute([$plan_name, $interest_rate, $min_amount, $max_amount, $duration_months, $risk_level]);

    header("Location: manage-investments.php"); // Redirect to refresh the page after adding
    exit();
}

// Handle editing an existing investment plan
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_plan'])) {
    // Sanitize user input
    $plan_id = (int) $_POST['plan_id'];
    $plan_name = htmlspecialchars($_POST['plan_name']);
    $interest_rate = (float) $_POST['interest_rate'];
    $min_amount = (float) $_POST['min_amount'];
    $max_amount = (float) $_POST['max_amount'];
    $duration_months = (int) $_POST['duration_months'];
    $risk_level = htmlspecialchars($_POST['risk_level']);

    // Update the investment plan in the database
    $stmt = $pdo->prepare("UPDATE investment_plans SET plan_name = ?, interest_rate = ?, min_amount = ?, max_amount = ?, 
                           duration_months = ?, risk_level = ? WHERE plan_id = ?");
    $stmt->execute([$plan_name, $interest_rate, $min_amount, $max_amount, $duration_months, $risk_level, $plan_id]);

    header("Location: manage-investments.php"); // Redirect to refresh the page after editing
    exit();
}

// Fetch all investment plans
$perPage = 10;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page - 1) * $perPage;
$totalCount = $pdo->query("SELECT COUNT(*) FROM investment_plans")->fetchColumn();
$totalPages = ceil($totalCount / $perPage);
$investmentPlans = $pdo->prepare("SELECT * FROM investment_plans LIMIT :perPage OFFSET :offset");
$investmentPlans->bindValue(':perPage', $perPage, PDO::PARAM_INT);
$investmentPlans->bindValue(':offset', $offset, PDO::PARAM_INT);
$investmentPlans->execute();
$investmentPlans = $investmentPlans->fetchAll();

// Get current admin info
$adminId = $_SESSION['user_id'];
$adminInfo = $pdo->prepare("SELECT full_name, email FROM users WHERE user_id = ?");
$adminInfo->execute([$adminId]);
$admin = $adminInfo->fetch();

// Calculate statistics
$totalPlans = $totalCount;
$avgInterest = $pdo->query("SELECT AVG(interest_rate) FROM investment_plans")->fetchColumn();
$avgInterest = $avgInterest ? round($avgInterest, 2) : 0;
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Oakshire Finance - Manage Investments</title>
    <link
        href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700;14..32,800&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --primary-navy: #0a2540;
            --primary-dark: #0a1c2f;
            --primary-blue: #1a4d8c;
            --accent-blue: #2c7da0;
            --success-green: #2e7d32;
            --warning-orange: #f39c12;
            --error-red: #dc3545;
            --info-blue: #17a2b8;
            --gray-50: #f8f9fc;
            --gray-100: #f1f3f9;
            --gray-200: #e9ecef;
            --gray-300: #dee2e6;
            --gray-400: #ced4da;
            --gray-500: #adb5bd;
            --gray-600: #6c757d;
            --gray-700: #495057;
            --gray-800: #343a40;
            --gray-900: #212529;
            --white: #ffffff;
            --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
            --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.05);
            --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.08);
            --shadow-hover: 0 12px 28px rgba(0, 0, 0, 0.12);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Inter', system-ui, -apple-system, sans-serif;
            background: var(--gray-50);
            color: var(--gray-800);
            line-height: 1.5;
            overflow-x: hidden;
        }

        /* Layout */
        .dashboard-wrapper {
            display: flex;
            min-height: 100vh;
        }

        /* Sidebar */
        .sidebar {
            width: 280px;
            background: var(--white);
            border-right: 1px solid var(--gray-200);
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: auto;
            transition: var(--transition);
            z-index: 100;
        }

        .sidebar-header {
            padding: 24px;
            border-bottom: 1px solid var(--gray-200);
            text-align: center;
        }

        .logo {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background: var(--primary-navy);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon i {
            font-size: 20px;
            color: white;
        }

        .logo-text {
            font-size: 22px;
            font-weight: 700;
            color: var(--primary-navy);
            letter-spacing: -0.3px;
        }

        .nav-menu {
            padding: 24px 16px;
        }

        .nav-item {
            margin-bottom: 8px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: var(--gray-700);
            transition: var(--transition);
            font-weight: 500;
            font-size: 14px;
        }

        .nav-link i {
            width: 20px;
            font-size: 18px;
        }

        .nav-link:hover {
            background: var(--gray-100);
            color: var(--primary-blue);
        }

        .nav-link.active {
            background: var(--primary-navy);
            color: white;
        }

        .nav-link.active i {
            color: white;
        }

        .logout-section {
            padding: 24px 16px;
            border-top: 1px solid var(--gray-200);
            margin-top: auto;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background: var(--white);
        }

        .logout-btn {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: var(--error-red);
            transition: var(--transition);
            font-weight: 500;
        }

        .logout-btn:hover {
            background: rgba(220, 53, 69, 0.1);
        }

        /* Main Content */
        .main-content {
            flex: 1;
            margin-left: 280px;
            min-height: 100vh;
        }

        /* Top Header */
        .top-header {
            background: var(--white);
            border-bottom: 1px solid var(--gray-200);
            padding: 16px 32px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 99;
        }

        .page-title h1 {
            font-size: 24px;
            font-weight: 700;
            color: var(--gray-900);
        }

        .user-menu {
            display: flex;
            align-items: center;
            gap: 24px;
        }

        .notification-btn {
            position: relative;
            background: none;
            border: none;
            font-size: 20px;
            color: var(--gray-600);
            cursor: pointer;
            transition: var(--transition);
        }

        .notification-btn:hover {
            color: var(--primary-blue);
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, var(--primary-navy), var(--primary-blue));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .user-avatar i {
            font-size: 18px;
            color: white;
        }

        .user-details h4 {
            font-size: 14px;
            font-weight: 600;
            color: var(--gray-900);
        }

        .user-details p {
            font-size: 12px;
            color: var(--gray-600);
        }

        /* Content Area */
        .content-area {
            padding: 32px;
        }

        /* Stats Cards */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 24px;
            margin-bottom: 32px;
        }

        .stat-card {
            background: var(--white);
            border-radius: 20px;
            padding: 20px;
            border: 1px solid var(--gray-200);
            transition: var(--transition);
        }

        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: var(--shadow-hover);
        }

        .stat-icon {
            width: 48px;
            height: 48px;
            background: rgba(26, 77, 140, 0.1);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 16px;
        }

        .stat-icon i {
            font-size: 24px;
            color: var(--primary-blue);
        }

        .stat-card h3 {
            font-size: 13px;
            font-weight: 500;
            color: var(--gray-600);
            margin-bottom: 8px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .stat-value {
            font-size: 28px;
            font-weight: 700;
            color: var(--gray-900);
        }

        /* Form Cards */
        .form-card {
            background: var(--white);
            border-radius: 20px;
            border: 1px solid var(--gray-200);
            padding: 24px;
            margin-bottom: 32px;
        }

        .form-card h2 {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .form-group label {
            font-size: 13px;
            font-weight: 500;
            color: var(--gray-700);
        }

        .form-group input,
        .form-group select {
            padding: 12px 16px;
            font-size: 14px;
            font-family: 'Inter', sans-serif;
            border: 1.5px solid var(--gray-200);
            border-radius: 12px;
            transition: var(--transition);
            outline: none;
        }

        .form-group input:focus,
        .form-group select:focus {
            border-color: var(--primary-blue);
            box-shadow: 0 0 0 3px rgba(26, 77, 140, 0.1);
        }

        .btn-submit {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 12px 24px;
            background: var(--primary-navy);
            color: white;
            border: none;
            border-radius: 12px;
            font-weight: 600;
            font-size: 14px;
            cursor: pointer;
            transition: var(--transition);
            margin-top: 24px;
        }

        .btn-submit:hover {
            background: var(--primary-dark);
            transform: translateY(-2px);
        }

        /* Table */
        .table-container {
            background: var(--white);
            border-radius: 20px;
            border: 1px solid var(--gray-200);
            overflow-x: auto;
            margin-bottom: 24px;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
        }

        .data-table th,
        .data-table td {
            padding: 16px 20px;
            text-align: left;
            border-bottom: 1px solid var(--gray-200);
        }

        .data-table th {
            background: var(--gray-50);
            font-weight: 600;
            font-size: 13px;
            color: var(--gray-700);
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .data-table td {
            font-size: 14px;
            color: var(--gray-800);
        }

        .data-table tr:hover td {
            background: var(--gray-50);
        }

        /* Badges */
        .badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .badge-low {
            background: rgba(46, 125, 50, 0.1);
            color: var(--success-green);
        }

        .badge-medium {
            background: rgba(243, 156, 18, 0.1);
            color: var(--warning-orange);
        }

        .badge-high {
            background: rgba(220, 53, 69, 0.1);
            color: var(--error-red);
        }

        /* Buttons */
        .btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 6px 12px;
            border-radius: 8px;
            font-size: 12px;
            font-weight: 500;
            text-decoration: none;
            transition: var(--transition);
            border: none;
            cursor: pointer;
        }

        .btn-sm {
            padding: 4px 10px;
            font-size: 11px;
        }

        .btn-primary {
            background: rgba(26, 77, 140, 0.1);
            color: var(--primary-blue);
        }

        .btn-primary:hover {
            background: var(--primary-blue);
            color: white;
        }

        /* Modal */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            align-items: center;
            justify-content: center;
        }

        .modal.active {
            display: flex;
        }

        .modal-content {
            background: var(--white);
            border-radius: 24px;
            max-width: 500px;
            width: 90%;
            max-height: 90vh;
            overflow-y: auto;
            animation: fadeInUp 0.3s ease;
        }

        .modal-header {
            padding: 20px 24px;
            border-bottom: 1px solid var(--gray-200);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-header h3 {
            font-size: 18px;
            font-weight: 600;
        }

        .modal-close {
            background: none;
            border: none;
            font-size: 20px;
            cursor: pointer;
            color: var(--gray-500);
            transition: var(--transition);
        }

        .modal-close:hover {
            color: var(--gray-800);
        }

        .modal-body {
            padding: 24px;
        }

        .modal-footer {
            padding: 16px 24px;
            border-top: 1px solid var(--gray-200);
            display: flex;
            justify-content: flex-end;
            gap: 12px;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Pagination */
        .pagination {
            display: flex;
            justify-content: center;
            gap: 8px;
            margin-top: 24px;
        }

        .pagination a {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 36px;
            height: 36px;
            padding: 0 12px;
            border-radius: 8px;
            background: var(--white);
            border: 1px solid var(--gray-200);
            color: var(--gray-700);
            text-decoration: none;
            font-size: 14px;
            transition: var(--transition);
        }

        .pagination a:hover {
            background: var(--gray-100);
            border-color: var(--gray-300);
        }

        .pagination a.active {
            background: var(--primary-navy);
            border-color: var(--primary-navy);
            color: white;
        }

        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 48px;
            color: var(--gray-500);
        }

        .empty-state i {
            font-size: 48px;
            margin-bottom: 16px;
            opacity: 0.5;
        }

        /* Hamburger Menu */
        .hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: var(--gray-700);
        }

        /* Mobile Responsive */
        @media (max-width: 1024px) {
            .sidebar {
                transform: translateX(-100%);
                z-index: 1000;
            }

            .sidebar.mobile-open {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .hamburger {
                display: block;
            }
        }

        @media (max-width: 768px) {
            .content-area {
                padding: 20px;
            }

            .top-header {
                padding: 12px 20px;
            }

            .page-title h1 {
                font-size: 20px;
            }

            .user-details {
                display: none;
            }

            .stats-grid {
                grid-template-columns: 1fr;
                gap: 16px;
            }

            .form-grid {
                grid-template-columns: 1fr;
            }

            .data-table th,
            .data-table td {
                padding: 12px 16px;
            }
        }
    </style>
    <link rel="stylesheet" href="../assets/css/nexus-theme.css">
</head>

<body>
    <div class="dashboard-wrapper">
        <!-- Sidebar Navigation -->
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <a href="dashboard.php" class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-building-columns"></i>
                    </div>
                    <span class="logo-text">Oakshire Finance</span>
                </a>
            </div>

            <nav class="nav-menu">
                <div class="nav-item">
                    <a href="dashboard.php" class="nav-link">
                        <i class="fas fa-chart-line"></i>
                        <span>Dashboard</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-users.php" class="nav-link">
                        <i class="fas fa-users"></i>
                        <span>Manage Users</span>
                    </a>
                </div>
                <div class="nav-item"><a href="manage-balance.php" class="nav-link"><i class="fas fa-wallet"></i> Manage
                        Balance</a></div>

                <div class="nav-item"><a href="manage-balance.php" class="nav-link"><i class="fas fa-wallet"></i> Manage
                        Balance</a></div>
                <div class="nav-item">
                    <a href="manage-loans.php" class="nav-link">
                        <i class="fas fa-hand-holding-usd"></i>
                        <span>Manage Loans</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-investments.php" class="nav-link active">
                        <i class="fas fa-chart-pie"></i>
                        <span>Manage Investments</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="track-investments.php" class="nav-link">
                        <i class="fas fa-chart-simple"></i>
                        <span>Users Investments</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="role.php" class="nav-link">
                        <i class="fas fa-user-tag"></i>
                        <span>Roles</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="recent_transactions.php" class="nav-link">
                        <i class="fas fa-exchange-alt"></i>
                        <span>Transactions</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="loan-history.php" class="nav-link">
                        <i class="fas fa-history"></i>
                        <span>Loan History</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="login-records.php" class="nav-link">
                        <i class="fas fa-sign-in-alt"></i>
                        <span>Login Records</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-messages.php" class="nav-link">
                        <i class="fas fa-envelope"></i>
                        <span>Contact Messages</span>
                    </a>
                </div>
            </nav>

            <div class="logout-section">
                <a href="../logout.php" class="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <span>Logout</span>
                </a>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <!-- Top Header -->
            <header class="top-header">
                <button class="hamburger" id="hamburger">
                    <i class="fas fa-bars"></i>
                </button>
                <div class="page-title">
                    <h1>Manage Investments</h1>
                </div>
                <div class="user-menu">
                    <button class="notification-btn">
                        <i class="fas fa-bell"></i>
                    </button>
                    <div class="user-info">
                        <div class="user-avatar">
                            <i class="fas fa-user-shield"></i>
                        </div>
                        <div class="user-details">
                            <h4><?= htmlspecialchars($admin['full_name'] ?? 'Admin User') ?></h4>
                            <p>System Administrator</p>
                        </div>
                    </div>
                </div>
            </header>

            <!-- Content Area -->
            <div class="content-area">
                <!-- Statistics Cards -->
                <div class="stats-grid">
                    <div class="stat-card">
                        <div class="stat-icon">
                            <i class="fas fa-chart-pie"></i>
                        </div>
                        <h3>Total Plans</h3>
                        <div class="stat-value"><?= number_format($totalPlans) ?></div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-icon">
                            <i class="fas fa-percent"></i>
                        </div>
                        <h3>Average Interest</h3>
                        <div class="stat-value"><?= $avgInterest ?>%</div>
                    </div>
                </div>

                <!-- Add New Investment Plan Form -->
                <div class="form-card">
                    <h2>
                        <i class="fas fa-plus-circle" style="color: var(--success-green);"></i>
                        Add New Investment Plan
                    </h2>
                    <form action="manage-investments.php" method="POST">
                        <div class="form-grid">
                            <div class="form-group">
                                <label for="plan_name">Plan Name *</label>
                                <input type="text" id="plan_name" name="plan_name" placeholder="e.g., Growth Plus"
                                    required>
                            </div>
                            <div class="form-group">
                                <label for="interest_rate">Interest Rate (%) *</label>
                                <input type="number" id="interest_rate" name="interest_rate" step="0.01"
                                    placeholder="e.g., 8.5" required>
                            </div>
                            <div class="form-group">
                                <label for="min_amount">Min Investment Amount ($) *</label>
                                <input type="number" id="min_amount" name="min_amount" step="0.01"
                                    placeholder="e.g., 1000" required>
                            </div>
                            <div class="form-group">
                                <label for="max_amount">Max Investment Amount ($) *</label>
                                <input type="number" id="max_amount" name="max_amount" step="0.01"
                                    placeholder="e.g., 100000" required>
                            </div>
                            <div class="form-group">
                                <label for="duration_months">Duration (Months) *</label>
                                <input type="number" id="duration_months" name="duration_months" placeholder="e.g., 12"
                                    required>
                            </div>
                            <div class="form-group">
                                <label for="risk_level">Risk Level *</label>
                                <select id="risk_level" name="risk_level" required>
                                    <option value="Low">Low Risk</option>
                                    <option value="Medium">Medium Risk</option>
                                    <option value="High">High Risk</option>
                                </select>
                            </div>
                        </div>
                        <button type="submit" name="add_plan" class="btn-submit">
                            <i class="fas fa-plus"></i> Create Investment Plan
                        </button>
                    </form>
                </div>

                <!-- Investment Plans Table -->
                <div class="form-card" style="padding: 0; overflow: hidden;">
                    <div style="padding: 20px 24px; border-bottom: 1px solid var(--gray-200);">
                        <h2 style="margin: 0;">
                            <i class="fas fa-chart-line" style="color: var(--primary-blue);"></i>
                            Investment Plans
                        </h2>
                    </div>

                    <div class="table-container" style="margin: 0; border-radius: 0;">
                        <?php if (empty($investmentPlans)): ?>
                            <div class="empty-state">
                                <i class="fas fa-chart-line"></i>
                                <p>No investment plans found. Create your first plan above.</p>
                            </div>
                        <?php else: ?>
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>Plan Name</th>
                                        <th>Interest Rate</th>
                                        <th>Min Investment</th>
                                        <th>Max Investment</th>
                                        <th>Duration</th>
                                        <th>Risk Level</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($investmentPlans as $plan): ?>
                                        <?php
                                        $riskClass = '';
                                        if ($plan['risk_level'] === 'Low')
                                            $riskClass = 'badge-low';
                                        elseif ($plan['risk_level'] === 'Medium')
                                            $riskClass = 'badge-medium';
                                        else
                                            $riskClass = 'badge-high';
                                        ?>
                                        <tr>
                                            <td>
                                                <strong><?= htmlspecialchars($plan['plan_name']) ?></strong>
                                            </td>
                                            <td>
                                                <span class="badge"
                                                    style="background: rgba(26, 77, 140, 0.1); color: var(--primary-blue);">
                                                    <?= $plan['interest_rate'] ?>%
                                                </span>
                                            </td>
                                            <td>$<?= number_format($plan['min_amount'], 2) ?></td>
                                            <td>$<?= number_format($plan['max_amount'], 2) ?></td>
                                            <td><?= $plan['duration_months'] ?> months</td>
                                            <td>
                                                <span class="badge <?= $riskClass ?>"><?= $plan['risk_level'] ?> Risk</span>
                                            </td>
                                            <td>
                                                <button onclick="openEditModal(
                                                    <?= $plan['plan_id'] ?>,
                                                    '<?= addslashes(htmlspecialchars($plan['plan_name'])) ?>',
                                                    <?= $plan['interest_rate'] ?>,
                                                    <?= $plan['min_amount'] ?>,
                                                    <?= $plan['max_amount'] ?>,
                                                    <?= $plan['duration_months'] ?>,
                                                    '<?= $plan['risk_level'] ?>'
                                                )" class="btn btn-primary btn-sm">
                                                    <i class="fas fa-edit"></i> Edit
                                                </button>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php endif; ?>
                    </div>

                    <!-- Pagination -->
                    <?php if ($totalPages > 1): ?>
                        <div class="pagination" style="padding: 20px;">
                            <?php if ($page > 1): ?>
                                <a href="?page=<?= $page - 1 ?>">&laquo; Prev</a>
                            <?php endif; ?>
                            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                                <a href="?page=<?= $i ?>" class="<?= $i == $page ? 'active' : '' ?>"><?= $i ?></a>
                            <?php endfor; ?>
                            <?php if ($page < $totalPages): ?>
                                <a href="?page=<?= $page + 1 ?>">Next &raquo;</a>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </main>
    </div>

    <!-- Edit Modal -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3><i class="fas fa-edit"></i> Edit Investment Plan</h3>
                <button class="modal-close" onclick="closeEditModal()">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <form action="manage-investments.php" method="POST">
                <div class="modal-body">
                    <input type="hidden" id="edit_plan_id" name="plan_id">

                    <div class="form-group" style="margin-bottom: 16px;">
                        <label for="edit_plan_name">Plan Name *</label>
                        <input type="text" id="edit_plan_name" name="plan_name" required>
                    </div>

                    <div class="form-group" style="margin-bottom: 16px;">
                        <label for="edit_interest_rate">Interest Rate (%) *</label>
                        <input type="number" id="edit_interest_rate" name="interest_rate" step="0.01" required>
                    </div>

                    <div class="form-group" style="margin-bottom: 16px;">
                        <label for="edit_min_amount">Min Investment Amount ($) *</label>
                        <input type="number" id="edit_min_amount" name="min_amount" step="0.01" required>
                    </div>

                    <div class="form-group" style="margin-bottom: 16px;">
                        <label for="edit_max_amount">Max Investment Amount ($) *</label>
                        <input type="number" id="edit_max_amount" name="max_amount" step="0.01" required>
                    </div>

                    <div class="form-group" style="margin-bottom: 16px;">
                        <label for="edit_duration_months">Duration (Months) *</label>
                        <input type="number" id="edit_duration_months" name="duration_months" required>
                    </div>

                    <div class="form-group" style="margin-bottom: 16px;">
                        <label for="edit_risk_level">Risk Level *</label>
                        <select id="edit_risk_level" name="risk_level" required>
                            <option value="Low">Low Risk</option>
                            <option value="Medium">Medium Risk</option>
                            <option value="High">High Risk</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn" onclick="closeEditModal()"
                        style="background: var(--gray-100);">Cancel</button>
                    <button type="submit" name="edit_plan" class="btn-submit" style="margin: 0;">Save Changes</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // Mobile sidebar toggle
        const hamburger = document.getElementById('hamburger');
        const sidebar = document.getElementById('sidebar');

        if (hamburger) {
            hamburger.addEventListener('click', function () {
                sidebar.classList.toggle('mobile-open');
            });
        }

        // Close sidebar when clicking outside on mobile
        document.addEventListener('click', function (event) {
            if (window.innerWidth <= 1024) {
                const isClickInside = sidebar.contains(event.target) || hamburger.contains(event.target);
                if (!isClickInside && sidebar.classList.contains('mobile-open')) {
                    sidebar.classList.remove('mobile-open');
                }
            }
        });

        // Edit Modal Functions
        const editModal = document.getElementById('editModal');

        function openEditModal(plan_id, plan_name, interest_rate, min_amount, max_amount, duration_months, risk_level) {
            document.getElementById('edit_plan_id').value = plan_id;
            document.getElementById('edit_plan_name').value = plan_name;
            document.getElementById('edit_interest_rate').value = interest_rate;
            document.getElementById('edit_min_amount').value = min_amount;
            document.getElementById('edit_max_amount').value = max_amount;
            document.getElementById('edit_duration_months').value = duration_months;
            document.getElementById('edit_risk_level').value = risk_level;

            editModal.classList.add('active');
        }

        function closeEditModal() {
            editModal.classList.remove('active');
        }

        // Close modal when clicking outside
        window.onclick = function (event) {
            if (event.target === editModal) {
                closeEditModal();
            }
        }
    </script>
</body>

</html>