<?php

$mediaRecord = $mediaRecord ?? null;
$formErrors = $formErrors ?? [];
$fixtureOptions = media_fixture_options();
$clubOptions = media_club_options();
$fieldValue = static function (string $field, mixed $default = '') use ($mediaRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $mediaRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <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="200" 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-md-6">
        <label class="form-label" for="media_type">Media type</label>
        <select class="form-select <?php echo validation_error($formErrors, 'media_type') ? 'is-invalid' : ''; ?>" id="media_type" name="media_type" required>
            <?php foreach (media_type_options() as $mediaType): ?>
                <option value="<?php echo e($mediaType); ?>" <?php echo (string) $fieldValue('media_type', 'image') === $mediaType ? 'selected' : ''; ?>><?php echo e(ucfirst($mediaType)); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'media_type')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'media_type')); ?></div><?php endif; ?>
    </div>
    <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">
            <option value="">No fixture linked</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 ' . $fixture['away_club_name']); ?><?php echo $fixture['match_date'] ? ' - ' . e($fixture['match_date']) : ''; ?></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-6">
        <label class="form-label" for="club_id">Club</label>
        <select class="form-select <?php echo validation_error($formErrors, 'club_id') ? 'is-invalid' : ''; ?>" id="club_id" name="club_id">
            <option value="">No club linked</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>
        <?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-12">
        <label class="form-label" for="media_file">Media file</label>
        <input class="form-control <?php echo validation_error($formErrors, 'media_file') ? 'is-invalid' : ''; ?>" id="media_file" name="media_file" type="file" accept="image/*,video/mp4,video/webm">
        <div class="form-text">Upload a file to add or replace the stored media item.</div>
        <?php if (validation_error($formErrors, 'media_file')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'media_file')); ?></div><?php endif; ?>
    </div>
</div>