<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

fixture_preparation_boot();

$clubId = user_club_id();
$isReadOnly = !can_edit_fixture_preparation();
$canConfirmKits = can_confirm_fixture_kits();
$fixtureId = (int) ($_GET['id'] ?? 0);
$pageTitle = $fixtureId > 0 ? 'Match Preparation' : 'Match Prep';
$formErrors = flash('errors') ?? [];
$fixtures = [];
$fixtureRecord = null;
$prepAlerts = [];
$playerPool = [];
$selectedStarterIds = [];
$selectedSubstituteIds = [];
$homeLineup = ['starters' => [], 'substitutes' => []];
$awayLineup = ['starters' => [], 'substitutes' => []];

if ($fixtureId > 0) {
    $fixtureRecord = $isReadOnly
        ? find_fixture_preparation_or_abort_for_admin($fixtureId)
        : find_fixture_preparation_or_abort($fixtureId, (int) $clubId);

    if ($fixtureRecord !== null) {
        $homeLineup = fixture_formation_lineup_for_club((int) $fixtureRecord['id'], (int) $fixtureRecord['home_club_id']);
        $awayLineup = fixture_formation_lineup_for_club((int) $fixtureRecord['id'], (int) $fixtureRecord['away_club_id']);

        if (!$isReadOnly) {
            $playerPool = fixture_preparation_player_pool((int) $clubId);
            $savedLineup = fixture_formation_lineup_for_club((int) $fixtureRecord['id'], (int) $clubId);
            $selectedStarterIds = array_map(static fn(array $player): int => (int) $player['player_id'], $savedLineup['starters']);
            $selectedSubstituteIds = array_map(static fn(array $player): int => (int) $player['player_id'], $savedLineup['substitutes']);

            $oldStarterIds = old('starter_player_ids', $selectedStarterIds);
            $oldSubstituteIds = old('substitute_player_ids', $selectedSubstituteIds);
            $selectedStarterIds = is_array($oldStarterIds) ? array_map('intval', $oldStarterIds) : $selectedStarterIds;
            $selectedSubstituteIds = is_array($oldSubstituteIds) ? array_map('intval', $oldSubstituteIds) : $selectedSubstituteIds;
        }
    }
} else {
    if ($isReadOnly) {
        $fixtures = fixture_preparation_list_for_admin();
    } else {
        $fixtures = fixture_preparation_list_for_club((int) $clubId);
        $prepAlerts = fixture_preparation_notifications((int) $clubId);
    }
}

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';
?>
<?php if ($fixtureRecord === null): ?>
    <section class="panel-card mb-4">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1"><?php echo $isReadOnly ? 'League oversight' : 'Coach workspace'; ?></p>
                <h2 class="h4 mb-0">Match preparation</h2>
            </div>
        </div>
        <?php if ($isReadOnly): ?>
            <div class="alert alert-info mb-4"><?php echo is_super_admin() ? 'Super Admin access is view-only here.' : 'League officials can review submissions and confirm matchday kits once both clubs have submitted.'; ?> Club-linked coaches and club admins are the only users who can submit formations and kit colors.</div>
        <?php endif; ?>
        <?php if ($prepAlerts !== []): ?>
            <div class="alert alert-warning mb-4">Formation windows are currently active or opening soon for your club fixtures.</div>
        <?php endif; ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Fixture</th>
                        <th>Competition</th>
                        <th>Schedule</th>
                        <th>Formation</th>
                        <th>Kit colors</th>
                        <th>Window</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($fixtures === []): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4"><?php echo $isReadOnly ? 'No fixtures are available for match preparation yet.' : 'No club fixtures are available for match preparation yet.'; ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php foreach ($fixtures as $fixture): ?>
                        <?php $windowState = $fixture['window_state'] ?? ['available' => false, 'message' => '']; ?>
                        <tr>
                            <td>
                                <strong><?php echo e($fixture['home_club_name']); ?> vs <?php echo e($fixture['away_club_name']); ?></strong>
                                <div class="small text-muted">Matchday <?php echo e((string) $fixture['matchday']); ?></div>
                            </td>
                            <td><?php echo e($fixture['competition_name']); ?></td>
                            <td><?php echo e(format_match_date_time($fixture['match_date'] ?? null, $fixture['match_time'] ?? null, 'TBD', 'Time TBC')); ?></td>
                            <td>
                                <?php if ($isReadOnly): ?>
                                    <div class="small">
                                        <strong><?php echo e($fixture['home_club_short_name'] ?: $fixture['home_club_name']); ?>:</strong>
                                        <?php echo e((string) (($fixture['home_formation'] ?? '') !== '' ? $fixture['home_formation'] : 'Not submitted')); ?>
                                    </div>
                                    <div class="small">
                                        <strong><?php echo e($fixture['away_club_short_name'] ?: $fixture['away_club_name']); ?>:</strong>
                                        <?php echo e((string) (($fixture['away_formation'] ?? '') !== '' ? $fixture['away_formation'] : 'Not submitted')); ?>
                                    </div>
                                <?php else: ?>
                                    <?php if (!empty($fixture['formation'])): ?>
                                        <strong><?php echo e($fixture['formation']); ?></strong>
                                        <div class="small text-muted">Updated <?php echo e((string) ($fixture['submitted_at'] ?? '')); ?></div>
                                    <?php else: ?>
                                        <span class="text-muted">Not submitted</span>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($isReadOnly): ?>
                                    <div class="small">
                                        <strong><?php echo e($fixture['home_club_short_name'] ?: $fixture['home_club_name']); ?>:</strong>
                                        <?php echo !empty($fixture['home_shirt_color']) ? e($fixture['home_shirt_color'] . ' / ' . $fixture['home_shorts_color'] . ' / ' . $fixture['home_socks_color']) : '<span class="text-muted">Not submitted</span>'; ?>
                                    </div>
                                    <div class="small">
                                        <strong><?php echo e($fixture['away_club_short_name'] ?: $fixture['away_club_name']); ?>:</strong>
                                        <?php echo !empty($fixture['away_shirt_color']) ? e($fixture['away_shirt_color'] . ' / ' . $fixture['away_shorts_color'] . ' / ' . $fixture['away_socks_color']) : '<span class="text-muted">Not submitted</span>'; ?>
                                    </div>
                                <?php else: ?>
                                    <?php if (!empty($fixture['shirt_color'])): ?>
                                        <strong><?php echo e($fixture['shirt_color']); ?></strong>
                                        <div class="small text-muted"><?php echo e($fixture['shorts_color'] . ' / ' . $fixture['socks_color']); ?></div>
                                        <div class="small text-muted"><?php echo !empty($fixture['kit_confirmed_at']) ? 'Confirmed' : 'Awaiting confirmation'; ?></div>
                                    <?php else: ?>
                                        <span class="text-muted">Not submitted</span>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="badge <?php echo ($windowState['available'] ?? false) ? 'badge-soft-success' : 'text-bg-light'; ?>">
                                    <?php echo ($windowState['available'] ?? false) ? 'Open' : 'Waiting'; ?>
                                </span>
                                <div class="small text-muted mt-1"><?php echo e((string) ($windowState['message'] ?? '')); ?></div>
                            </td>
                            <td class="text-end">
                                <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/modules/fixtures/preparation.php?id=' . (int) $fixture['id'])); ?>">
                                    <?php echo $isReadOnly ? 'View' : (($windowState['available'] ?? false) ? 'Set formation' : 'Open'); ?>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
