<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>بيانات اللاعب: <?= e($player['full_name_ar'] ?? '') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.player.manage')): ?>
<a href="/sa/players/<?= (int) $player['id'] ?>/edit" class="btn btn-primary"><i data-lucide="edit-3" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تعديل</a>
<?php endif; ?>
<a href="/sa/players/<?= (int) $player['id'] ?>/documents" class="btn btn-outline"><i data-lucide="file-text" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> المستندات</a>
<a href="/sa/players" 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'); ?>

<?php
$typeLabels = ['member' => 'عضو', 'non_member' => 'غير عضو'];
$medLabels = ['fit' => 'لائق', 'unfit' => 'غير لائق', 'pending' => 'في الانتظار', 'expired' => 'منتهي', 'conditional' => 'مشروط'];
$medColors = ['fit' => '#059669', 'unfit' => '#DC2626', 'pending' => '#D97706', 'expired' => '#6B7280', 'conditional' => '#F59E0B'];
$cardLabels = ['inactive' => 'غير مفعلة', 'active' => 'فعالة', 'suspended' => 'موقوفة', 'revoked' => 'ملغاة'];
$genderLabels = ['male' => 'ذكر', 'female' => 'أنثى'];
$relationLabels = ['father' => 'أب', 'mother' => 'أم', 'brother' => 'أخ', 'sister' => 'أخت', 'uncle' => 'عم / خال', 'other' => 'أخرى'];
?>

<?php if (!in_array($player['medical_status'] ?? 'pending', ['fit', 'conditional'])): ?>
<div style="background:#FEF3C7;border:1px solid #F59E0B;border-radius:8px;padding:12px 16px;margin-bottom:16px;display:flex;align-items:center;gap:10px;">
    <i data-lucide="alert-triangle" style="width:20px;height:20px;color:#D97706;flex-shrink:0;"></i>
    <div style="font-size:13px;color:#92400E;font-weight:600;">هذا اللاعب بدون شهادة طبية سارية — يمكنه التدريب لكن يجب تقديم الشهادة في أقرب وقت</div>
</div>
<?php endif; ?>

