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

<?php if($isAdmin): ?>
<div class="bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-6 md:p-8 mb-8 border-t-8 border-t-rose-500">
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-8 gap-4">
        <div>
            <h2 class="text-xl font-black text-gray-800 dark:text-white flex items-center gap-2">
                <span class="text-2xl">📊</span> رادار هوشمند وضعیت شبکه
            </h2>
            <p class="text-xs text-gray-500 dark:text-gray-400 font-bold mt-1">مانیتورینگ لحظه‌ای نقدینگی، ریسک مالی و حجم سرور</p>
        </div>
        <a href="provider.php" class="bg-cyan-50 dark:bg-cyan-900/30 text-cyan-600 dark:text-cyan-400 px-4 py-2 rounded-xl text-xs font-bold border border-cyan-200 dark:border-cyan-800/50 hover:bg-cyan-100 transition shadow-sm">مدیریت حساب سرور اصلی ←</a>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-12 gap-8">
        <div class="lg:col-span-7 bg-gray-50 dark:bg-slate-700/50 rounded-2xl p-6 border border-gray-200 dark:border-slate-600 transition-all duration-300" id="radar_box_1">
            <h3 class="text-sm font-black text-gray-700 dark:text-gray-300 mb-4">وضعیت سرمایه در گردش پنل</h3>
            <div class="flex justify-between text-sm font-bold mb-2">
                <span class="text-emerald-600 dark:text-emerald-400 flex items-center gap-1"><span class="w-2 h-2 rounded-full bg-emerald-500"></span> نقدینگی امن (<span id="radar_wallet_perc"><?php echo $walletPercentage; ?></span>%)</span>
                <span class="text-rose-600 dark:text-rose-400 flex items-center gap-1">بدهی اعتباری نمایندگان (<span id="radar_debt_perc"><?php echo $debtPercentage; ?></span>%) <span class="w-2 h-2 rounded-full bg-rose-500"></span></span>
            </div>
            <div class="h-6 w-full bg-gray-200 dark:bg-slate-700 rounded-full overflow-hidden flex shadow-inner mb-4">
                <div id="radar_wallet_bar" class="h-full bg-emerald-500 transition-all duration-1000 relative" style="width: <?php echo $walletPercentage; ?>%">
                    <span id="radar_wallet_text" class="absolute inset-0 flex items-center justify-center text-[10px] text-white font-bold <?php echo $walletPercentage <= 10 ? 'hidden' : ''; ?>"><?php echo number_format($totalWalletBalance); ?></span>
                </div>
                <div id="radar_debt_bar" class="h-full bg-rose-500 transition-all duration-1000 relative" style="width: <?php echo $debtPercentage; ?>%">
                    <span id="radar_debt_text" class="absolute inset-0 flex items-center justify-center text-[10px] text-white font-bold <?php echo $debtPercentage <= 10 ? 'hidden' : ''; ?>"><?php echo number_format($totalCreditDebt); ?></span>
                </div>
            </div>
            <div class="text-xs text-gray-500 dark:text-gray-400 font-bold">
                مجموع سرمایه درگیر شبکه فروش: <span id="radar_total_system" data-val="<?php echo $totalSystemMoney; ?>" class="font-black text-blue-600 dark:text-blue-400"><?php echo number_format($totalSystemMoney); ?> تومان</span>
            </div>
        </div>

        <div class="lg:col-span-5 grid grid-cols-2 gap-4">
            <div class="bg-cyan-50 dark:bg-cyan-900/20 p-4 rounded-2xl border border-cyan-100 dark:border-cyan-800 flex flex-col justify-center transition-all duration-300" id="radar_box_2">
                <span class="text-[10px] font-bold text-cyan-600 dark:text-cyan-400 mb-1">کل حجم فروش رفته</span>
                <span id="radar_total_gb" data-val="<?php echo $totalGBSold; ?>" class="text-2xl font-black text-cyan-700 dark:text-cyan-300"><?php echo number_format($totalGBSold); ?> <span class="text-xs font-normal">GB</span></span>
            </div>
            <div class="bg-rose-50 dark:bg-rose-900/20 p-4 rounded-2xl border border-rose-100 dark:border-rose-800 flex flex-col justify-center transition-all duration-300" id="radar_box_3">
                <span class="text-[10px] font-bold text-rose-600 dark:text-rose-400 mb-1">بدهی شما به سرور اصلی</span>
                <span id="radar_provider_debt" data-val="<?php echo $providerDebt; ?>" class="text-xl font-black text-rose-700 dark:text-rose-300" dir="ltr"><?php echo number_format($providerDebt); ?> <span class="text-[10px] font-normal">T</span></span>
            </div>
            <div class="col-span-2 bg-amber-50 dark:bg-amber-900/20 p-4 rounded-2xl border border-amber-100 dark:border-amber-800 flex justify-between items-center transition-all duration-300" id="radar_box_4">
                <span class="text-xs font-bold text-amber-600 dark:text-amber-400 mb-1">ارزش فیش‌های منتظر بررسی:</span>
                <span id="radar_pending_amt" data-val="<?php echo $radarPendingAmount; ?>" class="text-xl font-black text-amber-700 dark:text-amber-300" dir="ltr"><?php echo number_format($radarPendingAmount); ?> <span class="text-[10px] font-normal">T</span></span>
            </div>
        </div>
    </div>
