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

    return $playerRecord[$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']); ?>
                        <?php if (!empty($club['short_name'])): ?>
                            (<?php echo e($club['short_name']); ?>)
                        <?php endif; ?>
                    </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-3">
        <label class="form-label" for="first_name">First name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'first_name') ? 'is-invalid' : ''; ?>" id="first_name" name="first_name" value="<?php echo e((string) $fieldValue('first_name')); ?>" required>
        <?php if (validation_error($formErrors, 'first_name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'first_name')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="last_name">Last name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'last_name') ? 'is-invalid' : ''; ?>" id="last_name" name="last_name" value="<?php echo e((string) $fieldValue('last_name')); ?>" required>
        <?php if (validation_error($formErrors, 'last_name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'last_name')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="date_of_birth">Date of birth</label>
        <input class="form-control <?php echo validation_error($formErrors, 'date_of_birth') ? 'is-invalid' : ''; ?>" id="date_of_birth" name="date_of_birth" type="date" value="<?php echo e((string) $fieldValue('date_of_birth')); ?>" required>
        <?php if (validation_error($formErrors, 'date_of_birth')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'date_of_birth')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="nationality">Nationality</label>
        <input class="form-control" id="nationality" name="nationality" value="<?php echo e((string) $fieldValue('nationality')); ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label" for="position">Position</label>
        <select class="form-select <?php echo validation_error($formErrors, 'position') ? 'is-invalid' : ''; ?>" id="position" name="position" required>
            <?php foreach (player_position_options() as $position): ?>
                <option value="<?php echo e($position); ?>" <?php echo (string) $fieldValue('position', 'GK') === $position ? 'selected' : ''; ?>><?php echo e($position); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'position')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'position')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="jersey_number">Jersey number</label>
        <input class="form-control <?php echo validation_error($formErrors, 'jersey_number') ? 'is-invalid' : ''; ?>" id="jersey_number" name="jersey_number" type="number" min="0" max="99" value="<?php echo e((string) $fieldValue('jersey_number')); ?>">
        <?php if (validation_error($formErrors, 'jersey_number')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'jersey_number')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="nin">NIN</label>
        <input class="form-control <?php echo validation_error($formErrors, 'nin') ? 'is-invalid' : ''; ?>" id="nin" name="nin" value="<?php echo e((string) $fieldValue('nin')); ?>" inputmode="numeric" maxlength="11" required>
        <?php if (validation_error($formErrors, 'nin')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'nin')); ?></div><?php endif; ?>
        <div class="form-text">Use the player's 11-digit NIN as the match key.</div>
    </div>
    <div class="col-md-4">
        <label class="form-label">Player ID</label>
        <div class="form-control bg-light d-flex align-items-center"><?php echo e((string) $fieldValue('registration_number', 'Generated automatically after save')); ?></div>
        <div class="form-text">Generated from the club short name, for example SH-FC001.</div>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="photo">Photo</label>
        <input class="form-control <?php echo validation_error($formErrors, 'photo') ? 'is-invalid' : ''; ?>" id="photo" name="photo" type="file" accept=".jpg,.jpeg,.png,.webp,.gif">
        <?php if (validation_error($formErrors, 'photo')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'photo')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="medical_status">Medical status</label>
        <select class="form-select <?php echo validation_error($formErrors, 'medical_status') ? 'is-invalid' : ''; ?>" id="medical_status" name="medical_status">
            <?php foreach (player_medical_options() as $medicalStatus): ?>
                <option value="<?php echo e($medicalStatus); ?>" <?php echo (string) $fieldValue('medical_status', 'pending') === $medicalStatus ? 'selected' : ''; ?>><?php echo e(ucfirst($medicalStatus)); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'medical_status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'medical_status')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="transfer_status">Transfer status</label>
        <select class="form-select <?php echo validation_error($formErrors, 'transfer_status') ? 'is-invalid' : ''; ?>" id="transfer_status" name="transfer_status">
            <?php foreach (player_transfer_options() as $transferStatus): ?>
                <option value="<?php echo e($transferStatus); ?>" <?php echo (string) $fieldValue('transfer_status', 'active') === $transferStatus ? 'selected' : ''; ?>><?php echo e(ucwords(str_replace('_', ' ', $transferStatus))); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'transfer_status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'transfer_status')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <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 (player_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>
    <?php if (can_manage_player_approval()): ?>
        <div class="col-md-4">
            <label class="form-label" for="eligibility_status">Eligibility status</label>
            <select class="form-select <?php echo validation_error($formErrors, 'eligibility_status') ? 'is-invalid' : ''; ?>" id="eligibility_status" name="eligibility_status">
                <?php foreach (player_eligibility_options() as $eligibilityStatus): ?>
                    <option value="<?php echo e($eligibilityStatus); ?>" <?php echo (string) $fieldValue('eligibility_status', 'pending') === $eligibilityStatus ? 'selected' : ''; ?>><?php echo e(ucfirst($eligibilityStatus)); ?></option>
                <?php endforeach; ?>
            </select>
            <?php if (validation_error($formErrors, 'eligibility_status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'eligibility_status')); ?></div><?php endif; ?>
        </div>
    <?php else: ?>
        <div class="col-md-4">
            <label class="form-label">Eligibility status</label>
            <div class="form-control bg-light d-flex align-items-center"><?php echo e(ucfirst((string) $fieldValue('eligibility_status', 'pending'))); ?></div>
            <div class="form-text">Only authorized league staff can change player approval status.</div>
        </div>
    <?php endif; ?>
</div>