<?php
use App\Modules\TrainingGroups\Models\TrainingGroup;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>المجموعات التدريبية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/training-groups/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء مجموعة</a>
<a href="/waiting-list" class="btn btn-outline"><i data-lucide="clock" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> قائمة الانتظار</a>
<?php $__template->endSection(); ?>

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

<?php
$typeColors = [
    'private'     => '#7C3AED',
    'small_group' => '#2563EB',
    'group'       => '#059669',
    'team'        => '#D97706',
];
$currentType = $filters['group_type'] ?? '';
?>

<!-- Group Type 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="/training-groups"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentType === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentType === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($groupTypes as $typeKey => $typeLabel): ?>
        <a href="/training-groups?group_type=<?= e($typeKey) ?><?= ($filters['q'] ?? '') !== '' ? '&q=' . urlencode($filters['q']) : '' ?><?= ($filters['academy_id'] ?? '') !== '' ? '&academy_id=' . urlencode($filters['academy_id']) : '' ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentType === $typeKey ? ($typeColors[$typeKey] ?? '#0D7377') : 'transparent' ?>;color:<?= $currentType === $typeKey ? ($typeColors[$typeKey] ?? '#0D7377') : '#6B7280' ?>;white-space:nowrap;">
            <?= e($typeLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/training-groups" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <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" style="min-width:200px;">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">الأكاديمية</label>
            <select name="academy_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($academies as $acad): ?>
                <option value="<?= (int) $acad['id'] ?>" <?= ($filters['academy_id'] ?? '') == $acad['id'] ? 'selected' : '' ?>><?= e($acad['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentType !== ''): ?>
        <input type="hidden" name="group_type" value="<?= e($currentType) ?>">
        <?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="/training-groups" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Groups Grid -->
<?php if (!empty($groups)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:20px;margin-bottom:20px;">
    <?php foreach ($groups as $g):
        $isActive = (int) ($g['is_active'] ?? 0);
        $gType = $g['group_type'] ?? 'group';
        $gTypeLabel = $groupTypes[$gType] ?? $gType;
        $gColor = $typeColors[$gType] ?? '#6B7280';
        $currentCount = (int) ($g['current_count'] ?? 0);
        $maxCapacity = (int) ($g['max_capacity'] ?? 20);
        $capacityPct = $maxCapacity > 0 ? min(100, (int) round(($currentCount / $maxCapacity) * 100)) : 0;
        $capacityColor = $capacityPct >= 90 ? '#DC2626' : ($capacityPct >= 70 ? '#D97706' : '#059669');
    ?>
    <div class="card" style="transition:box-shadow 0.2s;position:relative;overflow:hidden;<?= !$isActive ? 'opacity:0.65;' : '' ?>">
        <?php if (!$isActive): ?>
        <div style="position:absolute;top:12px;left:12px;z-index:2;">
            <span class="badge" style="background:#FEE2E2;color:#DC2626;font-size:11px;padding:3px 10px;border-radius:10px;">معطّل</span>
        </div>
        <?php endif; ?>

        <a href="/training-groups/<?= (int) $g['id'] ?>" style="text-decoration:none;color:inherit;display:block;">
            <div style="padding:20px 20px 15px;">
                <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:10px;">
                    <h3 style="margin:0;font-size:16px;font-weight:700;color:#1A1A2E;"><?= e($g['name_ar']) ?></h3>
                    <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $gColor ?>15;color:<?= $gColor ?>;"><?= e($gTypeLabel) ?></span>
                </div>

                <div style="font-size:12px;color:#6B7280;margin-bottom:10px;">
                    <span style="display:inline-flex;align-items:center;gap:4px;margin-left:12px;">
                        <i data-lucide="graduation-cap" style="width:13px;height:13px;"></i>
                        <?= e($g['academy_name'] ?? '—') ?>
                    </span>
                    <span style="display:inline-flex;align-items:center;gap:4px;margin-left:12px;">
                        <i data-lucide="layers" style="width:13px;height:13px;"></i>
                        <?= e($g['level_name'] ?? '—') ?>
                    </span>
                </div>

                <?php if (!empty($g['coach_name'])): ?>
                <div style="font-size:12px;color:#6B7280;margin-bottom:10px;display:flex;align-items:center;gap:4px;">
                    <i data-lucide="user" style="width:13px;height:13px;"></i>
                    <?= e($g['coach_name']) ?>
                </div>
                <?php endif; ?>

                <?php if ($g['day_of_week'] !== null): ?>
                <?php
                    $dayNames = [0=>'الأحد',1=>'الإثنين',2=>'الثلاثاء',3=>'الأربعاء',4=>'الخميس',5=>'الجمعة',6=>'السبت'];
                ?>
                <div style="font-size:12px;color:#6B7280;margin-bottom:10px;display:flex;align-items:center;gap:4px;">
                    <i data-lucide="calendar" style="width:13px;height:13px;"></i>
                    <?= e($dayNames[(int) $g['day_of_week']] ?? '') ?>
                    <?php if (!empty($g['start_time']) && !empty($g['end_time'])): ?>
                    &nbsp;|&nbsp;<?= e(substr($g['start_time'], 0, 5)) ?> - <?= e(substr($g['end_time'], 0, 5)) ?>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <!-- Capacity Bar -->
                <div style="margin-top:12px;">
                    <div style="display:flex;justify-content:space-between;font-size:11px;color:#6B7280;margin-bottom:4px;">
                        <span>السعة</span>
                        <span><?= $currentCount ?> / <?= $maxCapacity ?></span>
                    </div>
                    <div style="height:6px;background:#E5E7EB;border-radius:3px;overflow:hidden;">
                        <div style="height:100%;width:<?= $capacityPct ?>%;background:<?= $capacityColor ?>;border-radius:3px;transition:width 0.3s;"></div>
                    </div>
                </div>
            </div>
        </a>

        <div style="padding:10px 20px;border-top:1px solid #F3F4F6;display:flex;justify-content:space-between;align-items:center;">
            <code style="font-size:11px;color:#9CA3AF;background:#F9FAFB;padding:2px 6px;border-radius:4px;"><?= e($g['code'] ?? '') ?></code>
            <div style="display:flex;gap:6px;">
                <a href="/training-groups/<?= (int) $g['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>
                <a href="/training-groups/<?= (int) $g['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                    <i data-lucide="edit-3" style="width:13px;height:13px;vertical-align:middle;"></i> تعديل
                </a>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</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="users" 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['academy_id']) || !empty($filters['group_type'])): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء مجموعة تدريبية جديدة.
        <?php endif; ?>
    </p>
    <a href="/training-groups/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء مجموعة</a>
</div>
<?php endif; ?>

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