<?php
$fixtureRecord = $fixtureRecord ?? null;
$formErrors = $formErrors ?? [];
$selectedCompetitionId = (int) old('competition_id', (string) ($fixtureRecord['competition_id'] ?? ($_GET['competition_id'] ?? 0)));
$competitionOptions = fixture_competition_options();
$clubOptions = fixture_club_options_for_competition($selectedCompetitionId);
$venueOptions = fixture_venue_options();
$refereeOptions = fixture_referee_options();
$fixtureMatchCommissionerOptions = 'fixture_match_commissioner_options';
$commissionerOptions = $fixtureMatchCommissionerOptions();
$fieldValue = static function (string $field, mixed $default = '') use ($fixtureRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $fixtureRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <div class="col-md-6">
        <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" required>
            <option value="">Select competition</option>
            <?php foreach ($competitionOptions as $competition): ?>
                <option value="<?php echo e((string) $competition['id']); ?>" <?php echo (int) $fieldValue('competition_id') === (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-3">
        <label class="form-label" for="matchday">Matchday</label>
        <input class="form-control <?php echo validation_error($formErrors, 'matchday') ? 'is-invalid' : ''; ?>" id="matchday" name="matchday" type="number" min="1" value="<?php echo e((string) $fieldValue('matchday')); ?>" required>
        <?php if (validation_error($formErrors, 'matchday')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'matchday')); ?></div><?php endif; ?>
    </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 (fixture_status_options() as $status): ?>
                <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('status', 'scheduled') === $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>

    <div class="col-md-6">
        <label class="form-label" for="home_club_id">Home club</label>
        <select class="form-select <?php echo validation_error($formErrors, 'home_club_id') ? 'is-invalid' : ''; ?>" id="home_club_id" name="home_club_id" required>
            <option value="">Select home club</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (int) $fieldValue('home_club_id') === (int) $club['id'] ? 'selected' : ''; ?>>
                    <?php echo e($club['name']); ?><?php echo $club['short_name'] ? ' (' . e($club['short_name']) . ')' : ''; ?>
                </option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'home_club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'home_club_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="away_club_id">Away club</label>
        <select class="form-select <?php echo validation_error($formErrors, 'away_club_id') ? 'is-invalid' : ''; ?>" id="away_club_id" name="away_club_id" required>
            <option value="">Select away club</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (int) $fieldValue('away_club_id') === (int) $club['id'] ? 'selected' : ''; ?>>
                    <?php echo e($club['name']); ?><?php echo $club['short_name'] ? ' (' . e($club['short_name']) . ')' : ''; ?>
                </option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'away_club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'away_club_id')); ?></div><?php endif; ?>
    </div>

    <div class="col-md-4">
        <label class="form-label" for="match_date">Match date</label>
        <input class="form-control <?php echo validation_error($formErrors, 'match_date') ? 'is-invalid' : ''; ?>" id="match_date" name="match_date" type="date" value="<?php echo e((string) $fieldValue('match_date')); ?>">
        <?php if (validation_error($formErrors, 'match_date')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'match_date')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="match_time">Kickoff time</label>
        <input class="form-control <?php echo validation_error($formErrors, 'match_time') ? 'is-invalid' : ''; ?>" id="match_time" name="match_time" type="time" value="<?php echo e((string) $fieldValue('match_time')); ?>">
        <?php if (validation_error($formErrors, 'match_time')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'match_time')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="venue_id">Venue</label>
        <select class="form-select <?php echo validation_error($formErrors, 'venue_id') ? 'is-invalid' : ''; ?>" id="venue_id" name="venue_id">
            <option value="">Unassigned</option>
            <?php foreach ($venueOptions as $venue): ?>
                <option value="<?php echo e((string) $venue['id']); ?>" <?php echo (int) $fieldValue('venue_id') === (int) $venue['id'] ? 'selected' : ''; ?>>
                    <?php echo e($venue['name']); ?> - <?php echo e($venue['location']); ?>
                </option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'venue_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'venue_id')); ?></div><?php endif; ?>
    </div>

    <div class="col-md-6">
        <label class="form-label" for="referee_id">Referee</label>
        <select class="form-select <?php echo validation_error($formErrors, 'referee_id') ? 'is-invalid' : ''; ?>" id="referee_id" name="referee_id">
            <option value="">Unassigned</option>
            <?php foreach ($refereeOptions as $referee): ?>
                <option value="<?php echo e((string) $referee['id']); ?>" <?php echo (int) $fieldValue('referee_id') === (int) $referee['id'] ? 'selected' : ''; ?>>
                    <?php echo e($referee['full_name']); ?> (<?php echo e(ucfirst($referee['level'])); ?>)
                </option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'referee_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'referee_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="match_commissioner">Match commissioner</label>
        <input class="form-control" id="match_commissioner" name="match_commissioner" list="match_commissioner_options" value="<?php echo e((string) $fieldValue('match_commissioner')); ?>" placeholder="Assigned official name">
        <datalist id="match_commissioner_options">
            <?php foreach ($commissionerOptions as $commissioner): ?>
                <option value="<?php echo e($commissioner['full_name']); ?>"></option>
            <?php endforeach; ?>
        </datalist>
        <div class="form-text">Leave blank to auto-assign from active match commissioners when the fixture is created or generated.</div>
    </div>

    <div class="col-md-4">
        <label class="form-label" for="assistant_referee_1">Assistant referee 1</label>
        <input class="form-control" id="assistant_referee_1" name="assistant_referee_1" value="<?php echo e((string) $fieldValue('assistant_referee_1')); ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label" for="assistant_referee_2">Assistant referee 2</label>
        <input class="form-control" id="assistant_referee_2" name="assistant_referee_2" value="<?php echo e((string) $fieldValue('assistant_referee_2')); ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label" for="fourth_official">Fourth official</label>
        <input class="form-control" id="fourth_official" name="fourth_official" value="<?php echo e((string) $fieldValue('fourth_official')); ?>">
    </div>
</div>