<?php
    $user = \App\Core\Auth::user();
    // $isAdmin, $isCoach, $isAssistant jsou dostupné z teams/show.php
    $canManagePolls = ($isAdmin ?? false) || ($isCoach ?? false) || ($isAssistant ?? false);
?>

<div class="mb-3">
    <?php if ($canManagePolls): ?>
        <a href="<?= BASE_URL ?>/anketa/create?team_id=<?= $team['id'] ?>" class="btn btn-primary">
            <i class="ti ti-plus"></i> Nová anketa
        </a>
    <?php endif; ?>
</div>

<?php if (empty($ankety)): ?>
    <div class="alert alert-info">
        <i class="ti ti-info-circle"></i>
        V týmu zatím nejsou žádné ankety.
    </div>
<?php else: ?>
    <div class="list-group">
        <?php foreach ($ankety as $anketa): ?>
            <a href="<?= BASE_URL ?>/anketa/show?id=<?= $anketa['id'] ?>" class="list-group-item list-group-item-action p-3">
                <div class="d-flex justify-content-between align-items-start">
                    <div class="flex-grow-1">
                        <h6 class="mb-1 fw-bold">
                            <?= htmlspecialchars($anketa['nazev']) ?>
                        </h6>
                        <small class="text-muted d-block">
                            <i class="ti ti-calendar"></i>
                            <?= date('d.m.Y H:i', strtotime($anketa['datum_vytvoreni'])) ?>
                        </small>
                    </div>
                    <div class="text-end">
                        <?php if ($anketa['aktivni']): ?>
                            <span class="badge bg-success"><i class="ti ti-circle-filled"></i> Aktivní</span>
                        <?php else: ?>
                            <span class="badge bg-danger"><i class="ti ti-lock"></i> Uzavřena</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="mt-2">
                    <small class="text-muted">
                        <i class="ti ti-check"></i>
                        <?php 
                            $totalVotes = 0;
                            if (!empty($anketa['options'])) {
                                $totalVotes = array_sum(array_column($anketa['options'], 'votes_count'));
                            }
                        ?>
                        <?= $totalVotes ?> hlasů
                    </small>
                </div>
            </a>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
