300) {
        foreach ($_SESSION["characters"] as $character_id => &$charData) {
            if (isset($charData["refresh_token"])) {
                // Check if the access token is expired or missing
                if (
                    !isset($charData["access_token"]) ||
                    is_token_expired($charData["access_token"])
                ) {
                    $new_tokens = refresh_token($charData["refresh_token"]);
                    if (!empty($new_tokens["access_token"])) {
                        // Update session with new access token
                        $charData["access_token"] = $new_tokens["access_token"];
                    } else {
                        error_log(
                            "Failed to refresh token for character ID $character_id"
                        );
                    }
                }
            }
        }
        unset($charData); // Break reference to avoid unexpected behavior
        $_SESSION["last_token_refresh"] = $page_load_time;
    }
    // Get list of character IDs
    $character_ids = array_keys($_SESSION["characters"]);
}
if (!isset($_SESSION["characters"]) || empty($_SESSION["characters"])) {
    echo "No characters logged in. Go back";
    exit();
}
// For AJAX requests, only fetch requested character data and return JSON
if ($is_ajax) {
    $requested_char_id = isset($_GET["character_id"])
        ? $_GET["character_id"]
        : null;
    if (
        $requested_char_id &&
        isset($_SESSION["characters"][$requested_char_id])
    ) {
        $charData = $_SESSION["characters"][$requested_char_id];
        $access_token = $charData["access_token"] ?? null;
        if ($access_token) {
            $jobs = fetch_character_jobs($requested_char_id, $access_token);
            // Format jobs for JSON response
            $formatted_jobs = [];
            foreach ($jobs as $job) {
                $end_timestamp = is_numeric($job["end_time_unix"])
                    ? $job["end_time_unix"]
                    : 0;
                $formatted_jobs[] = [
                    "character" => htmlspecialchars($job["character"]),
                    "blueprint" => htmlspecialchars($job["blueprint"]),
                    "activity" => htmlspecialchars($job["activity"]),
                    "location" => htmlspecialchars($job["location"]),
                    "start_time" => htmlspecialchars($job["start_time"]),
                    "end_time" => htmlspecialchars($job["end_time"]),
                    "end_time_unix" => $end_timestamp,
                    "time_left" => htmlspecialchars($job["time_left"]),
                ];
            }
            header("Content-Type: application/json");
            echo json_encode(["jobs" => $formatted_jobs]);
            exit();
        }
    }
    // If we get here, something went wrong
    header("HTTP/1.1 400 Bad Request");
    echo json_encode(["error" => "Invalid request"]);
    exit();
}
?>
    
    Industry Jobs Dashboard
    
    Industry Jobs
    
        
            
                | Character | 
                Blueprint | 
                Activity | 
                Location | 
                Start Time | 
                End Time | 
                Time Left | 
            
        
        
            | No jobs found for " . htmlspecialchars($charData["name"]) . " | 
";
                    } else {
                        foreach ($jobs as $job) {
                            $end_timestamp = is_numeric($job["end_time_unix"])
                                ? $job["end_time_unix"]
                                : 0;
                            $time_left_display = htmlspecialchars(
                                $job["time_left"]
                            );
                            echo "";
                            echo "| " .
                                htmlspecialchars($job["character"]) .
                                " | ";
                            echo "" .
                                htmlspecialchars($job["blueprint"]) .
                                " | ";
                            echo "" .
                                htmlspecialchars($job["activity"]) .
                                " | ";
                            echo "" .
                                htmlspecialchars($job["location"]) .
                                " | ";
                            echo "" .
                                htmlspecialchars($job["start_time"]) .
                                " | ";
                            echo "" .
                                htmlspecialchars($job["end_time"]) .
                                " | ";
                            echo "" .
                                $time_left_display .
                                " | ";
                            echo "
";
                        }
                    }
                }
                // Add loading placeholder rows for other characters
                if (count($character_ids) > 1) {
                    foreach (array_slice($character_ids, 1) as $char_id) {
                        $char_name =
                            $_SESSION["characters"][$char_id]["name"] ??
                            "Character";
                        echo "";
                        echo "| Loading jobs for " .
                            htmlspecialchars($char_name) .
                            "  | ";
                        echo "
";
                    }
                }
            } ?>
        
    
    
    
        Logged-in Characters
        
            
                
                    | Name | 
                    Character ID | 
                
            
            
                 $charData
                ): ?>
                    
                         | 
                         |