<?php
$protestRecord = $protestRecord ?? null;
$formErrors = $formErrors ?? [];
$fixtureOptions = protest_fixture_options();
$fieldValue = static function (string $field, mixed $default = '') use ($protestRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $protestRecord[$field] ?? $default;
};
$selectedFixtureId = (int) $fieldValue('fixture_id', (string) ($_GET['fixture_id'] ?? 0));
$clubOptions = protest_fixture_club_options($selectedFixtureId);
?>
<div class="row g-3">
    <div class="col-md-6">
        <label class="form-label" for="fixture_id">Fixture</label>
        <select class="form-select <?php echo validation_error($formErrors, 'fixture_id') ? 'is-invalid' : ''; ?>" id="fixture_id" name="fixture_id" required>
            <option value="">Select fixture</option>
            <?php foreach ($fixtureOptions as $fixture): ?>
                <option value="<?php echo e((string) $fixture['id']); ?>" <?php echo (int) $fieldValue('fixture_id', (string) ($_GET['fixture_id'] ?? 0)) === (int) $fixture['id'] ? 'selected' : ''; ?>>
                    <?php echo e($fixture['home_club_name']); ?> vs <?php echo e($fixture['away_club_name']); ?> - <?php echo e($fixture['competition_name']); ?>
                </option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'fixture_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'fixture_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="protesting_club_id">Protesting club</label>
        <select class="form-select <?php echo validation_error($formErrors, 'protesting_club_id') ? 'is-invalid' : ''; ?>" id="protesting_club_id" name="protesting_club_id" <?php echo is_club_admin() ? 'readonly' : ''; ?> required>
            <option value="">Select club</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (int) $fieldValue('protesting_club_id') === (int) $club['id'] ? 'selected' : ''; ?>><?php echo e($club['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'protesting_club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'protesting_club_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="against_club_id">Against club</label>
        <select class="form-select <?php echo validation_error($formErrors, 'against_club_id') ? 'is-invalid' : ''; ?>" id="against_club_id" name="against_club_id" required>
            <option value="">Select club</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (int) $fieldValue('against_club_id') === (int) $club['id'] ? 'selected' : ''; ?>><?php echo e($club['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'against_club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'against_club_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-12">
        <label class="form-label" for="title">Title</label>
        <input class="form-control <?php echo validation_error($formErrors, 'title') ? 'is-invalid' : ''; ?>" id="title" name="title" 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="complaint">Complaint</label>
        <textarea class="form-control <?php echo validation_error($formErrors, 'complaint') ? 'is-invalid' : ''; ?>" id="complaint" name="complaint" rows="5" required><?php echo e((string) $fieldValue('complaint')); ?></textarea>
        <?php if (validation_error($formErrors, 'complaint')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'complaint')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="evidence_file">Evidence file</label>
        <input class="form-control <?php echo validation_error($formErrors, 'evidence_file') ? 'is-invalid' : ''; ?>" id="evidence_file" name="evidence_file" type="file" accept=".pdf,.jpg,.jpeg,.png,.webp">
        <?php if (validation_error($formErrors, 'evidence_file')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'evidence_file')); ?></div><?php endif; ?>
        <?php if (!empty($protestRecord['evidence_file'])): ?><div class="form-text">Uploading a new file will replace the current evidence attachment.</div><?php endif; ?>
    </div>
    <?php if (!is_club_admin()): ?>
        <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">
                <?php foreach (protest_status_options() as $status): ?>
                    <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('status', 'submitted') === $status ? 'selected' : ''; ?>><?php echo e(ucwords(str_replace('_', ' ', $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>
        <?php if (can_decide_protests()): ?>
            <div class="col-md-3">
                <label class="form-label" for="decision">Decision summary</label>
                <input class="form-control" id="decision" name="decision" value="<?php echo e((string) $fieldValue('decision')); ?>">
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>