<?php 
    // show.php je vyvolán z teams tab, $poll obsahuje anketu
    // $userVote a $hasVoted jsou také dostupné
?>

<!-- Zpět tlačítko -->
<a href="<?= BASE_URL ?>/team/show?id=<?= $team['id'] ?>#polls-tab" title="Zpět" class="btn btn-sm btn-outline-secondary mb-3">
    <i class="ti ti-arrow-back-up fs-4"></i>
</a>

<!-- Anketa card -->
<div class="card border-0 shadow-sm">
    <div class="card-header bg-primary text-white border-0">
        <h4 class="mb-0">
            <i class="ti ti-poll"></i> <?= htmlspecialchars($poll['nazev'] ?? 'Anketa') ?>
        </h4>
        <small class="text-white-50">
            <?= date('d.m.Y H:i', strtotime($poll['datum_vytvoreni'] ?? 'now')) ?>
        </small>
    </div>
    
    <div class="card-body p-4">
        <!-- Stavy ankety -->
        <div class="mb-4">
            <?php if ($poll['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řená</span>
            <?php endif; ?>
        </div>

        <!-- Hlasovací možnosti -->
        <form method="post" action="<?= BASE_URL ?>/anketa/vote">
            <input type="hidden" name="poll_id" value="<?= $poll['id'] ?>">
            
            <div class="d-flex flex-column gap-2">
                <?php 
                    $totalVotes = array_sum(array_column($poll['options'] ?? [], 'votes_count'));
                ?>
                <?php foreach ($poll['options'] ?? [] as $option): ?>
                    <label class="option-card p-3 rounded-3 border-2 cursor-pointer">
                        <div class="d-flex align-items-start">
                            <input class="form-check-input mt-1 me-3" type="radio" 
                                   name="option_id" 
                                   value="<?= $option['id'] ?>"
                                   <?= ($userVote === (int)$option['id']) ? 'checked' : '' ?>
                                   <?= !$poll['aktivni'] ? 'disabled' : '' ?>>
                            
                            <div class="flex-grow-1">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <strong class="fs-5"><?= htmlspecialchars($option['text_moznost']) ?></strong>
                                    <span class="badge bg-info rounded-pill">
                                        <?= $option['votes_count'] ?>
                                    </span>
                                </div>
                                
                                <!-- Progress bar -->
                                <?php 
                                    $percentage = $totalVotes > 0 ? ($option['votes_count'] / $totalVotes) * 100 : 0;
                                ?>
                                <div class="progress" style="height: 8px;">
                                    <div class="progress-bar bg-success" role="progressbar" 
                                         style="width: <?= $percentage ?>%;" 
                                         aria-valuenow="<?= round($percentage) ?>" 
                                         aria-valuemin="0" 
                                         aria-valuemax="100"></div>
                                </div>
                                
                                <!-- Text počtu -->
                                <small class="text-muted d-block mt-2">
                                    <strong><?= round($percentage, 0) ?>%</strong> 
                                    (<?= $option['votes_count'] ?> <?= $option['votes_count'] == 1 ? 'sportovec' : ($option['votes_count'] > 4 ? 'sportovců' : 'sportovci') ?>)
                                </small>
                            </div>
                        </div>
                    </label>
                <?php endforeach; ?>
            </div>

            <!-- Hlasovací tlačítko -->
            <?php if ($poll['aktivni']): ?>
                <div class="d-grid gap-2 mt-4">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="ti ti-send"></i>
                        <?= $hasVoted ? 'Změnit hlas' : 'Hlasovat' ?>
                    </button>
                </div>
            <?php else: ?>
                <div class="alert alert-warning mt-4 alert-dismissible fade show">
                    <i class="ti ti-lock"></i>
                    <strong>Uzavřeno</strong> - Hlasování již není možné
                </div>
            <?php endif; ?>
        </form>

        <!-- Správa ankety (pro trenéra/asistenta/admin) -->
        <?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) && $poll['aktivni']): ?>
            <div class="mt-4 pt-4 border-top">
                <a href="<?= BASE_URL ?>/anketa/deactivate?id=<?= $poll['id'] ?>&team_id=<?= $team['id'] ?>" 
                   class="btn btn-warning btn-sm" 
                   onclick="return confirm('Opravdu chcete uzavřít tuto anketu? Hlasování již nebude možné.')">
                    <i class="ti ti-lock"></i> Uzavřít anketu
                </a>
            </div>
        <?php endif; ?>
    </div>
</div>

<style>
.option-card {
    border-color: #dee2e6;
    transition: all 0.3s ease;
    cursor: pointer;
}

.option-card:hover {
    border-color: #0d6efd;
    background-color: #f8f9ff;
    transform: translateX(4px);
}

.option-card input[type="radio"]:checked + * {
    color: #0d6efd;
}

.option-card input[type="radio"] {
    cursor: pointer;
}

.cursor-pointer {
    cursor: pointer;
}
</style>
