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

<?php $__template->section('page_actions'); ?>
<a href="/sa/swimming/pool-reservations/wizard" class="btn btn-primary"><i data-lucide="plus" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> حجز جديد</a>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:16px;padding:16px;">
    <form method="get" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="search" class="form-input" style="padding:10px;font-size:13px;border-radius:8px;" value="<?= e($search) ?>" placeholder="اسم الحاجز أو رقم الحجز...">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-input" style="padding:10px;font-size:13px;border-radius:8px;">
                <option value="">الكل</option>
                <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>نشط</option>
                <option value="completed" <?= $status === 'completed' ? 'selected' : '' ?>>مكتمل</option>
                <option value="cancelled" <?= $status === 'cancelled' ? 'selected' : '' ?>>ملغي</option>
                <option value="expired" <?= $status === 'expired' ? 'selected' : '' ?>>منتهي</option>
            </select>
        </div>
        <button type="submit" class="btn btn-outline" style="padding:10px 16px;font-size:13px;border-radius:8px;">بحث</button>
    </form>
</div>

<!-- Stats -->
<?php
    $activeCount = 0; $totalRevenue = 0;
    foreach ($reservations as $r) {
        if ($r['status'] === 'active') $activeCount++;
        $totalRevenue += (float) $r['total_amount'];
    }
?>
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(180px,1fr));gap:12px;margin-bottom:16px;">
    <div class="card" style="padding:16px;text-align:center;">
        <div style="font-size:24px;font-weight:800;color:#2563EB;"><?= $total ?></div>
        <div style="font-size:12px;color:#6B7280;">إجمالي الحجوزات</div>
    </div>
    <div class="card" style="padding:16px;text-align:center;">
        <div style="font-size:24px;font-weight:800;color:#059669;"><?= $activeCount ?></div>
        <div style="font-size:12px;color:#6B7280;">نشطة</div>
    </div>
</div>

<!-- Table -->
<div class="card">
    <div style="overflow-x:auto;">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>رقم الحجز</th>
                    <th>الحاجز</th>
                    <th>النوع</th>
                    <th>الحصص</th>
                    <th>المبلغ</th>
                    <th>الحالة</th>
                    <th>الدفع</th>
                    <th>التاريخ</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($reservations)): ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد حجوزات</td></tr>
                <?php else: ?>
                <?php foreach ($reservations as $r): ?>
                <tr>
                    <td><a href="/sa/swimming/pool-reservations/<?= (int) $r['id'] ?>" style="color:#2563EB;font-weight:600;text-decoration:none;"><?= e($r['reservation_number']) ?></a></td>
                    <td>
                        <div style="font-weight:600;"><?= e($r['booker_name']) ?></div>
                        <div style="font-size:11px;color:#6B7280;"><?= (int) $r['participant_count'] ?> مشارك</div>
                    </td>
                    <td><?php
                        $typeLabels = ['lane_50m' => 'حارة 50م', 'lane_25m' => 'حارة 25م', 'lane_mix' => 'ميكس', 'cards' => 'كروت'];
                        echo $typeLabels[$r['reservation_type']] ?? $r['reservation_type'];
                    ?></td>
                    <td>
                        <span style="font-weight:700;"><?= (int) $r['sessions_used'] ?></span>/<span style="color:#6B7280;"><?= (int) $r['sessions_total'] ?></span>
                    </td>
                    <td style="font-weight:600;"><?= number_format((float) $r['total_amount'], 0) ?> ج.م</td>
                    <td><?php
                        $sc = ['active' => '#059669', 'completed' => '#6B7280', 'cancelled' => '#DC2626', 'expired' => '#D97706'];
                        $sl = ['active' => 'نشط', 'completed' => 'مكتمل', 'cancelled' => 'ملغي', 'expired' => 'منتهي'];
                        $c = $sc[$r['status']] ?? '#6B7280'; $l = $sl[$r['status']] ?? $r['status'];
                    ?><span style="padding:3px 8px;border-radius:4px;font-size:11px;font-weight:600;background:<?= $c ?>15;color:<?= $c ?>;"><?= $l ?></span></td>
                    <td><?php
                        $pc = ['paid' => '#059669', 'pending' => '#D97706', 'unpaid' => '#DC2626', 'partial' => '#2563EB'];
                        $pl = ['paid' => 'مدفوع', 'pending' => 'معلق', 'unpaid' => 'غير مدفوع', 'partial' => 'جزئي'];
                        $pcc = $pc[$r['payment_status']] ?? '#6B7280'; $pll = $pl[$r['payment_status']] ?? $r['payment_status'];
                    ?><span style="padding:3px 8px;border-radius:4px;font-size:11px;font-weight:600;background:<?= $pcc ?>15;color:<?= $pcc ?>;"><?= $pll ?></span></td>
                    <td style="font-size:12px;color:#6B7280;"><?= date('Y-m-d', strtotime($r['created_at'])) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
    <div style="padding:12px 20px;border-top:1px solid #E5E7EB;display:flex;justify-content:center;gap:6px;">
        <?php for ($p = 1; $p <= $totalPages; $p++): ?>
        <a href="?page=<?= $p ?>&search=<?= urlencode($search) ?>&status=<?= urlencode($status) ?>"
           style="padding:6px 12px;border-radius:6px;font-size:13px;text-decoration:none;<?= $p === $page ? 'background:#2563EB;color:#fff;font-weight:700;' : 'background:#F3F4F6;color:#374151;' ?>"><?= $p ?></a>
        <?php endfor; ?>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
