<?php

declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';

$players = [];

try {
    $players = db_fetch_all(
        'SELECT p.first_name, p.last_name, p.position, p.jersey_number, p.registration_number, c.name AS club_name
         FROM players p
         INNER JOIN clubs c ON c.id = p.club_id
         WHERE p.status = :status
         ORDER BY c.name ASC, p.last_name ASC, p.first_name ASC
         LIMIT 100',
        ['status' => 'active']
    );
} catch (Throwable) {
}

render_public_page('Players', 'Registered player directory across JLFC clubs.', static function () use ($players): void {
?>
    <section class="panel-card">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Squads</p>
                <h2 class="h4 mb-0">Player directory</h2>
            </div>
        </div>
        <?php if ($players === []): ?>
            <p class="text-muted mb-0">No active players are available yet.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Player</th>
                            <th>Club</th>
                            <th>Position</th>
                            <th>No.</th>
                            <th>Player ID</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($players as $player): ?>
                            <tr>
                                <td><?php echo e($player['first_name'] . ' ' . $player['last_name']); ?></td>
                                <td><?php echo e($player['club_name']); ?></td>
                                <td><?php echo e($player['position']); ?></td>
                                <td><?php echo e((string) ($player['jersey_number'] ?? '-')); ?></td>
                                <td><?php echo e($player['registration_number']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </section>
<?php
});
