<?php
set_time_limit(36000); // 10 hours in seconds
session_start();



// Connect to SQLite database
$db = new PDO('sqlite:database.db');

// ✅ SET created_at for existing rows (current time)
$db->exec("UPDATE students SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL");

// Before using the table anywhere
$db->exec('CREATE TABLE IF NOT EXISTS activation_slot (number INTEGER)');

// Fetch current slots
$slot_count_stmt = $db->query('SELECT COALESCE(SUM(number),0) as total FROM activation_slot');
$slot_count = $slot_count_stmt->fetchColumn();

// Handle Add Slot
if (isset($_POST['add_slot_code'])) {
    $code = trim($_POST['add_slot_code']);
    if (!empty($code)) {
        // VERIFY externally using cURL
        $verifyUrl = 'https://cbt.net.ng/verify_pin.php';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $verifyUrl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['pin' => $code]));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $verify = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($verify, true);

        if ($result && $result['status'] === 'success') {
            // Extract number at the beginning of the code
            if (preg_match('/^(\d+)/', $code, $matches)) {
                $number = (int)$matches[1];

                if ($number > 0) {
                    // Get the current slots if exists
                    $current_slots = $db->query('SELECT number FROM activation_slot LIMIT 1')->fetchColumn();

                    if ($current_slots === false) {
                        // No slots exist, insert new record
                        $stmt = $db->prepare('INSERT INTO activation_slot (number) VALUES (:number)');
                        $stmt->bindParam(':number', $number, PDO::PARAM_INT);
                        $stmt->execute();
                        header('Location: success.php');
                        exit;
                    } else {
                        // Slots exist, update by adding
                        $new_number = $current_slots + $number;
                        $stmt = $db->prepare('UPDATE activation_slot SET number = :number');
                        $stmt->bindParam(':number', $new_number, PDO::PARAM_INT);
                        $stmt->execute();
                        header('Location: success.php');
                        exit;
                    }
                } else {
                    echo "<script>alert('No valid number found at the beginning of the code.');</script>";
                }
            } else {
                echo "<script>alert('Code does not begin with a number.');</script>";
            }
        } else {
            echo "<script>alert('Invalid pin or verification failed.');</script>";
        }
    } else {
        echo "<script>alert('Please enter a valid code.');</script>";
    }
}

// Initialize variables
$searched_user = null;
$search_performed = false;
$search_query = '';

// Check if a search request has been made
if (isset($_GET['search']) && !empty(trim($_GET['search']))) {
    $search_performed = true;
    $search_query = trim($_GET['search']);

    // Search for user by user_id or name (exact match first, then like)
    $stmt = $db->prepare("SELECT * FROM students WHERE user_id = :search_term OR name LIKE :search_like LIMIT 1");
    $search_like = '%' . $search_query . '%';
    $stmt->bindParam(':search_term', $search_query, PDO::PARAM_STR);
    $stmt->bindParam(':search_like', $search_like, PDO::PARAM_STR);
    $stmt->execute();
    $searched_user = $stmt->fetch(PDO::FETCH_ASSOC);
}

// Handle delete request
if (isset($_POST['delete_user_id'])) {
    $delete_user_id = $_POST['delete_user_id'];
    if ($delete_user_id !== "0") {
        $delete_stmt = $db->prepare('DELETE FROM students WHERE user_id = :user_id');
        $delete_stmt->bindParam(':user_id', $delete_user_id, PDO::PARAM_STR);
        $delete_stmt->execute();

        // Refresh the current user data if we're viewing the deleted user
        if ($searched_user && $searched_user['user_id'] == $delete_user_id) {
            $searched_user = null;
        }
    }
}

