<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الحجوزات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.booking.create')): ?>
<a href="/sa/bookings/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حجز جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/bookings" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <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" style="direction:ltr;text-align:left;">
        </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" style="direction:ltr;text-align:left;">
        </div>
        <div style="min-width:160px;">
            <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 style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="booking_type" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($bookingTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['booking_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($statuses as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </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/bookings" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Bookings Table -->
<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>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($bookings)): ?>
                <?php
                $statusColors = ['pending' => '#D97706', 'confirmed' => '#2563EB', 'checked_in' => '#059669', 'completed' => '#6B7280', 'cancelled' => '#DC2626', 'no_show' => '#9CA3AF'];
                $statusBgs = ['pending' => '#FEF3C7', 'confirmed' => '#EFF6FF', 'checked_in' => '#ECFDF5', 'completed' => '#F3F4F6', 'cancelled' => '#FEE2E2', 'no_show' => '#F3F4F6'];
                $typeColors = ['hourly' => '#F59E0B', 'training' => '#3B82F6', 'maintenance' => '#6B7280', 'blocked' => '#4B5563'];
                $typeBgs = ['hourly' => '#FEF3C7', 'training' => '#EFF6FF', 'maintenance' => '#F3F4F6', 'blocked' => '#E5E7EB'];
                ?>
                <?php foreach ($bookings as $b): ?>
                <tr>
                    <td>
                        <a href="/sa/bookings/<?= (int) $b['id'] ?>" style="text-decoration:none;color:#1A1A2E;font-weight:600;">
                            <?= e($b['booking_number']) ?>
                        </a>
                    </td>
                    <td>
                        <?= e($b['facility_name'] ?? '') ?>
                        <?php if (!empty($b['unit_name'])): ?>
                        <div style="font-size:11px;color:#6B7280;"><?= e($b['unit_name']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php $bt = $b['booking_type'] ?? ''; ?>
                        <span style="background:<?= $typeBgs[$bt] ?? '#F3F4F6' ?>;color:<?= $typeColors[$bt] ?? '#6B7280' ?>;padding:2px 8px;border-radius:4px;font-size:12px;">
                            <?= e($bookingTypes[$bt] ?? $bt) ?>
                        </span>
                    </td>
                    <td style="direction:ltr;text-align:right;"><?= e($b['booking_date']) ?></td>
                    <td style="direction:ltr;text-align:right;"><?= e(substr($b['start_time'], 0, 5)) ?> - <?= e(substr($b['end_time'], 0, 5)) ?></td>
                    <td><?= e($b['booker_name'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:right;font-weight:600;"><?= money((float) ($b['total_amount'] ?? 0)) ?></td>
                    <td>
                        <?php $bs = $b['status'] ?? ''; ?>
                        <span style="background:<?= $statusBgs[$bs] ?? '#F3F4F6' ?>;color:<?= $statusColors[$bs] ?? '#6B7280' ?>;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;">
                            <?= e($statuses[$bs] ?? $bs) ?>
                        </span>
                    </td>
                    <td>
                        <a href="/sa/bookings/<?= (int) $b['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="9" style="text-align:center;padding:40px;color:#6B7280;">
                        <i data-lucide="calendar-x" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        لا توجد حجوزات مطابقة
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;">
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination, 'baseUrl' => '/sa/bookings?' . http_build_query(array_filter($filters))]); ?>
    </div>
    <?php endif; ?>
</div>

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