<?php
// modules/dashboard/widgets/top_stats.php
if (basename($_SERVER['PHP_SELF']) == 'top_stats.php') { die('Access Denied'); }
?>

<?php if ($isAdmin): ?>
    <div class="flex flex-col md:flex-row justify-between items-center bg-white dark:bg-slate-800 rounded-2xl p-4 shadow-sm border border-gray-100 dark:border-slate-700 mb-6 gap-4">
        <div class="flex items-center gap-3 w-full md:w-auto">
             <div class="w-10 h-10 bg-blue-100 dark:bg-blue-900/50 text-blue-600 dark:text-blue-400 rounded-full flex items-center justify-center text-xl shrink-0">👨‍💻</div>
             <div class="flex-1">
                 <h1 class="text-sm font-black text-gray-800 dark:text-white mb-0.5">سلام <?php echo htmlspecialchars($displayName); ?></h1>
                 <div class="flex items-center gap-1.5">
                     <span class="relative flex h-2 w-2"><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 w-2 bg-red-500"></span></span>
                     <span class="text-[10px] text-gray-500 dark:text-gray-400 font-bold tracking-widest uppercase">اتاق فرمان و مانیتورینگ زنده</span>
                 </div>
             </div>
        </div>
        <form method="POST" class="flex w-full md:w-auto items-center gap-2">
            <input type="hidden" name="action" value="save_announcement">
            <input type="text" name="announcement_text" class="w-full md:w-96 bg-gray-50 dark:bg-slate-700 border border-gray-200 dark:border-slate-600 outline-none focus:border-amber-500 text-xs py-2.5 px-3 rounded-xl dark:text-white transition" placeholder="ثبت اطلاعیه سراسری سیستم..." value="<?php echo htmlspecialchars($globalAnnouncement); ?>">
            <button type="submit" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-2.5 px-4 rounded-xl text-xs transition shadow-sm shrink-0">ثبت اطلاعیه</button>
        </form>
    </div>
<?php else: ?>
    <?php if(!empty($globalAnnouncement)): ?>
    <div class="bg-gradient-to-r from-amber-500 to-orange-600 rounded-2xl p-4 mb-6 shadow-md flex items-start gap-4 text-white relative overflow-hidden">
        <div class="absolute -right-4 -top-4 opacity-20 text-6xl">📢</div>
        <div class="text-3xl relative z-10 hidden sm:block">📢</div>
        <div class="relative z-10 w-full">
            <div class="text-xs font-black text-amber-100 uppercase tracking-wider mb-1">اطلاعیه سراسری</div>
            <div class="text-sm font-bold leading-relaxed whitespace-pre-line"><?php echo nl2br($globalAnnouncement); ?></div>
        </div>
    </div>
    <?php endif; ?>
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
        <div class="lg:col-span-2 bg-gradient-to-l from-blue-700 to-indigo-800 rounded-3xl p-8 text-white relative overflow-hidden shadow-lg h-full flex flex-col justify-center">
            <div class="absolute -right-10 -bottom-10 opacity-10 text-9xl">🚀</div>
            <div class="relative z-10">
                <h1 class="text-3xl font-black mb-2">سلام <?php echo htmlspecialchars($displayName); ?>، خوش آمدید!</h1>
                <p class="text-indigo-200 font-bold text-sm">از طریق این داشبورد می‌توانید به ابزارهای شبکه دسترسی سریع داشته باشید.</p>
            </div>
        </div>
        <?php if($isBonusActive && $nextBonusTier): ?>
        <div class="lg:col-span-1 w-full bg-white dark:bg-slate-800 rounded-3xl p-6 shadow-sm border border-gray-100 dark:border-slate-700 border-t-8 border-t-fuchsia-500 flex flex-col justify-center relative overflow-hidden">
            <h3 class="text-sm font-black text-gray-800 dark:text-white mb-2 flex items-center gap-2">
                <span class="text-xl">🎁</span> جایزه بعدی شما!
            </h3>
            <p class="text-[11px] text-gray-500 dark:text-gray-400 font-bold mb-4 leading-relaxed">
                برای رسیدن به سطح <span class="text-fuchsia-600 dark:text-fuchsia-400 font-black">«<?php echo $nextBonusTier['tier_name']; ?>»</span> و دریافت <span class="font-black bg-fuchsia-100 dark:bg-fuchsia-900/30 text-fuchsia-700 dark:text-fuchsia-400 px-1 rounded"><?php echo $nextBonusTier['bonus_percent']; ?>٪</span> اعتبار هدیه، موجودی خود را افزایش دهید.
            </p>
            <div class="relative pt-1">
                <div class="flex justify-between items-end mb-1">
                    <span class="text-[10px] font-bold text-fuchsia-600 dark:text-fuchsia-400">پیشرفت: <?php echo round($bonusProgress); ?>٪</span>
                    <span class="text-[10px] font-mono text-gray-400" dir="ltr"><?php echo number_format($walletBalance); ?> / <?php echo number_format($nextBonusTier['min_amount']); ?></span>
                </div>
                <div class="overflow-hidden h-2.5 text-xs flex rounded-full bg-fuchsia-100 dark:bg-slate-700">
                    <div style="width: <?php echo $bonusProgress; ?>%" class="shadow-none flex flex-col text-center whitespace-nowrap text-white justify-center bg-fuchsia-500 transition-all duration-1000"></div>
                </div>
            </div>
            <a href="wallet/wallet.php" class="mt-4 text-[11px] font-bold text-center bg-gray-50 dark:bg-slate-700 text-gray-600 dark:text-gray-300 py-2 rounded-xl hover:bg-gray-100 dark:hover:bg-slate-600 transition">شارژ کیف پول ←</a>
        </div>
        <?php endif; ?>
    </div>