// Handle activation with PIN
if (isset($_POST['activate_user_id']) && isset($_POST['activation_pin'])) {
    $activate_user_id = $_POST['activate_user_id'];
    $activation_pin = $_POST['activation_pin'];

    if (strlen($activation_pin) > 100) {
        echo "<script>alert('Activation pin cannot exceed 100 characters.');</script>";
    } else {
        $verification_url = 'https://cbt.net.ng/verify_pin.php';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $verification_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['pin' => $activation_pin]));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            echo "<script>alert('Error connecting to verification server.');</script>";
            curl_close($ch);
            exit;
        }

        curl_close($ch);
        $response_data = json_decode($response, true);

        if ($response_data && $response_data['status'] === 'success') {
            $check_stmt = $db->prepare('SELECT COUNT(*) FROM activated_students WHERE user_id = :user_id');
            $check_stmt->bindParam(':user_id', $activate_user_id, PDO::PARAM_STR);
            $check_stmt->execute();

            if ($check_stmt->fetchColumn() == 0) {
                $insert_stmt = $db->prepare('INSERT INTO activated_students (user_id) VALUES (:user_id)');
                $insert_stmt->bindParam(':user_id', $activate_user_id, PDO::PARAM_STR);
                $insert_stmt->execute();
                echo "<script>alert('Student activated successfully!');</script>";

                // Refresh the current user data
                if ($searched_user && $searched_user['user_id'] == $activate_user_id) {
                    $refresh_stmt = $db->prepare("SELECT * FROM students WHERE user_id = :user_id");
                    $refresh_stmt->bindParam(':user_id', $activate_user_id, PDO::PARAM_STR);
                    $refresh_stmt->execute();
                    $searched_user = $refresh_stmt->fetch(PDO::FETCH_ASSOC);
                }
            } else {
                echo "<script>alert('User already activated.');</script>";
            }
        } else {
            echo "<script>alert('Invalid activation pin!');</script>";
        }
    }
}

// Handle manual activation with slot
if (isset($_POST['manual_activate_user_id'])) {
    $user_id = $_POST['manual_activate_user_id'];

    if ($slot_count <= 0) {
        echo "<script>alert('No activation slot is left. Please buy more slots.');</script>";
    } else {
        $check_stmt = $db->prepare('SELECT COUNT(*) FROM activated_students WHERE user_id = :user_id');
        $check_stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
        $check_stmt->execute();

        if ($check_stmt->fetchColumn() > 0) {
            echo "<script>alert('This user is already activated. No slot was used.');</script>";
        } else {
            // Activate the user
            $stmt = $db->prepare('INSERT INTO activated_students (user_id) VALUES (:user_id)');
            $stmt->bindParam(':user_id', $user_id);
            $stmt->execute();

            // Fetch one slot
            $row = $db->query('SELECT rowid, number FROM activation_slot WHERE number > 0 LIMIT 1')->fetch(PDO::FETCH_ASSOC);
            if ($row) {
                $new_number = $row['number'] - 1;
                if ($new_number > 0) {
                    $db->exec('UPDATE activation_slot SET number = ' . (int)$new_number . ' WHERE rowid = ' . (int)$row['rowid']);
                } else {
                    $db->exec('DELETE FROM activation_slot WHERE rowid = ' . (int)$row['rowid']);
                }
            }

            // Refresh slot count
            $slot_count_stmt = $db->query('SELECT COALESCE(SUM(number),0) as total FROM activation_slot');
            $slot_count = $slot_count_stmt->fetchColumn();

            echo "<script>alert('Student activated successfully using a slot!');</script>";

            // Refresh the current user data
            if ($searched_user && $searched_user['user_id'] == $user_id) {
                $refresh_stmt = $db->prepare("SELECT * FROM students WHERE user_id = :user_id");
                $refresh_stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
                $refresh_stmt->execute();
                $searched_user = $refresh_stmt->fetch(PDO::FETCH_ASSOC);
            }
        }
    }
}

// Check activation status helper function
function isActivated($db, $user_id)
{
    $stmt = $db->prepare('SELECT COUNT(*) FROM activated_students WHERE user_id = :user_id');
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
    $stmt->execute();
    return $stmt->fetchColumn() > 0;
}

