<?php
use App\Modules\Sessions\Models\TrainingSession;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>الحصص التدريبية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sessions/calendar" class="btn btn-outline"><i data-lucide="calendar" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> عرض التقويم</a>
<button type="button" class="btn btn-primary" onclick="document.getElementById('generateModal').style.display='flex'"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء حصص الأسبوع</button>
<?php $__template->endSection(); ?>

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

<?php
$statusColors = [
    'scheduled'   => '#3B82F6',
    'in_progress' => '#F59E0B',
    'completed'   => '#10B981',
    'cancelled'   => '#EF4444',
];
$typeColors = [
    'regular'    => '#6B7280',
    'makeup'     => '#7C3AED',
    'extra'      => '#D97706',
    'assessment' => '#2563EB',
];
?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/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>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المجموعة</label>
            <select name="group_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($groups as $grp): ?>
                <option value="<?= (int) $grp['id'] ?>" <?= ($filters['group_id'] ?? '') == $grp['id'] ? 'selected' : '' ?>><?= e($grp['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </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>
        <div style="min-width:120px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($statuses as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:120px;">
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="session_type" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($sessionTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['session_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/sessions" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Sessions Table -->
<?php if (!empty($sessions)): ?>
<div class="card" style="padding:0;overflow:hidden;">
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:14px;">
            <thead>
                <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">التاريخ</th>
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الوقت</th>
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">المجموعة</th>
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الأكاديمية</th>
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">المدرب</th>
                    <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">الحضور</th>
                    <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">النوع</th>
                    <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">الحالة</th>
                    <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sessions as $s):
                    $sStatus = $s['status'] ?? 'scheduled';
                    $sType = $s['session_type'] ?? 'regular';
                    $sColor = $statusColors[$sStatus] ?? '#6B7280';
                    $tColor = $typeColors[$sType] ?? '#6B7280';
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:12px 16px;white-space:nowrap;"><?= e($s['session_date']) ?></td>
                    <td style="padding:12px 16px;white-space:nowrap;"><?= e(substr($s['start_time'], 0, 5)) ?> - <?= e(substr($s['end_time'], 0, 5)) ?></td>
                    <td style="padding:12px 16px;"><?= e($s['group_name'] ?? '—') ?></td>
                    <td style="padding:12px 16px;"><?= e($s['academy_name'] ?? '—') ?></td>
                    <td style="padding:12px 16px;"><?= e($s['coach_name'] ?? '—') ?></td>
                    <td style="padding:12px 16px;text-align:center;">
                        <span style="font-weight:600;"><?= (int) $s['players_attended'] ?></span>
                        <span style="color:#9CA3AF;">/</span>
                        <span style="color:#6B7280;"><?= (int) $s['players_expected'] ?></span>
                    </td>
                    <td style="padding:12px 16px;text-align:center;">
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $tColor ?>15;color:<?= $tColor ?>;">
                            <?= e($sessionTypes[$sType] ?? $sType) ?>
                        </span>
                    </td>
                    <td style="padding:12px 16px;text-align:center;">
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $sColor ?>15;color:<?= $sColor ?>;">
                            <?= e($statuses[$sStatus] ?? $sStatus) ?>
                        </span>
                    </td>
                    <td style="padding:12px 16px;text-align:center;">
                        <div style="display:flex;gap:4px;justify-content:center;">
                            <a href="/sessions/<?= (int) $s['id'] ?>" class="btn btn-sm btn-outline" style="font-size:11px;padding:4px 8px;">
                                <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i>
                            </a>
                            <?php if (in_array($sStatus, ['scheduled', 'in_progress'], true)): ?>
                            <a href="/sessions/<?= (int) $s['id'] ?>/attendance" class="btn btn-sm btn-outline" style="font-size:11px;padding:4px 8px;color:#10B981;">
                                <i data-lucide="clipboard-check" style="width:13px;height:13px;vertical-align:middle;"></i>
                            </a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </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; ?>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="calendar-days" 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['date_from']) || !empty($filters['group_id']) || !empty($filters['status'])): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء حصص تدريبية من خلال زر "إنشاء حصص الأسبوع".
        <?php endif; ?>
    </p>
</div>
<?php endif; ?>

<!-- Generate Sessions Modal -->
<div id="generateModal" style="display:none;position:fixed;inset:0;z-index:1000;align-items:center;justify-content:center;background:rgba(0,0,0,0.5);">
    <div class="card" style="width:100%;max-width:400px;padding:24px;">
        <h3 style="margin:0 0 16px;font-size:18px;font-weight:700;">إنشاء حصص الأسبوع</h3>
        <form method="POST" action="/sessions/generate">
            <?= csrf_field() ?>
            <div style="margin-bottom:16px;">
                <label class="form-label">بداية الأسبوع (الأحد)</label>
                <input type="date" name="week_start" class="form-input" required>
            </div>
            <p style="font-size:13px;color:#6B7280;margin-bottom:16px;">
                سيتم إنشاء حصص تدريبية لجميع المجموعات التي لها جدول محدد (يوم + وقت) ولم يتم إنشاء حصص لها في هذا الأسبوع.
            </p>
            <div style="display:flex;gap:10px;justify-content:end;">
                <button type="button" class="btn btn-outline" onclick="document.getElementById('generateModal').style.display='none'">إلغاء</button>
                <button type="submit" class="btn btn-primary">إنشاء</button>
            </div>
        </form>
    </div>
</div>

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