<?php
$competitionRecord = $competitionRecord ?? null;
$formErrors = $formErrors ?? [];
$seasonOptions = $seasonOptions ?? competition_season_options();

$fieldValue = static function (string $field, mixed $default = '') use ($competitionRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $competitionRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <?php $editableStatuses = array_values(array_filter(competition_status_options(), static fn(string $status): bool => $status !== 'paused')); ?>
    <div class="col-md-6">
        <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 (string) $fieldValue('season_id', (string) ($_GET['season_id'] ?? '')) === (string) $season['id'] ? 'selected' : ''; ?>><?php echo e($season['name']); ?> (<?php echo e(ucfirst($season['status'])); ?>)</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-6">
        <label class="form-label" for="name">Competition name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'name') ? 'is-invalid' : ''; ?>" id="name" name="name" value="<?php echo e((string) $fieldValue('name')); ?>" required>
        <?php if (validation_error($formErrors, 'name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'name')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="sequence_order">Rotation order</label>
        <input class="form-control <?php echo validation_error($formErrors, 'sequence_order') ? 'is-invalid' : ''; ?>" id="sequence_order" name="sequence_order" type="number" min="1" value="<?php echo e((string) $fieldValue('sequence_order')); ?>">
        <?php if (validation_error($formErrors, 'sequence_order')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'sequence_order')); ?></div><?php endif; ?>
        <div class="form-text">Lower numbers rotate first inside the season.</div>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="type">Type</label>
        <select class="form-select <?php echo validation_error($formErrors, 'type') ? 'is-invalid' : ''; ?>" id="type" name="type" required>
            <?php foreach (competition_type_options() as $type): ?>
                <option value="<?php echo e($type); ?>" <?php echo (string) $fieldValue('type', 'league') === $type ? 'selected' : ''; ?>><?php echo e(ucwords(str_replace('_', ' ', $type))); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'type')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'type')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-5">
        <label class="form-label" for="number_of_teams">Number of teams</label>
        <input class="form-control <?php echo validation_error($formErrors, 'number_of_teams') ? 'is-invalid' : ''; ?>" id="number_of_teams" name="number_of_teams" type="number" min="0" value="<?php echo e((string) $fieldValue('number_of_teams', '0')); ?>">
        <?php if (validation_error($formErrors, 'number_of_teams')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'number_of_teams')); ?></div><?php endif; ?>
        <div class="form-text">Required for league competitions.</div>
    </div>
    <div class="col-md-2">
        <label class="form-label" for="group_count">Groups</label>
        <input class="form-control <?php echo validation_error($formErrors, 'group_count') ? 'is-invalid' : ''; ?>" id="group_count" name="group_count" type="number" min="0" value="<?php echo e((string) $fieldValue('group_count', '0')); ?>">
        <?php if (validation_error($formErrors, 'group_count')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'group_count')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-2">
        <label class="form-label" for="qualifiers_per_group">Qualifiers</label>
        <input class="form-control <?php echo validation_error($formErrors, 'qualifiers_per_group') ? 'is-invalid' : ''; ?>" id="qualifiers_per_group" name="qualifiers_per_group" type="number" min="0" value="<?php echo e((string) $fieldValue('qualifiers_per_group', '0')); ?>">
        <?php if (validation_error($formErrors, 'qualifiers_per_group')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'qualifiers_per_group')); ?></div><?php endif; ?>
        <div class="form-text">Use for group stage or mixed competitions.</div>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="status">Status</label>
        <select class="form-select <?php echo validation_error($formErrors, 'status') ? 'is-invalid' : ''; ?>" id="status" name="status" required>
            <?php foreach ($editableStatuses as $status): ?>
                <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('status', 'draft') === $status ? 'selected' : ''; ?>><?php echo e(ucfirst($status)); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'status')); ?></div><?php endif; ?>
        <div class="form-text">Use the dedicated pause and resume controls to hold one competition while another continues in the same season.</div>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="points_win">Points for win</label>
        <input class="form-control <?php echo validation_error($formErrors, 'points_win') ? 'is-invalid' : ''; ?>" id="points_win" name="points_win" type="number" min="0" value="<?php echo e((string) $fieldValue('points_win', (string) site_setting('points_win', '3'))); ?>">
        <?php if (validation_error($formErrors, 'points_win')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'points_win')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="points_draw">Points for draw</label>
        <input class="form-control <?php echo validation_error($formErrors, 'points_draw') ? 'is-invalid' : ''; ?>" id="points_draw" name="points_draw" type="number" min="0" value="<?php echo e((string) $fieldValue('points_draw', (string) site_setting('points_draw', '1'))); ?>">
        <?php if (validation_error($formErrors, 'points_draw')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'points_draw')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="points_loss">Points for loss</label>
        <input class="form-control <?php echo validation_error($formErrors, 'points_loss') ? 'is-invalid' : ''; ?>" id="points_loss" name="points_loss" type="number" min="0" value="<?php echo e((string) $fieldValue('points_loss', (string) site_setting('points_loss', '0'))); ?>">
        <?php if (validation_error($formErrors, 'points_loss')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'points_loss')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="tie_breaker_type">Tie breaker</label>
        <select class="form-select <?php echo validation_error($formErrors, 'tie_breaker_type') ? 'is-invalid' : ''; ?>" id="tie_breaker_type" name="tie_breaker_type" required>
            <?php foreach (competition_tie_breaker_options() as $tieBreaker): ?>
                <option value="<?php echo e($tieBreaker); ?>" <?php echo (string) $fieldValue('tie_breaker_type', 'goal_difference') === $tieBreaker ? 'selected' : ''; ?>><?php echo e(ucwords(str_replace('_', ' ', $tieBreaker))); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'tie_breaker_type')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'tie_breaker_type')); ?></div><?php endif; ?>
    </div>
</div>