</div>
<?php else: ?>
<div class="bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-6 md:p-8 mb-8 border-t-8 border-t-blue-500">
    <h2 class="text-xl font-black text-gray-800 dark:text-white mb-6 flex items-center gap-2">
        <span class="text-2xl">📊</span> رادار عملکرد و فروش شما
    </h2>
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        <div class="bg-blue-50 dark:bg-blue-900/20 p-5 rounded-2xl border border-blue-100 dark:border-blue-800 flex items-center gap-4 transition-all duration-300" id="radar_box_1">
            <div class="w-12 h-12 bg-blue-100 dark:bg-blue-800 text-blue-600 dark:text-blue-300 rounded-full flex items-center justify-center text-2xl shrink-0">🛒</div>
            <div>
                <span class="block text-xs font-bold text-blue-600 dark:text-blue-400 mb-1">مجموع خریدهای شما (کل)</span>
                <span id="radar_total_buys" data-val="<?php echo $radarTotalBuys; ?>" class="text-xl font-black text-blue-800 dark:text-blue-200"><?php echo number_format($radarTotalBuys); ?> <span class="text-xs font-normal">سرویس</span></span>
            </div>
        </div>
        <div class="bg-emerald-50 dark:bg-emerald-900/20 p-5 rounded-2xl border border-emerald-100 dark:border-emerald-800 flex items-center gap-4 transition-all duration-300" id="radar_box_2">
            <div class="w-12 h-12 bg-emerald-100 dark:bg-emerald-800 text-emerald-600 dark:text-emerald-300 rounded-full flex items-center justify-center text-2xl shrink-0">💎</div>
            <div>
                <span class="block text-xs font-bold text-emerald-600 dark:text-emerald-400 mb-1">مجموع پرداختی به سیستم</span>
                <span id="radar_total_spent" data-val="<?php echo $radarTotalSpent; ?>" class="text-lg font-black text-emerald-800 dark:text-emerald-200" dir="ltr"><?php echo number_format($radarTotalSpent); ?> <span class="text-[10px] font-normal">T</span></span>
            </div>
        </div>
        <div class="bg-purple-50 dark:bg-purple-900/20 p-5 rounded-2xl border border-purple-100 dark:border-purple-800 flex items-center gap-4 transition-all duration-300" id="radar_box_3">
            <div class="w-12 h-12 bg-purple-100 dark:bg-purple-800 text-purple-600 dark:text-purple-300 rounded-full flex items-center justify-center text-2xl shrink-0">📈</div>
            <div>
                <span class="block text-[11px] font-bold text-purple-600 dark:text-purple-400 mb-1">نسبت فروش امروز</span>
                <div class="text-xs font-bold text-gray-500">
                    جدید: <span class="text-emerald-600" id="radar_today_new"><?php echo $todayNewCount ?? 0; ?></span> 
                    | تمدید: <span class="text-blue-600" id="radar_today_renew"><?php echo $todayRenewCount ?? 0; ?></span>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<h2 class="text-lg font-black text-gray-800 dark:text-white mb-4 mt-8">میانبرهای عملیاتی و دسترسی سریع</h2>
