<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

player_module_boot();

$pageTitle = 'Players';
$players = player_list();

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Player management</h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/players/bulk.php' . (isset($_GET['club_id']) ? '?club_id=' . (int) $_GET['club_id'] : ''))); ?>"><i class="bi bi-file-earmark-arrow-up"></i> Bulk register</a>
            <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/players/create.php' . (isset($_GET['club_id']) ? '?club_id=' . (int) $_GET['club_id'] : ''))); ?>"><i class="bi bi-person-plus-fill"></i> Add player</a>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Player</th>
                    <th>Club</th>
                    <th>Position</th>
                    <th>Player ID</th>
                    <th>NIN</th>
                    <th>Eligibility</th>
                    <th>Documents</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($players === []): ?>
                    <tr>
                        <td colspan="8" class="text-center text-muted py-4">No player records found.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($players as $player): ?>
                    <tr>
                        <td>
                            <strong><?php echo e(player_full_name($player)); ?></strong>
                            <div class="small text-muted">Jersey <?php echo e((string) ($player['jersey_number'] ?? '-')); ?> • <?php echo e(ucfirst($player['status'])); ?></div>
                        </td>
                        <td><?php echo e($player['club_name']); ?></td>
                        <td><?php echo e($player['position']); ?></td>
                        <td><?php echo e($player['registration_number']); ?></td>
                        <td><?php echo e($player['nin'] ?: '-'); ?></td>
                        <td><span class="badge <?php echo e(player_badge_class($player['eligibility_status'])); ?>"><?php echo e(ucfirst($player['eligibility_status'])); ?></span></td>
                        <td><?php echo e((string) $player['documents_count']); ?></td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/players/view.php?id=' . (int) $player['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/players/edit.php?id=' . (int) $player['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/players/delete.php?id=' . (int) $player['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>