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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel</title>
    <link href="https://fonts.googleapis.com/css2?family=Great+Vibes&family=Playball&family=Lobster+Two&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --sidebar-width: 250px;
            --main-content-width: calc(100% - 250px);
            --primary-color: #e74c3c;        /* Vibrant red for primary actions */
            --secondary-color: #2c3e50;       /* Dark navy for secondary elements */
            --accent-color: #f39c12;         /* Orange for highlights */
            --success-color:rgb(39, 174, 96);        /* Green for success messages */
            --light-color: #f8f9fa;          /* Light background */
            --dark-color: #1a1a1a;           /* Dark text */
            --text-color: #333;              /* Main text */
            --text-light: #7f8c8d;           /* Secondary text */
            --sidebar-bg: #2c3e50;           /* Sidebar background */
            --sidebar-text: #ecf0f1;         /* Sidebar text */
            --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: var(--light-color);
            color: var(--text-color);
            overflow-x: hidden;
        }

        .admin-container {
            display: flex;
            min-height: 100vh;
        }

        /* Sidebar Styles */
        .sidebar {
            width: var(--sidebar-width);
            background-color: var(--sidebar-bg);
            color: var(--sidebar-text);
            display: flex;
            flex-direction: column;
            transition: var(--transition);
            position: fixed;
            z-index: 1000;
            height: 100vh;
            overflow-y: auto;
        }

        .sidebar-header {
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            position: relative;
        }

        .sidebar-controls {
            display: flex;
            gap: 10px;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 1.2rem;
            font-weight: bold;
            transition: var(--transition);
        }

        .logo i {
            font-size: 1.5rem;
            transition: var(--transition);
        }

        .toggle-btn {
            background: none;
            border: none;
            color: white;
            font-size: 1rem;
            cursor: pointer;
            transition: var(--transition);
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .close-btn {
            background: none;
            border: none;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            display: none;
            transition: var(--transition);
        }

        .sidebar-nav {
            flex: 1;
            padding: 20px 0;
        }

        .sidebar-nav ul {
            list-style: none;
            padding-left: 10px;
        }

        .sidebar-nav li {
            margin: 5px 0;
        }

        .sidebar-nav a {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            color: var(--sidebar-text);
            text-decoration: none;
            transition: var(--transition);
            gap: 10px;
        }

        .sidebar-nav a:hover {
            background-color: rgba(255, 255, 255, 0.1);
        }

        .sidebar-nav a i {
            width: 20px;
            text-align: center;
        }

        .sidebar-nav .active a {
            background-color: rgba(255, 255, 255, 0.2);
            border-left: 4px solid var(--accent-color);
        }

        .sidebar-footer {
            padding: 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        .user-profile {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 20px;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.2);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
        }

        .user-info {
            display: flex;
            flex-direction: column;
            transition: var(--transition);
        }

        .username {
            font-weight: bold;
        }

        .role {
            font-size: 0.8rem;
            opacity: 0.8;
        }

        .logout-btn {
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--sidebar-text);
            text-decoration: none;
            padding: 8px 15px;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.1);
            transition: var(--transition);
        }

        .logout-btn:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }

        /* Collapsed state */
        .sidebar.collapsed {
            width: 70px;
        }

        .sidebar.collapsed .logo span,
        .sidebar.collapsed .sidebar-nav span,
        .sidebar.collapsed .user-info,
        .sidebar.collapsed .logout-btn span {
            display: none;
        }

        .sidebar.collapsed .logo {
            justify-content: center;
            padding: 0;
        }

        .sidebar.collapsed .logo i {
            margin-right: 0;
        }

        .sidebar.collapsed .sidebar-nav a {
            justify-content: center;
            padding: 12px 0;
        }

        .sidebar.collapsed .user-profile {
            justify-content: center;
        }

        .sidebar.collapsed .logout-btn {
            justify-content: center;
            padding: 8px 0;
        }

        .sidebar.collapsed .toggle-btn {
            display: none;
        }

        .sidebar.collapsed .close-btn {
            display: block;
            margin: 0 auto;
        }

        /* Main Content Styles */
        .main-content {
            width: var(--main-content-width);
            transition: var(--transition);
            margin-left: var(--sidebar-width);
        }

        .sidebar.collapsed + .main-content {
            margin-left: 70px;
            width: calc(100% - 70px);
        }

        .top-nav {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 25px;
            background-color: white;
            box-shadow: var(--shadow);
            position: sticky;
            top: 0;
            z-index: 900;
        }

        .menu-btn {
            background: none;
            border: none;
            font-size: 1.2rem;
            color: var(--text-color);
            cursor: pointer;
            display: none;
        }

        .page-title h1 {
            font-size: 1.5rem;
            color: var(--dark-color);
        }

        .user-actions {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-dropdown {
            position: relative;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
        }

        .user-dropdown .avatar {
            width: 36px;
            height: 36px;
            font-size: 1.2rem;
            background-color: var(--primary-color);
            color: white;
        }

        .dropdown-menu {
            border: none;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            padding: 10px 0;
            min-width: 220px;
        }

        .dropdown-item {
            padding: 8px 15px;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: var(--transition);
        }

        .dropdown-item:hover {
            background-color: #f8f9fa;
        }

        .dropdown-item i {
            width: 20px;
            text-align: center;
            color: var(--primary-color);
        }

        .dropdown-divider {
            margin: 5px 0;
            border-color: rgba(0, 0, 0, 0.05);
        }

        /* Content Area Styles */
        .content-area {
            padding: 25px;
            min-height: calc(100vh - 70px);
        }

        .welcome-message {
            background-color: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: var(--shadow);
            text-align: center;
        }

        .welcome-message h2 {
            color: var(--primary-color);
            margin-bottom: 15px;
        }

        .welcome-message p {
            color: var(--text-light);
            font-size: 1.1rem;
        }

        /* Filter Section */
        .filter-section {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 25px;
            padding: 20px;
            transition: all 0.3s ease;
        }

        .filter-section:hover {
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        /* Table Styles */
        #applicationsTable {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        #applicationsTable thead th {
            background-color: var(--primary-color);
            color: white;
            font-weight: 600;
            padding: 12px 15px;
            position: sticky;
            top: 0;
        }

        #applicationsTable tbody tr {
            transition: all 0.2s ease;
        }

        #applicationsTable tbody tr:hover {
            background-color: rgba(231, 76, 60, 0.05);
        }

        #applicationsTable td {
            padding: 12px 15px;
            vertical-align: middle;
            border-bottom: 1px solid #e9ecef;
        }

        .jhpTable {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        .jhpTable thead th {
            background-color: var(--primary-color);
            color: white;
            font-weight: 600;
            padding: 12px 15px;
            position: sticky;
            top: 0;
        }

        .jhpTable tbody tr {
            transition: all 0.2s ease;
        }

        .jhpTable tbody tr:hover {
            background-color: rgba(231, 76, 60, 0.05);
        }

        .jhpTable td {
            padding: 12px 15px;
            vertical-align: middle;
            border-bottom: 1px solid #e9ecef;
        }

        /* Status Badges */
        .badge-status {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
        }

        .badge-active {
            background-color: #d1fae5;
            color: #065f46;
        }

        .badge-pending {
            background-color: #fef3c7;
            color: #92400e;
        }

        .badge-closed {
            background-color: #fee2e2;
            color: #991b1b;
        }

        #spinner {
            height: 90vh;
            width: 80vw;
            background-color: var(--light-color);
            position: fixed;
            bottom: 0;
            left: 20%;
            justify-content: center !important;
            align-items: center !important;
            font-weight: bold;
            z-index: 99;
        }

        /* Mobile Styles */
        @media (max-width: 992px) {
            .sidebar {
                left: -100%;
                width: 280px;
            }

            .sidebar.active {
                left: 0;
            }

            .main-content {
                width: 100%;
                margin-left: 0;
            }

            .sidebar.collapsed + .main-content {
                width: 100%;
                margin-left: 0;
            }

            .menu-btn {
                display: block;
            }

            .overlay {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-color: rgba(0, 0, 0, 0.5);
                z-index: 999;
                display: none;
            }

            .overlay.active {
                display: block;
            }
        }

        /* Responsive Adjustments */
        @media (max-width: 768px) {
            #spinner {
                width: 100vw;
                left: 0;
            }
        }
        /* Add these to your existing styles */
