<?php
$userRecord = $userRecord ?? null;
$roleOptions = $roleOptions ?? user_role_options();
$clubOptions = $clubOptions ?? user_club_options();
$formErrors = $formErrors ?? [];
$isEdit = $userRecord !== null;

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

    return $userRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <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-6">
        <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')); ?>" required>
        <?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="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-3">
        <label class="form-label" for="role_id">Role</label>
        <select class="form-select <?php echo validation_error($formErrors, 'role_id') ? 'is-invalid' : ''; ?>" id="role_id" name="role_id" required>
            <option value="">Select role</option>
            <?php foreach ($roleOptions as $role): ?>
                <option value="<?php echo e((string) $role['id']); ?>" <?php echo (string) $fieldValue('role_id') === (string) $role['id'] ? 'selected' : ''; ?>><?php echo e($role['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'role_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'role_id')); ?></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>
            <option value="active" <?php echo (string) $fieldValue('status', 'active') === 'active' ? 'selected' : ''; ?>>Active</option>
            <option value="inactive" <?php echo (string) $fieldValue('status', 'active') === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
        </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="club_id">Assigned 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</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (string) $fieldValue('club_id') === (string) $club['id'] ? 'selected' : ''; ?>><?php echo e($club['name']); ?> (<?php echo e($club['short_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 class="form-text">Required when the selected role is Club Admin.</div>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="password"><?php echo $isEdit ? 'New password' : 'Password'; ?></label>
        <input class="form-control <?php echo validation_error($formErrors, 'password') ? 'is-invalid' : ''; ?>" id="password" name="password" type="password" <?php echo $isEdit ? '' : 'required'; ?>>
        <?php if (validation_error($formErrors, 'password')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'password')); ?></div><?php endif; ?>
        <div class="form-text"><?php echo $isEdit ? 'Leave blank to keep the current password.' : 'Use at least 8 characters.'; ?></div>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="password_confirmation"><?php echo $isEdit ? 'Confirm new password' : 'Confirm password'; ?></label>
        <input class="form-control <?php echo validation_error($formErrors, 'password_confirmation') ? 'is-invalid' : ''; ?>" id="password_confirmation" name="password_confirmation" type="password" <?php echo $isEdit ? '' : 'required'; ?>>
        <?php if (validation_error($formErrors, 'password_confirmation')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'password_confirmation')); ?></div><?php endif; ?>
    </div>
</div>