<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>سجل دخول الضيوف<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/carnets/guest-entries/create" class="btn btn-primary">+ تسجيل دخول ضيف</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/carnets/guest-entries" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div><label class="form-label" style="font-size:12px;">بحث</label><input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" placeholder="اسم الضيف أو الهاتف..." class="form-input" style="min-width:180px;"></div>
        <div><label class="form-label" style="font-size:12px;">المرفق</label><select name="facility_id" class="form-select"><option value="">الكل</option><?php foreach ($facilities as $f): ?><option value="<?= (int) $f['id'] ?>" <?= ($filters['facility_id'] ?? '') == $f['id'] ? 'selected' : '' ?>><?= e($f['name_ar']) ?></option><?php endforeach; ?></select></div>
        <div><label class="form-label" style="font-size:12px;">النشاط</label><select name="activity_type" class="form-select"><option value="">الكل</option><?php foreach ($activityTypes as $k => $v): ?><option value="<?= e($k) ?>" <?= ($filters['activity_type'] ?? '') === $k ? 'selected' : '' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
        <div><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><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">بحث</button>
        <a href="/carnets/guest-entries" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </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><th>عدد</th><th>المبلغ</th><th>الحالة</th><th>الإجراءات</th>
</tr></thead><tbody>
<?php if (empty($rows)): ?>
<tr><td colspan="11" style="text-align:center;padding:40px;color:#6B7280;">لا توجد سجلات دخول ضيوف</td></tr>
<?php else: ?>
<?php foreach ($rows as $r): ?>
<tr>
    <td style="font-size:13px;white-space:nowrap;"><?= e($r['entry_date']) ?><br><small style="color:#6B7280;"><?= e(substr($r['entry_time'], 0, 5)) ?></small></td>
    <td style="font-weight:600;"><?= e($r['guest_name']) ?></td>
    <td style="font-size:13px;direction:ltr;text-align:right;"><?= e($r['guest_phone'] ?? '—') ?></td>
    <td><a href="/members/<?= (int) $r['member_id'] ?>" style="color:#0D7377;"><?= e($r['member_name'] ?? '—') ?></a></td>
    <td style="font-size:12px;direction:ltr;text-align:right;"><?= e($r['carnet_number'] ?? '') ?></td>
    <td style="font-size:13px;"><?= e($r['facility_name'] ?? '—') ?></td>
    <td style="font-size:13px;"><?= e($activityTypes[$r['activity_type']] ?? $r['activity_type']) ?></td>
    <td style="text-align:center;"><?= (int) $r['guest_count'] ?></td>
    <td style="font-weight:600;"><?= money($r['amount_paid'] ?? 0) ?></td>
    <td>
        <?php if ($r['status'] === 'active'): ?>
            <span style="color:#059669;font-weight:600;">● داخل</span>
        <?php elseif ($r['status'] === 'checked_out'): ?>
            <span style="color:#6B7280;">● خرج</span>
        <?php else: ?>
            <span style="color:#DC2626;">● ملغى</span>
        <?php endif; ?>
    </td>
    <td>
        <?php if ($r['status'] === 'active'): ?>
        <div style="display:flex;gap:4px;">
            <form method="POST" action="/carnets/guest-entries/<?= (int) $r['id'] ?>/checkout" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline">خروج</button></form>
            <form method="POST" action="/carnets/guest-entries/<?= (int) $r['id'] ?>/cancel" style="display:inline;"><?= csrf_field() ?><input type="hidden" name="reason" value="إلغاء يدوي"><button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;" onclick="return confirm('إلغاء الدخول؟')">إلغاء</button></form>
        </div>
        <?php else: ?>
            —
        <?php endif; ?>
    </td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody></table></div>

<?php if (($pagination['last_page'] ?? 1) > 1): ?>
<div style="display:flex;justify-content:center;gap:5px;margin-top:15px;">
    <?php for ($p = 1; $p <= $pagination['last_page']; $p++): ?>
        <a href="?page=<?= $p ?>&<?= http_build_query(array_filter($filters)) ?>" class="btn btn-sm <?= $p === $pagination['current_page'] ? 'btn-primary' : 'btn-outline' ?>"><?= $p ?></a>
    <?php endfor; ?>
</div>
<?php endif; ?>
</div>
<?php $__template->endSection(); ?>