.dashboard-container {
    padding: 20px;
}

/* Quick Action Cards */
.quick-action-card {
    cursor: pointer;
    transition: all 0.3s ease;
    border: none;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    height: 100%;
}

.quick-action-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.action-icon {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 15px;
    color: white;
    font-size: 24px;
}

.action-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 0;
}

/* Stat Cards */
.stat-card {
    border: none;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    height: 100%;
}

.stat-icon {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    float: left;
    margin-right: 15px;
    background-color: rgba(231, 76, 60, 0.1);
    color: var(--primary-color);
    font-size: 20px;
}

.stat-content {
    overflow: hidden;
}

.stat-value {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 5px;
}

.stat-label {
    color: var(--text-light);
    margin-bottom: 5px;
}

.stat-change {
    font-size: 12px;
    color: var(--text-light);
}

/* Chart Cards */
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: white;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.card-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 0;
}

.chart-period .btn {
    padding: 2px 10px;
    font-size: 12px;
}

/* Upcoming Classes */
.upcoming-classes-list {
    max-height: 300px;
    overflow-y: auto;
}

.class-item {
    padding: 15px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
}

.class-item:hover {
    background-color: rgba(231, 76, 60, 0.05);
}

.class-name {
    font-weight: 600;
    margin-bottom: 5px;
}