<?php else: ?>
    <?php $windowState = $fixtureRecord['window_state'] ?? ['available' => false, 'message' => '']; ?>
    <section class="panel-card mb-4">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Coach workspace</p>
                <h2 class="h4 mb-0"><?php echo e($fixtureRecord['home_club_name']); ?> vs <?php echo e($fixtureRecord['away_club_name']); ?></h2>
            </div>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/fixtures/preparation.php')); ?>">Back to prep list</a>
        </div>

        <div class="row g-4 align-items-start">
            <div class="col-12 col-xl-5">
                <article class="match-share-card h-100">
                    <div class="match-share-top">
                        <div>
                            <p class="eyebrow mb-1"><?php echo $isReadOnly ? 'Preparation overview' : 'Preparation focus'; ?></p>
                            <h3 class="h4 mb-0"><?php echo e($fixtureRecord['competition_name']); ?></h3>
                        </div>
                        <span class="match-share-stage"><?php echo e($fixtureRecord['group_label'] ?: ($fixtureRecord['knockout_round'] ?: ucwords(str_replace('_', ' ', (string) ($fixtureRecord['stage_type'] ?? 'league'))))); ?></span>
                    </div>
                    <div class="match-share-body">
                        <div class="match-share-team">
                            <div class="match-share-logo">
                                <?php if (!empty($fixtureRecord['home_club_logo'])): ?>
                                    <img src="<?php echo e(base_url((string) $fixtureRecord['home_club_logo'])); ?>" alt="<?php echo e($fixtureRecord['home_club_name']); ?> logo">
                                <?php else: ?>
                                    <span><?php echo e($fixtureRecord['home_club_short_name'] ?: mb_substr((string) $fixtureRecord['home_club_name'], 0, 3)); ?></span>
                                <?php endif; ?>
                            </div>
                            <span><?php echo e($fixtureRecord['home_club_name']); ?></span>
                            <strong><?php echo e($fixtureRecord['home_club_short_name'] ?: $fixtureRecord['home_club_name']); ?></strong>
                        </div>
                        <div class="match-share-score"><strong>VS</strong></div>
                        <div class="match-share-team text-end">
                            <div class="match-share-logo ms-auto">
                                <?php if (!empty($fixtureRecord['away_club_logo'])): ?>
                                    <img src="<?php echo e(base_url((string) $fixtureRecord['away_club_logo'])); ?>" alt="<?php echo e($fixtureRecord['away_club_name']); ?> logo">
                                <?php else: ?>
                                    <span><?php echo e($fixtureRecord['away_club_short_name'] ?: mb_substr((string) $fixtureRecord['away_club_name'], 0, 3)); ?></span>
                                <?php endif; ?>
                            </div>
                            <span><?php echo e($fixtureRecord['away_club_name']); ?></span>
                            <strong><?php echo e($fixtureRecord['away_club_short_name'] ?: $fixtureRecord['away_club_name']); ?></strong>
                        </div>
                    </div>
                    <div class="match-share-meta">
                        <span><i class="bi bi-calendar-event"></i> <?php echo e(format_match_date_time($fixtureRecord['match_date'] ?? null, $fixtureRecord['match_time'] ?? null, 'TBD', 'Time TBC')); ?></span>
                        <span><i class="bi bi-geo-alt"></i> <?php echo e($fixtureRecord['venue_name'] ?? 'Venue TBC'); ?></span>
                        <span><i class="bi bi-bell"></i> <?php echo e((string) ($windowState['message'] ?? '')); ?></span>
                    </div>
                </article>
            </div>
            <div class="col-12 col-xl-7">
                <article class="panel-card h-100">
                    <div class="panel-card-header mb-3">
                        <div>
                            <p class="eyebrow mb-1"><?php echo $isReadOnly ? 'Formation overview' : 'Formation desk'; ?></p>
                            <h3 class="h5 mb-0"><?php echo $isReadOnly ? 'Submitted shapes and tactical notes' : 'Submit team shape and tactical notes'; ?></h3>
                        </div>
                    </div>

                    <div class="alert <?php echo ($windowState['available'] ?? false) ? 'alert-success' : 'alert-secondary'; ?>">
                        <?php echo e((string) ($windowState['message'] ?? '')); ?>
                    </div>
                    <?php if ($isReadOnly): ?>
                        <div class="row g-3">
                            <div class="col-12 col-lg-6">
                                <div class="border rounded-3 p-3 h-100">
                                    <p class="eyebrow mb-1"><?php echo e($fixtureRecord['home_club_name']); ?></p>
                                    <h4 class="h6 mb-2"><?php echo e((string) (($fixtureRecord['home_formation'] ?? '') !== '' ? $fixtureRecord['home_formation'] : 'Not submitted')); ?></h4>
                                    <p class="small text-muted mb-2"><?php echo !empty($fixtureRecord['home_submitted_at']) ? 'Updated ' . e((string) $fixtureRecord['home_submitted_at']) : 'No submission yet.'; ?></p>
                                    <p class="mb-0 text-muted"><?php echo e((string) (($fixtureRecord['home_tactical_notes'] ?? '') !== '' ? $fixtureRecord['home_tactical_notes'] : 'No tactical notes submitted.')); ?></p>
                                    <?php if ($homeLineup['starters'] !== [] || $homeLineup['substitutes'] !== []): ?>
                                        <div class="small mt-3">
                                            <div class="fw-semibold text-dark mb-1">Starting XI</div>
                                            <div class="text-muted mb-2"><?php echo e(implode(', ', array_map('fixture_lineup_player_label', $homeLineup['starters']))); ?></div>
                                            <div class="fw-semibold text-dark mb-1">Substitutes</div>
                                            <div class="text-muted"><?php echo e(implode(', ', array_map('fixture_lineup_player_label', $homeLineup['substitutes']))); ?></div>
                                        </div>
                                    <?php endif; ?>
                                    <div class="small mt-3 border-top pt-3">
                                        <div class="fw-semibold text-dark mb-1">Matchday kit</div>
                                        <div class="text-muted mb-2"><?php echo !empty($fixtureRecord['home_shirt_color']) ? e('Shirt: ' . $fixtureRecord['home_shirt_color'] . ', Shorts: ' . $fixtureRecord['home_shorts_color'] . ', Socks: ' . $fixtureRecord['home_socks_color']) : 'No kit submission yet.'; ?></div>
                                        <div class="text-muted mb-2"><?php echo !empty($fixtureRecord['home_goalkeeper_shirt_color']) ? e('Goalkeeper: ' . $fixtureRecord['home_goalkeeper_shirt_color'] . ' / ' . $fixtureRecord['home_goalkeeper_shorts_color']) : ''; ?></div>
                                        <div class="text-muted mb-2"><?php echo !empty($fixtureRecord['home_kit_confirmed_at']) ? e('Confirmed on ' . (string) $fixtureRecord['home_kit_confirmed_at']) : 'Awaiting official confirmation.'; ?></div>
                                        <?php if ($canConfirmKits && !empty($fixtureRecord['home_shirt_color']) && empty($fixtureRecord['home_kit_confirmed_at'])): ?>
                                            <form method="post" action="<?php echo e(base_url('app/modules/fixtures/preparation-actions.php')); ?>">
                                                <?php echo csrf_input(); ?>
                                                <input type="hidden" name="action" value="confirm_kit">
                                                <input type="hidden" name="fixture_id" value="<?php echo e((string) $fixtureRecord['id']); ?>">
                                                <input type="hidden" name="confirmed_club_id" value="<?php echo e((string) $fixtureRecord['home_club_id']); ?>">
                                                <button class="btn btn-outline-jlfc btn-sm" type="submit">Confirm <?php echo e($fixtureRecord['home_club_short_name'] ?: $fixtureRecord['home_club_name']); ?> kit</button>
                                            </form>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12 col-lg-6">
                                <div class="border rounded-3 p-3 h-100">
                                    <p class="eyebrow mb-1"><?php echo e($fixtureRecord['away_club_name']); ?></p>
                                    <h4 class="h6 mb-2"><?php echo e((string) (($fixtureRecord['away_formation'] ?? '') !== '' ? $fixtureRecord['away_formation'] : 'Not submitted')); ?></h4>
                                    <p class="small text-muted mb-2"><?php echo !empty($fixtureRecord['away_submitted_at']) ? 'Updated ' . e((string) $fixtureRecord['away_submitted_at']) : 'No submission yet.'; ?></p>
                                    <p class="mb-0 text-muted"><?php echo e((string) (($fixtureRecord['away_tactical_notes'] ?? '') !== '' ? $fixtureRecord['away_tactical_notes'] : 'No tactical notes submitted.')); ?></p>
                                    <?php if ($awayLineup['starters'] !== [] || $awayLineup['substitutes'] !== []): ?>
                                        <div class="small mt-3">
                                            <div class="fw-semibold text-dark mb-1">Starting XI</div>
                                            <div class="text-muted mb-2"><?php echo e(implode(', ', array_map('fixture_lineup_player_label', $awayLineup['starters']))); ?></div>
                                            <div class="fw-semibold text-dark mb-1">Substitutes</div>
                                            <div class="text-muted"><?php echo e(implode(', ', array_map('fixture_lineup_player_label', $awayLineup['substitutes']))); ?></div>
                                        </div>
                                    <?php endif; ?>
                                    <div class="small mt-3 border-top pt-3">
                                        <div class="fw-semibold text-dark mb-1">Matchday kit</div>
                                        <div class="text-muted mb-2"><?php echo !empty($fixtureRecord['away_shirt_color']) ? e('Shirt: ' . $fixtureRecord['away_shirt_color'] . ', Shorts: ' . $fixtureRecord['away_shorts_color'] . ', Socks: ' . $fixtureRecord['away_socks_color']) : 'No kit submission yet.'; ?></div>
                                        <div class="text-muted mb-2"><?php echo !empty($fixtureRecord['away_goalkeeper_shirt_color']) ? e('Goalkeeper: ' . $fixtureRecord['away_goalkeeper_shirt_color'] . ' / ' . $fixtureRecord['away_goalkeeper_shorts_color']) : ''; ?></div>
                                        <div class="text-muted mb-2"><?php echo !empty($fixtureRecord['away_kit_confirmed_at']) ? e('Confirmed on ' . (string) $fixtureRecord['away_kit_confirmed_at']) : 'Awaiting official confirmation.'; ?></div>
                                        <?php if ($canConfirmKits && !empty($fixtureRecord['away_shirt_color']) && empty($fixtureRecord['away_kit_confirmed_at'])): ?>
                                            <form method="post" action="<?php echo e(base_url('app/modules/fixtures/preparation-actions.php')); ?>">
                                                <?php echo csrf_input(); ?>
                                                <input type="hidden" name="action" value="confirm_kit">
                                                <input type="hidden" name="fixture_id" value="<?php echo e((string) $fixtureRecord['id']); ?>">
                                                <input type="hidden" name="confirmed_club_id" value="<?php echo e((string) $fixtureRecord['away_club_id']); ?>">
                                                <button class="btn btn-outline-jlfc btn-sm" type="submit">Confirm <?php echo e($fixtureRecord['away_club_short_name'] ?: $fixtureRecord['away_club_name']); ?> kit</button>
                                            </form>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <form method="post" action="<?php echo e(base_url('app/modules/fixtures/preparation-actions.php')); ?>">
                            <?php echo csrf_input(); ?>
                            <input type="hidden" name="action" value="save_preparation">
                            <input type="hidden" name="fixture_id" value="<?php echo e((string) $fixtureRecord['id']); ?>">
                            <div class="mb-3">
                                <label class="form-label" for="formation">Formation</label>
                                <input class="form-control <?php echo validation_error($formErrors, 'formation') ? 'is-invalid' : ''; ?>" id="formation" name="formation" type="text" maxlength="20" placeholder="4-3-3 or 4-2-3-1" value="<?php echo e((string) old('formation', (string) ($fixtureRecord['formation'] ?? ''))); ?>">
                                <?php if (validation_error($formErrors, 'formation')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'formation')); ?></div><?php endif; ?>
                            </div>
                            <div class="mb-3">
                                <label class="form-label" for="tactical_notes">Tactical notes</label>
                                <textarea class="form-control <?php echo validation_error($formErrors, 'tactical_notes') ? 'is-invalid' : ''; ?>" id="tactical_notes" name="tactical_notes" rows="6" placeholder="Pressing plan, rest defence, set-piece focus, or player instructions."><?php echo e((string) old('tactical_notes', (string) ($fixtureRecord['tactical_notes'] ?? ''))); ?></textarea>
                                <?php if (validation_error($formErrors, 'tactical_notes')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'tactical_notes')); ?></div><?php endif; ?>
                            </div>
                            <div class="row g-3 mb-3">
                                <div class="col-md-4">
                                    <label class="form-label" for="shirt_color">Shirt color</label>
                                    <input class="form-control <?php echo validation_error($formErrors, 'shirt_color') ? 'is-invalid' : ''; ?>" id="shirt_color" name="shirt_color" type="text" maxlength="60" value="<?php echo e((string) old('shirt_color', (string) ($fixtureRecord['shirt_color'] ?? ''))); ?>">
                                    <?php if (validation_error($formErrors, 'shirt_color')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'shirt_color')); ?></div><?php endif; ?>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label" for="shorts_color">Shorts color</label>
                                    <input class="form-control <?php echo validation_error($formErrors, 'shorts_color') ? 'is-invalid' : ''; ?>" id="shorts_color" name="shorts_color" type="text" maxlength="60" value="<?php echo e((string) old('shorts_color', (string) ($fixtureRecord['shorts_color'] ?? ''))); ?>">
                                    <?php if (validation_error($formErrors, 'shorts_color')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'shorts_color')); ?></div><?php endif; ?>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label" for="socks_color">Socks color</label>
                                    <input class="form-control <?php echo validation_error($formErrors, 'socks_color') ? 'is-invalid' : ''; ?>" id="socks_color" name="socks_color" type="text" maxlength="60" value="<?php echo e((string) old('socks_color', (string) ($fixtureRecord['socks_color'] ?? ''))); ?>">
                                    <?php if (validation_error($formErrors, 'socks_color')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'socks_color')); ?></div><?php endif; ?>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label" for="goalkeeper_shirt_color">Goalkeeper shirt color</label>
                                    <input class="form-control <?php echo validation_error($formErrors, 'goalkeeper_shirt_color') ? 'is-invalid' : ''; ?>" id="goalkeeper_shirt_color" name="goalkeeper_shirt_color" type="text" maxlength="60" value="<?php echo e((string) old('goalkeeper_shirt_color', (string) ($fixtureRecord['goalkeeper_shirt_color'] ?? ''))); ?>">
                                    <?php if (validation_error($formErrors, 'goalkeeper_shirt_color')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'goalkeeper_shirt_color')); ?></div><?php endif; ?>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label" for="goalkeeper_shorts_color">Goalkeeper shorts color</label>
                                    <input class="form-control <?php echo validation_error($formErrors, 'goalkeeper_shorts_color') ? 'is-invalid' : ''; ?>" id="goalkeeper_shorts_color" name="goalkeeper_shorts_color" type="text" maxlength="60" value="<?php echo e((string) old('goalkeeper_shorts_color', (string) ($fixtureRecord['goalkeeper_shorts_color'] ?? ''))); ?>">
                                    <?php if (validation_error($formErrors, 'goalkeeper_shorts_color')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'goalkeeper_shorts_color')); ?></div><?php endif; ?>
                                </div>
                            </div>
                            <div class="small text-muted mb-3">
                                <?php echo !empty($fixtureRecord['kit_confirmed_at']) ? e('Current kit set was confirmed on ' . (string) $fixtureRecord['kit_confirmed_at'] . '. Saving changes will require re-confirmation.') : 'Submit the exact matchday kit colors for official confirmation.'; ?>
                            </div>
                            <div class="row g-3 mb-3">
                                <div class="col-12 col-lg-6">
                                    <label class="form-label" for="starter_player_ids">Starting XI</label>
                                    <select class="form-select <?php echo validation_error($formErrors, 'starter_player_ids') ? 'is-invalid' : ''; ?>" id="starter_player_ids" name="starter_player_ids[]" multiple size="12">
                                        <?php foreach ($playerPool as $player): ?>
                                            <option value="<?php echo e((string) $player['id']); ?>" <?php echo in_array((int) $player['id'], $selectedStarterIds, true) ? 'selected' : ''; ?>><?php echo e(fixture_lineup_player_label($player)); ?> (<?php echo e((string) $player['position']); ?>)</option>
                                        <?php endforeach; ?>
                                    </select>
                                    <?php if (validation_error($formErrors, 'starter_player_ids')): ?><div class="invalid-feedback d-block"><?php echo e((string) validation_error($formErrors, 'starter_player_ids')); ?></div><?php endif; ?>
                                    <div class="form-text">Select exactly 11 players in order.</div>
                                </div>
                                <div class="col-12 col-lg-6">
                                    <label class="form-label" for="substitute_player_ids">Substitutes</label>
                                    <select class="form-select <?php echo validation_error($formErrors, 'substitute_player_ids') ? 'is-invalid' : ''; ?>" id="substitute_player_ids" name="substitute_player_ids[]" multiple size="8">
                                        <?php foreach ($playerPool as $player): ?>
                                            <option value="<?php echo e((string) $player['id']); ?>" <?php echo in_array((int) $player['id'], $selectedSubstituteIds, true) ? 'selected' : ''; ?>><?php echo e(fixture_lineup_player_label($player)); ?> (<?php echo e((string) $player['position']); ?>)</option>
                                        <?php endforeach; ?>
                                    </select>
                                    <?php if (validation_error($formErrors, 'substitute_player_ids')): ?><div class="invalid-feedback d-block"><?php echo e((string) validation_error($formErrors, 'substitute_player_ids')); ?></div><?php endif; ?>
                                    <div class="form-text">Select exactly 5 substitutes.</div>
                                </div>
                            </div>
                            <?php if (!empty($fixtureRecord['submitted_at'])): ?>
                                <p class="small text-muted">Current submission saved on <?php echo e((string) $fixtureRecord['submitted_at']); ?>.</p>
                            <?php endif; ?>
                            <button class="btn btn-jlfc" type="submit" <?php echo ($windowState['available'] ?? false) ? '' : 'disabled'; ?>>Save formation</button>
                        </form>
                    <?php endif; ?>
                </article>
            </div>
        </div>
    </section>
<?php endif; ?>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>