<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ob_start();
session_start();
require_once __DIR__ . '/db.php';
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
require_once __DIR__ . '/jdf.php';
require_once __DIR__ . '/notif.php';
require_once __DIR__ . '/session.php';
require_once __DIR__ . '/fun.php';
session_login_wizwiz();

// Load admin info + language once for all pages
$stmt = $conn->prepare("SELECT * FROM admins LIMIT 1");
$stmt->execute();
$result_admins = $stmt->get_result();
$row_admins    = $result_admins->fetch_assoc();
$stmt->close();

$_lang_code = !empty($row_admins['lang']) ? $row_admins['lang'] : 'fa';
$_lang_file  = __DIR__ . '/../langs/lang_' . $_lang_code . '.php';
if (file_exists($_lang_file)) {
    include $_lang_file;
}
if (!isset($_LANG)) {
    $_LANG = [];
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>WizWiz Panel</title>
    <script src="./assets/js/tailwind.js"></script>
    <link rel="stylesheet" href="./assets/css/tailwind.output.css"/>
    <link rel="stylesheet" href="./monitor/gauge/css/asPieProgress.css">
    <script src="./assets/js/jquery.js"></script>
    <script src="./assets/wizwiz.js"></script>
    <script src="./assets/js/init-alpine.js"></script>
    <script src="./assets/js/alpine.js" defer></script>
    <style>
        @font-face {
            font-family: IRANSans;
            src: url('./assets/fonts/IRANSans.ttf') format('truetype');
        }
        * { font-family: IRANSans, Tahoma, sans-serif !important; box-sizing: border-box; }
        body { margin:0; padding:0; background:#f1f5f9; color:#1e293b; }

        /* ── Layout ── */
        #wiz-app    { display:flex; min-height:100vh; }
        #wiz-sidebar{ width:240px; min-height:100vh; background:#1e1b4b;
                      display:flex; flex-direction:column; flex-shrink:0;
                      position:sticky; top:0; height:100vh; overflow-y:auto; }
        #wiz-content{ flex:1; min-width:0; display:flex; flex-direction:column; min-height:100vh; }
        #wiz-topbar { background:#fff; border-bottom:1px solid #e2e8f0;
                      padding:0 24px; height:52px; display:flex; align-items:center;
                      justify-content:space-between; position:sticky; top:0; z-index:50;
                      box-shadow:0 1px 3px rgba(0,0,0,.06); }
        #wiz-main   { flex:1; padding:24px; overflow-y:auto; }

        /* ── Sidebar nav links ── */
        #wiz-sidebar a { transition: background .15s, color .15s; }
        #wiz-sidebar a:hover { background:rgba(139,92,246,.15) !important; color:#c4b5fd !important; }

        /* ── Cards ── */
        .wiz-card { background:#fff; border-radius:12px; border:1px solid #e2e8f0;
                    box-shadow:0 1px 3px rgba(0,0,0,.06); padding:20px; }

        /* ── Tables ── */
        table { border-collapse:collapse; width:100%; }
        thead tr { background:#f8fafc; }
        thead th { color:#64748b; font-size:12px; font-weight:600; text-transform:uppercase;
                   letter-spacing:.05em; padding:10px 14px; border-bottom:2px solid #e2e8f0; }
        tbody tr { border-bottom:1px solid #f1f5f9; transition:background .1s; }
        tbody tr:hover { background:#faf5ff; }
        tbody td { padding:10px 14px; color:#334155; font-size:13px; }

        /* ── Inputs ── */
        input[type="text"], input[type="number"], input[type="url"],
        input[type="password"], select, textarea {
            background:#fff !important; border:1px solid #cbd5e1 !important;
            border-radius:8px !important; color:#1e293b !important; padding:8px 12px;
        }
        input:focus, select:focus, textarea:focus {
            border-color:#7c3aed !important; outline:none !important;
            box-shadow:0 0 0 3px rgba(124,58,237,.12) !important;
        }

        /* ── Buttons ── */
        .btn-primary { background:#7c3aed; color:#fff; border:none; border-radius:8px;
                       padding:8px 18px; cursor:pointer; font-size:13px; transition:background .15s; }
        .btn-primary:hover { background:#6d28d9; }
        .btn-danger  { background:#ef4444; color:#fff; border:none; border-radius:8px; padding:8px 18px; cursor:pointer; }
        .btn-success { background:#22c55e; color:#fff; border:none; border-radius:8px; padding:8px 18px; cursor:pointer; }

        /* ── Flash ── */
        #myDiv { position:fixed; top:16px; right:20px; z-index:9999; border-radius:10px; }

        /* ── Scrollbar ── */
        ::-webkit-scrollbar { width:5px; height:5px; background:#f1f5f9; }
        ::-webkit-scrollbar-thumb { background:#c4b5fd; border-radius:3px; }

        /* ── Dark-mode compat: preserve existing page classes ── */
        .bg-white { background:#fff !important; }
        .dark\:bg-gray-800 { background:#fff !important; }
        .dark\:text-gray-400 { color:#475569 !important; }
        .text-gray-700, .dark\:text-gray-200 { color:#374151 !important; }
        .border-gray-200, .dark\:border-gray-700 { border-color:#e2e8f0 !important; }
        .bg-gray-50, .dark\:bg-gray-700 { background:#f8fafc !important; }
        .shadow-sm { box-shadow:0 1px 3px rgba(0,0,0,.08) !important; }
        .rounded-xl, .rounded-lg { border-radius:12px !important; }

        /* ── Mobile toggle ── */
        #sidebar-toggle { display:none; }
        @media(max-width:767px) {
            #sidebar-toggle { display:flex; }
            #wiz-sidebar { position:fixed; left:0; top:0; z-index:200; transform:translateX(-100%); transition:transform .25s; }
            #wiz-sidebar.open { transform:translateX(0); }
        }
    </style>
</head>
<body id="result">
<?php session_notif_wizwiz(); ?>

<div id="wiz-app" x-data="data()">
    <!-- Sidebar -->
    <aside id="wiz-sidebar">
        <!-- Logo -->
        <div style="padding:16px 20px;border-bottom:1px solid rgba(255,255,255,.1);display:flex;align-items:center;gap:10px;flex-shrink:0">
            <img width="34px" src="./icons/wizwiz.png" style="border-radius:8px" alt="WizWiz">
            <a style="font-size:16px;font-weight:700;color:#c4b5fd;text-decoration:none" href="index.php">
                WizWiz
                <span style="font-size:10px;background:rgba(139,92,246,.3);color:#a78bfa;border-radius:4px;padding:2px 6px;margin-right:4px">v10.3.1</span>
            </a>
        </div>
        <!-- Nav -->
        <?php include __DIR__ . '/sidebar.php'; ?>
    </aside>

    <!-- Main content wrapper -->
    <div id="wiz-content">
        <!-- Top bar -->
        <div id="wiz-topbar">
            <div style="display:flex;align-items:center;gap:12px">
                <button id="sidebar-toggle" onclick="document.getElementById('wiz-sidebar').classList.toggle('open')"
                        style="background:none;border:none;cursor:pointer;padding:4px;color:#6b7280">
                    <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M4 6h16M4 12h16M4 18h16" stroke-linecap="round"/>
                    </svg>
                </button>
                <span style="font-weight:600;color:#4c1d95;font-size:15px">
                    <?php echo htmlspecialchars($row_admins['username'] ?? 'Admin'); ?>
                </span>
            </div>
            <div style="display:flex;align-items:center;gap:10px">
                <span style="font-size:12px;color:#94a3b8"><?php echo date('Y/m/d'); ?></span>
                <a href="exit.php" style="font-size:12px;color:#ef4444;text-decoration:none;padding:4px 10px;border:1px solid #fca5a5;border-radius:6px">خروج</a>
            </div>
        </div>

        <!-- Page content -->
        <div id="wiz-main">
