<div class="mb-3">
    <h3>Přidat člena do týmu: <?= htmlspecialchars($team['nazev']) ?></h3>
</div>

<div class="card">
    <div class="card-body">
        <?php if (count($availableUsers) > 0): ?>
            <form method="post" action="">
                <div class="mb-3">
                    <label for="uzivatel_id" class="form-label">Uživatel *</label>
                    <select name="uzivatel_id" id="uzivatel_id" class="form-select" required>
                        <option value="">-- Vyberte uživatele --</option>
                        <?php foreach($availableUsers as $user): ?>
                            <option value="<?= $user['id'] ?>">
                                <?= htmlspecialchars($user['jmeno'] . ' ' . $user['prijmeni']) ?> 
                                (<?= htmlspecialchars($user['uzivatelske_jmeno']) ?>) - 
                                <?php 
                                    $roleLabel = [
                                        'admin' => 'Správce',
                                        'trener' => 'Trenér',
                                        'sportovec' => 'Sportovec'
                                    ];
                                    echo $roleLabel[$user['role']] ?? $user['role'];
                                ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <?php $currentUser = \App\Core\Auth::user(); $isAdmin = $currentUser['role'] === 'admin'; ?>
                <div class="mb-3">
                    <label for="role_v_tymu" class="form-label">Role v týmu *</label>
                    <select name="role_v_tymu" id="role_v_tymu" class="form-select" required>
                        <option value="hrac">Hráč</option>
                        <option value="asistent">Asistent</option>
                        <?php if ($isAdmin): ?>
                            <option value="trener" id="option-trener">Trenér</option>
                        <?php endif; ?>
                    </select>
                </div>

                <div class="mb-3">
                    <button type="submit" title="Uložit" class="btn btn-success"><i class="ti ti-device-floppy fs-4"></i></button>
                    <a href="<?= BASE_URL ?>/team/show?id=<?= $team['id'] ?>" class="btn btn-outline-secondary" title="Zpět"><i class="ti ti-arrow-back-up fs-4"></i></a>
                </div>
            </form>

<?php if ($isAdmin): ?>
<script>
const userRoles = <?= json_encode($userRoles ?? []) ?>;

const userSelect = document.getElementById('uzivatel_id');
const roleSelect = document.getElementById('role_v_tymu');
const trenerOption = document.getElementById('option-trener');

function updateRoleOptions() {
    const selectedUserId = parseInt(userSelect.value);
    const role = userRoles[selectedUserId] ?? null;
    const isSportovec = role === 'sportovec';

    trenerOption.disabled = isSportovec;
    trenerOption.hidden = isSportovec;

    if (isSportovec && roleSelect.value === 'trener') {
        roleSelect.value = 'hrac';
    }
}

userSelect.addEventListener('change', updateRoleOptions);
updateRoleOptions();
</script>
<?php endif; ?>
        <?php else: ?>
            <div class="alert alert-info">
                <p>Všichni uživatelé jsou již členy tohoto týmu.</p>
                <a href="<?= BASE_URL ?>/team/show?id=<?= $team['id'] ?>" class="btn btn-outline-secondary" title="Zpět"><i class="ti ti-arrow-back-up fs-4"></i></a>
            </div>
        <?php endif; ?>
    </div>
</div>
