<?php
$seasonRecord = $seasonRecord ?? null;
$formErrors = $formErrors ?? [];

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

    return $seasonRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <?php $editableStatuses = array_values(array_filter(season_status_options(), static fn(string $status): bool => $status !== 'paused')); ?>
    <div class="col-md-6">
        <label class="form-label" for="name">Season 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="start_date">Start date</label>
        <input class="form-control <?php echo validation_error($formErrors, 'start_date') ? 'is-invalid' : ''; ?>" id="start_date" name="start_date" type="date" value="<?php echo e((string) $fieldValue('start_date')); ?>" required>
        <?php if (validation_error($formErrors, 'start_date')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'start_date')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="end_date">End date</label>
        <input class="form-control <?php echo validation_error($formErrors, 'end_date') ? 'is-invalid' : ''; ?>" id="end_date" name="end_date" type="date" value="<?php echo e((string) $fieldValue('end_date')); ?>" required>
        <?php if (validation_error($formErrors, 'end_date')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'end_date')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <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', 'upcoming') === $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 buttons to temporarily stop a season and shift future fixtures forward.</div>
    </div>
</div>