.class-trainer {
    font-size: 13px;
    color: var(--text-light);
    margin-bottom: 5px;
}

.class-time {
    font-size: 14px;
    color: var(--primary-color);
    font-weight: 500;
}

/* Equipment Status */
.equipment-stat-item {
    margin-bottom: 15px;
}

.stat-label {
    font-size: 14px;
    color: var(--text-light);
}

.stat-value {
    font-size: 24px;
    font-weight: 700;
}

.equipment-conditions {
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    padding-top: 15px;
}

.condition-item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
}

.condition-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 8px;
}

/* Responsive Adjustments */
@media (max-width: 992px) {
    .stat-icon {
        float: none;
        margin: 0 auto 10px;
    }
    
    .stat-content {
        text-align: center;
    }
}
body.dark-mode {
    background-color: #121212;
    color: #f1f1f1;
}
.ironpulse-text {
    font-size: 17px;
    font-family: 'Great Vibes', cursive;
    font-weight: 600;
}



    </style>
</head>

<body>
    <div class="admin-container">
        <div class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <div class="logo" id="logo">
    <i class="fas fa-dumbbell" style="color:#00cc66;"></i>
    <span class="ironpulse-text">
        <span style="color: #00ff88;">I</span>
        <span style="color: #aaff00;">R</span>
        <span style="color: #ffff00;">O</span>
        <span style="color: #ffaa00;">N</span>
        <span style="color: #ff6600;">P</span>
        <span style="color: #ff3300;">U</span>
        <span style="color: #ff0033;">L</span>
        <span style="color: #cc0099;">S</span>
        <span style="color: #9900cc;">E</span>
    </span>
</div>

                <div class="sidebar-controls">
                    <button class="toggle-btn" id="toggle-btn" title="Toggle Sidebar">
                        <i class="fas fa-bars"></i>
                    </button>
                    <button class="close-btn" id="close-btn">
                        <i class="fas fa-times"></i>
                    </button>
                </div>
            </div>
            <nav class="sidebar-nav">
                <ul>
                   <li class="active">
    <a href="#" data-page="/admin/pages/dashboard">
        <i class="fas fa-tachometer-alt"></i>
        <span>Dashboard</span>
    </a>
</li>
                    <li>
    <a href="#" data-page="/admin/pages/members">
        <i class="fas fa-users"></i>
        <span>Members</span>
    </a>
</li>
                   <li class="nav-item">
    <a href="#" data-page="/admin/pages/trainers" class="nav-link">
        <i class="fas fa-users-cog me-2"></i>
        <span>Trainers</span>
    </a>
