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

<div class="d-flex justify-content-between align-items-center mb-3">
    <h5>Statistiky týmu</h5>
    <div class="d-flex gap-2">
        <?php if ($canEdit): ?>
            <a href="<?= BASE_URL ?>/statistics/editStats?team_id=<?= $team['id'] ?>" title="Upravit pro celý tým" class="btn btn-sm btn-success">
                <i class="ti ti-edit fs-4"></i>
            </a>
            <a href="<?= BASE_URL ?>/statistictype/create?team_id=<?= $team['id'] ?>" title="Přidat typ statistiky" class="btn btn-sm btn-primary">
                <i class="ti ti-plus fs-4"></i>
            </a>
        <?php endif; ?>
    </div>
</div>

<?php if (empty($statsMatrix['types'])): ?>
    <div class="alert alert-info alert-sm mb-0">
        <i class="ti ti-info-circle"></i> Zatím není definován žádný typ statistiky
    </div>
<?php else: ?>
    <?php if (empty($statsMatrix['members'])): ?>
        <div class="alert alert-warning alert-sm mb-0">
            <i class="ti ti-alert-circle"></i> Tým nemá žádné členy
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table table-sm table-hover align-middle">
                <thead>
                    <tr>
                        <th>Hráč</th>
                        <?php foreach ($statsMatrix['types'] as $type): ?>
                            <th class="text-center" style="min-width: 80px;">
                                <small><?= htmlspecialchars($type['nazev']) ?></small>
                            </th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($statsMatrix['members'] as $member): ?>
                        <tr>
                            <td>
                                <?= htmlspecialchars($member['jmeno'] . ' ' . $member['prijmeni']) ?>
                            </td>
                            <?php foreach ($statsMatrix['types'] as $type): ?>
                                <td class="text-center">
                                    <?php 
                                        $value = $statsMatrix['values'][$member['id']][$type['id']] ?? 0;
                                    ?>
                                    <span><?= (int)$value ?></span>
                                </td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
<?php endif; ?>
