<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

season_module_boot();

$pageTitle = 'Seasons';
$seasons = season_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">Season management</h2>
        </div>
        <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/seasons/create.php')); ?>"><i class="bi bi-calendar-plus"></i> Add season</a>
    </div>

    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Season</th>
                    <th>Dates</th>
                    <th>Status</th>
                    <th>Competitions</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($seasons === []): ?>
                    <tr>
                        <td colspan="5" class="text-center text-muted py-4">No seasons created yet.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($seasons as $season): ?>
                    <tr>
                        <td><strong><?php echo e($season['name']); ?></strong></td>
                        <td><?php echo e($season['start_date']); ?> to <?php echo e($season['end_date']); ?></td>
                        <td><span class="badge <?php echo e(season_status_badge_class($season['status'])); ?>"><?php echo e(ucfirst($season['status'])); ?></span></td>
                        <td><?php echo e((string) $season['competition_count']); ?></td>
                        <td class="text-end">
                            <div class="d-inline-flex flex-wrap justify-content-end gap-1">
                                <a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/seasons/view.php?id=' . (int) $season['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/seasons/edit.php?id=' . (int) $season['id'])); ?>">Edit</a>
                                <?php if ($season['status'] === 'active'): ?>
                                    <form method="post" action="<?php echo e(base_url('app/modules/seasons/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) $season['id']); ?>">
                                        <input type="hidden" name="status" value="paused">
                                        <button class="btn btn-outline-warning btn-sm" type="submit">Pause season</button>
                                    </form>
                                <?php elseif ($season['status'] === 'paused'): ?>
                                    <form method="post" action="<?php echo e(base_url('app/modules/seasons/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) $season['id']); ?>">
                                        <input type="hidden" name="status" value="active">
                                        <button class="btn btn-outline-jlfc btn-sm" type="submit">Resume season</button>
                                    </form>
                                <?php else: ?>
                                    <form method="post" action="<?php echo e(base_url('app/modules/seasons/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) $season['id']); ?>">
                                        <input type="hidden" name="status" value="active">
                                        <button class="btn btn-outline-jlfc btn-sm" type="submit">Set active</button>
                                    </form>
                                <?php endif; ?>
                                <a class="btn btn-outline-danger btn-sm" href="<?php echo e(base_url('app/modules/seasons/delete.php?id=' . (int) $season['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>