</li>

                    <li>
                        <a href="#" data-page="/admin/pages/equipment">
                            <i class="fas fa-cogs"></i>
                            <span>Equipment</span>
                        </a>
                    </li>
                    <li>
    <a href="#" data-page="/admin/pages/classes">
        <i class="fas fa-calendar-alt"></i>
        <span>Classes</span>
    </a>
</li>
                  <li class="nav-item">
    <a href="#" data-page="/admin/pages/payments" class="nav-link">
        <i class="fas fa-hand-holding-dollar me-2"></i>
        <span>Payments</span>
    </a>
</li>
                    <li>
                        <a href="#" data-page="/admin/pages/reports">
                            <i class="fas fa-file-alt"></i>
                            <span>Reports</span>
                        </a>
                    </li>
                    <li>
                        <a href="#" data-page="/admin/pages/analytics">
                            <i class="fas fa-chart-line"></i>
                            <span>Analytics</span>
                        </a>
                    </li>
                </ul>
            </nav>
            <div class="sidebar-footer">
                <div class="user-profile">
                    <div class="avatar">
                        <i class="fas fa-user-circle"></i>
                    </div>
                    <a href="#" data-page="/admin/pages/admin_profile" class="user-info">
                        <span class="username">Admin User</span>
                        <span class="role">Super Admin</span>
                    </a>
                </div>
                <a href="#" class="logout-btn" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
                    <i class="fas fa-sign-out-alt"></i>
                    <span>Logout</span>
                </a>
                <form id="logout-form" action="{{ route('admin.logout') }}" method="POST" style="display: none;">
                    @csrf
                </form>
            </div>
        </div>

        <div class="main-content">
           <nav class="top-nav">
    <button class="menu-btn" id="menu-btn">
        <i class="fas fa-bars"></i>
    </button>

    <div class="page-title">
        <h1>Dashboard</h1>
    </div>

    <div class="user-actions d-flex align-items-center">
        <!-- 🌗 Dark/Light Mode Toggle Button -->
        <button id="theme-toggle" class="btn btn-outline-secondary me-3" title="Toggle Theme" style="border-radius: 50%; padding: 8px 10px;">
            <i class="fas fa-moon fa-lg" id="theme-icon"></i>
        </button>

        <!-- 🔄 Refresh Button -->
        <button onclick="location.reload()" class="btn btn-outline-primary me-3" title="Refresh Page" style="border-radius: 50%; padding: 8px 10px;">
            <i class="fas fa-sync-alt fa-lg"></i>
        </button>

        <!-- 👤 User Dropdown with Image -->
     <div class="user-dropdown dropdown">
    <div data-bs-toggle="dropdown" aria-expanded="false" style="cursor: pointer; display: flex; align-items: center;">
        <i class="fas fa-user-shield" style="font-size: 20px; color: #333; background-color: #e0e0e0; padding: 7px; border-radius: 50%; width: 35px; height: 35px; display: flex; align-items: center; justify-content: center; margin-right: 10px;"></i>
    </div>
    <ul class="dropdown-menu dropdown-menu-end">
        <li>
            <a class="dropdown-item" href="#">
                <i class="fas fa-envelope"></i>
                <span>admin@example.com</span>
            </a>
        </li>
        <li>
            <a class="dropdown-item" href="#">
                <i class="fas fa-user"></i>
                <span>Admin User</span>
            </a>
        </li>
        <li><hr class="dropdown-divider"></li>
        <li>
            <a class="dropdown-item" href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
                <i class="fas fa-sign-out-alt"></i>
                <span>Logout</span>
            </a>
        </li>
    </ul>
</div>

    </div>
</nav>
<script>
    const toggleBtn = document.getElementById('theme-toggle');
    const themeIcon = document.getElementById('theme-icon');

    toggleBtn.addEventListener('click', () => {
        document.body.classList.toggle('dark-mode');

        // Toggle icon between moon and sun
        if (document.body.classList.contains('dark-mode')) {
            themeIcon.classList.remove('fa-moon');
            themeIcon.classList.add('fa-sun');
        } else {
            themeIcon.classList.remove('fa-sun');
            themeIcon.classList.add('fa-moon');
        }
    });
