<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}
$currentPage = basename($_SERVER['PHP_SELF']);
$adminName   = htmlspecialchars($_SESSION['admin_name'] ?? 'Admin');
$adminRole   = $_SESSION['admin_role'] ?? 'admin';

$navLinks = [
    ['file' => 'index.php',         'label' => 'Dashboard',    'icon' => '📊'],
    ['file' => 'map.php',           'label' => 'Parcel Map',   'icon' => '🗺️'],
    ['file' => 'live_map.php',      'label' => 'Live Tracker', 'icon' => '📡'],
    ['file' => 'activity_log.php',  'label' => 'Activity Log', 'icon' => '📜'],
    ['file' => 'surveys.php',       'label' => 'Surveys',      'icon' => '📋'],
    ['file' => 'surveyors.php',     'label' => 'Surveyors',    'icon' => '👤'],
    ['file' => 'import.php',        'label' => 'Import Data',  'icon' => '⬆️'],
    ['file' => 'export.php',        'label' => 'Export',       'icon' => '⬇️'],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bancharee Admin</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
  <style>
    [x-cloak] { display: none !important; }
    .scrollbar-thin::-webkit-scrollbar { width: 4px; }
    .scrollbar-thin::-webkit-scrollbar-track { background: transparent; }
    .scrollbar-thin::-webkit-scrollbar-thumb { background: #475569; border-radius: 2px; }
  </style>
</head>
<body class="bg-gray-50 font-sans text-slate-800" x-data="{ sideOpen: true }">

<!-- Layout wrapper -->
<div class="flex min-h-screen">

  <!-- ── Sidebar ───────────────────────────────────────────────────────────── -->
  <aside class="w-60 bg-slate-900 flex-shrink-0 flex flex-col min-h-screen">

    <!-- Brand -->
    <div class="px-5 py-5 border-b border-slate-700">
      <div class="text-white font-bold text-base leading-tight">🗺 Bancharee</div>
      <div class="text-slate-400 text-xs mt-0.5">Survey Admin Panel</div>
    </div>

    <!-- Nav -->
    <nav class="flex-1 px-3 py-4 space-y-0.5 overflow-y-auto scrollbar-thin">
      <?php foreach ($navLinks as $link): ?>
        <?php $active = $currentPage === $link['file']; ?>
        <a href="<?= $link['file'] ?>"
           class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm transition-colors
                  <?= $active
                    ? 'bg-blue-600 text-white font-semibold'
                    : 'text-slate-300 hover:bg-slate-800 hover:text-white' ?>">
          <span class="text-base leading-none"><?= $link['icon'] ?></span>
          <?= $link['label'] ?>
        </a>
      <?php endforeach ?>
    </nav>

    <!-- User info -->
    <div class="px-4 py-4 border-t border-slate-700">
      <div class="flex items-center gap-3">
        <div class="w-8 h-8 rounded-full bg-blue-600 flex items-center justify-center text-white text-sm font-bold flex-shrink-0">
          <?= strtoupper(substr($adminName, 0, 1)) ?>
        </div>
        <div class="min-w-0">
          <div class="text-white text-xs font-medium truncate"><?= $adminName ?></div>
          <div class="text-slate-400 text-xs capitalize"><?= $adminRole ?></div>
        </div>
        <a href="logout.php" title="Logout"
           class="ml-auto text-slate-400 hover:text-red-400 transition-colors flex-shrink-0">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                  d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/>
          </svg>
        </a>
      </div>
    </div>
  </aside>

  <!-- ── Main content ──────────────────────────────────────────────────────── -->
  <div class="flex-1 flex flex-col min-w-0">

    <!-- Top bar -->
    <header class="bg-white border-b border-gray-200 px-6 py-3 flex items-center justify-between sticky top-0 z-10">
      <h1 class="font-semibold text-slate-700 text-sm">
        <?php
        $titles = [
            'index.php'        => 'Dashboard',
            'map.php'          => 'Parcel Map',
            'live_map.php'     => 'Live Tracker',
            'activity_log.php' => 'Activity Log',
            'surveys.php'      => 'Surveys',
            'survey_detail.php'=> 'Survey Detail',
            'surveyors.php'    => 'Surveyors',
            'import.php'       => 'Import Data',
            'export.php'       => 'Export',
        ];
        echo $titles[$currentPage] ?? 'Bancharee Admin';
        ?>
      </h1>
      <div class="flex items-center gap-3 text-xs text-slate-500">
        <span>Bancharedanda Compensation Survey</span>
        <span class="text-slate-300">|</span>
        <a href="logout.php" class="text-red-500 hover:text-red-700 font-medium transition-colors">Logout</a>
      </div>
    </header>

    <!-- Page content -->
    <main class="flex-1 p-6">