// 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 ----- */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            margin: 0;
            font-family: 'Poppins', 'Inter', sans-serif;
            background: #f0f4fb;
            min-height: 100vh;
            color: #10233f;
            display: flex;
            flex-direction: column;
        }

        /* ----- TOP 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;
        }

        /* ACTIVE link with solid underline */
        .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;
        }

        /* ----- CONTAINER (CARD) ----- */
        .container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 24px;
            width: 90%;
            max-width: 800px;
            margin: 40px auto;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.08);
            flex: 1;
        }

        h1 {
            text-align: center;
            color: #10233f;
            font-size: 28px;
            margin-bottom: 30px;
            font-weight: 600;
        }

        .go-back {
            display: inline-block;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            transition: transform 0.2s, box-shadow 0.2s;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }

        .go-back:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .search-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 16px;
            margin-bottom: 30px;
        }

        .search-form {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .search-input {
            flex: 1;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        .search-input:focus {
            outline: none;
            border-color: #667eea;
        }

        .search-btn {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 12px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: transform 0.2s;
        }

        .search-btn:hover {
            transform: translateY(-2px);
        }

        .clear-btn {
            background: #6c757d;
        }

        .clear-btn:hover {
            background: #5a6268;
        }

        .user-card {
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }

        .user-header {
            background: linear-gradient(135deg, #667eea, #764ba2);
            padding: 20px;
            color: white;
            text-align: center;
        }

        .user-header h2 {
            margin: 0;
            font-size: 24px;
        }

        .user-body {
            padding: 30px;
        }

        .user-detail {
            margin-bottom: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 12px;
        }

        .detail-label {
            font-weight: 600;
            color: #667eea;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 8px;
        }

        .detail-value {
            font-size: 18px;
            color: #333;
            word-break: break-word;
        }

        .user-picture {
            text-align: center;
            margin: 20px 0;
        }

        .user-picture img {
            border-radius: 16px;
            max-width: 200px;
            height: auto;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .status-badge {
            display: inline-block;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 14px;
            font-weight: 500;
        }

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

        .status-not-activated {
            background: #fff3cd;
            color: #856404;
        }

        .action-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 20px;
            flex-wrap: wrap;
        }

        .delete-btn,
        .activate-btn {
            padding: 12px 24px;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: transform 0.2s;
        }

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

        .delete-btn:hover {
            background: #c82333;
            transform: translateY(-2px);
        }

        .activate-btn {
            background: linear-gradient(135deg, #28a745, #20c997);
            color: white;
        }

        .activate-btn:hover {
            transform: translateY(-2px);
        }

        .no-result {
            text-align: center;
            padding: 40px;
            background: #f8f9fa;
            border-radius: 16px;
            color: #6c757d;
        }

        .slot-info {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            gap: 15px;
            margin-bottom: 20px;
            padding: 10px 15px;
            background: #e9ecef;
            border-radius: 12px;
        }

        .slot-count {
            font-weight: bold;
            color: #28a745;
            font-size: 18px;
        }

        .add-slot-btn {
            background: #17a2b8;
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background: white;
            padding: 30px;
            border-radius: 20px;
            max-width: 400px;
            width: 90%;
            text-align: center;
        }

        .modal-content input {
            width: 100%;
            padding: 12px;
            margin: 15px 0;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
        }

        .modal-buttons {
            display: flex;
            gap: 10px;
            justify-content: center;
        }

        .modal-btn {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
        }

        .close {
            float: right;
            font-size: 24px;
            cursor: pointer;
            color: #999;
        }

        /* ----- FOOTER (solid) ----- */
        .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: 20px;
            }
        }

        @media (max-width: 600px) {
            .container {
                padding: 15px;
                margin: 20px auto;
            }

            .search-form {
                flex-direction: column;
            }

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

            .slot-info {
                flex-wrap: wrap;
                justify-content: center;
            }

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

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

            .brand-icon {
                width: 30px;
                height: 30px;
                font-size: 14px;
            }
        }
    </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
         ============================================================ -->
    <div class="container">
        <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px;">
            <a href="admin.php" class="go-back">← Go Back</a>
            <div class="slot-info">
                <span>📦 Available Slots:</span>
                <span class="slot-count"><?= $slot_count ?></span>
                <button class="add-slot-btn" onclick="document.getElementById('addSlotModal').style.display='flex'">+ Add Slot</button>
            </div>
        </div>

        <h1>🔍 Student details in the exam page only</h1>

        <div class="search-section">
            <form class="search-form" method="GET" action="">
                <input type="text" name="search" class="search-input"
                    placeholder="Enter an Exam Number or Name to Search..."
                    value="<?= htmlspecialchars($search_query) ?>" required>
                <button type="submit" class="search-btn">Search Student</button>
                <?php if ($search_performed): ?>
                    <a href="<?= $_SERVER['PHP_SELF'] ?>" class="search-btn clear-btn">Clear</a>
                <?php endif; ?>
            </form>
        </div>

        <?php if (!$search_performed): ?>
            <div class="no-result">
                <p>👋 Please enter an Exam number or Name above to search for a student.</p>
            </div>
        <?php elseif ($searched_user === false || $searched_user === null): ?>
            <div class="no-result">
                <p>❌ No student found with the search term: <strong><?= htmlspecialchars($search_query) ?></strong></p>
                <p>Please try a different Matric number or Name.</p>
            </div>
        <?php else: ?>
            <div class="user-card">
                <div class="user-header">
                    <h2>Student Information</h2>
                </div>
                <div class="user-body">
                    <?php if ($searched_user['picture']): ?>
                        <div class="user-picture">
                            <img src="data:image/jpeg;base64,<?= base64_encode($searched_user['picture']) ?>" alt="Student Picture">
                        </div>
                    <?php endif; ?>

                    <div class="user-detail">
                        <div class="detail-label">Matric Number</div>
                        <div class="detail-value"><?= htmlspecialchars($searched_user['user_id']) ?></div>
                    </div>

                    <div class="user-detail">
                        <div class="detail-label">Full Name</div>
                        <div class="detail-value"><?= htmlspecialchars($searched_user['name']) ?></div>
                    </div>

                    <div class="user-detail">
                        <div class="detail-label">Gender</div>
                        <div class="detail-value"><?= htmlspecialchars($searched_user['sex'] ?? 'Not specified') ?></div>
                    </div>

                    <div class="user-detail">
                        <div class="detail-label">Activation Status</div>
                        <div class="detail-value">
                            <?php if (isActivated($db, $searched_user['user_id'])): ?>
                                <span class="status-badge status-activated">✅ Activated</span>
                            <?php else: ?>
                                <span class="status-badge status-not-activated">⏳ Not Activated</span>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="action-buttons">
                        <?php if ($searched_user['user_id'] !== "0"): ?>
                            <form method="POST" onsubmit="return confirm('Are you sure you want to delete this user?');" style="display: inline;">
                                <input type="hidden" name="delete_user_id" value="<?= htmlspecialchars($searched_user['user_id']) ?>">
                                <button type="submit" class="delete-btn">🗑️ Delete User</button>
                            </form>
                        <?php endif; ?>

                        <?php if (!isActivated($db, $searched_user['user_id'])): ?>
                            <button class="activate-btn" onclick="openActivationModal('<?= htmlspecialchars($searched_user['user_id']) ?>')">
                                🔓 Activate Student
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <!-- Activation Method Modal -->
    <div id="activationMethodModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('activationMethodModal')">&times;</span>
            <h3>Activate Student</h3>
            <p>How would you like to activate this student?</p>
            <div class="modal-buttons">
                <button class="modal-btn" style="background: #28a745; color: white;" onclick="showPinModal()">🔐 Use PIN</button>
                <button class="modal-btn" style="background: #17a2b8; color: white;" onclick="useSlot()">🎟️ Use SLOT</button>
            </div>
        </div>
    </div>

    <!-- PIN Activation Modal -->
    <div id="pinModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('pinModal')">&times;</span>
            <h3>Enter Activation PIN</h3>
            <form method="POST" action="">
                <input type="hidden" name="activate_user_id" id="pin_activate_user_id">
                <input type="text" name="activation_pin" placeholder="Enter activation PIN" required>
                <button type="submit" class="search-btn" style="width: 100%;">Activate</button>
            </form>
        </div>
    </div>

    <!-- Add Slot Modal -->
    <div id="addSlotModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('addSlotModal')">&times;</span>
            <h3>Add Activation Slot</h3>
            <form method="POST" action="">
                <input type="text" name="add_slot_code" placeholder="Enter slot code" required>
                <button type="submit" class="search-btn" style="width: 100%;">Add Slot</button>
            </form>
        </div>
    </div>

    <!-- ============================================================
         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>
        let currentUserId = null;

        function openActivationModal(userId) {
            currentUserId = userId;
            document.getElementById('activationMethodModal').style.display = 'flex';
        }

        function closeModal(modalId) {
            document.getElementById(modalId).style.display = 'none';
        }

        function showPinModal() {
            closeModal('activationMethodModal');
            document.getElementById('pin_activate_user_id').value = currentUserId;
            document.getElementById('pinModal').style.display = 'flex';
        }

        function useSlot() {
            closeModal('activationMethodModal');
            var form = document.createElement('form');
            form.method = 'POST';
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = 'manual_activate_user_id';
            input.value = currentUserId;
            form.appendChild(input);
            document.body.appendChild(form);
            form.submit();
        }

        window.onclick = function(event) {
            if (event.target.classList.contains('modal')) {
                event.target.style.display = 'none';
            }
        }
    </script>

</body>

</html>