</script>



            <div class="content-area" id="content-area"><!-- Page content --></div>
            <div id="spinner" style="display:none;">Loading . . .</div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
    // Elements
    const sidebar = document.getElementById('sidebar');
    const menuBtn = document.getElementById('menu-btn');
    const closeBtn = document.getElementById('close-btn');
    const toggleBtn = document.getElementById('toggle-btn');
    const contentArea = document.getElementById('content-area');
    let navLinks = Array.from(document.querySelectorAll('.sidebar-nav a'));
    
    // Admin profile link
    const extraLink = document.querySelector('a[data-page="/admin/pages/admin_profile"]');
    
    // Add Admin Profile link to nav links
    if (extraLink && !navLinks.includes(extraLink)) {
        navLinks.push(extraLink);
    }

    // Create overlay for mobile sidebar toggle
    const overlay = document.createElement('div');
    overlay.className = 'overlay';
    document.body.appendChild(overlay);

    // Function to toggle sidebar visibility
    function toggleSidebar() {
        sidebar.classList.toggle('active');
        overlay.classList.toggle('active');
    }

    menuBtn.addEventListener('click', toggleSidebar);
    closeBtn.addEventListener('click', toggleSidebar);
    overlay.addEventListener('click', toggleSidebar);

    // Toggle sidebar collapse
    toggleBtn.addEventListener('click', function() {
        sidebar.classList.toggle('collapsed');
        
        // Store state in localStorage
        const isCollapsed = sidebar.classList.contains('collapsed');
        localStorage.setItem('sidebarCollapsed', isCollapsed);
    });

    // Close button functionality
    closeBtn.addEventListener('click', function() {
        sidebar.classList.remove('collapsed');
        
        // Store state in localStorage
        localStorage.setItem('sidebarCollapsed', 'false');
    });
    
    // Check for saved sidebar state
    if (localStorage.getItem('sidebarCollapsed') === 'true') {
        sidebar.classList.add('collapsed');
    }

    let isLoading = false;
    const spinner = document.getElementById('spinner');

    // Function to load page content via fetch and execute scripts
    function loadPage(page) {
        if (isLoading) return; // Prevent concurrent loading
        isLoading = true;

        // Show spinner
        spinner.style.display = 'flex';

        // Add timestamp to URL to prevent caching
        const timestamp = new Date().getTime();
        const url = page.includes('?') ? `${page}&_=${timestamp}` : `${page}?_=${timestamp}`;

        fetch(url)
            .then(response => {
                if (!response.ok) throw new Error('Network response was not ok');
                return response.text();
            })
            .then(html => {
                // Replace content
                contentArea.innerHTML = html;

                // Re-execute scripts in the loaded content
                const scripts = contentArea.querySelectorAll('script');
                scripts.forEach(oldScript => {
                    const newScript = document.createElement('script');
                    if (oldScript.src) {
                        newScript.src = oldScript.src;
                        newScript.async = false; // Maintain script execution order
                    } else {
                        newScript.textContent = oldScript.textContent;
                    }
                    oldScript.parentNode.replaceChild(newScript, oldScript);
                });

                // If the loaded page contains a DataTable, refresh it
                if (contentArea.querySelector('#applicationsTable')) {
                    if ($.fn.DataTable.isDataTable('#applicationsTable')) {
                        $('#applicationsTable').DataTable().ajax.reload();
                    }
                }
            })
            .catch(error => {
                contentArea.innerHTML = `
                    <div class="error-message">
                        <h2>Error Loading Page</h2>
                        <p>${error.message}</p>
                    </div>
                `;
            })
            .finally(() => {
                isLoading = false;
                spinner.style.display = 'none';
            });
    }

    // Set active link and load content when nav link is clicked
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            const page = this.getAttribute('data-page');

            // Update active sidebar item visually
            navLinks.forEach(navLink => navLink.parentElement.classList.remove('active'));
            this.parentElement.classList.add('active');

            // Update page title
            const pageTitle = this.querySelector('span').textContent;
            document.querySelector('.page-title h1').textContent = pageTitle;

            // Load the requested page content
            loadPage(page);

            // Close sidebar on mobile after selection
            if (window.innerWidth < 992) {
                toggleSidebar();
            }
        });
    });

    // Auto-load initial page (Dashboard or first active link)
    const initialLink = document.querySelector('.sidebar-nav li.active a') || navLinks[0];
    if (initialLink) {
        initialLink.click();
    }
});

