<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تسجيل الحضور<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/attendance/report" class="btn btn-outline"><i data-lucide="bar-chart-2" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تقرير الحضور</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:15px;padding:15px;background:#F0FDF4;border-right:4px solid #059669;">
    <strong>حصص التدريب اليوم:</strong> <?= e($today) ?>
</div>

<!-- Filters -->
<div class="card" style="margin-bottom:15px;padding:12px 15px;">
    <form method="GET" action="/sa/attendance" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">النشاط الرياضي</label>
            <select name="discipline_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($disciplines as $d): ?>
                <option value="<?= (int) $d['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المدرب</label>
            <select name="coach_id" class="form-select" data-searchable="true" data-placeholder="-- الكل --">
                <option value="">-- الكل --</option>
                <?php foreach ($coaches as $c): ?>
                <option value="<?= (int) $c['id'] ?>" <?= ($filters['coach_id'] ?? '') == $c['id'] ? 'selected' : '' ?>><?= e($c['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="filter" style="width:14px;height:14px;vertical-align:middle;"></i> تصفية</button>
        <?php if (($filters['discipline_id'] ?? '') !== '' || ($filters['coach_id'] ?? '') !== ''): ?>
        <a href="/sa/attendance" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
        <?php endif; ?>
    </form>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الوقت</th>
                    <th>المجموعة</th>
                    <th>المدرب</th>
                    <th>المرفق</th>
                    <th>الوحدة</th>
                    <th>الحالة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($bookings)): ?>
                <?php foreach ($bookings as $b): ?>
                <tr>
                    <td style="font-weight:600;white-space:nowrap;">
                        <?= e($b['start_time'] ?? '') ?> - <?= e($b['end_time'] ?? '') ?>
                    </td>
                    <td><?= e($b['group_name'] ?? '—') ?></td>
                    <td><?= e($b['coach_name'] ?? '—') ?></td>
                    <td><?= e($b['facility_name'] ?? '—') ?></td>
                    <td><?= e($b['unit_name'] ?? '—') ?></td>
                    <td>
                        <?php
                        $statusMap = ['pending' => 'في الانتظار', 'confirmed' => 'مؤكد', 'checked_in' => 'تم الحضور', 'completed' => 'مكتمل'];
                        $statusLabel = $statusMap[$b['status'] ?? ''] ?? ($b['status'] ?? '');
                        ?>
                        <span style="background:#ECFDF5;color:#059669;padding:3px 10px;border-radius:10px;font-size:12px;"><?= e($statusLabel) ?></span>
                    </td>
                    <td>
                        <?php if (can('sa.attendance.manage')): ?>
                        <a href="/sa/attendance/record/<?= (int) $b['id'] ?>" class="btn btn-sm btn-primary" style="font-size:12px;padding:4px 12px;">
                            <i data-lucide="clipboard-check" style="width:13px;height:13px;vertical-align:middle;margin-left:4px;"></i> تسجيل الحضور
                        </a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="7" style="text-align:center;padding:40px;color:#6B7280;">
                        <i data-lucide="calendar-x" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        لا توجد حصص تدريب اليوم
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') { lucide.createIcons(); }
});
</script>
<?php $__template->endSection(); ?>
