<div class="mb-3 d-flex justify-content-between align-items-start">
    <div>
        <h3 class="mb-0"><?= htmlspecialchars($team['nazev']) ?></h3>
        <p class="text-muted mb-0">Vytvořeno: <?= date('d.m.Y H:i', strtotime($team['vytvoreno'])) ?></p>
    </div>
    <div class="no-print">
        <?php
            $user = \App\Core\Auth::user();
            $isAdmin = $user['role'] === 'admin';
            $isCoach = false;
            $isAssistant = false;
            foreach ($members as $member) {
                if ($member['id'] == $user['id']) {
                    if ($member['role_v_tymu'] === 'trener') {
                        $isCoach = true;
                    } elseif ($member['role_v_tymu'] === 'asistent') {
                        $isAssistant = true;
                    }
                    break;
                }
            }
        ?>

        <?php if ($isAdmin || $isCoach || $isAssistant): ?>
            <a href="<?= BASE_URL ?>/team/edit?id=<?= $team['id'] ?>" title="Upravit" class="btn btn-sm btn-success"><i class="ti ti-edit fs-4"></i></a>
        <?php endif; ?>
        <?php if ($isAdmin || $isCoach): ?>
            <a href="<?= BASE_URL ?>/team/delete?id=<?= $team['id'] ?>" title="Smazat tým" class="btn btn-sm btn-danger" onclick="return confirm('Opravdu smazat tým?')"><i class="ti ti-trash fs-4"></i></a>
        <?php endif; ?>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <ul class="nav nav-tabs card-header-tabs no-print" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="members-tab" data-bs-toggle="tab" data-bs-target="#members-pane" type="button" role="tab" aria-controls="members-pane" aria-selected="false">
                    Členové (<?= count($members) ?>)
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="attendance-tab" data-bs-toggle="tab" data-bs-target="#attendance-pane" type="button" role="tab" aria-controls="attendance-pane" aria-selected="false">
                    Docházka
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="chat-tab" data-bs-toggle="tab" data-bs-target="#chat-pane" type="button" role="tab" aria-controls="chat-pane" aria-selected="false">
                    Chat
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="stats-tab" data-bs-toggle="tab" data-bs-target="#stats-pane" type="button" role="tab" aria-controls="stats-pane" aria-selected="false">
                    Statistiky
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="polls-tab" data-bs-toggle="tab" data-bs-target="#polls-pane" type="button" role="tab" aria-controls="polls-pane" aria-selected="false">
                    Ankety
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="team-description-tab" data-bs-toggle="tab" data-bs-target="#team-description-pane" type="button" role="tab" aria-controls="team-description-pane" aria-selected="true">
                    Popis
                </button>
            </li>
        </ul>
    </div>

    <div class="card-body">
        <div class="tab-content">


            <!-- členové -->
            <div class="tab-pane fade show active" id="members-pane" role="tabpanel" aria-labelledby="members-tab">
                <?php if ($isAdmin || $isCoach): ?>
                    <div class="mb-3">
                        <a href="<?= BASE_URL ?>/team/addMember?id=<?= $team['id'] ?>" title="Přidat člena" class="btn btn-primary"><i class="ti ti-user-plus fs-4"></i></a>
                    </div>
                <?php endif; ?>

                <?php if (count($members) > 0): ?>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle" id="TableMembers">
                            <thead>
                                <tr>
                                    <th>Jméno</th>
                                    <th>Příjmení</th>                                   
                                    <th>Uživatelské jméno</th>
                                    <th>Role v systému</th>
                                    <th>Role v týmu</th>
                                    <?php if ($isAdmin || $isCoach): ?>
                                        <th>Akce</th>
                                    <?php endif; ?>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($members as $member): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($member['jmeno']) ?></td>
                                        <td><?= htmlspecialchars($member['prijmeni']) ?></td>
                                        <td><?= htmlspecialchars($member['uzivatelske_jmeno']) ?></td>
                                        <td>
                                            <?php 
                                                $roleLabel = [
                                                    'admin' => 'Správce',
                                                    'trener' => 'Trenér',
                                                    'sportovec' => 'Sportovec'
                                                ];
                                                echo $roleLabel[$member['role']] ?? $member['role'];
                                            ?>
                                        </td>
                                        <td>
                                            <?php 
                                                $roleInTeamLabel = [
                                                    'trener' => 'Trenér',
                                                    'asistent' => 'Asistent',
                                                    'hrac' => 'Hráč'
                                                ];
                                                echo $roleInTeamLabel[$member['role_v_tymu']] ?? $member['role_v_tymu'];
                                            ?>
                                        </td>
                                        <?php if ($isAdmin || $isCoach): ?>
                                            <td>
                                                <?php if ($member['id'] != $user['id']): ?>
                                                    <a href="<?= BASE_URL ?>/team/removeMember?id=<?= $team['id'] ?>&member=<?= $member['clenstvo_id'] ?>" title="Odebrat člena" class="btn btn-sm btn-danger" onclick="return confirm('Opravdu odebrat člena?')"><i class="ti ti-trash fs-4"></i></a>
                                                <?php endif; ?>
                                            </td>
                                        <?php endif; ?>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info">V týmu nejsou žádní členové. <a href="<?= BASE_URL ?>/team/addMember?id=<?= $team['id'] ?>">Přidat člena</a></div>
                <?php endif; ?>
            </div>

            <!-- docházka -->
            <div class="tab-pane fade" id="attendance-pane" role="tabpanel" aria-labelledby="attendance-tab">
                <?php include __DIR__ . '/dochazka/index.php'; ?>
            </div>

            <!-- chat -->
            <div class="tab-pane fade" id="chat-pane" role="tabpanel" aria-labelledby="chat-tab">
                <div style="margin-top: 15px;">
                    <?php include __DIR__ . '/chat.php'; ?>
                </div>
            </div>

            <!-- statistiky -->
            <div class="tab-pane fade" id="stats-pane" role="tabpanel" aria-labelledby="stats-tab">
                <?php include __DIR__ . '/statistiky/index.php'; ?>
            </div>

            <!-- ankety -->
            <div class="tab-pane fade" id="polls-pane" role="tabpanel" aria-labelledby="polls-tab">
                <?php include __DIR__ . '/ankety/index.php'; ?>
            </div>

            <!-- popis -->
            <div class="tab-pane fade" id="team-description-pane" role="tabpanel" aria-labelledby="team-description-tab">
                <div class="row">
                    <div class="col-md-8">
                        <h5>Popis</h5>
                        <p><?= htmlspecialchars($team['popis'] ?? 'Žádný popis') ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="mt-3 no-print">
    <a href="<?= BASE_URL ?>/team/index" class="btn btn-outline-secondary" title="Zpět"><i class="ti ti-arrow-back-up fs-4"></i></a>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Aktivuj kartu na základě anchor tag v URL
    const hash = window.location.hash;
    if (hash) {
        const tabElement = document.querySelector(hash);
        if (tabElement) {
            const tab = new bootstrap.Tab(tabElement);
            tab.show();
        }
    }
});
</script>

