<?php
// header.php - common header/topbar/sidebar
// Expects $pagetitle and $activemenu to be set by the calling page BEFORE including this file.
// db.php and functions.php must already be included by the calling page.

if (!isset($pagetitle)) { $pagetitle = 'Divar Ferry Pass App'; }
if (!isset($activemenu)) { $activemenu = ''; }

$role = currentRole();

// Session role names don't always match the file-name prefix used for that portal
// (e.g. role 'panchayatuser' -> files prefixed 'panchayat_', role 'rnduser' -> 'rnd_'),
// so map explicitly rather than concatenating the role directly into the URL.
$urlprefix = ['admin' => 'admin', 'panchayatuser' => 'panchayat', 'rnduser' => 'rnd'][$role] ?? $role;

// Menu items per role: key => [label, url]
$menus = [];
if ($role === 'admin') {
    $menus = [
        'dashboard' => ['Dashboard', 'admin_dashboard.php'],
        'villagers' => ['Villagers', 'admin_villagers.php'],
        'payments' => ['Payments', 'admin_payments.php'],
        'panchayats' => ['Panchayats', 'admin_panchayats.php'],
        'rndusers' => ['RND Users', 'admin_rndusers.php'],
        'checkers' => ['Ticket Checkers', 'admin_ticketcheckers.php'],
        'reports' => ['Reports', 'admin_reports.php'],
        'auditlog' => ['Audit Log', 'admin_auditlog.php'],
        'settings' => ['Settings', 'admin_settings.php'],
        'users' => ['Admin Users', 'admin_users.php'],
    ];
} elseif ($role === 'panchayatuser') {
    $menus = [
        'dashboard' => ['Dashboard', 'panchayat_dashboard.php'],
        'registrations' => ['Registrations', 'panchayat_registrations.php'],
        'expiring' => ['Expiring Passes', 'panchayat_expiring.php'],
        'reports' => ['Reports', 'panchayat_reports.php'],
    ];
} elseif ($role === 'rnduser') {
    $menus = [
        'dashboard' => ['Dashboard', 'rnd_dashboard.php'],
        'registrations' => ['Registrations', 'rnd_registrations.php'],
        'reports' => ['Reports', 'rnd_reports.php'],
    ];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo safeOut($pagetitle); ?> - Divar Ferry Pass App</title>
<link rel="icon" href="logo.png" type="image/png">
<link rel="stylesheet" href="style.css">
</head>
<body>

<div id="dfpToastWrap"><div class="dfp-toast" id="dfpToastBox"></div></div>

<div class="dfp-topbar">
  <div style="display:flex;align-items:center;">
    <button class="dfp-hamburger" id="dfpHamburgerBtn">&#9776;</button>
    <div class="brand"><img src="logo.png" alt="logo">Divar Ferry Pass</div>
  </div>
  <div class="dfp-usermenu">
    <span><?php echo safeOut($_SESSION['username'] ?? ''); ?> (<?php echo safeOut($role); ?>)</span>
    <a href="<?php echo $urlprefix . '_profile.php'; ?>">Profile</a>
    <a href="<?php echo $urlprefix . '_logout.php'; ?>">Logout</a>
  </div>
</div>

<div class="dfp-container">
  <div class="dfp-sidebar" id="dfpSidebar">
    <?php foreach ($menus as $key => $item): ?>
      <a href="<?php echo $item[1]; ?>" class="<?php echo ($activemenu === $key) ? 'active' : ''; ?>"><?php echo safeOut($item[0]); ?></a>
    <?php endforeach; ?>
  </div>
  <div class="dfp-main">
