<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

club_module_boot();

$pageTitle = 'Clubs';
$clubs = club_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">Club management</h2>
        </div>
        <?php if (can_create_club()): ?>
            <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/clubs/create.php')); ?>"><i class="bi bi-shield-plus"></i> Add club</a>
        <?php endif; ?>
    </div>

    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Club</th>
                    <th>License</th>
                    <th>Payment</th>
                    <th>Status</th>
                    <th>Squad</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($clubs === []): ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">No clubs found.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($clubs as $club): ?>
                    <tr>
                        <td>
                            <strong><?php echo e($club['name']); ?></strong>
                            <div class="small text-muted"><?php echo e($club['short_name']); ?></div>
                        </td>
                        <td><span class="badge <?php echo e(club_badge_class($club['license_status'])); ?>"><?php echo e(ucfirst($club['license_status'])); ?></span></td>
                        <td><span class="badge <?php echo e(club_badge_class($club['payment_status'])); ?>"><?php echo e(ucfirst($club['payment_status'])); ?></span></td>
                        <td><span class="badge <?php echo e(club_badge_class($club['status'])); ?>"><?php echo e(ucfirst($club['status'])); ?></span></td>
                        <td><?php echo e((string) $club['staff_count']); ?> staff / <?php echo e((string) $club['player_count']); ?> players</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/clubs/view.php?id=' . (int) $club['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/clubs/edit.php?id=' . (int) $club['id'])); ?>">Edit</a>
                                <?php if (!is_club_admin()): ?>
                                    <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/clubs/delete.php?id=' . (int) $club['id'])); ?>">Delete</a>
                                <?php endif; ?>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>