<?php
use App\Modules\Reservations\Models\Reservation;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>الحجوزات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/reservations/calendar" class="btn btn-outline"><i data-lucide="calendar" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> عرض التقويم</a>
<?php if (can('reservation.create')): ?>
<a href="/reservations/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'); ?>

<?php
$currentStatus = $filters['status'] ?? '';
$allStatuses = Reservation::getStatuses();
?>

<!-- Status Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/reservations?<?= http_build_query(array_merge($filters, ['status' => '', 'page' => 1])) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentStatus === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($allStatuses as $sKey => $sLabel): ?>
        <a href="/reservations?<?= http_build_query(array_merge($filters, ['status' => $sKey, 'page' => 1])) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === $sKey ? '#0D7377' : 'transparent' ?>;color:<?= $currentStatus === $sKey ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($sLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/reservations" style="display:flex;flex-wrap:wrap;gap:10px;align-items:end;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="رقم الحجز، اسم الحاجز، الهاتف..." class="form-input">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المرفق</label>
            <select name="facility_id" class="form-input">
                <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>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </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">
        </div>
        <?php if ($currentStatus !== ''): ?>
        <input type="hidden" name="status" value="<?= e($currentStatus) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/reservations" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Reservations Table -->
<?php if (!empty($reservations)): ?>
<div class="card" style="padding:0;overflow-x:auto;">
    <table style="width:100%;border-collapse:collapse;font-size:14px;">
        <thead>
            <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">رقم الحجز</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">المرفق</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">التاريخ</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الوقت</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الحاجز</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">المبلغ</th>
                <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">الحالة</th>
                <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($reservations as $r):
                $statusColor = Reservation::getStatusColor($r['status'] ?? 'pending');
                $statusLabel = Reservation::getStatusLabel($r['status'] ?? 'pending');
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;">
                <td style="padding:12px 16px;">
                    <code style="font-size:12px;background:#F0F9FF;color:#0284C7;padding:2px 8px;border-radius:4px;"><?= e($r['reservation_number'] ?? '') ?></code>
                </td>
                <td style="padding:12px 16px;"><?= e($r['facility_name'] ?? '—') ?></td>
                <td style="padding:12px 16px;"><?= e($r['reservation_date'] ?? '') ?></td>
                <td style="padding:12px 16px;white-space:nowrap;">
                    <?= e(substr($r['start_time'] ?? '', 0, 5)) ?> - <?= e(substr($r['end_time'] ?? '', 0, 5)) ?>
                </td>
                <td style="padding:12px 16px;">
                    <div><?= e($r['booker_name'] ?? '—') ?></div>
                    <?php if (!empty($r['booker_phone'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;"><?= e($r['booker_phone']) ?></div>
                    <?php endif; ?>
                </td>
                <td style="padding:12px 16px;font-weight:600;"><?= money($r['total_amount'] ?? 0) ?></td>
                <td style="padding:12px 16px;text-align:center;">
                    <span class="badge" style="background:<?= $statusColor ?>15;color:<?= $statusColor ?>;font-size:12px;padding:4px 12px;border-radius:10px;font-weight:600;">
                        <?= e($statusLabel) ?>
                    </span>
                </td>
                <td style="padding:12px 16px;text-align:center;">
                    <a href="/reservations/<?= (int) $r['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; ?>
        </tbody>
    </table>
</div>

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

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="calendar-x" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد حجوزات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (!empty($filters['q']) || !empty($filters['facility_id']) || !empty($filters['date_from'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء حجز جديد للمرافق.
        <?php endif; ?>
    </p>
    <?php if (can('reservation.create')): ?>
    <a href="/reservations/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حجز جديد</a>
    <?php endif; ?>
</div>
<?php endif; ?>

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