$(document).ready(function() {
    // Initialize DataTable
    var table = $('#applicationsTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ],
        ajax: {
            url: 'fetch_applications.php',
            dataSrc: ''
        },
        columns: [{
                data: 'id'
            },
            {
                data: 'fullName'
            },
            {
                data: 'email'
            },
            {
                data: 'phone'
            },
            {
                data: 'project'
            },
            {
                data: 'propertyPurpose'
            },
            {
                data: 'submissionDate',
                render: function(data) {
                    return new Date(data).toLocaleString();
                }
            },
            {
                data: 'status',
                render: function(data) {
                    var badgeClass = '';
                    switch (data) {
                        case 'new':
                            badgeClass = 'bg-primary';
                            break;
                        case 'contacted':
                            badgeClass = 'bg-warning text-dark';
                            break;
                        case 'qualified':
                            badgeClass = 'bg-success';
                            break;
                        case 'closed':
                            badgeClass = 'bg-danger';
                            break;
                    }
                    return '<span class="badge ' + badgeClass + '">' + data.charAt(0)
                        .toUpperCase() + data.slice(1) + '</span>';
                }
            },
            {
                data: null,
                render: function(data, type, row) {
                    return '<button class="btn btn-sm btn-info view-btn" data-id="' + row
                        .id + '"><i class="bi bi-eye"></i> View</button> ' +
                        '<button class="btn btn-sm btn-primary status-btn" data-id="' + row
                        .id + '"><i class="bi bi-pencil"></i> Update</button>';
                }
            }
        ],
        createdRow: function(row, data, dataIndex) {
            switch (data.status) {
                case 'new':
                    $(row).addClass('status-new');
                    break;
                case 'contacted':
                    $(row).addClass('status-contacted');
                    break;
                case 'qualified':
                    $(row).addClass('status-qualified');
                    break;
                case 'closed':
                    $(row).addClass('status-closed');
                    break;
            }
        }
    });

    // View Application Details
    $(document).on('click', '.view-btn', function() {
        var id = $(this).data('id');
        $.ajax({
            url: 'fetch_application_details.php',
            type: 'GET',
            data: {
                id: id
            },
            success: function(response) {
                $('#applicationDetails').html(response);
                $('#viewApplicationModal').modal('show');
            }
        });
    });

    // Update Status Button
    $(document).on('click', '.status-btn', function() {
        var id = $(this).data('id');
        $('#applicationId').val(id);

        // Fetch current status and notes
        $.ajax({
            url: 'fetch_application_status.php',
            type: 'GET',
            data: {
                id: id
            },
            success: function(response) {
                $('#statusSelect').val(response.status);
                $('#adminNotes').val(response.notes);
                $('#updateStatusModal').modal('show');
            }
        });
    });

    // Save Status Changes
    $('#saveStatus').click(function() {
        var id = $('#applicationId').val();
        var status = $('#statusSelect').val();
        var notes = $('#adminNotes').val();

        $.ajax({
            url: 'update_application_status.php',
            type: 'POST',
            data: {
                id: id,
                status: status,
                notes: notes
            },
            success: function() {
                table.ajax.reload();
                $('#updateStatusModal').modal('hide');
            }
        });
    });

    // Apply Filters
    $('#applyFilters').click(function() {
        var project = $('#projectFilter').val();
        var status = $('#statusFilter').val();
        var dateFrom = $('#dateFrom').val();
        var dateTo = $('#dateTo').val();

        table.ajax.url('fetch_applications.php?project=' + project + '&status=' + status +
            '&dateFrom=' + dateFrom + '&dateTo=' + dateTo).load();
    });

    // Reset Filters
    $('#resetFilters').click(function() {
        $('#projectFilter').val('');
        $('#statusFilter').val('');
        $('#dateFrom').val('');
        $('#dateTo').val('');
        table.ajax.url('fetch_applications.php').load();
    });
});
    </script>
</body>
</html>