<style>
.delete-stat-btn { display: none; }
.stat-header:hover .delete-stat-btn { display: inline-flex; }
</style>

<h2>Úprava statistik – <?= htmlspecialchars($team['nazev']) ?></h2>

<?php if (empty($statsMatrix['types'])): ?>
    <div class="alert alert-info">
        <i class="ti ti-info-circle"></i> V týmu nejsou definovány žádné statistiky
    </div>
    <a href="<?= BASE_URL ?>/team/show?id=<?= $team['id'] ?>" class="btn btn-secondary" title="Zpět">
        <i class="ti ti-arrow-back-up fs-4"></i>
    </a>
<?php else: ?>
    <?php if (empty($statsMatrix['members'])): ?>
        <div class="alert alert-warning">
            <i class="ti ti-alert-circle"></i> Tým nemá žádné členy
        </div>
        <a href="<?= BASE_URL ?>/team/show?id=<?= $team['id'] ?>" class="btn btn-secondary" title="Zpět">
            <i class="ti ti-arrow-back-up fs-4"></i>
        </a>
    <?php else: ?>

        <form method="post" action="<?= BASE_URL ?>/statistics/saveStats">
            <input type="hidden" name="team_id" value="<?= $team['id'] ?>">
                <div class="card mb-4">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle">
                                <thead class="">
                                    <tr>
                                        <th>Hráč</th>
                                        <?php foreach ($statsMatrix['types'] as $type): ?>
                                            <th class="text-center" style="min-width: 150px;">
                                                <div class="stat-header d-flex align-items-center justify-content-center gap-1">
                                                    <input type="text" name="type_names[<?= $type['id'] ?>]"
                                                        value="<?= htmlspecialchars($type['nazev']) ?>"
                                                        class="form-control form-control-sm text-center"
                                                        style="width: 100px;"
                                                        required>
                                                    <a href="<?= BASE_URL ?>/statistictype/delete?team_id=<?= $team['id'] ?>&id=<?= $type['id'] ?>"
                                                        title="Smazat statistiku" class="btn btn-sm btn-danger delete-stat-btn"
                                                        onclick="return confirm('Opravdu smazat statistiku &quot;<?= htmlspecialchars($type['nazev']) ?>&quot;?')">
                                                        <i class="ti ti-trash fs-4"></i>
                                                    </a>
                                                </div>
                                            </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;
                                                        $fieldName = "stats[{$member['id']}][{$type['id']}]";
                                                    ?>
                                                    <input type="number" name="<?= htmlspecialchars($fieldName) ?>" 
                                                        value="<?= (int)$value ?>" 
                                                        class="form-control form-control-sm text-center"
                                                        style="width: 80px; display: block; margin: 0 auto;"
                                                        min="0" required>
                                                </td>
                                            <?php endforeach; ?>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            
            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-success" title="Uložit">
                    <i class="ti ti-device-floppy fs-4"></i>
                </button>
                <a href="<?= BASE_URL ?>/team/show?id=<?= $team['id'] ?>#stats-tab" class="btn btn-outline-secondary" title="Zpět">
                    <i class="ti ti-arrow-back-up fs-4"></i>
                </a>
            </div>
        </form>
    <?php endif; ?>
<?php endif; ?>
