<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

discipline_module_boot();

$pageTitle = 'Discipline';
$competitionOptions = discipline_competition_options();
$selectedCompetitionId = (int) ($_GET['competition_id'] ?? 0);
$suspensions = suspension_list($selectedCompetitionId > 0 ? $selectedCompetitionId : null);
$cases = disciplinary_case_list($selectedCompetitionId > 0 ? $selectedCompetitionId : null);

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 mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Discipline and suspensions</h2>
        </div>
    </div>
    <form class="row g-3" method="get">
        <div class="col-md-8 col-lg-6">
            <label class="form-label" for="competition_id">Competition</label>
            <select class="form-select" id="competition_id" name="competition_id">
                <option value="">All competitions</option>
                <?php foreach ($competitionOptions as $competition): ?>
                    <option value="<?php echo e((string) $competition['id']); ?>" <?php echo $selectedCompetitionId === (int) $competition['id'] ? 'selected' : ''; ?>><?php echo e($competition['name']); ?> (<?php echo e($competition['season_name']); ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4 col-lg-3 d-flex align-items-end gap-2">
            <button class="btn btn-jlfc" type="submit">Apply</button>
            <?php if ($selectedCompetitionId > 0): ?><a class="btn btn-link" href="<?php echo e(base_url('app/modules/discipline/index.php')); ?>">Reset</a><?php endif; ?>
        </div>
    </form>
</section>

<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Suspensions</p>
            <h3 class="h5 mb-0">Active and historical suspensions</h3>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Player</th>
                    <th>Club</th>
                    <th>Competition</th>
                    <th>Reason</th>
                    <th>Matches</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($suspensions === []): ?><tr>
                        <td colspan="7" class="text-center text-muted py-4">No suspensions found.</td>
                    </tr><?php endif; ?>
                <?php foreach ($suspensions as $suspension): ?>
                    <tr>
                        <td><strong><?php echo e(trim($suspension['first_name'] . ' ' . $suspension['last_name'])); ?></strong></td>
                        <td><?php echo e($suspension['club_name']); ?></td>
                        <td><?php echo e($suspension['competition_name'] ?: 'N/A'); ?></td>
                        <td><?php echo e($suspension['reason']); ?></td>
                        <td><?php echo e((string) $suspension['matches_served']); ?> / <?php echo e((string) $suspension['number_of_matches']); ?></td>
                        <td><span class="badge <?php echo e(suspension_status_badge_class($suspension['status'])); ?>"><?php echo e(ucfirst($suspension['status'])); ?></span></td>
                        <td class="text-end"><a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/discipline/suspension.php?id=' . (int) $suspension['id'])); ?>">Review</a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Cases</p>
            <h3 class="h5 mb-0">Disciplinary case register</h3>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Case</th>
                    <th>Player</th>
                    <th>Club</th>
                    <th>Competition</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($cases === []): ?><tr>
                        <td colspan="6" class="text-center text-muted py-4">No disciplinary cases found.</td>
                    </tr><?php endif; ?>
                <?php foreach ($cases as $case): ?>
                    <tr>
                        <td><strong><?php echo e($case['case_type']); ?></strong>
                            <div class="small text-muted"><?php echo e($case['description']); ?></div>
                        </td>
                        <td><?php echo e(trim(((string) ($case['first_name'] ?? '')) . ' ' . ((string) ($case['last_name'] ?? '')))); ?></td>
                        <td><?php echo e($case['club_name'] ?: 'N/A'); ?></td>
                        <td><?php echo e($case['competition_name'] ?: 'N/A'); ?></td>
                        <td><span class="badge <?php echo e(disciplinary_case_status_badge_class($case['status'])); ?>"><?php echo e(ucfirst($case['status'])); ?></span></td>
                        <td class="text-end"><a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/discipline/case.php?id=' . (int) $case['id'])); ?>">Review</a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>