<?php
if(!isset($_SESSION)) session_start();

ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);
ini_set('session.use_strict_mode', 1);

if(!isset($_SESSION['admin_logged'])){
    header("Location: login.php");
    exit;
}

$currentPage = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GoMAPS Admin</title>

<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<style>
:root{
    --primary:#007185;
    --primary-dark:#005f6f;
    --light-bg:#f1f5f9;
    --card-bg:#ffffff;
    --text:#111;
    --muted:#555;
    --border:#e5e7eb;
}

body.dark{
    --light-bg:#0f172a;
    --card-bg:#1e293b;
    --text:#f1f5f9;
    --muted:#94a3b8;
    --border:#334155;
}

*{
    box-sizing:border-box;
}

html, body{
    margin:0;
    padding:0;
    font-family:'Segoe UI',system-ui,-apple-system,sans-serif;
    background:var(--light-bg);
    color:var(--text);
    height:100%;
    overflow-x:hidden;
}

/* ================= SIDEBAR ================= */

.sidebar{
    position:fixed;
    top:0;
    left:0;
    height:100vh;
    width:250px;
    background:linear-gradient(180deg,var(--primary),var(--primary-dark));
    color:#fff;
    padding:30px 20px;
    overflow-y:auto;
    box-shadow:4px 0 20px rgba(0,0,0,0.08);
}

.logo-title{
    display:flex;
    align-items:center;
    gap:12px;
    margin-bottom:35px;
    font-size:19px;
    font-weight:600;
    letter-spacing:.5px;
}

.logo-title img{
    width:36px;
    height:36px;
    object-fit:contain;
}

.sidebar a{
    display:flex;
    align-items:center;
    gap:12px;
    padding:12px 14px;
    border-radius:10px;
    margin-bottom:6px;
    color:#fff;
    text-decoration:none;
    font-size:14px;
    font-weight:500;
    transition:all .25s ease;
}

.sidebar a i{
    width:18px;
    text-align:center;
    font-size:14px;
}

.sidebar a:hover{
    background:rgba(255,255,255,0.18);
    transform:translateX(3px);
}

.sidebar a.active{
    background:rgba(255,255,255,0.28);
    font-weight:600;
    box-shadow:0 4px 10px rgba(0,0,0,0.15);
}

/* ================= MAIN ================= */

.main{
    margin-left:250px;
    min-height:100vh;
    display:flex;
    flex-direction:column;
}

/* ================= HEADER ================= */

.topbar{
    position:fixed;
    top:0;
    left:250px;
    right:0;
    height:70px;
    background:var(--card-bg);
    display:flex;
    justify-content:space-between;
    align-items:center;
    padding:0 30px;
    border-bottom:1px solid var(--border);
    backdrop-filter:blur(6px);
    box-shadow:0 2px 10px rgba(0,0,0,0.03);
    z-index:1000;
}

/* ================= CONTENT ================= */

.content{
    margin-top:70px;
    padding:30px;
    flex:1;
}

/* ================= PROFILE ================= */

.profile{
    position:relative;
}

.profile-menu{
    position:absolute;
    right:0;
    top:48px;
    background:var(--card-bg);
    border:1px solid var(--border);
    display:none;
    border-radius:12px;
    overflow:hidden;
    box-shadow:0 12px 30px rgba(0,0,0,0.08);
    min-width:170px;
}

.profile-menu a{
    display:block;
    padding:12px 16px;
    color:var(--text);
    text-decoration:none;
    font-size:14px;
}

.profile-menu a:hover{
    background:var(--light-bg);
}

/* ================= BUTTON ================= */

.btn{
    padding:8px 16px;
    border:none;
    border-radius:8px;
    background:var(--primary);
    color:#fff;
    cursor:pointer;
    font-size:14px;
    font-weight:500;
    transition:.2s ease;
}

.btn:hover{
    background:var(--primary-dark);
    transform:translateY(-1px);
}

/* ================= CARD ================= */

.card{
    background:var(--card-bg);
    padding:28px;
    border-radius:14px;
    border:1px solid var(--border);
    margin-bottom:22px;
    box-shadow:0 6px 18px rgba(0,0,0,0.04);
    transition:.2s ease;
}

.card:hover{
    transform:translateY(-3px);
    box-shadow:0 12px 28px rgba(0,0,0,0.06);
}

/* ================= FOOTER ================= */

.admin-footer{
    text-align:center;
    padding:18px;
    background:var(--card-bg);
    border-top:1px solid var(--border);
    color:var(--muted);
    font-size:13px;
}

/* ================= MOBILE ================= */

@media(max-width:768px){
    .sidebar{
        position:relative;
        width:100%;
        height:auto;
        box-shadow:none;
    }
    .main{
        margin-left:0;
    }
    .topbar{
        left:0;
    }
}
</style>
</head>
<body>

<div class="sidebar">
    <h2 class="logo-title">
    <img src="https://pritsco.com/android-apps/search-engine/assets/gomaps-logo.png" alt="GoMAPS Logo">
    GoMAPS
</h2>

    <a href="dashboard.php" class="<?= $currentPage=='dashboard.php'?'active':'' ?>">
        <i class="fa-solid fa-chart-line"></i> Dashboard
    </a>

    <a href="view_search_logs.php" class="<?= $currentPage=='view_search_logs.php'?'active':'' ?>">
        <i class="fa-solid fa-magnifying-glass-chart"></i> Search Logs
    </a>

    <a href="blocked_ips.php" class="<?= $currentPage=='blocked_ips.php'?'active':'' ?>">
        <i class="fa-solid fa-ban"></i> Blocked IPs
    </a>

    <a href="recommend_sponsored.php" class="<?= $currentPage=='recommend_sponsored.php'?'active':'' ?>">
        <i class="fa-solid fa-robot"></i> Auto Recommend
    </a>

    <a href="add_sponsored.php" class="<?= $currentPage=='add_sponsored.php'?'active':'' ?>">
        <i class="fa-solid fa-plus-circle"></i> Add Sponsored
    </a>

    <a href="view_sponsored.php" class="<?= $currentPage=='view_sponsored.php'?'active':'' ?>">
        <i class="fa-solid fa-list-check"></i> View Sponsored
    </a>

    <a href="activity_logs.php" class="<?= $currentPage=='activity_logs.php'?'active':'' ?>">
        <i class="fa-solid fa-clock-rotate-left"></i> Activity Logs
    </a>

    <a href="logout.php">
        <i class="fa-solid fa-right-from-bracket"></i> Logout
    </a>
</div>

<div class="main">

<div class="topbar">
    <button class="btn" onclick="toggleDark()">Toggle Dark</button>

    <div class="profile">
        <button class="btn" onclick="toggleProfile()">
            <?= $_SESSION['admin_name'] ?> ▼
        </button>
        <div class="profile-menu" id="profileMenu">
            <a href="#" onclick="toggleDark()">Dark Mode</a>
            <a href="/android-apps/search-engine/index.php" target="_blank">Frontend</a>
            <a href="logout.php">Logout</a>
        </div>
    </div>
</div>

<div class="content">

<script>
function toggleProfile(){
    const menu = document.getElementById('profileMenu');
    menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
}

function toggleDark(){
    document.body.classList.toggle('dark');
    if(document.body.classList.contains('dark')){
        localStorage.setItem("adminTheme","dark");
    } else {
        localStorage.setItem("adminTheme","light");
    }
}

document.addEventListener("DOMContentLoaded", function(){
    if(localStorage.getItem("adminTheme")==="dark"){
        document.body.classList.add("dark");
    }
});
</script>