<?php
$staffRecord = $staffRecord ?? null;
$formErrors = $formErrors ?? [];
$clubOptions = club_options();

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

    return $staffRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <?php if (!is_club_admin()): ?>
        <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" required>
                <option value="">Select club</option>
                <?php foreach ($clubOptions as $club): ?>
                    <option value="<?php echo e((string) $club['id']); ?>" <?php echo (string) $fieldValue('club_id', (string) ($_GET['club_id'] ?? '')) === (string) $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>
    <?php endif; ?>
    <div class="col-md-6">
        <label class="form-label" for="full_name">Full name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'full_name') ? 'is-invalid' : ''; ?>" id="full_name" name="full_name" value="<?php echo e((string) $fieldValue('full_name')); ?>" required>
        <?php if (validation_error($formErrors, 'full_name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'full_name')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="role">Role</label>
        <select class="form-select <?php echo validation_error($formErrors, 'role') ? 'is-invalid' : ''; ?>" id="role" name="role" required>
            <?php foreach (staff_role_options() as $role): ?>
                <option value="<?php echo e($role); ?>" <?php echo (string) $fieldValue('role', 'coach') === $role ? 'selected' : ''; ?>><?php echo e(ucwords(str_replace('_', ' ', $role))); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'role')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'role')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="phone">Phone</label>
        <input class="form-control <?php echo validation_error($formErrors, 'phone') ? 'is-invalid' : ''; ?>" id="phone" name="phone" value="<?php echo e((string) $fieldValue('phone')); ?>">
        <?php if (validation_error($formErrors, 'phone')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'phone')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="email">Email</label>
        <input class="form-control <?php echo validation_error($formErrors, 'email') ? 'is-invalid' : ''; ?>" id="email" name="email" type="email" value="<?php echo e((string) $fieldValue('email')); ?>">
        <?php if (validation_error($formErrors, 'email')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'email')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="photo">Photo</label>
        <input class="form-control" id="photo" name="photo" type="file" accept=".jpg,.jpeg,.png,.webp,.gif">
    </div>
    <div class="col-md-6">
        <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 (staff_status_options() as $status): ?>
                <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('status', 'active') === $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>