<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>لوحة تحكم: <?= e($pool->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/pool/<?= (int) $pool->id ?>/grid" class="btn btn-primary"><i data-lucide="grid-3x3" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> الشبكة التفاعلية</a>
<a href="/pool/<?= (int) $pool->id ?>/schedules" class="btn btn-outline"><i data-lucide="repeat" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> الجداول</a>
<a href="/pool" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة</a>
<?php $__template->endSection(); ?>

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

<?php
$dayNames = [1 => 'الأحد', 2 => 'الإثنين', 3 => 'الثلاثاء', 4 => 'الأربعاء', 5 => 'الخميس', 6 => 'الجمعة', 7 => 'السبت'];
$hours = $pool->getOperatingHours();
$startHr = (int) substr($hours['start'] ?? '06:00', 0, 2);
$endHr = (int) substr($hours['end'] ?? '22:00', 0, 2);
?>

<!-- KPI Cards -->
<div style="display:grid;grid-template-columns:repeat(4, 1fr);gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;border-top:4px solid #0D7377;">
        <div style="font-size:28px;font-weight:700;color:#0D7377;"><?= (int) $todayStats['bookings'] ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">حجوزات اليوم</div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:2px;"><?= (int) $todayStats['swimmers'] ?> سبّاح</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;border-top:4px solid #2563EB;">
        <div style="font-size:28px;font-weight:700;color:#2563EB;"><?= (int) $weekStats['bookings'] ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">حجوزات الأسبوع</div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:2px;"><?= (int) $weekStats['swimmers'] ?> سبّاح</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;border-top:4px solid #059669;">
        <div style="font-size:28px;font-weight:700;color:#059669;"><?= number_format((float) $monthStats['revenue'], 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">إيرادات الشهر (ج.م)</div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:2px;"><?= (int) $monthStats['bookings'] ?> حجز</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;border-top:4px solid #D97706;">
        <?php
        $totalLaneMin = array_sum($laneMinutes);
        $totalPoolMin = $totalAvailableMinutes * count($lanes);
        $utilPct = $totalPoolMin > 0 ? round(($totalLaneMin / $totalPoolMin) * 100) : 0;
        ?>
        <div style="font-size:28px;font-weight:700;color:#D97706;"><?= $utilPct ?>%</div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">نسبة الاستخدام (الشهر)</div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:2px;"><?= round($totalLaneMin / 60) ?> ساعة مستخدمة</div>
    </div>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;margin-bottom:20px;">
    <!-- Heatmap -->
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="flame" style="width:18px;height:18px;color:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">خريطة الازدحام (آخر 30 يوم)</h3>
        </div>
        <div style="padding:15px;overflow-x:auto;">
            <table style="width:100%;border-collapse:collapse;font-size:10px;">
                <thead>
                    <tr>
                        <th style="padding:4px;text-align:right;font-size:10px;color:#9CA3AF;"></th>
                        <?php for ($h = $startHr; $h < $endHr; $h++): ?>
                        <th style="padding:4px;text-align:center;font-size:10px;color:#6B7280;direction:ltr;"><?= sprintf('%02d', $h) ?></th>
                        <?php endfor; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($dayNames as $dow => $dayName): ?>
                    <tr>
                        <td style="padding:4px 8px;font-size:11px;color:#374151;font-weight:600;white-space:nowrap;"><?= e($dayName) ?></td>
                        <?php for ($h = $startHr; $h < $endHr; $h++):
                            $cnt = $heatmap[$dow][$h] ?? 0;
                            $maxCnt = 10;
                            $intensity = min(1, $cnt / $maxCnt);
                            if ($cnt === 0) {
                                $bg = '#F3F4F6';
                                $fg = '#9CA3AF';
                            } elseif ($intensity < 0.3) {
                                $bg = '#D1FAE5';
                                $fg = '#059669';
                            } elseif ($intensity < 0.6) {
                                $bg = '#FEF3C7';
                                $fg = '#D97706';
                            } elseif ($intensity < 0.85) {
                                $bg = '#FED7AA';
                                $fg = '#EA580C';
                            } else {
                                $bg = '#FEE2E2';
                                $fg = '#DC2626';
                            }
                        ?>
                        <td style="padding:2px;">
                            <div style="width:100%;height:28px;background:<?= $bg ?>;border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:10px;font-weight:700;color:<?= $fg ?>;" title="<?= e($dayName) ?> <?= sprintf('%02d:00', $h) ?> — <?= $cnt ?> حجز">
                                <?= $cnt > 0 ? $cnt : '' ?>
                            </div>
                        </td>
                        <?php endfor; ?>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <div style="margin-top:10px;display:flex;gap:8px;align-items:center;font-size:10px;color:#6B7280;">
                <span>أقل</span>
                <span style="width:16px;height:12px;background:#F3F4F6;border-radius:2px;display:inline-block;"></span>
                <span style="width:16px;height:12px;background:#D1FAE5;border-radius:2px;display:inline-block;"></span>
                <span style="width:16px;height:12px;background:#FEF3C7;border-radius:2px;display:inline-block;"></span>
                <span style="width:16px;height:12px;background:#FED7AA;border-radius:2px;display:inline-block;"></span>
                <span style="width:16px;height:12px;background:#FEE2E2;border-radius:2px;display:inline-block;"></span>
                <span>أكثر</span>
            </div>
        </div>
    </div>

    <!-- Breakdown by Type -->
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="pie-chart" style="width:18px;height:18px;color:#2563EB;"></i>
            <h3 style="margin:0;color:#2563EB;font-size:15px;">حسب نوع النشاط</h3>
        </div>
        <div style="padding:15px 20px;">
            <?php if (!empty($typeBreakdown)):
                $typeColors = ['lap_swimming'=>'#0D7377','lessons'=>'#2563EB','free_swim'=>'#059669','academy_session'=>'#7C3AED','competition'=>'#D97706','maintenance'=>'#EF4444'];
                $totalBookings = array_sum(array_column($typeBreakdown, 'cnt'));
            ?>
            <?php foreach ($typeBreakdown as $tb):
                $pct = $totalBookings > 0 ? round(((int)$tb['cnt'] / $totalBookings) * 100) : 0;
                $color = $typeColors[$tb['booking_type']] ?? '#6B7280';
            ?>
            <div style="margin-bottom:12px;">
                <div style="display:flex;justify-content:space-between;font-size:12px;margin-bottom:4px;">
                    <span style="color:#374151;font-weight:600;"><?= e($bookingTypes[$tb['booking_type']] ?? $tb['booking_type']) ?></span>
                    <span style="color:#6B7280;"><?= (int) $tb['cnt'] ?> حجز (<?= (int) $tb['swimmers'] ?> سبّاح)</span>
                </div>
                <div style="height:8px;background:#F3F4F6;border-radius:4px;overflow:hidden;">
                    <div style="height:100%;width:<?= $pct ?>%;background:<?= $color ?>;border-radius:4px;transition:width 0.5s;"></div>
                </div>
            </div>
            <?php endforeach; ?>
            <?php else: ?>
            <p style="color:#9CA3AF;font-size:13px;text-align:center;padding:20px 0;">لا توجد بيانات هذا الشهر</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <!-- Lane Utilization -->
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="bar-chart-3" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">استخدام الحارات (هذا الشهر)</h3>
        </div>
        <div style="padding:20px;">
            <?php foreach ($lanes as $lane):
                $laneNum = (int) $lane['lane_number'];
                $minutes = $laneMinutes[$laneNum] ?? 0;
                $lanePct = $totalAvailableMinutes > 0 ? round(($minutes / $totalAvailableMinutes) * 100) : 0;
                $laneHrs = round($minutes / 60, 1);
                $barColor = $lanePct > 70 ? '#059669' : ($lanePct > 40 ? '#D97706' : '#EF4444');
            ?>
            <div style="margin-bottom:14px;">
                <div style="display:flex;justify-content:space-between;font-size:12px;margin-bottom:4px;">
                    <span style="font-weight:600;color:#1A1A2E;"><?= e($lane['label_ar'] ?? ('حارة ' . $laneNum)) ?></span>
                    <span style="color:#6B7280;"><?= $laneHrs ?> ساعة — <?= $lanePct ?>%</span>
                </div>
                <div style="height:12px;background:#F3F4F6;border-radius:6px;overflow:hidden;">
                    <div style="height:100%;width:<?= $lanePct ?>%;background:<?= $barColor ?>;border-radius:6px;transition:width 0.5s;"></div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- Peak Hours + Recommendations -->
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="trending-up" style="width:18px;height:18px;color:#DC2626;"></i>
            <h3 style="margin:0;color:#DC2626;font-size:15px;">أوقات الذروة</h3>
        </div>
        <div style="padding:20px;">
            <?php if (!empty($peakHours)): ?>
            <?php foreach ($peakHours as $i => $ph):
                $hr = (int) $ph['hr'];
                $barW = (int) $ph['cnt'];
                $maxBar = (int) ($peakHours[0]['cnt'] ?? 1);
                $pctBar = round(($barW / max($maxBar, 1)) * 100);
            ?>
            <div style="display:flex;align-items:center;gap:12px;margin-bottom:12px;">
                <span style="min-width:50px;font-size:13px;font-weight:700;color:#1A1A2E;direction:ltr;"><?= sprintf('%02d:00', $hr) ?></span>
                <div style="flex:1;height:20px;background:#F3F4F6;border-radius:6px;overflow:hidden;">
                    <div style="height:100%;width:<?= $pctBar ?>%;background:linear-gradient(90deg, #DC2626, #F59E0B);border-radius:6px;"></div>
                </div>
                <span style="font-size:12px;color:#6B7280;min-width:40px;"><?= (int) $ph['cnt'] ?> حجز</span>
            </div>
            <?php endforeach; ?>

            <div style="margin-top:20px;padding:12px;background:#FEF3C7;border:1px solid #FDE68A;border-radius:8px;">
                <div style="font-size:12px;font-weight:600;color:#92400E;margin-bottom:6px;"><i data-lucide="lightbulb" style="width:13px;height:13px;vertical-align:middle;margin-left:4px;"></i> توصيات لزيادة الإشغال</div>
                <ul style="margin:0;padding:0 20px;font-size:11px;color:#92400E;line-height:1.8;">
                    <?php
                    $lowHours = [];
                    for ($h = $startHr; $h < $endHr; $h++) {
                        $found = false;
                        foreach ($peakHours as $ph) { if ((int)$ph['hr'] === $h) { $found = true; break; } }
                        if (!$found) $lowHours[] = sprintf('%02d:00', $h);
                    }
                    if (count($lowHours) > 3): ?>
                    <li>أوقات منخفضة الإشغال: <?= implode('، ', array_slice($lowHours, 0, 4)) ?> — ننصح بخصومات أو دروس مجانية</li>
                    <?php endif; ?>
                    <?php if ($utilPct < 50): ?>
                    <li>الإشغال الكلي <?= $utilPct ?>% — إمكانية إضافة حصص أكاديمية أو فتح حجز مؤسسي</li>
                    <?php endif; ?>
                    <li>استخدم الجداول الأسبوعية لضمان استمرارية الحجوزات في الأوقات الهادئة</li>
                </ul>
            </div>
            <?php else: ?>
            <p style="color:#9CA3AF;font-size:13px;text-align:center;padding:20px 0;">لا توجد بيانات كافية</p>
            <?php endif; ?>
        </div>
    </div>
</div>

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