<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isLoggedIn()) {
    redirect('../login.php');
}

$current_script = basename($_SERVER['SCRIPT_NAME'] ?? '');
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($page_title ?? 'پنل مدیریت') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;500;600;700&display=swap" rel="stylesheet">
    <?php
    $theme_path = (defined('ROOT_PATH') ? ROOT_PATH : dirname(__DIR__)) . '/assets/css/theme.css';
            $theme_ver = file_exists($theme_path) ? filemtime($theme_path) : '1';
            ?>
            <link href="../assets/css/theme.css?v=<?= $theme_ver ?>" rel="stylesheet">
</head>
<body>
    <header class="top-header">
        <h1 class="page-title"><?= htmlspecialchars($page_title ?? 'داشبورد') ?></h1>
        <div class="header-actions">
            <span class="user-badge">
                <i class="bi bi-person-circle"></i>
                <?= htmlspecialchars($_SESSION['username'] ?? 'کاربر') ?>
            </span>
            <a href="../logout.php" class="btn-icon" title="خروج"><i class="bi bi-box-arrow-right"></i></a>
        </div>
    </header>

    <div class="sidebar-wrap">
        <aside class="sidebar">
            <div class="brand">
                <h1><i class="bi bi-speedometer2"></i> داشبورد</h1>
            </div>
            <nav class="nav flex-column">
                <?php if (isAdmin()): ?>
                    <a class="nav-link <?= $current_script === 'dashboard.php' ? 'active' : '' ?>" href="../admin/dashboard.php">
                        <i class="bi bi-grid-1x2"></i> خلاصه
                    </a>
                    <a class="nav-link <?= $current_script === 'xui_management.php' ? 'active' : '' ?>" href="../admin/xui_management.php">
                        <i class="bi bi-gear-wide-connected"></i> مدیریت X-UI
                    </a>
                    <a class="nav-link <?= $current_script === 'panels.php' ? 'active' : '' ?>" href="../admin/panels.php">
                        <i class="bi bi-server"></i> پنل‌ها
                    </a>
                    <a class="nav-link <?= $current_script === 'panel_monitor.php' ? 'active' : '' ?>" href="../admin/panel_monitor.php">
                        <i class="bi bi-hdd-network"></i> چک پنل‌ها
                    </a>
                    <a class="nav-link <?= $current_script === 'representatives.php' ? 'active' : '' ?>" href="../admin/representatives.php">
                        <i class="bi bi-people"></i> نمایندگان
                    </a>
                    <a class="nav-link <?= $current_script === 'online_users.php' ? 'active' : '' ?>" href="../admin/online_users.php">
                        <i class="bi bi-broadcast"></i> کاربران آنلاین
                    </a>
                    <a class="nav-link <?= $current_script === 'monitoring.php' ? 'active' : '' ?>" href="../admin/monitoring.php">
                        <i class="bi bi-graph-up"></i> مانیتورینگ
                    </a>
                    <a class="nav-link <?= $current_script === 'tickets.php' ? 'active' : '' ?>" href="../admin/tickets.php">
                        <i class="bi bi-chat-dots"></i> تیکت‌ها
                    </a>
                    <a class="nav-link <?= $current_script === 'announcements.php' ? 'active' : '' ?>" href="../admin/announcements.php">
                        <i class="bi bi-megaphone"></i> اعلانات
                    </a>
                    <a class="nav-link <?= $current_script === 'backup.php' ? 'active' : '' ?>" href="../admin/backup.php">
                        <i class="bi bi-cloud-arrow-down"></i> پشتیبان‌گیری
                    </a>
                    <a class="nav-link <?= $current_script === 'settings.php' ? 'active' : '' ?>" href="../admin/settings.php">
                        <i class="bi bi-gear"></i> تنظیمات
                    </a>
                    <a class="nav-link <?= $current_script === 'profile.php' ? 'active' : '' ?>" href="../admin/profile.php">
                        <i class="bi bi-person-circle"></i> پروفایل
                    </a>
                <?php elseif (isRepresentative()): ?>
                    <a class="nav-link <?= $current_script === 'dashboard.php' ? 'active' : '' ?>" href="../representative/dashboard.php">
                        <i class="bi bi-grid-1x2"></i> خلاصه
                    </a>
                    <a class="nav-link <?= $current_script === 'users.php' ? 'active' : '' ?>" href="../representative/users.php">
                        <i class="bi bi-person-plus"></i> کاربران
                    </a>
                    <a class="nav-link <?= $current_script === 'user_credits.php' ? 'active' : '' ?>" href="../representative/user_credits.php">
                        <i class="bi bi-credit-card"></i> مدیریت اعتبارات
                    </a>
                    <a class="nav-link <?= $current_script === 'wallet.php' ? 'active' : '' ?>" href="../representative/wallet.php">
                        <i class="bi bi-wallet2"></i> کیف پول
                    </a>
                    <a class="nav-link <?= $current_script === 'tickets.php' ? 'active' : '' ?>" href="../representative/tickets.php">
                        <i class="bi bi-chat-dots"></i> تیکت
                    </a>
                    <a class="nav-link <?= $current_script === 'profile.php' ? 'active' : '' ?>" href="../admin/profile.php">
                        <i class="bi bi-person-circle"></i> پروفایل
                    </a>
                <?php endif; ?>
                <a class="nav-link logout" href="../logout.php">
                    <i class="bi bi-box-arrow-right"></i> خروج
                </a>
            </nav>
        </aside>

        <main class="main-content">
            <?php
            if (function_exists('isAdmin') && isAdmin() && strpos($_SERVER['SCRIPT_NAME'] ?? '', '/admin/') !== false) {
                require_once __DIR__ . '/admin_layout.php';
                admin_page_shell_open();
            }
            ?>
            <?php
            if (isRepresentative() && isset($pdo)) {
                try {
                    $ann_stmt = $pdo->query("SHOW TABLES LIKE 'announcements'");
                    if ($ann_stmt && $ann_stmt->rowCount() > 0) {
                        $ann = $pdo->query("SELECT id, title, content, type FROM announcements WHERE active = 1 AND (expires_at IS NULL OR expires_at > NOW()) ORDER BY created_at DESC");
                        if ($ann) {
                            $announcements = $ann->fetchAll();
                            foreach ($announcements as $a):
                                $type_class = ($a['type'] ?? '') === 'contest' ? 'warning' : (($a['type'] ?? '') === 'ad' ? 'info' : 'primary');
                                $type_label = ($a['type'] ?? '') === 'contest' ? 'جشواره' : (($a['type'] ?? '') === 'ad' ? 'تبلیغ' : 'پیام');
                            ?>
                            <div class="alert alert-<?= $type_class ?> alert-dismissible fade show mb-3" role="alert">
                                <strong><i class="bi bi-megaphone"></i> <?= htmlspecialchars($type_label) ?>:</strong> <?= htmlspecialchars($a['title']) ?>
                                <?php if (!empty($a['content'])): ?>
                                    <?php
                                    $safe_content = strip_tags($a['content'], '<p><br><strong><b><em><i><u><a><ul><ol><li><span><div><h1><h2><h3><small>');
                                    ?>
                                    <div class="mt-2 announcement-content"><?= $safe_content ?></div>
                                <?php endif; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="بستن"></button>
                            </div>
                            <?php
                            endforeach;
                        }
                    }
                } catch (Exception $e) { /* نادیده */ }
            }
            ?>
