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

<?php $__template->section('page_actions'); ?>
<a href="/sa/attendance" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة</a>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/attendance/report" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المجموعة</label>
            <select name="group_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($groups as $g): ?>
                <option value="<?= (int) $g['id'] ?>" <?= ($filters['group_id'] ?? '') === (string) $g['id'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">رقم اللاعب</label>
            <input type="number" name="player_id" value="<?= e($filters['player_id'] ?? '') ?>" class="form-input" placeholder="ID">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">من تاريخ</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/sa/attendance/report" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Summary -->
<?php if ($summary): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(140px, 1fr));gap:12px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= (int) $summary['total'] ?></div>
        <div style="font-size:12px;color:#6B7280;">إجمالي السجلات</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#059669;"><?= (int) $summary['present_count'] ?></div>
        <div style="font-size:12px;color:#6B7280;">حاضر</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#DC2626;"><?= (int) $summary['absent_count'] ?></div>
        <div style="font-size:12px;color:#6B7280;">غائب</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#D97706;"><?= (int) $summary['late_count'] ?></div>
        <div style="font-size:12px;color:#6B7280;">متأخر</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#6366F1;"><?= (int) $summary['excused_count'] ?></div>
        <div style="font-size:12px;color:#6B7280;">معذور</div>
    </div>
</div>
<?php endif; ?>

<!-- Records Table -->
<?php if (!empty($records)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>التاريخ</th>
                    <th>الوقت</th>
                    <th>اللاعب</th>
                    <th>المجموعة</th>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($records as $r): ?>
                <tr>
                    <td><?= e($r['booking_date'] ?? '') ?></td>
                    <td><?= e($r['start_time'] ?? '') ?> - <?= e($r['end_time'] ?? '') ?></td>
                    <td style="font-weight:600;"><?= e($r['player_name'] ?? '—') ?></td>
                    <td><?= e($r['group_name'] ?? '—') ?></td>
                    <td>
                        <?php
                        $attStatusMap = [
                            'present' => ['حاضر', 'background:#ECFDF5;color:#059669;'],
                            'absent'  => ['غائب', 'background:#FEE2E2;color:#DC2626;'],
                            'late'    => ['متأخر', 'background:#FEF3C7;color:#D97706;'],
                            'excused' => ['معذور', 'background:#EDE9FE;color:#6366F1;'],
                        ];
                        $attInfo = $attStatusMap[$r['status'] ?? ''] ?? [$r['status'] ?? '', 'background:#F3F4F6;color:#374151;'];
                        ?>
                        <span style="<?= $attInfo[1] ?>padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;"><?= e($attInfo[0]) ?></span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php elseif ($summary === null): ?>
<div class="card" style="padding:40px;text-align:center;color:#6B7280;">
    <i data-lucide="bar-chart-2" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
    اختر فلاتر البحث لعرض تقرير الحضور
</div>
<?php else: ?>
<div class="card" style="padding:40px;text-align:center;color:#6B7280;">
    لا توجد سجلات حضور مطابقة
</div>
<?php endif; ?>

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