<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>لوحة المرفق — <?= e($facility['name_ar'] ?? '') ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/facilities/<?= (int) $facility['id'] ?>/dashboard/export?filter=<?= e($filterMode) ?>&date_from=<?= e($dateFrom) ?>&date_to=<?= e($dateTo) ?>" class="btn btn-outline" target="_blank">تحميل التقرير</a>
<a href="/facilities/<?= (int) $facility['id'] ?>" class="btn btn-outline">بيانات المرفق</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php
$bookerLabels = ['member' => 'عضو', 'non_member' => 'غير عضو', 'player' => 'لاعب', 'guest' => 'ضيف', 'carnet_guest' => 'ضيف بدعوة'];
$tierLabels = ['peak' => 'ذروة', 'off_peak' => 'عادي', 'standard' => 'عادي'];
?>

<!-- Filter Bar -->
<div class="card" style="padding:15px;margin-bottom:20px;">
    <form method="GET" action="/facilities/<?= (int) $facility['id'] ?>/dashboard" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">الفترة</label>
            <select name="filter" class="form-select" onchange="this.form.submit()">
                <option value="day" <?= $filterMode === 'day' ? 'selected' : '' ?>>اليوم</option>
                <option value="week" <?= $filterMode === 'week' ? 'selected' : '' ?>>هذا الأسبوع</option>
                <option value="month" <?= $filterMode === 'month' ? 'selected' : '' ?>>هذا الشهر</option>
                <option value="year" <?= $filterMode === 'year' ? 'selected' : '' ?>>هذه السنة</option>
                <option value="5years" <?= $filterMode === '5years' ? 'selected' : '' ?>>آخر 5 سنوات</option>
                <option value="custom" <?= $filterMode === 'custom' ? 'selected' : '' ?>>مخصص</option>
            </select>
        </div>
        <?php if ($filterMode === 'custom'): ?>
        <div><label class="form-label" style="font-size:12px;">من</label><input type="date" name="date_from" value="<?= e($dateFrom) ?>" class="form-input"></div>
        <div><label class="form-label" style="font-size:12px;">إلى</label><input type="date" name="date_to" value="<?= e($dateTo) ?>" class="form-input"></div>
        <button type="submit" class="btn btn-outline">تطبيق</button>
        <?php endif; ?>
        <div style="margin-right:auto;font-size:14px;font-weight:600;color:#1e3a5f;"><?= e($periodLabel) ?></div>
    </form>
</div>

<!-- Revenue Cards -->
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;border-top:3px solid #059669;">
        <div style="font-size:28px;font-weight:700;color:#059669;"><?= money($todayRevenue) ?></div>
        <div style="font-size:13px;color:#6B7280;">إيرادات اليوم</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;border-top:3px solid #2563EB;">
        <div style="font-size:28px;font-weight:700;color:#2563EB;"><?= money($weekRevenue) ?></div>
        <div style="font-size:13px;color:#6B7280;">إيرادات الأسبوع</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;border-top:3px solid #7C3AED;">
        <div style="font-size:28px;font-weight:700;color:#7C3AED;"><?= money($monthRevenue) ?></div>
        <div style="font-size:13px;color:#6B7280;">إيرادات الشهر</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;border-top:3px solid #D97706;">
        <div style="font-size:28px;font-weight:700;color:#D97706;"><?= money($yearRevenue) ?></div>
        <div style="font-size:13px;color:#6B7280;">إيرادات السنة</div>
    </div>
</div>

<!-- Period Summary -->
<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;margin-bottom:20px;">
    <div class="card" style="padding:20px;">
        <h4 style="margin-bottom:15px;">ملخص الفترة: <?= e($periodLabel) ?></h4>
        <div style="display:grid;grid-template-columns:repeat(4, 1fr);gap:12px;">
            <div style="text-align:center;background:#F0F9FF;padding:15px;border-radius:8px;">
                <div style="font-size:24px;font-weight:700;color:#1e3a5f;"><?= money($periodRevenue) ?></div>
                <div style="font-size:12px;color:#6B7280;">الإيرادات</div>
            </div>
            <div style="text-align:center;background:#F0FDF4;padding:15px;border-radius:8px;">
                <div style="font-size:24px;font-weight:700;color:#059669;"><?= count($periodBookings) ?></div>
                <div style="font-size:12px;color:#6B7280;">الحجوزات</div>
            </div>
            <div style="text-align:center;background:#FEF3C7;padding:15px;border-radius:8px;">
                <div style="font-size:24px;font-weight:700;color:#D97706;"><?= $uniqueVisitors ?></div>
                <div style="font-size:12px;color:#6B7280;">زوار فريدون</div>
            </div>
            <div style="text-align:center;background:#F5F3FF;padding:15px;border-radius:8px;">
                <div style="font-size:24px;font-weight:700;color:#7C3AED;"><?= $occupancyRate ?>%</div>
                <div style="font-size:12px;color:#6B7280;">نسبة الإشغال</div>
            </div>
        </div>
    </div>

    <div class="card" style="padding:20px;">
        <h4 style="margin-bottom:15px;">حسب نوع العميل</h4>
        <?php if (!empty($revenueByBookerType)): ?>
        <?php foreach ($revenueByBookerType as $bt): ?>
        <div style="display:flex;justify-content:space-between;align-items:center;padding:6px 0;border-bottom:1px solid #F3F4F6;">
            <span style="font-size:13px;"><?= e($bookerLabels[$bt['booker_type']] ?? $bt['booker_type']) ?></span>
            <div style="text-align:left;">
                <span style="font-weight:600;"><?= money($bt['revenue']) ?></span>
                <span style="font-size:11px;color:#6B7280;margin-right:5px;">(<?= (int) $bt['booking_count'] ?>)</span>
            </div>
        </div>
        <?php endforeach; ?>
        <?php else: ?>
        <p style="color:#6B7280;text-align:center;">لا توجد بيانات</p>
        <?php endif; ?>
    </div>
