<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>سجل البوابة<?php $__template->endSection(); ?>

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

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

<?php if (isset($summary)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(140px, 1fr));gap:12px;margin-bottom:20px;">
    <div class="card" style="padding:14px;text-align:center;"><div style="font-size:11px;color:#6B7280;">إجمالي</div><div style="font-size:20px;font-weight:800;"><?= (int) ($summary['total_scans'] ?? 0) ?></div></div>
    <div class="card" style="padding:14px;text-align:center;"><div style="font-size:11px;color:#6B7280;">دخول</div><div style="font-size:20px;font-weight:800;color:#059669;"><?= (int) ($summary['entries_granted'] ?? 0) ?></div></div>
    <div class="card" style="padding:14px;text-align:center;"><div style="font-size:11px;color:#6B7280;">مرفوض</div><div style="font-size:20px;font-weight:800;color:#DC2626;"><?= (int) ($summary['entries_denied'] ?? 0) ?></div></div>
    <div class="card" style="padding:14px;text-align:center;"><div style="font-size:11px;color:#6B7280;">لاعبين</div><div style="font-size:20px;font-weight:800;color:#2563EB;"><?= (int) ($summary['unique_players'] ?? 0) ?></div></div>
</div>
<?php endif; ?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;align-items:end;gap:15px;flex-wrap:wrap;">
            <div class="form-group" style="margin:0;min-width:150px;">
                <label class="form-label" style="font-size:12px;">التاريخ</label>
                <input type="date" name="date" value="<?= e($filters['date'] ?? date('Y-m-d')) ?>" class="form-input" style="direction:ltr;">
            </div>
            <div class="form-group" style="margin:0;min-width:130px;">
                <label class="form-label" style="font-size:12px;">النتيجة</label>
                <select name="granted" class="form-select">
                    <option value="">— الكل —</option>
                    <option value="1" <?= ($filters['granted'] ?? '') === '1' ? 'selected' : '' ?>>مسموح</option>
                    <option value="0" <?= ($filters['granted'] ?? '') === '0' ? 'selected' : '' ?>>مرفوض</option>
                </select>
            </div>
            <div class="form-group" style="margin:0;min-width:180px;">
                <label class="form-label" style="font-size:12px;">بحث</label>
                <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" class="form-input" placeholder="اسم أو رقم كارت...">
            </div>
            <button type="submit" class="btn btn-primary" style="padding:9px 20px;"><i data-lucide="search" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> بحث</button>
        </form>
    </div>
</div>

<!-- Log Table -->
<div class="card">
    <table style="width:100%;border-collapse:collapse;font-size:13px;">
        <thead>
            <tr style="background:#F9FAFB;">
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">الوقت</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">اللاعب</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">النوع</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">الكارت</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">العملية</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">النتيجة</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">السبب</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($entries)): ?>
            <tr><td colspan="7" style="padding:30px;text-align:center;color:#9CA3AF;">لا توجد سجلات</td></tr>
            <?php else: ?>
            <?php foreach ($entries as $entry): ?>
            <tr style="border-top:1px solid #F3F4F6;">
                <td style="padding:10px 15px;direction:ltr;text-align:right;"><?= e(substr($entry['recorded_at'], 11, 5)) ?></td>
                <td style="padding:10px 15px;"><?= e($entry['full_name_ar']) ?></td>
                <td style="padding:10px 15px;font-size:11px;"><?= ($entry['player_type'] ?? '') === 'member' ? 'عضو' : 'غير عضو' ?></td>
                <td style="padding:10px 15px;direction:ltr;text-align:right;font-size:12px;"><?= e($entry['card_number'] ?? '—') ?></td>
                <td style="padding:10px 15px;"><?= $entry['access_type'] === 'entry' ? 'دخول' : 'خروج' ?></td>
                <td style="padding:10px 15px;">
                    <?php if ((int) $entry['granted']): ?>
                    <span style="padding:3px 8px;border-radius:4px;font-size:11px;font-weight:600;background:#ECFDF5;color:#059669;">مسموح</span>
                    <?php else: ?>
                    <span style="padding:3px 8px;border-radius:4px;font-size:11px;font-weight:600;background:#FEF2F2;color:#DC2626;">مرفوض</span>
                    <?php endif; ?>
                </td>
                <td style="padding:10px 15px;font-size:12px;color:#6B7280;"><?= e($entry['denial_reason'] ?? '') ?></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php if (isset($pagination) && $pagination && $pagination['total_pages'] > 1): ?>
<div style="margin-top:15px;display:flex;justify-content:center;gap:5px;">
    <?php for ($p = 1; $p <= $pagination['total_pages']; $p++): ?>
    <a href="?<?= http_build_query(array_merge($filters, ['page' => $p])) ?>" class="btn <?= $p === $pagination['current_page'] ? 'btn-primary' : 'btn-outline' ?>" style="padding:6px 12px;font-size:12px;"><?= $p ?></a>
    <?php endfor; ?>
</div>
<?php endif; ?>

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