<div class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-8 gap-3 mb-8">
    <?php if ($isAdmin): ?>
        <a href="store/store.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🛒</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">فروشگاه</span>
        </a>
        <a href="inventory/inventory.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-emerald-50 dark:bg-emerald-900/30 text-emerald-600 dark:text-emerald-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">📦</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">انبار</span>
        </a>
        <a href="reports.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-purple-50 dark:bg-purple-900/30 text-purple-600 dark:text-purple-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">📊</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">گزارشات</span>
        </a>
        <a href="wallet/wallet.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border <?php echo ($pendingReceipts ?? 0) > 0 ? 'border-amber-400 bg-amber-50 dark:bg-amber-900/10' : 'border-gray-100 dark:border-slate-700'; ?> hover:-translate-y-1 transition-transform group relative">
            <?php if(($pendingReceipts ?? 0) > 0): ?><span class="absolute top-2 right-2 flex h-2 w-2"><span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-amber-400 opacity-75"></span><span class="relative inline-flex rounded-full h-2 w-2 bg-amber-500"></span></span><?php endif; ?>
            <div class="w-10 h-10 bg-amber-50 dark:bg-amber-900/30 text-amber-600 dark:text-amber-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">💳</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">امور مالی</span>
        </a>
        <a href="subscriptions/subscriptions.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-indigo-50 dark:bg-indigo-900/30 text-indigo-600 dark:text-indigo-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🌐</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">اشتراک‌ها</span>
        </a>
        <a href="users.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-pink-50 dark:bg-pink-900/30 text-pink-600 dark:text-pink-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">👥</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">کاربران</span>
        </a>
        <a href="provider.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-cyan-50 dark:bg-cyan-900/30 text-cyan-600 dark:text-cyan-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🏦</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">تسویه با سرور</span>
        </a>
        <a href="festivals.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-rose-50 dark:bg-rose-900/30 text-rose-600 dark:text-rose-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🎉</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">تخفیف‌ها</span>
        </a>
    <?php else: ?>
        <a href="store/store.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🛒</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">خرید سرویس</span>
        </a>
        <a href="wallet/wallet.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border <?php echo $creditDebt > 0 ? 'border-rose-400 bg-rose-50 dark:bg-rose-900/10' : 'border-gray-100 dark:border-slate-700'; ?> hover:-translate-y-1 transition-transform group relative">
            <?php if($creditDebt > 0): ?><span class="absolute top-2 right-2 flex h-2 w-2"><span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-rose-400 opacity-75"></span><span class="relative inline-flex rounded-full h-2 w-2 bg-rose-500"></span></span><?php endif; ?>
            <div class="w-10 h-10 <?php echo $creditDebt > 0 ? 'bg-rose-100 text-rose-600 dark:bg-rose-900/50 dark:text-rose-400' : 'bg-emerald-50 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400'; ?> rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">💳</div>
            <span class="text-xs font-bold <?php echo $creditDebt > 0 ? 'text-rose-600 dark:text-rose-400' : 'text-gray-700 dark:text-gray-300'; ?>">کیف پول</span>
        </a>
        <a href="subscriptions/subscriptions.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-purple-50 dark:bg-purple-900/30 text-purple-600 dark:text-purple-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🌐</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">مشتریان من</span>
        </a>
        <a href="reports.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-amber-50 dark:bg-amber-900/30 text-amber-600 dark:text-amber-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">📊</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">گزارشات</span>
        </a>
        <a href="settings.php" class="bg-white dark:bg-slate-800 rounded-2xl p-4 flex flex-col items-center justify-center gap-2 shadow-sm border border-gray-100 dark:border-slate-700 hover:-translate-y-1 transition-transform group">
            <div class="w-10 h-10 bg-cyan-50 dark:bg-cyan-900/30 text-cyan-600 dark:text-cyan-400 rounded-xl flex items-center justify-center text-xl group-hover:scale-110 transition-transform">🏦</div>
            <span class="text-xs font-bold text-gray-700 dark:text-gray-300">حساب‌های من</span>
        </a>
    <?php endif; ?>
</div>