<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>لوحة التحكم<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:15px;margin-bottom:25px;">
    <div class="card" style="padding:20px;border-right:4px solid #0D7377;">
        <div style="font-size:28px;font-weight:700;color:#0D7377;"><?= number_format($total_active ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">أعضاء نشطون</div>
    </div>
    <div class="card" style="padding:20px;border-right:4px solid #059669;">
        <div style="font-size:28px;font-weight:700;color:#059669;"><?= number_format($new_this_month ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">جدد هذا الشهر</div>
    </div>
    <div class="card" style="padding:20px;border-right:4px solid #0284C7;">
        <div style="font-size:28px;font-weight:700;color:#0284C7;"><?= money($total_revenue_month ?? '0') ?></div>
        <div style="color:#6B7280;font-size:13px;">إيرادات الشهر</div>
    </div>
    <div class="card" style="padding:20px;border-right:4px solid #D97706;">
        <div style="font-size:28px;font-weight:700;color:#D97706;"><?= (int) ($pending_interviews ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">مقابلات معلقة</div>
    </div>
    <div class="card" style="padding:20px;border-right:4px solid #DC2626;">
        <div style="font-size:28px;font-weight:700;color:#DC2626;"><?= (int) ($overdue_installments ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">أقساط متأخرة</div>
    </div>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;">
    <div>
        <!-- Revenue Chart -->
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#0D7377;">الإيرادات الشهرية</h3></div>
            <div style="padding:20px;" id="revenue-chart">
                <?php $maxRevenue = max(array_column($monthly_revenue ?? [], 'total') ?: [1]); ?>
                <div style="display:flex;align-items:flex-end;gap:8px;height:200px;">
                    <?php foreach (($monthly_revenue ?? []) as $mr): ?>
                    <?php $pct = $maxRevenue > 0 ? ((float) $mr['total'] / (float) $maxRevenue) * 100 : 0; ?>
                    <div style="flex:1;text-align:center;">
                        <div style="background:#0D7377;height:<?= max(4, $pct) ?>%;border-radius:4px 4px 0 0;min-height:4px;transition:height 0.3s;" title="<?= money($mr['total']) ?>"></div>
                        <div style="font-size:10px;color:#6B7280;margin-top:4px;"><?= e(substr($mr['month'], 5)) ?></div>
                    </div>
                    <?php endforeach; ?>
                    <?php if (empty($monthly_revenue)): ?><div style="width:100%;text-align:center;color:#9CA3AF;padding:60px 0;">لا توجد بيانات</div><?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Branch Comparison -->
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#0D7377;">الأعضاء حسب الفرع</h3></div>
            <div style="padding:20px;">
                <?php $maxBranch = max(array_column($total_members_by_branch ?? [], 'cnt') ?: [1]); ?>
                <?php foreach (($total_members_by_branch ?? []) as $br): ?>
                <div style="margin-bottom:12px;">
                    <div style="display:flex;justify-content:space-between;margin-bottom:4px;"><span style="font-size:13px;"><?= e($br['name_ar']) ?></span><strong><?= number_format((int) $br['cnt']) ?></strong></div>
                    <div style="background:#E5E7EB;border-radius:4px;height:8px;"><div style="background:#0D7377;border-radius:4px;height:8px;width:<?= $maxBranch > 0 ? ((int) $br['cnt'] / (int) $maxBranch) * 100 : 0 ?>%;"></div></div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <div>
        <!-- Alerts -->
        <?php if (!empty($alerts)): ?>
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#DC2626;">⚠ تنبيهات</h3></div>
            <div style="padding:15px;">
                <?php foreach ($alerts as $alert): ?>
                <a href="<?= e($alert['link'] ?? '#') ?>" style="display:block;padding:10px;margin-bottom:8px;border-radius:6px;background:<?= match($alert['type']) { 'danger' => '#FEF2F2', 'warning' => '#FFF7ED', 'info' => '#EFF6FF', default => '#F9FAFB' } ?>;border:1px solid <?= match($alert['type']) { 'danger' => '#FECACA', 'warning' => '#FED7AA', 'info' => '#BFDBFE', default => '#E5E7EB' } ?>;color:<?= match($alert['type']) { 'danger' => '#DC2626', 'warning' => '#D97706', 'info' => '#0284C7', default => '#6B7280' } ?>;font-size:13px;font-weight:600;text-decoration:none;">
                    <?= e($alert['message']) ?>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Recent Activity -->
        <div class="card">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#0D7377;">النشاط الأخير</h3></div>
            <div style="padding:10px 15px;max-height:400px;overflow-y:auto;">
                <?php foreach (($recent_activity ?? []) as $act): ?>
                <div style="padding:8px 0;border-bottom:1px solid #F3F4F6;font-size:12px;">
                    <div style="display:flex;justify-content:space-between;">
                        <strong style="color:#1A1A2E;"><?= e($act['employee_name'] ?? 'النظام') ?></strong>
                        <span style="color:#9CA3AF;"><?= e(substr($act['created_at'], 11, 5)) ?></span>
                    </div>
                    <div style="color:#6B7280;"><?= e($act['action']) ?> — <?= e($act['entity_label'] ?? $act['entity_type'] ?? '') ?></div>
                </div>
                <?php endforeach; ?>
                <?php if (empty($recent_activity)): ?><div style="padding:20px;text-align:center;color:#9CA3AF;">لا يوجد نشاط</div><?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>