<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تسجيل دخول ضيف<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="max-width:700px;margin:0 auto;padding:25px;">
    <h3 style="margin-bottom:20px;color:#1e3a5f;">تسجيل دخول ضيف بالدعوة</h3>

    <?php if (!$carnet): ?>
    <div style="margin-bottom:20px;">
        <label class="form-label">بحث عن العضو صاحب الكارنيه</label>
        <div style="display:flex;gap:10px;">
            <input type="text" id="member-search" placeholder="اسم العضو أو رقم العضوية..." class="form-input" style="flex:1;">
            <button type="button" id="search-btn" class="btn btn-outline">بحث</button>
        </div>
        <div id="search-results" style="margin-top:10px;"></div>
    </div>
    <?php endif; ?>

    <?php if ($carnet): ?>
    <?php
        $remaining = max(0, (int) ($carnet['total_invitations'] ?? 10) - (int) ($carnet['used_invitations'] ?? 0));
        $total = (int) ($carnet['total_invitations'] ?? 10);
    ?>
    <div style="background:#F0FDF4;border:1px solid #BBF7D0;border-radius:8px;padding:15px;margin-bottom:20px;">
        <div style="display:flex;justify-content:space-between;align-items:center;">
            <div>
                <strong><?= e($member['full_name_ar'] ?? '') ?></strong>
                <span style="margin-right:10px;font-size:13px;color:#6B7280;">كارنيه: <?= e($carnet['carnet_number']) ?></span>
            </div>
            <div style="background:#fff;border-radius:20px;padding:5px 15px;font-weight:700;color:<?= $remaining <= 2 ? '#DC2626' : '#059669' ?>;">
                <?= $remaining ?> / <?= $total ?> دعوة متبقية
            </div>
        </div>
    </div>

    <?php if ($remaining <= 0): ?>
    <div style="background:#FEF2F2;border:1px solid #FECACA;border-radius:8px;padding:15px;text-align:center;color:#DC2626;font-weight:600;">
        تم استنفاذ جميع الدعوات لهذا الكارنيه
    </div>
    <?php else: ?>
    <form method="POST" action="/carnets/guest-entries">
        <?= csrf_field() ?>
        <input type="hidden" name="carnet_id" value="<?= (int) $carnet['id'] ?>">
        <input type="hidden" name="member_id" value="<?= (int) $member['id'] ?>">

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div style="grid-column:span 2;">
                <label class="form-label">اسم الضيف <span style="color:#DC2626;">*</span></label>
                <input type="text" name="guest_name" value="<?= e(old('guest_name')) ?>" class="form-input" required>
            </div>
            <div>
                <label class="form-label">هاتف الضيف</label>
                <input type="text" name="guest_phone" value="<?= e(old('guest_phone')) ?>" class="form-input" style="direction:ltr;text-align:right;">
            </div>
            <div>
                <label class="form-label">الرقم القومي</label>
                <input type="text" name="guest_national_id" value="<?= e(old('guest_national_id')) ?>" class="form-input" style="direction:ltr;text-align:right;">
            </div>
            <div>
                <label class="form-label">عدد الضيوف</label>
                <input type="number" name="guest_count" value="1" min="1" max="<?= $remaining ?>" class="form-input" style="direction:ltr;text-align:right;">
            </div>
            <div>
                <label class="form-label">النشاط <span style="color:#DC2626;">*</span></label>
                <select name="activity_type" class="form-select" required>
                    <option value="">— اختر —</option>
                    <?php foreach ($activityTypes as $k => $v): ?>
                    <option value="<?= e($k) ?>"><?= e($v) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label">المرفق</label>
                <select name="facility_id" class="form-select">
                    <option value="">— اختياري —</option>
                    <?php foreach ($facilities as $f): ?>
                    <option value="<?= (int) $f['id'] ?>"><?= e($f['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label">المبلغ المدفوع</label>
                <input type="number" name="amount_paid" value="0" min="0" step="0.01" class="form-input" style="direction:ltr;text-align:right;">
            </div>
            <div style="grid-column:span 2;">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-textarea" rows="2"><?= e(old('notes')) ?></textarea>
            </div>
        </div>

        <div style="margin-top:20px;display:flex;gap:10px;justify-content:flex-start;">
            <button type="submit" class="btn btn-primary">تسجيل الدخول</button>
            <a href="/carnets/guest-entries" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
    <?php endif; ?>
    <?php endif; ?>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.getElementById('member-search');
    const searchBtn = document.getElementById('search-btn');
    const resultsDiv = document.getElementById('search-results');
    if (!searchBtn) return;

    searchBtn.addEventListener('click', function() {
        const q = (searchInput.value || '').trim();
        if (q.length < 2) return;
        fetch('/api/carnets/member-search?q=' + encodeURIComponent(q))
            .then(r => r.json())
            .then(data => {
                if (!data.length) { resultsDiv.innerHTML = '<p style="color:#6B7280;">لا توجد نتائج</p>'; return; }
                resultsDiv.innerHTML = data.map(m =>
                    '<a href="/carnets/guest-entries/create?member_id=' + m.id + '" style="display:block;padding:8px 12px;border:1px solid #E5E7EB;border-radius:6px;margin-bottom:5px;text-decoration:none;color:#1F2937;">' +
                    '<strong>' + m.full_name_ar + '</strong> <span style="color:#6B7280;font-size:13px;">' + (m.membership_number || '') + '</span></a>'
                ).join('');
            });
    });
    searchInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { e.preventDefault(); searchBtn.click(); } });
});
</script>
<?php $__template->endSection(); ?>
