<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الحصص التعويضية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.makeup.manage')): ?>
<a href="/sa/makeup-sessions/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'); ?>

<div class="card" style="margin-bottom:15px;padding:12px 15px;">
    <form method="GET" action="/sa/makeup-sessions" 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>
            <select name="status" class="form-select">
                <option value="">-- الكل --</option>
                <option value="eligible" <?= ($filters['status'] ?? '') === 'eligible' ? 'selected' : '' ?>>متاحة</option>
                <option value="scheduled" <?= ($filters['status'] ?? '') === 'scheduled' ? 'selected' : '' ?>>مجدولة</option>
                <option value="completed" <?= ($filters['status'] ?? '') === 'completed' ? 'selected' : '' ?>>مكتملة</option>
                <option value="expired" <?= ($filters['status'] ?? '') === 'expired' ? 'selected' : '' ?>>منتهية</option>
                <option value="cancelled" <?= ($filters['status'] ?? '') === 'cancelled' ? 'selected' : '' ?>>ملغاة</option>
            </select>
        </div>
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المجموعة الأصلية</label>
            <select name="group_id" class="form-select" data-searchable="true">
                <option value="">-- الكل --</option>
                <?php foreach ($groups as $g): ?>
                <option value="<?= (int)$g['id'] ?>" <?= ($filters['group_id'] ?? '') == $g['id'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="filter" style="width:14px;height:14px;"></i> تصفية</button>
    </form>
</div>

<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>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($makeups)): ?>
                <?php foreach ($makeups as $m): ?>
                <?php
                $statusBadge = match($m['status']) {
                    'eligible'  => '<span style="background:#EFF6FF;color:#2563EB;padding:2px 8px;border-radius:8px;font-size:11px;">متاحة</span>',
                    'scheduled' => '<span style="background:#FEF3C7;color:#D97706;padding:2px 8px;border-radius:8px;font-size:11px;">مجدولة</span>',
                    'completed' => '<span style="background:#ECFDF5;color:#059669;padding:2px 8px;border-radius:8px;font-size:11px;">مكتملة</span>',
                    'expired'   => '<span style="background:#FEF2F2;color:#DC2626;padding:2px 8px;border-radius:8px;font-size:11px;">منتهية</span>',
                    'cancelled' => '<span style="background:#F3F4F6;color:#6B7280;padding:2px 8px;border-radius:8px;font-size:11px;">ملغاة</span>',
                    default     => e($m['status']),
                };
                ?>
                <tr>
                    <td style="font-weight:500;"><?= e($m['player_name'] ?? '') ?></td>
                    <td><?= e($m['original_group_name'] ?? '') ?></td>
                    <td><?= e($m['missed_session_date']) ?></td>
                    <td><?= e($m['makeup_group_name'] ?? '—') ?></td>
                    <td><?= e($m['makeup_date'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= e($m['expires_at']) ?></td>
                    <td><?= $statusBadge ?></td>
                    <td style="white-space:nowrap;">
                        <?php if ($m['status'] === 'eligible' && can('sa.makeup.manage')): ?>
                        <a href="/sa/makeup-sessions/<?= (int)$m['id'] ?>/schedule" class="btn btn-sm btn-outline" style="font-size:11px;">جدولة</a>
                        <form method="POST" action="/sa/makeup-sessions/<?= (int)$m['id'] ?>/cancel" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm" style="font-size:11px;color:#DC2626;" onclick="return confirm('هل أنت متأكد؟')">إلغاء</button>
                        </form>
                        <?php elseif ($m['status'] === 'scheduled' && can('sa.makeup.manage')): ?>
                        <form method="POST" action="/sa/makeup-sessions/<?= (int)$m['id'] ?>/complete" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-primary" style="font-size:11px;">تأكيد الحضور</button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:#6B7280;">لا توجد حصص تعويضية</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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