<?php
// modules/dashboard/widgets/live_monitoring.php
if (basename($_SERVER['PHP_SELF']) == 'live_monitoring.php') { die('Access Denied'); }
?>
<div class="lg:col-span-5 bg-gray-900 dark:bg-black rounded-3xl shadow-xl border border-gray-800 p-6 flex flex-col relative overflow-hidden">
    <div class="absolute -right-4 -top-4 opacity-5 text-7xl">📡</div>
    <h2 class="text-lg font-black text-white flex items-center gap-2 mb-2 relative z-10">
        <span class="flex h-2.5 w-2.5 relative">
          <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span>
          <span class="relative inline-flex rounded-full h-2.5 w-2.5 bg-red-500"></span>
        </span>
        تایم‌لاین زنده شبکه
    </h2>
    <p class="text-[11px] text-gray-400 mb-6 font-bold border-b border-gray-800 pb-3 relative z-10">موارد جدید با افکت نوری ظاهر می‌شوند.</p>
    
    <div class="flex-1 overflow-y-auto custom-scrollbar pr-2 relative z-10" style="max-height: 500px;">
        <div id="timeline_container" class="space-y-3">
            <?php if(empty($recentActivity)): ?>
                <div id="empty_timeline" class="text-center text-gray-500 text-xs py-10 font-bold">در حال شنود شبکه...</div>
            <?php else: foreach($recentActivity as $act): 
                $opName = ($act['fullname']) ? htmlspecialchars($act['fullname']) : (empty($act['username']) ? 'سیستم' : htmlspecialchars($act['username']));
                $timeOnly = date('H:i', strtotime($act['created_at']));
                
                $isEmergency = (strpos($act['extra'] ?? '', 'اضطراری') !== false);
                $bgClass = $isEmergency ? 'bg-rose-900/30 border-rose-800/50' : 'bg-gray-800/50 border-gray-700/50';
                $uid = $act['act_type'] . '_' . $act['ref_id'];
                
                $icon = '🔔'; $color = 'text-blue-400';
                if($act['act_type'] == 'sale') { $icon = '🛒'; $color = 'text-emerald-400'; }
                if($act['act_type'] == 'receipt') { $icon = '📄'; $color = 'text-amber-400'; }
                if($act['act_type'] == 'wallet') {
                    if($act['plan'] == 'bonus_add') { $icon = '🎁'; $color = 'text-purple-400'; }
                    elseif($act['plan'] == 'manual_sub') { $icon = '➖'; $color = 'text-rose-400'; }
                    else { $icon = '💳'; $color = 'text-indigo-400'; }
                }
                if($isEmergency) { $icon = '🚨'; $color = 'text-rose-500'; }
                
                $volDisplay = ($act['volume_unit'] === 'MB') ? ($act['volume'] . 'M') : ($act['volume'] . 'G');
            ?>
                <div data-uid="<?php echo $uid; ?>" class="flex items-start gap-3 p-3.5 rounded-xl border <?php echo $bgClass; ?>">
                    <div class="w-8 h-8 rounded-full bg-black/30 flex items-center justify-center shrink-0 shadow-inner <?php echo $color; ?> text-sm mt-1"><?php echo $icon; ?></div>
                    
                    <div class="flex-1 w-full">
                        <div class="flex justify-between items-center mb-1.5">
                            <span class="font-bold text-gray-200 text-sm"><?php echo $opName; ?></span>
                            <span class="text-[11px] text-gray-500 font-mono bg-gray-800 px-2 py-0.5 rounded-lg" dir="ltr"><?php echo $timeOnly; ?></span>
                        </div>
                        
                        <?php if($act['act_type'] == 'sale'): ?>
                            <div class="text-xs font-bold text-gray-300 leading-tight mb-2 bg-gray-800/50 p-2 rounded border border-gray-700/50 inline-block">
                                طرح <?php echo htmlspecialchars($act['plan']); ?> <span class="text-blue-400 font-black">(<?php echo $volDisplay; ?>)</span>
                            </div>
                            <div class="grid grid-cols-2 <?php echo $isAdmin ? 'sm:grid-cols-3' : ''; ?> gap-2 mt-1 bg-black/30 p-2.5 rounded-xl border border-gray-700/50">
                                <div class="flex flex-col gap-1">
                                    <span class="text-gray-500 text-[10px] font-bold">پرداخت مشتری:</span>
                                    <span class="text-emerald-400 font-black text-sm" dir="ltr"><?php echo number_format($act['amount']); ?> <span class="text-[9px] font-normal">T</span></span>
                                </div>
                                <div class="flex flex-col gap-1">
                                    <span class="text-gray-500 text-[10px] font-bold">سود نماینده:</span>
                                    <span class="text-green-400 font-black text-sm" dir="ltr"><?php echo number_format($act['op_profit']); ?> <span class="text-[9px] font-normal">T</span></span>
                                </div>
                                <?php if($isAdmin): ?>
                                <div class="flex flex-col gap-1 col-span-2 sm:col-span-1 border-t sm:border-t-0 sm:border-r border-gray-700/50 pt-2 sm:pt-0 sm:pr-2">
                                    <span class="text-gray-500 text-[10px] font-bold">سود ادمین:</span>
                                    <span class="text-purple-400 font-black text-sm" dir="ltr"><?php echo number_format($act['ad_profit']); ?> <span class="text-[9px] font-normal">T</span></span>
                                </div>
                                <?php endif; ?>
                            </div>
                        <?php else: 
                            $title = $act['extra'] ?: 'فیش واریزی (منتظر تایید)';
                        ?>
                            <div class="text-xs font-bold text-gray-300 leading-tight mb-2"><?php echo $title; ?></div>
                            <div class="mt-2 bg-black/30 p-2.5 rounded-xl border border-gray-700/50 flex justify-between items-center">
                                <span class="text-gray-500 text-[10px] font-bold">مبلغ تراکنش:</span>
                                <span class="<?php echo $color; ?> font-black text-sm" dir="ltr"><?php echo number_format($act['amount']); ?> <span class="text-[9px] font-normal">T</span></span>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; endif; ?>
        </div>
    </div>
</div>