<?php
declare(strict_types=1);
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" class="btn btn-outline"><i data-lucide="list" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> عرض القائمة</a>
<?php $__template->endSection(); ?>

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

<?php
// Group sessions by date
$sessionsByDate = [];
foreach ($sessions as $s) {
    $sessionsByDate[$s['session_date']][] = $s;
}

// Calendar calculations
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$daysInMonth = (int) date('t', $firstDay);
$startDayOfWeek = (int) date('w', $firstDay); // 0=Sunday

$prevMonth = $month - 1;
$prevYear = $year;
if ($prevMonth < 1) { $prevMonth = 12; $prevYear--; }

$nextMonth = $month + 1;
$nextYear = $year;
if ($nextMonth > 12) { $nextMonth = 1; $nextYear++; }

$arabicMonths = [
    1 => 'يناير', 2 => 'فبراير', 3 => 'مارس', 4 => 'أبريل',
    5 => 'مايو', 6 => 'يونيو', 7 => 'يوليو', 8 => 'أغسطس',
    9 => 'سبتمبر', 10 => 'أكتوبر', 11 => 'نوفمبر', 12 => 'ديسمبر',
];

$arabicDays = ['الأحد', 'الاثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة', 'السبت'];
?>

<!-- Month Navigation & Filter -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px;">
        <div style="display:flex;align-items:center;gap:12px;">
            <a href="/sessions/calendar?year=<?= $prevYear ?>&month=<?= $prevMonth ?><?= $academyId ? '&academy_id=' . $academyId : '' ?>"
               class="btn btn-sm btn-outline" style="padding:6px 10px;">
                <i data-lucide="chevron-right" style="width:16px;height:16px;"></i>
            </a>
            <h2 style="margin:0;font-size:20px;font-weight:700;color:#1F2937;">
                <?= e($arabicMonths[$month]) ?> <?= $year ?>
            </h2>
            <a href="/sessions/calendar?year=<?= $nextYear ?>&month=<?= $nextMonth ?><?= $academyId ? '&academy_id=' . $academyId : '' ?>"
               class="btn btn-sm btn-outline" style="padding:6px 10px;">
                <i data-lucide="chevron-left" style="width:16px;height:16px;"></i>
            </a>
        </div>
        <form method="GET" action="/sessions/calendar" style="display:flex;gap:8px;align-items:center;">
            <input type="hidden" name="year" value="<?= $year ?>">
            <input type="hidden" name="month" value="<?= $month ?>">
            <select name="academy_id" class="form-input" style="min-width:160px;" onchange="this.form.submit()">
                <option value="">كل الأكاديميات</option>
                <?php foreach ($academies as $acad): ?>
                <option value="<?= (int) $acad['id'] ?>" <?= $academyId == $acad['id'] ? 'selected' : '' ?>><?= e($acad['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </form>
    </div>
</div>

<!-- Legend -->
<div style="margin-bottom:15px;display:flex;gap:15px;flex-wrap:wrap;font-size:12px;">
    <span style="display:inline-flex;align-items:center;gap:4px;">
        <span style="width:12px;height:12px;border-radius:3px;background:#3B82F6;display:inline-block;"></span> مجدولة
    </span>
    <span style="display:inline-flex;align-items:center;gap:4px;">
        <span style="width:12px;height:12px;border-radius:3px;background:#F59E0B;display:inline-block;"></span> جارية
    </span>
    <span style="display:inline-flex;align-items:center;gap:4px;">
        <span style="width:12px;height:12px;border-radius:3px;background:#10B981;display:inline-block;"></span> مكتملة
    </span>
    <span style="display:inline-flex;align-items:center;gap:4px;">
        <span style="width:12px;height:12px;border-radius:3px;background:#EF4444;display:inline-block;"></span> ملغاة
    </span>
</div>

<!-- Calendar Grid -->
<div class="card" style="padding:0;overflow:hidden;">
    <!-- Day Names Header -->
    <div style="display:grid;grid-template-columns:repeat(7, 1fr);background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
        <?php foreach ($arabicDays as $dayName): ?>
        <div style="padding:10px 8px;text-align:center;font-weight:600;font-size:13px;color:#374151;">
            <?= e($dayName) ?>
        </div>
        <?php endforeach; ?>
    </div>

    <!-- Calendar Days -->
    <div style="display:grid;grid-template-columns:repeat(7, 1fr);">
        <?php
        // Empty cells before first day
        for ($i = 0; $i < $startDayOfWeek; $i++): ?>
        <div style="min-height:100px;border:1px solid #F3F4F6;background:#FAFAFA;"></div>
        <?php endfor; ?>

        <?php for ($day = 1; $day <= $daysInMonth; $day++):
            $dateStr = sprintf('%04d-%02d-%02d', $year, $month, $day);
            $isToday = ($dateStr === date('Y-m-d'));
            $daySessions = $sessionsByDate[$dateStr] ?? [];
        ?>
        <div style="min-height:100px;border:1px solid #F3F4F6;padding:6px;<?= $isToday ? 'background:#F0FDFA;border-color:#0D7377;' : '' ?>">
            <div style="font-size:13px;font-weight:<?= $isToday ? '700' : '500' ?>;color:<?= $isToday ? '#0D7377' : '#374151' ?>;margin-bottom:4px;">
                <?= $day ?>
            </div>
            <?php foreach ($daySessions as $ds):
                $statusColor = TrainingSession::getStatusColor($ds['status'] ?? 'scheduled');
            ?>
            <a href="/sessions/<?= (int) $ds['id'] ?>"
               style="display:block;margin-bottom:3px;padding:3px 6px;border-radius:4px;font-size:11px;text-decoration:none;color:#fff;background:<?= $statusColor ?>;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"
               title="<?= e($ds['group_name'] ?? '') ?> - <?= e(substr($ds['start_time'] ?? '', 0, 5)) ?>">
                <?= e(substr($ds['start_time'] ?? '', 0, 5)) ?> <?= e($ds['group_name'] ?? '') ?>
            </a>
            <?php endforeach; ?>
        </div>
        <?php endfor; ?>

        <?php
        // Empty cells after last day
        $totalCells = $startDayOfWeek + $daysInMonth;
        $remainingCells = (7 - ($totalCells % 7)) % 7;
        for ($i = 0; $i < $remainingCells; $i++): ?>
        <div style="min-height:100px;border:1px solid #F3F4F6;background:#FAFAFA;"></div>
        <?php endfor; ?>
    </div>
</div>

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