<!-- Player Info Card -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;display:flex;align-items:center;gap:20px;border-bottom:1px solid #E5E7EB;">
        <div style="width:64px;height:64px;border-radius:50%;background:linear-gradient(135deg, #0D737715, #0D737730);display:flex;align-items:center;justify-content:center;">
            <i data-lucide="user" style="width:32px;height:32px;color:#0D7377;"></i>
        </div>
        <div>
            <h2 style="margin:0 0 4px;font-size:20px;font-weight:700;"><?= e($player['full_name_ar'] ?? '') ?></h2>
            <?php if (!empty($player['full_name_en'])): ?>
            <div style="font-size:13px;color:#6B7280;margin-bottom:4px;" dir="ltr"><?= e($player['full_name_en']) ?></div>
            <?php endif; ?>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <code style="font-size:11px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($player['registration_serial'] ?? '') ?></code>
                <?php $mStatus = $player['medical_status'] ?? 'pending'; $mColor = $medColors[$mStatus] ?? '#6B7280'; ?>
                <span style="padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $mColor ?>15;color:<?= $mColor ?>;"><?= e($medLabels[$mStatus] ?? $mStatus) ?></span>
            </div>
        </div>
    </div>

    <div style="padding:20px;display:grid;grid-template-columns:repeat(auto-fit, minmax(220px, 1fr));gap:20px;">
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">نوع اللاعب</div>
            <div style="font-size:14px;font-weight:600;"><?= e($typeLabels[$player['player_type'] ?? ''] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الرقم القومي</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['national_id'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">تاريخ الميلاد</div>
            <div style="font-size:14px;font-weight:600;"><?= e($player['date_of_birth'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">النوع</div>
            <div style="font-size:14px;font-weight:600;"><?= e($genderLabels[$player['gender'] ?? ''] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الهاتف</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['phone'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">البريد الإلكتروني</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['email'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">حالة البطاقة</div>
            <div style="font-size:14px;font-weight:600;"><?= e($cardLabels[$player['card_status'] ?? ''] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">انتهاء الكشف الطبي</div>
            <div style="font-size:14px;font-weight:600;"><?= e($player['medical_expiry_date'] ?? '-') ?></div>
        </div>
        <?php if (!empty($player['member_id'])): ?>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">رقم العضوية</div>
            <div style="font-size:14px;font-weight:600;"><?= (int) $player['member_id'] ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Guardian Info -->
<?php if (!empty($player['guardian_name'])): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">بيانات ولي الأمر</h3>
    </div>
    <div style="padding:20px;display:grid;grid-template-columns:repeat(auto-fit, minmax(220px, 1fr));gap:20px;">
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الاسم</div>
            <div style="font-size:14px;font-weight:600;"><?= e($player['guardian_name'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الهاتف</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['guardian_phone'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الرقم القومي</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['guardian_national_id'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">صلة القرابة</div>
            <div style="font-size:14px;font-weight:600;"><?= e($relationLabels[$player['guardian_relationship'] ?? ''] ?? '-') ?></div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Notes -->
<?php if (!empty($player['notes'])): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">ملاحظات</h3>
    </div>
    <div style="padding:20px;font-size:14px;line-height:1.8;color:#374151;">
        <?= nl2br(e($player['notes'])) ?>
    </div>
</div>
<?php endif; ?>

<!-- Medical History Timeline + Inline Edit -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">
            <i data-lucide="heart-pulse" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;color:#DC2626;"></i>
            السجل الطبي
        </h3>
        <?php if (!empty($canApproveMedical)): ?>
        <button type="button" class="btn btn-sm btn-outline" onclick="document.getElementById('medicalEditForm').style.display = document.getElementById('medicalEditForm').style.display === 'none' ? 'block' : 'none';">
            <i data-lucide="edit-3" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> تعديل الحالة
        </button>
        <?php endif; ?>
    </div>

    <!-- Inline Medical Edit Form -->
    <?php if (!empty($canApproveMedical)): ?>
    <div id="medicalEditForm" style="display:none;padding:20px;background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
        <form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/medical/update">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(180px, 1fr));gap:12px;margin-bottom:12px;">
                <div>
                    <label class="form-label" style="font-size:11px;">الحالة الطبية</label>
                    <select name="medical_status" class="form-input" style="font-size:13px;">
                        <?php foreach ($medLabels as $key => $label): ?>
                        <option value="<?= $key ?>" <?= ($player['medical_status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">تاريخ انتهاء الصلاحية</label>
                    <input type="date" name="medical_expiry_date" class="form-input" value="<?= e($player['medical_expiry_date'] ?? '') ?>" style="font-size:13px;">
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">تاريخ الكشف</label>
                    <input type="date" name="exam_date" class="form-input" style="font-size:13px;">
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">اسم الطبيب</label>
                    <input type="text" name="doctor_name" class="form-input" placeholder="د. ..." style="font-size:13px;">
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">العيادة / المستشفى</label>
                    <input type="text" name="clinic_name" class="form-input" placeholder="اسم العيادة" style="font-size:13px;">
                </div>
            </div>
            <button type="submit" class="btn btn-sm btn-primary">
                <i data-lucide="save" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> حفظ التعديلات
            </button>
        </form>
    </div>
    <?php endif; ?>

    <!-- Timeline -->
    <?php if (!empty($medicalHistory)): ?>
    <div style="padding:20px;">
        <?php
        $approvalLabels = ['pending' => 'في الانتظار', 'approved' => 'معتمد', 'rejected' => 'مرفوض', 'expired' => 'منتهي'];
        $approvalColors = ['pending' => '#D97706', 'approved' => '#059669', 'rejected' => '#DC2626', 'expired' => '#6B7280'];
        ?>
        <?php foreach ($medicalHistory as $idx => $doc): ?>
        <?php $aStatus = $doc['approval_status'] ?? 'pending'; $aColor = $approvalColors[$aStatus] ?? '#6B7280'; ?>
        <div style="display:flex;gap:16px;padding-bottom:16px;<?= $idx < count($medicalHistory) - 1 ? 'border-bottom:1px dashed #E5E7EB;margin-bottom:16px;' : '' ?>">
            <div style="width:10px;height:10px;border-radius:50%;background:<?= $aColor ?>;margin-top:6px;flex-shrink:0;"></div>
            <div style="flex:1;">
                <div style="display:flex;align-items:center;gap:8px;margin-bottom:6px;">
                    <span style="padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $aColor ?>15;color:<?= $aColor ?>;">
                        <?= e($approvalLabels[$aStatus] ?? $aStatus) ?>
                    </span>
                    <span style="font-size:12px;color:#9CA3AF;"><?= e($doc['created_at'] ?? '') ?></span>
                </div>
                <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(150px, 1fr));gap:8px;font-size:13px;color:#374151;">
                    <?php if (!empty($doc['exam_date'])): ?>
                    <div><span style="color:#9CA3AF;">تاريخ الكشف:</span> <?= e($doc['exam_date']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($doc['expiry_date'])): ?>
                    <div><span style="color:#9CA3AF;">ينتهي:</span> <?= e($doc['expiry_date']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($doc['doctor_name'])): ?>
                    <div><span style="color:#9CA3AF;">الطبيب:</span> <?= e($doc['doctor_name']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($doc['clinic_name'])): ?>
                    <div><span style="color:#9CA3AF;">العيادة:</span> <?= e($doc['clinic_name']) ?></div>
                    <?php endif; ?>
                </div>
                <?php if (!empty($doc['conditional_notes'])): ?>
                <div style="margin-top:6px;padding:8px 12px;background:#FEF3C7;border-radius:6px;font-size:12px;color:#92400E;">
                    <strong>ملاحظات مشروطة:</strong> <?= e($doc['conditional_notes']) ?>
                </div>
                <?php endif; ?>
                <?php if (!empty($doc['rejection_reason'])): ?>
                <div style="margin-top:6px;padding:8px 12px;background:#FEE2E2;border-radius:6px;font-size:12px;color:#991B1B;">
                    <strong>سبب الرفض:</strong> <?= e($doc['rejection_reason']) ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#9CA3AF;">
        لا يوجد سجل طبي بعد.
    </div>
    <?php endif; ?>
</div>

<!-- Unpaid Subscriptions -->
<?php if (!empty($unpaidSubs)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">
            <i data-lucide="alert-circle" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;color:#DC2626;"></i>
            اشتراكات غير مدفوعة
            <span style="padding:2px 8px;border-radius:10px;font-size:12px;font-weight:700;background:#FEE2E2;color:#DC2626;margin-right:8px;"><?= count($unpaidSubs) ?></span>
        </h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>المجموعة</th>
                    <th>الفترة</th>
                    <th>المبلغ</th>
                    <th>الحالة</th>
                    <th>إجراء</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $subStatusLabels = ['unpaid' => 'غير مدفوع', 'overdue' => 'متأخر', 'partial' => 'جزئي'];
                $subStatusColors = ['unpaid' => '#D97706', 'overdue' => '#DC2626', 'partial' => '#1D4ED8'];
                ?>
                <?php foreach ($unpaidSubs as $sub): ?>
                <?php $sColor = $subStatusColors[$sub['payment_status'] ?? ''] ?? '#6B7280'; ?>
                <tr>
                    <td style="font-weight:600;"><?= e($sub['group_name'] ?? '—') ?></td>
                    <td style="font-size:13px;"><?= e($sub['period_start'] ?? '') ?> → <?= e($sub['period_end'] ?? '') ?></td>
                    <td style="font-weight:600;color:#059669;"><?= money((float) ($sub['final_amount'] ?? 0)) ?></td>
                    <td>
                        <span style="padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $sColor ?>15;color:<?= $sColor ?>;">
                            <?= e($subStatusLabels[$sub['payment_status'] ?? ''] ?? '') ?>
                        </span>
                    </td>
                    <td>
                        <a href="/sa/subscriptions/<?= (int) $sub['id'] ?>" class="btn btn-sm btn-primary" style="padding:4px 10px;font-size:11px;">
                            <i data-lucide="banknote" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i> ادفع
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Documents Section -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">المستندات</h3>
        <a href="/sa/players/<?= (int) $player['id'] ?>/documents" class="btn btn-sm btn-primary">
            <i data-lucide="upload" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إدارة المستندات
        </a>
    </div>
    <?php if (!empty($documents)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>العنوان</th>
                    <th>النوع</th>
                    <th>الحالة</th>
                    <th>تاريخ الرفع</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($documents as $doc): ?>
                <tr>
                    <td><?= e($doc['file_name'] ?? '') ?></td>
                    <td>
                        <?php
                        $docTypes = ['medical_cert' => 'شهادة طبية', 'national_id' => 'صورة بطاقة', 'photo' => 'صورة شخصية', 'birth_cert' => 'شهادة ميلاد'];
                        ?>
                        <?= e($docTypes[$doc['document_type'] ?? ''] ?? $doc['document_type'] ?? '') ?>
                    </td>
                    <td>
                        <?php
                        $approvalLabels2 = ['pending' => 'في الانتظار', 'approved' => 'معتمد', 'rejected' => 'مرفوض', 'expired' => 'منتهي'];
                        $approvalColors2 = ['pending' => '#D97706', 'approved' => '#059669', 'rejected' => '#DC2626', 'expired' => '#6B7280'];
                        $aStatus2 = $doc['approval_status'] ?? 'pending';
                        $aColor2 = $approvalColors2[$aStatus2] ?? '#6B7280';
                        ?>
                        <span style="padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $aColor2 ?>15;color:<?= $aColor2 ?>;">
                            <?= e($approvalLabels2[$aStatus2] ?? $aStatus2) ?>
                        </span>
                    </td>
                    <td style="font-size:13px;"><?= e($doc['created_at'] ?? '') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#9CA3AF;">
        لا توجد مستندات مرفوعة بعد.
    </div>
    <?php endif; ?>
</div>

<!-- Activity Map: Enrolled Groups -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="users" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">المجموعات المسجل بها</h3>
        <span style="margin-right:auto;background:#EFF6FF;color:#2563EB;padding:2px 8px;border-radius:8px;font-size:11px;"><?= count($enrolledGroups) ?></span>
    </div>
    <?php if (!empty($enrolledGroups)): ?>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">المجموعة</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">البرنامج</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">المدرب</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">تاريخ الانضمام</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($enrolledGroups as $eg): ?>
                <tr style="border-top:1px solid #F3F4F6;">
                    <td style="padding:10px 12px;"><a href="/sa/groups/<?= (int) $eg['group_id'] ?>" style="color:#2563EB;font-size:13px;font-weight:600;text-decoration:none;"><?= e($eg['group_name']) ?></a></td>
                    <td style="padding:10px 12px;font-size:13px;"><?= e($eg['program_name'] ?? '—') ?></td>
                    <td style="padding:10px 12px;font-size:13px;"><?= e($eg['coach_name'] ?? '—') ?></td>
                    <td style="padding:10px 12px;font-size:12px;color:#6B7280;"><?= e($eg['joined_at'] ?? '—') ?></td>
                    <td style="padding:10px 12px;">
                        <?php $eColor = $eg['enrollment_status'] === 'active' ? '#059669' : '#F59E0B'; ?>
                        <span style="padding:2px 8px;border-radius:8px;font-size:11px;background:<?= $eColor ?>15;color:<?= $eColor ?>;">
                            <?= $eg['enrollment_status'] === 'active' ? 'نشط' : 'في انتظار الدفع' ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#9CA3AF;text-align:center;padding:20px 0;margin:0;font-size:13px;">لم يتم التسجيل في أي مجموعة بعد</p>
    <?php endif; ?>
</div>

<!-- Recent Bookings -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="calendar-check" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">آخر الحجوزات</h3>
    </div>
    <?php if (!empty($recentBookings)): ?>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">التاريخ</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">الوقت</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">المرفق</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">النوع</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentBookings as $rb): ?>
                <tr style="border-top:1px solid #F3F4F6;">
                    <td style="padding:10px 12px;font-size:13px;"><?= e($rb['booking_date']) ?></td>
                    <td style="padding:10px 12px;font-size:12px;direction:ltr;text-align:right;"><?= e($rb['start_time']) ?> - <?= e($rb['end_time']) ?></td>
                    <td style="padding:10px 12px;font-size:13px;"><?= e($rb['facility_name']) ?> — <?= e($rb['unit_name']) ?></td>
                    <td style="padding:10px 12px;font-size:12px;color:#6B7280;"><?= e($rb['booking_type']) ?></td>
                    <td style="padding:10px 12px;font-size:12px;"><?= e($rb['status']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#9CA3AF;text-align:center;padding:20px 0;margin:0;font-size:13px;">لا توجد حجوزات</p>
    <?php endif; ?>
</div>

<!-- Gate Access History -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="scan-line" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">سجل الدخول</h3>
    </div>
    <?php if (!empty($gateHistory)): ?>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">الوقت</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">البوابة</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">النتيجة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($gateHistory as $gh): ?>
                <tr style="border-top:1px solid #F3F4F6;">
                    <td style="padding:10px 12px;font-size:12px;color:#6B7280;"><?= e($gh['scan_time']) ?></td>
                    <td style="padding:10px 12px;font-size:13px;"><?= e($gh['gate_name'] ?? '—') ?></td>
                    <td style="padding:10px 12px;">
                        <?php if ($gh['access_granted']): ?>
                        <span style="color:#059669;font-size:12px;">● مسموح</span>
                        <?php else: ?>
                        <span style="color:#DC2626;font-size:12px;">● مرفوض<?= $gh['denial_reason'] ? ' — ' . e($gh['denial_reason']) : '' ?></span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#9CA3AF;text-align:center;padding:20px 0;margin:0;font-size:13px;">لا يوجد سجل دخول</p>
    <?php endif; ?>
</div>

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