<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

competition_module_boot();

$pageTitle = 'Competitions';
$competitions = competition_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>
            <p class="eyebrow mb-1">Competition calendar</p>
            <h2 class="h4 mb-0">Competition management</h2>
        </div>
        <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/competitions/create.php')); ?>"><i class="bi bi-trophy"></i> Add competition</a>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Competition</th>
                    <th>Season</th>
                    <th>Order</th>
                    <th>Type</th>
                    <th>Teams</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($competitions === []): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">No competitions created yet.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($competitions as $competition): ?>
                    <tr>
                        <td><strong><?php echo e($competition['name']); ?></strong></td>
                        <td><?php echo e($competition['season_name']); ?></td>
                        <td><?php echo e((string) $competition['sequence_order']); ?></td>
                        <td><?php echo e(ucwords(str_replace('_', ' ', $competition['type']))); ?></td>
                        <td><?php echo e((string) $competition['number_of_teams']); ?></td>
                        <td><span class="badge <?php echo e(competition_status_badge_class($competition['status'])); ?>"><?php echo e(ucfirst($competition['status'])); ?></span></td>
                        <td class="text-end">
                            <div class="d-inline-flex flex-wrap justify-content-end gap-1">
                                <?php if ($competition['status'] === 'active'): ?>
                                    <form method="post" action="<?php echo e(base_url('app/modules/competitions/actions.php')); ?>" class="d-inline">
                                        <?php echo csrf_input(); ?>
                                        <input type="hidden" name="action" value="set_status">
                                        <input type="hidden" name="id" value="<?php echo e((string) $competition['id']); ?>">
                                        <input type="hidden" name="status" value="paused">
                                        <button class="btn btn-outline-warning btn-sm" type="submit">Pause</button>
                                    </form>
                                <?php else: ?>
                                    <form method="post" action="<?php echo e(base_url('app/modules/competitions/actions.php')); ?>" class="d-inline">
                                        <?php echo csrf_input(); ?>
                                        <input type="hidden" name="action" value="set_status">
                                        <input type="hidden" name="id" value="<?php echo e((string) $competition['id']); ?>">
                                        <input type="hidden" name="status" value="active">
                                        <button class="btn btn-outline-jlfc btn-sm" type="submit"><?php echo $competition['status'] === 'paused' ? 'Resume' : 'Activate'; ?></button>
                                    </form>
                                <?php endif; ?>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/competitions/view.php?id=' . (int) $competition['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/competitions/edit.php?id=' . (int) $competition['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/competitions/delete.php?id=' . (int) $competition['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>