<?php
// modules/pasarguard/pasarguard.php
require_once '../../includes/auth.php';
checkLogin();
if (!isAdmin()) {
    header("Location: ../dashboard.php");
    exit;
}
require_once '../../config/db.php';
require_once '../../includes/header.php';

$activeTab = $_GET['tab'] ?? 'stats';
?>

<style>
    main .max-w-7xl { max-width: 100% !important; margin: 0 !important; padding-left: 0.5rem !important; padding-right: 0.5rem !important; }
</style>

<div class="mb-5 border-b border-gray-200 dark:border-slate-700 pb-2 overflow-x-auto text-sm flex gap-2 custom-scrollbar">
    <a href="?tab=stats" class="px-5 py-2.5 rounded-t-xl font-bold transition flex items-center gap-2 whitespace-nowrap <?php echo $activeTab == 'stats' ? 'bg-blue-600 text-white shadow-sm' : 'bg-gray-100 dark:bg-slate-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-slate-700'; ?>">
        🖥️ مانیتورینگ سرور
    </a>
    <a href="?tab=users" class="px-5 py-2.5 rounded-t-xl font-bold transition flex items-center gap-2 whitespace-nowrap <?php echo $activeTab == 'users' ? 'bg-blue-600 text-white shadow-sm' : 'bg-gray-100 dark:bg-slate-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-slate-700'; ?>">
        👥 کاربران پاسارگارد
    </a>
    <a href="?tab=create" class="px-5 py-2.5 rounded-t-xl font-bold transition flex items-center gap-2 whitespace-nowrap <?php echo $activeTab == 'create' ? 'bg-blue-600 text-white shadow-sm' : 'bg-gray-100 dark:bg-slate-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-slate-700'; ?>">
        ⚡ ساخت اکانت مستقیم
    </a>
    <a href="?tab=bulk_edit" class="px-5 py-2.5 rounded-t-xl font-bold transition flex items-center gap-2 whitespace-nowrap <?php echo $activeTab == 'bulk_edit' ? 'bg-blue-600 text-white shadow-sm' : 'bg-gray-100 dark:bg-slate-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-slate-700'; ?>">
        🛠️ ویرایش هوشمند دسته‌جمعی
    </a>
</div>

<div class="w-full pb-10">
    <?php if (isset($_GET['msg']) && $_GET['msg'] == 'success'): ?>
        <div class="bg-emerald-100 dark:bg-emerald-900/30 border border-emerald-400 dark:border-emerald-800 text-emerald-800 dark:text-emerald-400 px-5 py-4 rounded-2xl mb-6 font-bold flex flex-col gap-3 shadow-sm">
            <span class="flex items-center gap-2">✅ اکانت با موفقیت در سرور پاسارگارد ساخته شد!</span>
            <input type="text" readonly value="<?php echo htmlspecialchars($_GET['link']); ?>" class="w-full bg-white dark:bg-slate-800 border border-emerald-200 dark:border-emerald-700 p-2.5 rounded-xl outline-none font-mono text-sm cursor-pointer transition focus:ring-2 focus:ring-emerald-500 dark:text-emerald-300" dir="ltr" onclick="this.select(); document.execCommand('copy'); alert('کپی شد!');" title="برای کپی کلیک کنید">
        </div>
    <?php endif; ?>
    
    <?php if (isset($_GET['msg']) && $_GET['msg'] == 'error'): ?>
        <div class="bg-rose-100 dark:bg-rose-900/30 border border-red-400 dark:border-rose-800 text-rose-800 dark:text-rose-400 px-5 py-4 rounded-2xl mb-6 font-bold shadow-sm">
            ❌ خطا در ارتباط با سرور: 
            <span class="font-mono text-xs block mt-2 p-3 bg-white/60 dark:bg-black/30 rounded-xl border border-rose-200 dark:border-rose-700/50" dir="ltr"><?php echo htmlspecialchars($_GET['detail']); ?></span>
        </div>
    <?php endif; ?>

    <?php
    if ($activeTab == 'stats') {
        include 'widgets/psg_stats.php';
        include 'widgets/psg_nodes.php';
    } 
    elseif ($activeTab == 'users') {
        include 'widgets/psg_users.php';
    } 
    elseif ($activeTab == 'create') {
        include 'widgets/psg_create.php';
    }
    elseif ($activeTab == 'bulk_edit') {
        include 'widgets/psg_bulk_edit.php';
    }
    ?>
</div>

<?php require_once '../../includes/footer.php'; ?>