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

<?php $__template->section('page_actions'); ?>
<a href="/sa/swimming/register" class="btn btn-primary"><i data-lucide="user-plus" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تسجيل لاعب جديد</a>
<a href="/sa/swimming/assign" class="btn btn-outline"><i data-lucide="users" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تعيين في مجموعة</a>
<?php $__template->endSection(); ?>

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

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(200px, 1fr));gap:12px;margin-bottom:20px;">
    <div class="card" style="padding:16px;border-right:3px solid #06B6D4;">
        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= (int) ($counts['active_players'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">لاعب نشط</div>
    </div>
    <div class="card" style="padding:16px;border-right:3px solid #8B5CF6;">
        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= (int) ($counts['active_groups'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">مجموعة نشطة</div>
    </div>
    <div class="card" style="padding:16px;border-right:3px solid #F59E0B;">
        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= (int) ($counts['pending_payment'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">بانتظار الدفع</div>
    </div>
    <div class="card" style="padding:16px;border-right:3px solid #059669;">
        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= number_format((float) ($counts['revenue_this_month'] ?? 0)) ?> ج.م</div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">إيراد الشهر</div>
    </div>
</div>

<!-- Two Column: Groups + Unassigned Players -->
<div style="display:grid;grid-template-columns:2fr 1fr;gap:15px;margin-bottom:20px;">

    <!-- Groups Table -->
    <div class="card">
        <div style="padding:12px 16px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;justify-content:space-between;">
            <span style="font-weight:700;font-size:14px;">المجموعات (<?= count($groups) ?>)</span>
            <a href="/sa/groups" style="font-size:12px;color:#2563EB;text-decoration:none;">عرض الكل</a>
        </div>
        <?php if (empty($groups)): ?>
        <div style="padding:30px;text-align:center;color:#9CA3AF;font-size:13px;">لا توجد مجموعات سباحة</div>
        <?php else: ?>
        <div style="overflow-x:auto;">
            <table style="width:100%;border-collapse:collapse;font-size:12px;">
                <thead>
                    <tr style="background:#F9FAFB;">
                        <th style="padding:8px 12px;text-align:right;font-weight:600;">المجموعة</th>
                        <th style="padding:8px 12px;text-align:right;font-weight:600;">البرنامج</th>
                        <th style="padding:8px 12px;text-align:right;font-weight:600;">المدرب</th>
                        <th style="padding:8px 12px;text-align:center;font-weight:600;">السعة</th>
                        <th style="padding:8px 12px;text-align:center;font-weight:600;">الحالة</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($groups as $group):
                        $capacity = (int) ($group['max_capacity'] ?? 0);
                        $current = (int) ($group['active_count'] ?? 0);
                        $pct = $capacity > 0 ? round(($current / $capacity) * 100) : 0;
                        $barColor = $pct >= 90 ? '#DC2626' : ($pct >= 70 ? '#F59E0B' : '#059669');
                    ?>
                    <tr style="border-bottom:1px solid #F3F4F6;">
                        <td style="padding:8px 12px;font-weight:600;"><?= e($group['name_ar']) ?></td>
                        <td style="padding:8px 12px;"><?= e($group['program_name']) ?></td>
                        <td style="padding:8px 12px;"><?= e($group['coach_name'] ?? '—') ?></td>
                        <td style="padding:8px 12px;text-align:center;">
                            <div style="display:flex;align-items:center;gap:6px;justify-content:center;">
                                <div style="width:60px;height:6px;background:#E5E7EB;border-radius:3px;overflow:hidden;">
                                    <div style="height:100%;width:<?= $pct ?>%;background:<?= $barColor ?>;border-radius:3px;"></div>
                                </div>
                                <span style="font-size:11px;color:#6B7280;"><?= $current ?>/<?= $capacity ?></span>
                            </div>
                        </td>
                        <td style="padding:8px 12px;text-align:center;">
                            <?php if ($group['status'] === 'active'): ?>
                            <span style="background:#ECFDF5;color:#059669;padding:2px 8px;border-radius:4px;font-size:11px;font-weight:600;">نشطة</span>
                            <?php else: ?>
                            <span style="background:#FEF2F2;color:#DC2626;padding:2px 8px;border-radius:4px;font-size:11px;font-weight:600;"><?= e($group['status']) ?></span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>

    <!-- Unassigned Players -->
    <div class="card">
        <div style="padding:12px 16px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;justify-content:space-between;">
            <span style="font-weight:700;font-size:14px;">بانتظار التعيين (<?= count($unassignedPlayers) ?>)</span>
            <a href="/sa/swimming/assign" style="font-size:12px;color:#2563EB;text-decoration:none;">عرض الكل</a>
        </div>
        <?php if (empty($unassignedPlayers)): ?>
        <div style="padding:30px;text-align:center;color:#9CA3AF;font-size:13px;">لا يوجد لاعبين بانتظار التعيين</div>
        <?php else: ?>
        <div style="max-height:350px;overflow-y:auto;">
            <?php foreach ($unassignedPlayers as $pl): ?>
            <a href="/sa/swimming/assign/<?= (int) $pl['id'] ?>" style="display:flex;align-items:center;gap:10px;padding:10px 16px;border-bottom:1px solid #F3F4F6;text-decoration:none;color:inherit;">
                <div style="width:32px;height:32px;border-radius:50%;background:#F3F4F6;display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                    <i data-lucide="user" style="width:16px;height:16px;color:#9CA3AF;"></i>
                </div>
                <div style="flex:1;min-width:0;">
                    <div style="font-size:13px;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><?= e($pl['full_name_ar']) ?></div>
                    <div style="font-size:11px;color:#6B7280;">
                        <?php if ($pl['date_of_birth']): ?>
                        <?= (int) date_diff(date_create($pl['date_of_birth']), date_create())->y ?> سنة
                        <?php endif; ?>
                        • <?= e($pl['medical_status'] === 'fit' ? 'لائق' : ($pl['medical_status'] === 'pending' ? 'بانتظار' : $pl['medical_status'])) ?>
                    </div>
                </div>
                <i data-lucide="chevron-left" style="width:14px;height:14px;color:#9CA3AF;flex-shrink:0;"></i>
            </a>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>

</div>

<!-- Programs Overview -->
<?php if (!empty($programs)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 16px;border-bottom:1px solid #E5E7EB;">
        <span style="font-weight:700;font-size:14px;">البرامج</span>
    </div>
    <div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(220px, 1fr));gap:12px;padding:16px;">
        <?php foreach ($programs as $prog): ?>
        <div style="border:1px solid #E5E7EB;border-radius:8px;padding:12px;">
            <div style="font-size:14px;font-weight:700;color:#1A1A2E;margin-bottom:8px;"><?= e($prog['name_ar']) ?></div>
            <div style="display:flex;justify-content:space-between;font-size:12px;color:#6B7280;margin-bottom:4px;">
                <span>مجموعات: <?= (int) $prog['group_count'] ?></span>
                <span>مسجلين: <?= (int) $prog['total_enrolled'] ?></span>
            </div>
            <div style="display:flex;justify-content:space-between;font-size:11px;margin-top:6px;padding-top:6px;border-top:1px solid #F3F4F6;">
                <span style="color:#059669;">عضو: <?= number_format((float) $prog['monthly_fee_member']) ?> ج.م</span>
                <span style="color:#D97706;">غير عضو: <?= number_format((float) $prog['monthly_fee_nonmember']) ?> ج.م</span>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

<!-- Recent Activity -->
<?php if (!empty($recentRegistrations)): ?>
<div class="card">
    <div style="padding:12px 16px;border-bottom:1px solid #E5E7EB;">
        <span style="font-weight:700;font-size:14px;">آخر التسجيلات</span>
    </div>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:12px;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <th style="padding:8px 12px;text-align:right;">اللاعب</th>
                    <th style="padding:8px 12px;text-align:right;">المجموعة</th>
                    <th style="padding:8px 12px;text-align:center;">الحالة</th>
                    <th style="padding:8px 12px;text-align:right;">التاريخ</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentRegistrations as $reg): ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 12px;font-weight:600;"><?= e($reg['full_name_ar']) ?></td>
                    <td style="padding:8px 12px;"><?= e($reg['group_name'] ?? '—') ?></td>
                    <td style="padding:8px 12px;text-align:center;">
                        <?php
                        $statusColors = [
                            'completed' => ['#ECFDF5','#059669','مكتمل'],
                            'pending_payment' => ['#FEF3C7','#D97706','بانتظار الدفع'],
                            'in_progress' => ['#EFF6FF','#2563EB','قيد التسجيل'],
                        ];
                        $s = $statusColors[$reg['status']] ?? ['#F3F4F6','#6B7280',$reg['status']];
                        ?>
                        <span style="background:<?= $s[0] ?>;color:<?= $s[1] ?>;padding:2px 8px;border-radius:4px;font-size:11px;font-weight:600;"><?= $s[2] ?></span>
                    </td>
                    <td style="padding:8px 12px;color:#6B7280;direction:ltr;"><?= e(substr($reg['created_at'], 0, 10)) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php $__template->endSection(); ?>
