<?php if (!empty($error)): ?>
    <div class="alert alert-warning text-center" role="alert">
        <i class="fas fa-exclamation-triangle me-2"></i><?= htmlspecialchars($error) ?>
    </div>
<?php endif; ?>


    <div class="d-flex justify-content-between align-items-center mb-2">
        <h5 class="mb-0">Deník docházky</h5>
        <button class="btn btn-outline-secondary btn-sm no-print" onclick="window.print()" title="Tisk">
            <i class="ti ti-printer fs-5"></i>
        </button>
    </div>
    <div class="table-responsive">
            <table class="table table-sm table-hover align-middle" id="TableAttendance">
                <thead>
                    <tr>
                        <th>Datum</th>
                        <th>Typ</th>
                        <th class="text-center">Účastníků</th>
                        <th class="text-center">Přítomných</th>
                        <th class="text-center">Nepřítomných</th>
                        <th class="text-center">Omluvených</th>
                        <th class="text-center">Pozdních</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($meetings) && is_array($meetings)): 
                        foreach ($meetings as $meeting):
                            $summary = $attendanceSummary[$meeting['id']] ?? [
                                'celkem' => '-', 
                                'pritomny' => '-', 
                                'nepritomny' => '-', 
                                'omluven' => '-', 
                                'pozde' => '-'
                            ];
                            $percentage = ($summary['celkem'] > 0) 
                                ? round(($summary['pritomny'] / $summary['celkem']) * 100, 0) 
                                : 0;
                    ?>
                        <tr class="attendance-row" style="cursor: pointer;" onclick="window.location='<?= BASE_URL ?>/dochazka/show?id=<?= $meeting['id'] ?>';">
                            <td class="text-nowrap" style="text-decoration: underline;">
                                <?= date('d.m.Y', strtotime($meeting['datum'])) ?>
                            </td>
                            <td>
                                <?php 
                                switch($meeting['typ'] ?? '') {
                                    case 'trenink':
                                        echo '<span class="badge bg-success">Trénink</span>';
                                        break;
                                    case 'zapas':
                                        echo '<span class="badge bg-danger">Zápas</span>';
                                        break;
                                    default:
                                        echo '<span class="badge bg-secondary">' . htmlspecialchars($meeting['typ'] ?? '-') . '</span>';
                                }
                                ?>
                            </td>
                            <td class="text-center">
                                <?php
                                    $pritomni = ($summary['pritomny'] !== '-' ? (int)$summary['pritomny'] : 0)
                                              + ($summary['pozde']    !== '-' ? (int)$summary['pozde']    : 0);
                                    $celkem   = $summary['celkem'];
                                    echo $celkem !== '-' ? $pritomni . ' z ' . $celkem : '-';
                                ?>
                            </td>
                            <td class="text-center">
                                <span><?= $summary['pritomny'] ?></span>
                            </td>
                            <td class="text-center">
                                <span><?= $summary['nepritomny'] ?></span>
                            </td>
                            <td class="text-center">
                                <span><?= $summary['omluven'] ?></span>
                            </td>
                            <td class="text-center">
                                <span><?= $summary['pozde'] ?></span>
                            </td>
                        </tr>
                    <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>