<?php endif; ?>

<div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-4 mb-8">
    <?php if($isAdmin): ?>
        <a href="wallet/wallet.php" class="block bg-white dark:bg-slate-800 p-4 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-amber-500 relative transition-all duration-300 hover:shadow-md hover:-translate-y-1 min-h-[120px] flex flex-col">
            <div class="flex justify-between items-center mb-1.5 shrink-0">
                <span class="text-[10px] font-bold text-gray-500 dark:text-gray-400">فیش‌های منتظر تایید</span>
                <span id="live_pending" data-val="<?php echo $pendingReceipts; ?>" class="text-xl font-black text-amber-500 transition-all duration-300"><?php echo $pendingReceipts; ?></span>
            </div>
            <div id="live_pending_list" class="flex-1 overflow-y-auto custom-scrollbar text-[9px] text-gray-600 dark:text-gray-400 space-y-1">
                <?php 
                if(empty($initPend)) echo '<span>موردی نیست</span>';
                foreach($initPend as $p): ?>
                    <div class="flex justify-between border-b border-gray-50 dark:border-slate-700/50 pb-0.5"><span class="truncate w-16"><?php echo htmlspecialchars($p['op_name']); ?></span><span class="font-mono text-amber-600 dark:text-amber-400" dir="ltr"><?php echo number_format($p['amount']); ?></span></div>
                <?php endforeach; ?>
            </div>
        </a>
        <a href="inventory/inventory.php" class="block bg-white dark:bg-slate-800 p-4 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-rose-500 relative transition-all duration-300 hover:shadow-md hover:-translate-y-1 min-h-[120px] flex flex-col">
            <div class="flex justify-between items-center mb-1.5 shrink-0">
                <span class="text-[10px] font-bold text-gray-500 dark:text-gray-400">هشدار موجودی انبار</span>
                <span id="live_stock" data-val="<?php echo $lowStockCount; ?>" class="text-xl font-black text-rose-500 transition-all duration-300"><?php echo $lowStockCount; ?></span>
            </div>
            <div id="live_stock_list" class="flex-1 overflow-y-auto custom-scrollbar text-[9px] text-gray-600 dark:text-gray-400 space-y-1">
                <?php 
                if(empty($initStock)) echo '<span>موجودی تکمیل است</span>';
                foreach($initStock as $s): ?>
                    <div class="flex justify-between border-b border-gray-50 dark:border-slate-700/50 pb-0.5"><span class="truncate w-16"><?php echo htmlspecialchars($s['plan_name']); ?> (<?php echo $s['volume']; ?>G)</span><span class="font-mono font-bold text-rose-600 dark:text-rose-400"><?php echo $s['qty']; ?> عدد</span></div>
                <?php endforeach; ?>
            </div>
        </a>
    <?php else: ?>
        <div class="bg-white dark:bg-slate-800 p-5 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-blue-500 relative overflow-hidden transition-all duration-300 min-h-[120px] flex flex-col justify-center">
            <span class="block text-[10px] font-bold text-gray-500 dark:text-gray-400 mb-1">موجودی کیف پول</span>
            <span class="text-xl font-black text-blue-600 dark:text-blue-400" dir="ltr"><?php echo number_format($walletBalance); ?> <span class="text-[10px]">T</span></span>
        </div>
        <div class="bg-white dark:bg-slate-800 p-5 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-rose-500 relative overflow-hidden transition-all duration-300 min-h-[120px] flex flex-col justify-center">
            <span class="block text-[10px] font-bold text-gray-500 dark:text-gray-400 mb-1">بدهی اعتباری</span>
            <span class="text-xl font-black text-rose-600 dark:text-rose-400" dir="ltr"><?php echo number_format($creditDebt); ?> <span class="text-[10px]">T</span></span>
        </div>
    <?php endif; ?>

    <div class="bg-white dark:bg-slate-800 p-5 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-emerald-500 relative overflow-hidden transition-all duration-300 min-h-[120px] flex flex-col justify-center">
        <span class="block text-[10px] font-bold text-gray-500 dark:text-gray-400 mb-1">سود خالص امروز</span>
        <span id="live_profit" data-val="<?php echo $todayProfit; ?>" class="text-xl font-black text-emerald-600 dark:text-emerald-400 transition-all duration-300" dir="ltr"><?php echo number_format($todayProfit); ?> <span class="text-[10px]">T</span></span>
    </div>
    <div class="bg-white dark:bg-slate-800 p-5 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-cyan-500 relative overflow-hidden transition-all duration-300 min-h-[120px] flex flex-col justify-center">
        <span class="block text-[10px] font-bold text-gray-500 dark:text-gray-400 mb-1">تعداد فروش امروز</span>
        <span id="live_sales" data-val="<?php echo $todaySalesCount; ?>" class="text-2xl font-black text-cyan-600 dark:text-cyan-400 transition-all duration-300"><?php echo $todaySalesCount; ?></span>
    </div>
    <div class="bg-white dark:bg-slate-800 p-5 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 border-t-4 border-t-fuchsia-500 relative overflow-hidden transition-all duration-300 min-h-[120px] flex flex-col justify-center">
        <span class="block text-[10px] font-bold text-gray-500 dark:text-gray-400 mb-1">حجم فروش امروز</span>
        <span id="live_volume" data-val="<?php echo $todayVolume; ?>" class="text-2xl font-black text-fuchsia-600 dark:text-fuchsia-400 transition-all duration-300"><?php echo $todayVolume; ?> <span class="text-[10px] font-normal text-gray-400">GB</span></span>
    </div>
</div>