<?php
session_start();

// Connect to SQLite database
$db = new PDO('sqlite:database.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Update expiry_date if it's NULL, setting it to 30 days after activation_date
$update_stmt = $db->prepare("
    UPDATE activated_students
    SET expiry_date = datetime(activation_date, '+31 days')
    WHERE expiry_date IS NULL;
");
$update_stmt->execute();

// Fetch all activated users
$query = "
    SELECT a.user_id, s.name, a.activation_date, a.expiry_date
    FROM activated_students a
    JOIN students s ON a.user_id = s.user_id
";
$stmt = $db->prepare($query);
$stmt->execute();

// Get current page name for active navigation
$current_page = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CAOSCE · Network</title>
    <link rel="icon" type="image/x-icon" href="CAOSCE Network.ico">

    <!-- Font Awesome 6 (Free) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        /* ----- RESET & BASE ----- */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', 'Roboto', sans-serif;
            background: #f0f4fb;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            color: #10233f;
        }

        /* ----- NAVIGATION (SOLID, NON-TRANSPARENT) ----- */
        .navbar {
            position: sticky;
            top: 0;
            z-index: 60;
            background: #ffffff;
            border-bottom: 2px solid #e2eaf2;
            padding: 0 5vw;
            height: 74px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 12px rgba(16, 35, 63, 0.06);
        }

        .brand {
            display: flex;
            align-items: center;
            gap: 12px;
            font-weight: 800;
            font-size: 20px;
            color: #10233f;
            text-decoration: none;
        }

        .brand-icon {
            width: 38px;
            height: 38px;
            background: linear-gradient(135deg, #1a6cf0, #6d5dfc);
            border-radius: 12px;
            display: grid;
            place-items: center;
            color: white;
            font-size: 18px;
            font-weight: 900;
            box-shadow: 0 8px 18px rgba(26, 108, 240, 0.25);
        }

        .brand-light {
            font-weight: 500;
            color: #4f6580;
        }

        .nav-links {
            display: flex;
            align-items: center;
            gap: 28px;
        }

        .nav-links a {
            font-size: 14px;
            font-weight: 600;
            color: #4f6580;
            text-decoration: none;
            transition: 0.2s;
            position: relative;
            padding: 6px 0;
        }

        .nav-links a i {
            margin-right: 6px;
            font-size: 13px;
        }

        .nav-links a:hover {
            color: #1a6cf0;
        }

        .nav-links a.active {
            color: #1a6cf0;
        }

        .nav-links a.active::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 100%;
            height: 3px;
            background: #1a6cf0;
            border-radius: 4px;
        }

        .nav-logout {
            background: #f0f4fb;
            border: 1px solid #d9e2eb;
            border-radius: 30px;
            padding: 6px 18px;
            font-weight: 600;
            font-size: 13px;
            color: #10233f;
            transition: 0.2s;
        }

        .nav-logout:hover {
            background: #e2eaf2;
            border-color: #b0c4d8;
            color: #1a6cf0;
        }

        /* ----- MAIN CONTAINER ----- */
        main {
            flex: 1;
            padding: 30px 5vw;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: #ffffff;
            padding: 35px 40px 40px;
            border-radius: 24px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.08);
            border: 1px solid #eef4fa;
            position: relative;
        }

        .page-header {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 25px;
        }

        .page-header .title-group {
            display: flex;
            align-items: center;
            gap: 14px;
        }

        .page-header .title-group .emoji-icon {
            font-size: 36px;
        }

        .page-header h2 {
            font-size: 28px;
            font-weight: 700;
            color: #10233f;
            margin: 0;
        }

        .page-header .subtitle {
            font-size: 14px;
            color: #6b7a8f;
            margin-top: 4px;
        }

        .action-buttons {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 22px;
            border-radius: 30px;
            font-size: 14px;
            font-weight: 600;
            text-decoration: none;
            border: none;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: linear-gradient(135deg, #1a6cf0, #6d5dfc);
            color: white;
            box-shadow: 0 4px 14px rgba(26, 108, 240, 0.25);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(26, 108, 240, 0.35);
        }

        .btn-danger {
            background: #dc3545;
            color: white;
        }

        .btn-danger:hover {
            background: #c82333;
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(220, 53, 69, 0.25);
        }

        .btn-outline {
            background: transparent;
            border: 2px solid #d9e2eb;
            color: #4f6580;
        }

        .btn-outline:hover {
            background: #f0f4fb;
            border-color: #b0c4d8;
        }

        /* ----- SEARCH BAR ----- */
        .search-bar {
            margin: 20px 0 25px 0;
        }

        .search-bar input[type="text"] {
            width: 100%;
            max-width: 400px;
            padding: 12px 18px;
            border-radius: 30px;
            border: 2px solid #e9ecef;
            font-size: 15px;
            transition: all 0.3s;
            background: #fafcff;
        }

        .search-bar input[type="text"]:focus {
            outline: none;
            border-color: #1a6cf0;
            box-shadow: 0 0 0 4px rgba(26, 108, 240, 0.12);
        }

        /* ----- TABLE ----- */
        .table-wrapper {
            overflow-x: auto;
            border-radius: 16px;
            border: 1px solid #eef4fa;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 15px;
        }

        th {
            background: #f8fafc;
            color: #10233f;
            font-weight: 700;
            padding: 14px 18px;
            text-align: left;
            border-bottom: 2px solid #e2eaf2;
            font-size: 13px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        td {
            padding: 14px 18px;
            border-bottom: 1px solid #eef4fa;
        }

        tr:last-child td {
            border-bottom: none;
        }

        tr:hover td {
            background: #f8fafc;
        }

        .status-badge {
            display: inline-block;
            padding: 4px 14px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 600;
        }

        .status-active {
            background: #d4edda;
            color: #155724;
        }

        .status-expired {
            background: #f8d7da;
            color: #721c24;
        }

        .days-active {
            color: #28a745;
            font-weight: 600;
        }

        .days-expired {
            color: #dc3545;
            font-weight: 600;
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
            color: #6b7a8f;
        }

        .empty-state i {
            font-size: 48px;
            color: #d9e2eb;
            margin-bottom: 16px;
        }

        /* ----- FOOTER ----- */
        .footer {
            margin-top: auto;
            padding: 24px 5vw 20px;
            background: #071a31;
            color: #b6c9dd;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            border-top: 1px solid #1f3550;
        }

        .footer strong {
            color: #dce7f5;
        }

        .footer .meta {
            display: flex;
            gap: 24px;
            align-items: center;
        }

        .footer .meta span {
            color: #7c92ab;
        }

        /* ----- RESPONSIVE ----- */
        @media (max-width: 900px) {
            .navbar {
                padding: 0 4vw;
                height: 68px;
            }

            .nav-links a:not(.nav-logout) {
                display: none;
            }

            .nav-links a.nav-logout {
                display: inline-block;
            }

            .container {
                padding: 25px 20px;
            }

            .page-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .action-buttons {
                width: 100%;
            }

            .action-buttons .btn {
                flex: 1;
                justify-content: center;
            }
        }

        @media (max-width: 600px) {
            .container {
                padding: 18px 14px;
                border-radius: 16px;
            }

            .page-header h2 {
                font-size: 22px;
            }

            .page-header .title-group .emoji-icon {
                font-size: 28px;
            }

            th,
            td {
                padding: 10px 12px;
                font-size: 13px;
            }

            .search-bar input[type="text"] {
                max-width: 100%;
            }

            .footer {
                flex-direction: column;
                gap: 10px;
                align-items: flex-start;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-buttons .btn {
                width: 100%;
                justify-content: center;
            }
        }

        @media (max-width: 400px) {
            .brand {
                font-size: 16px;
            }

            .brand-icon {
                width: 30px;
                height: 30px;
                font-size: 14px;
            }

            th,
            td {
                padding: 8px 10px;
                font-size: 12px;
            }
        }
    </style>
</head>

<body>

    <!-- ============================================================
         NAVIGATION (SOLID, NON-TRANSPARENT)
         ============================================================ -->
    <header class="navbar">
        <a href="admin.php" class="brand">
            <span class="brand-icon">CN</span>
            <span>CAOSCE <span class="brand-light">Network</span></span>
        </a>
        <div class="nav-links">
            <a href="admin.php" class="<?php echo $current_page == 'admin.php' ? 'active' : ''; ?>">
                <i class="fas fa-th-large"></i> Dashboard
            </a>
            <a href="classes.php" class="<?php echo $current_page == 'classes.php' ? 'active' : ''; ?>">
                <i class="fas fa-user-plus"></i> Class lists
            </a>
            <a href="result.php" class="<?php echo $current_page == 'result.php' ? 'active' : ''; ?>">
                <i class="fas fa-chart-pie"></i>OSCE Result
            </a>
            <a href="exam_manager.php" class="<?php echo $current_page == 'exam_manager.php' ? 'active' : ''; ?>">
                <i class="fas fa-sliders-h"></i> Exams settings
            </a>
            <a href="logout.php" class="nav-logout">
                <i class="fas fa-sign-out-alt"></i> Logout
            </a>
        </div>
    </header>

    <!-- ============================================================
         MAIN CONTENT
         ============================================================ -->
    <main>
        <div class="container">
            <!-- Header -->
            <div class="page-header">
                <div class="title-group">
                    <span class="emoji-icon">🎓</span>
                    <div>
                        <h2>Activated Students</h2>
                        <div class="subtitle">Manage and monitor student activations</div>
                    </div>
                </div>
                <div class="action-buttons">
                    <a href="activation_page.php" class="btn btn-primary">
                        <i class="fas fa-user-check"></i> Activate Student
                    </a>
                    <a href="admin.php" class="btn btn-outline">
                        <i class="fas fa-arrow-left"></i> Go Back
                    </a>
                </div>
            </div>

            <!-- Search -->
            <div class="search-bar">
                <input type="text" id="searchInput" onkeyup="filterTable()" placeholder="🔍 Search by User ID or Name...">
            </div>

            <!-- Table -->
            <div class="table-wrapper">
                <table id="studentsTable">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>User ID</th>
                            <th>Name</th>
                            <th>Days Left</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $serial_number = 1;
                        $current_date = new DateTime();
                        $has_rows = false;

                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                            $has_rows = true;
                            $user_id = htmlspecialchars($row['user_id']);
                            $name = htmlspecialchars($row['name']);
                            $expiry_date = new DateTime($row['expiry_date']);

                            // Calculate days remaining
                            $interval = $current_date->diff($expiry_date);
                            $days_left = (int)$interval->format('%r%a');

                            if ($days_left <= 0) {
                                $status = '<span class="status-badge status-expired">Expired</span>';
                                $days_display = '<span class="days-expired">0 days left</span>';
                            } else {
                                $status = '<span class="status-badge status-active">Active</span>';
                                $days_display = '<span class="days-active">' . $days_left . ' days left</span>';
                            }

                            echo "<tr>";
                            echo "<td>" . $serial_number++ . "</td>";
                            echo "<td><strong>$user_id</strong></td>";
                            echo "<td>$name</td>";
                            echo "<td>$days_display</td>";
                            echo "<td>$status</td>";
                            echo "</tr>";
                        }

                        if (!$has_rows) {
                            echo '<tr><td colspan="5"><div class="empty-state">
                                    <i class="fas fa-users-slash"></i>
                                    <p>No activated students found.</p>
                                    <p style="font-size:14px; margin-top:4px;">Activate a student to get started.</p>
                                </div></td></tr>';
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>

    <!-- ============================================================
         FOOTER
         ============================================================ -->
    <footer class="footer">
        <div>
            <strong><i class="fas fa-network-wired" style="margin-right:8px;"></i> CAOSCE Network</strong>
            <span style="margin-left:18px;">© <?php echo date('Y'); ?> All rights reserved</span>
        </div>
        <div class="meta">
            <span><i class="fas fa-code"></i> v2.0</span>
            <span>|</span>
            <span><i class="fas fa-user-cog"></i> Admin Panel</span>
            <span>|</span>
        </div>
    </footer>

    <!-- ============================================================
         SCRIPT
         ============================================================ -->
    <script>
        function filterTable() {
            const input = document.getElementById("searchInput").value.toUpperCase();
            const table = document.getElementById("studentsTable");
            const tr = table.getElementsByTagName("tr");

            for (let i = 1; i < tr.length; i++) {
                const cells = tr[i].getElementsByTagName("td");
                if (cells.length === 0) continue;

                // Search by User ID (column 1) or Name (column 2)
                const userId = cells[1]?.textContent || cells[1]?.innerText || '';
                const name = cells[2]?.textContent || cells[2]?.innerText || '';
                const match = userId.toUpperCase().indexOf(input) > -1 || name.toUpperCase().indexOf(input) > -1;
                tr[i].style.display = match ? "" : "none";
            }
        }
    </script>

</body>

</html>