<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/session_bootstrap.php';
if (empty($_SESSION['logged_in'])) {
    header('Location: /login/');
    exit();
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Moje Ligi</title>
    <link rel="stylesheet" href="/css/header.css" type="text/css" media="all"/>
    <link rel="stylesheet" href="/css/footer.css" type="text/css" media="all"/>
    <link href="//fonts.googleapis.com/css?family=Lato:400,500,600,700,800,900" rel="stylesheet">
    <style>
        nav.navigation { margin-top: 0 !important; }
        body { background: linear-gradient(135deg, #e3f2fd 0%, #ffffff 100%); min-height: 100vh; }
        .wrap { max-width: 1200px; margin: 0 auto; padding: 24px; }
        .card { background: #fff; border-radius: 12px; box-shadow: 0 8px 24px rgba(100,181,246,.2); padding: 20px; }
        h1 { color: #1976d2; margin: 0 0 14px; }
        .status { color: #555; margin-bottom: 12px; font-size: 14px; }
        .status.error { color: #b32d2e; font-weight: 600; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 10px 8px; border-bottom: 1px solid #eaeaea; text-align: left; font-size: 14px; }
        th { background: #f8fbff; color: #1976d2; }
        .empty { padding: 16px; text-align: center; color: #666; }
        .top-nav { margin: 12px 0 18px; }
        .top-nav a { color: #1976d2; text-decoration: none; font-weight: 600; }
    </style>
</head>
<body>
<?php include $_SERVER['DOCUMENT_ROOT'].'/global/navLogined.php'; ?>
<main class="wrap">
    <div class="top-nav"><a href="/account/profile/">← Wróć do konta</a></div>
    <section class="card">
        <h1>🏆 Moje Ligi</h1>
        <div id="status" class="status">Ładowanie danych...</div>
        <div style="overflow-x:auto;">
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nazwa ligi</th>
                        <th>Status</th>
                        <th>Data</th>
                    </tr>
                </thead>
                <tbody id="rows"></tbody>
            </table>
        </div>
    </section>
</main>
<?php include $_SERVER['DOCUMENT_ROOT'].'/global/footerLogined.php'; ?>
<script>
(function () {
    const statusEl = document.getElementById('status');
    const rowsEl = document.getElementById('rows');

    function escapeHtml(value) {
        return String(value ?? '')
            .replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/\"/g, '&quot;')
            .replace(/'/g, '&#039;');
    }

    fetch('/userApi/my-leagues.php', { credentials: 'same-origin' })
        .then(r => r.json())
        .then(json => {
            if (!json || json.success === false) {
                throw new Error(json && json.error ? json.error : 'Błąd pobierania danych');
            }
            const data = Array.isArray(json.data) ? json.data : [];
            if (data.length === 0) {
                statusEl.textContent = 'Brak lig do wyświetlenia';
                rowsEl.innerHTML = '<tr><td colspan="4" class="empty">Brak danych</td></tr>';
                return;
            }
            statusEl.textContent = 'Znaleziono: ' + data.length;
            rowsEl.innerHTML = data.map(row => '<tr>' +
                '<td>' + escapeHtml(row.id) + '</td>' +
                '<td>' + escapeHtml(row.name || row.league_name || '-') + '</td>' +
                '<td>' + escapeHtml(row.status || '-') + '</td>' +
                '<td>' + escapeHtml(row.created_at || row.start_date || '-') + '</td>' +
            '</tr>').join('');
        })
        .catch(err => {
            statusEl.textContent = 'Nie udało się pobrać lig: ' + (err && err.message ? err.message : 'nieznany błąd');
            statusEl.classList.add('error');
            rowsEl.innerHTML = '<tr><td colspan="4" class="empty">Błąd ładowania</td></tr>';
        });
})();
</script>
</body>
</html>