</div>

<!-- Peak Hours & Monthly Trend -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <div class="card" style="padding:20px;">
        <h4 style="margin-bottom:15px;">أوقات الذروة</h4>
        <?php if (!empty($peakHours)): ?>
        <?php $maxCnt = max(array_column($peakHours, 'cnt')); ?>
        <?php foreach (array_slice($peakHours, 0, 8) as $ph): ?>
        <div style="display:flex;align-items:center;gap:10px;margin-bottom:8px;">
            <span style="min-width:50px;font-size:13px;font-weight:600;direction:ltr;text-align:center;"><?= sprintf('%02d:00', (int) $ph['hr']) ?></span>
            <div style="flex:1;background:#F3F4F6;border-radius:4px;overflow:hidden;height:20px;">
                <div style="height:100%;background:#1e3a5f;width:<?= round(((int)$ph['cnt'] / $maxCnt) * 100) ?>%;border-radius:4px;"></div>
            </div>
            <span style="font-size:12px;color:#6B7280;min-width:30px;"><?= (int) $ph['cnt'] ?></span>
        </div>
        <?php endforeach; ?>
        <?php else: ?>
        <p style="color:#6B7280;text-align:center;">لا توجد بيانات</p>
        <?php endif; ?>
    </div>

    <div class="card" style="padding:20px;">
        <h4 style="margin-bottom:15px;">الاتجاه الشهري</h4>
        <?php if (!empty($monthlyTrend)): ?>
        <?php $maxRev = max(array_column($monthlyTrend, 'revenue')) ?: 1; ?>
        <div style="display:flex;align-items:end;gap:4px;height:150px;">
            <?php foreach ($monthlyTrend as $mt): ?>
            <div style="flex:1;display:flex;flex-direction:column;align-items:center;justify-content:end;height:100%;" title="<?= e($mt['month']) ?>: <?= money($mt['revenue']) ?>">
                <div style="width:100%;background:#1e3a5f;border-radius:3px 3px 0 0;min-height:4px;height:<?= round(((float)$mt['revenue'] / $maxRev) * 100) ?>%;"></div>
                <span style="font-size:9px;color:#6B7280;margin-top:4px;"><?= substr($mt['month'], 5) ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php else: ?>
        <p style="color:#6B7280;text-align:center;">لا توجد بيانات</p>
        <?php endif; ?>
    </div>
</div>

<!-- Today's Schedule & Upcoming -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
    <div class="card" style="padding:20px;">
        <h4 style="margin-bottom:15px;">حجوزات اليوم (<?= count($todayBookings) ?>)</h4>
        <?php if (empty($todayBookings)): ?>
        <p style="color:#6B7280;text-align:center;">لا توجد حجوزات اليوم</p>
        <?php else: ?>
        <div style="max-height:300px;overflow-y:auto;">
            <?php foreach ($todayBookings as $b): ?>
            <div style="display:flex;justify-content:space-between;padding:8px;border-bottom:1px solid #F3F4F6;font-size:13px;">
                <div>
                    <span style="font-weight:600;direction:ltr;"><?= e(substr($b['start_time'] ?? '', 0, 5)) ?>—<?= e(substr($b['end_time'] ?? '', 0, 5)) ?></span>
                    <span style="margin-right:8px;"><?= e($b['booker_name'] ?? '—') ?></span>
                </div>
                <span style="font-weight:600;color:#059669;"><?= money($b['total_amount'] ?? 0) ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>

    <div class="card" style="padding:20px;">
        <h4 style="margin-bottom:15px;">الحجوزات القادمة</h4>
        <?php if (empty($upcomingBookings)): ?>
        <p style="color:#6B7280;text-align:center;">لا توجد حجوزات قادمة</p>
        <?php else: ?>
        <div style="max-height:300px;overflow-y:auto;">
            <?php foreach ($upcomingBookings as $b): ?>
            <div style="display:flex;justify-content:space-between;padding:8px;border-bottom:1px solid #F3F4F6;font-size:13px;">
                <div>
                    <span style="color:#6B7280;"><?= e($b['reservation_date'] ?? '') ?></span>
                    <span style="font-weight:600;margin-right:5px;"><?= e(substr($b['start_time'] ?? '', 0, 5)) ?></span>
                    <span><?= e($b['booker_name'] ?? '—') ?></span>
                </div>
                <span style="font-weight:600;"><?= money($b['total_amount'] ?? 0) ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php $__template->endSection(); ?>
