<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>حجوزات الملاعب الخاصة<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('reservation.create')): ?>
<a href="/private-matches/create" class="btn btn-primary">حجز جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<!-- Filters -->
<div class="card" style="padding:15px;margin-bottom:20px;">
    <form method="GET" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div><label class="form-label" style="font-size:11px;">الحالة</label>
            <select name="status" class="form-input" style="min-width:120px;">
                <option value="">الكل</option>
                <option value="pending" <?= ($filters['status'] ?? '') === 'pending' ? 'selected' : '' ?>>معلق</option>
                <option value="confirmed" <?= ($filters['status'] ?? '') === 'confirmed' ? 'selected' : '' ?>>مؤكد</option>
                <option value="completed" <?= ($filters['status'] ?? '') === 'completed' ? 'selected' : '' ?>>مكتمل</option>
                <option value="cancelled" <?= ($filters['status'] ?? '') === 'cancelled' ? 'selected' : '' ?>>ملغي</option>
            </select>
        </div>
        <div><label class="form-label" style="font-size:11px;">من تاريخ</label><input type="date" name="date_from" class="form-input" value="<?= e($filters['date_from'] ?? '') ?>"></div>
        <div><label class="form-label" style="font-size:11px;">إلى تاريخ</label><input type="date" name="date_to" class="form-input" value="<?= e($filters['date_to'] ?? '') ?>"></div>
        <button type="submit" class="btn btn-outline">بحث</button>
    </form>
</div>

<div class="card" style="padding:20px;">
    <?php if (empty($bookings)): ?>
    <p style="color:#6B7280;text-align:center;padding:30px;">لا توجد حجوزات</p>
    <?php else: ?>
    <table class="table">
        <thead>
            <tr>
                <th>المرفق</th>
                <th>التاريخ</th>
                <th>الوقت</th>
                <th>النوع</th>
                <th>الحاجز</th>
                <th>المبلغ</th>
                <th>الحالة</th>
                <th>إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($bookings as $b): ?>
            <tr>
                <td style="font-weight:600;"><?= e($b['facility_name'] ?? '') ?></td>
                <td><?= e($b['booking_date'] ?? '') ?></td>
                <td style="direction:ltr;text-align:right;font-size:13px;"><?= e(substr($b['start_time'] ?? '', 0, 5)) ?>-<?= e(substr($b['end_time'] ?? '', 0, 5)) ?></td>
                <td><span style="font-size:11px;background:#EDE9FE;color:#7C3AED;padding:2px 6px;border-radius:4px;"><?= e($matchTypes[$b['match_type']] ?? $b['match_type']) ?></span></td>
                <td style="font-size:13px;"><?= e($b['booked_by_name'] ?? '') ?></td>
                <td style="font-weight:600;"><?= money((float) $b['total_cost']) ?>
                    <?php if ($b['payment_status'] === 'paid'): ?><span style="color:#059669;font-size:10px;">✓ مدفوع</span>
                    <?php elseif ($b['payment_status'] === 'partial'): ?><span style="color:#F59E0B;font-size:10px;">جزئي</span>
                    <?php else: ?><span style="color:#DC2626;font-size:10px;">غير مدفوع</span><?php endif; ?>
                </td>
                <td>
                    <?php if ($b['status'] === 'pending'): ?><span style="color:#F59E0B;">● معلق</span>
                    <?php elseif ($b['status'] === 'confirmed'): ?><span style="color:#059669;">● مؤكد</span>
                    <?php elseif ($b['status'] === 'completed'): ?><span style="color:#0D7377;">● مكتمل</span>
                    <?php else: ?><span style="color:#DC2626;">● ملغي</span><?php endif; ?>
                </td>
                <td style="white-space:nowrap;">
                    <?php if ($b['status'] === 'pending'): ?>
                    <?php if (can('reservation.confirm')): ?>
                    <form method="POST" action="/private-matches/<?= (int) $b['id'] ?>/confirm" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-primary">تأكيد</button></form>
                    <?php endif; ?>
                    <?php if (can('reservation.cancel')): ?>
                    <form method="POST" action="/private-matches/<?= (int) $b['id'] ?>/cancel" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;">إلغاء</button></form>
                    <?php endif; ?>
                    <?php elseif ($b['status'] === 'confirmed'): ?>
                    <?php if (can('reservation.confirm')): ?>
                    <form method="POST" action="/private-matches/<?= (int) $b['id'] ?>/complete" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline">إتمام</button></form>
                    <?php endif; ?>
                    <?php if ($b['payment_status'] !== 'paid' && can('reservation.confirm')): ?>
                    <form method="POST" action="/private-matches/<?= (int) $b['id'] ?>/pay" style="display:inline-flex;gap:3px;"><?= csrf_field() ?><input type="number" name="amount" step="0.01" placeholder="مبلغ" style="width:70px;font-size:11px;padding:3px;border:1px solid #D1D5DB;border-radius:4px;"><button type="submit" class="btn btn-sm btn-outline">دفع</button></form>
                    <?php endif; ?>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
