<?php

$awardRecord = $awardRecord ?? null;
$formErrors = $formErrors ?? [];
$seasonOptions = season_list();
$competitionOptions = award_competition_options();
$clubOptions = award_club_options();
$playerOptions = award_player_options();
$fieldValue = static function (string $field, mixed $default = '') use ($awardRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $awardRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <div class="col-md-4">
        <label class="form-label" for="season_id">Season</label>
        <select class="form-select <?php echo validation_error($formErrors, 'season_id') ? 'is-invalid' : ''; ?>" id="season_id" name="season_id" required>
            <option value="">Select season</option>
            <?php foreach ($seasonOptions as $season): ?>
                <option value="<?php echo e((string) $season['id']); ?>" <?php echo (int) $fieldValue('season_id', (string) ($_GET['season_id'] ?? 0)) === (int) $season['id'] ? 'selected' : ''; ?>><?php echo e($season['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'season_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'season_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="competition_id">Competition</label>
        <select class="form-select <?php echo validation_error($formErrors, 'competition_id') ? 'is-invalid' : ''; ?>" id="competition_id" name="competition_id">
            <option value="">All season awards</option>
            <?php foreach ($competitionOptions as $competition): ?>
                <option value="<?php echo e((string) $competition['id']); ?>" <?php echo (int) $fieldValue('competition_id', (string) ($_GET['competition_id'] ?? 0)) === (int) $competition['id'] ? 'selected' : ''; ?>><?php echo e($competition['name']); ?> (<?php echo e($competition['season_name']); ?>)</option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'competition_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'competition_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="award_type">Award type</label>
        <select class="form-select <?php echo validation_error($formErrors, 'award_type') ? 'is-invalid' : ''; ?>" id="award_type" name="award_type" required>
            <option value="">Select award type</option>
            <?php foreach (award_type_options() as $awardType): ?>
                <option value="<?php echo e($awardType); ?>" <?php echo (string) $fieldValue('award_type') === $awardType ? 'selected' : ''; ?>><?php echo e(award_type_label($awardType)); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'award_type')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'award_type')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="club_id">Club recipient</label>
        <select class="form-select <?php echo validation_error($formErrors, 'club_id') ? 'is-invalid' : ''; ?>" id="club_id" name="club_id">
            <option value="">No club recipient</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (int) $fieldValue('club_id') === (int) $club['id'] ? 'selected' : ''; ?>><?php echo e($club['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <div class="form-text">Required for champion, runner-up, fair play, and best coach awards.</div>
        <?php if (validation_error($formErrors, 'club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'club_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="player_id">Player recipient</label>
        <select class="form-select <?php echo validation_error($formErrors, 'player_id') ? 'is-invalid' : ''; ?>" id="player_id" name="player_id">
            <option value="">No player recipient</option>
            <?php foreach ($playerOptions as $player): ?>
                <?php $playerName = trim($player['first_name'] . ' ' . ($player['middle_name'] ? $player['middle_name'] . ' ' : '') . $player['last_name']); ?>
                <option value="<?php echo e((string) $player['id']); ?>" <?php echo (int) $fieldValue('player_id') === (int) $player['id'] ? 'selected' : ''; ?>><?php echo e($playerName); ?> (<?php echo e($player['club_name']); ?>)</option>
            <?php endforeach; ?>
        </select>
        <div class="form-text">Required for top scorer, best goalkeeper, and best player awards.</div>
        <?php if (validation_error($formErrors, 'player_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'player_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="title">Title</label>
        <input class="form-control <?php echo validation_error($formErrors, 'title') ? 'is-invalid' : ''; ?>" id="title" name="title" type="text" maxlength="150" value="<?php echo e((string) $fieldValue('title')); ?>" required>
        <?php if (validation_error($formErrors, 'title')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'title')); ?></div><?php endif; ?>
    </div>
    <div class="col-12">
        <label class="form-label" for="description">Description</label>
        <textarea class="form-control <?php echo validation_error($formErrors, 'description') ? 'is-invalid' : ''; ?>" id="description" name="description" rows="5"><?php echo e((string) $fieldValue('description')); ?></textarea>
        <?php if (validation_error($formErrors, 'description')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'description')); ?></div><?php endif; ?>
    </div>
</div>