<?php
require_once __DIR__ . '/../_init.php';
admin_require_login();
$cfg = app_config();
$admin = admin_current();
$active = $active ?? '';
$reportActive = $reportActive ?? '';
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title><?php echo htmlspecialchars($cfg['APP_NAME']); ?> Admin</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <?php
  $scriptPath = $_SERVER['SCRIPT_NAME'] ?? $_SERVER['PHP_SELF'] ?? '';
  $inReports = (strpos($scriptPath, '/reports/') !== false);
  $adminAssetBase = $inReports ? '../../' : '../';
  $adminBase = $inReports ? '../' : '';   // for links to admin root (dashboard, leads, etc.)
  $reportBase = $inReports ? '' : 'reports/';  // for links to report pages
  ?>
  <link href="<?php echo $adminAssetBase; ?>assets/css/admin.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
</head>
<body>
<div class="sidebar">
  <div class="brand"><?php echo htmlspecialchars($cfg['APP_NAME']); ?> Admin</div>

  <div class="nav-section">Overview</div>
  <a class="<?php echo $active==='dashboard'?'active':''; ?>" href="<?php echo $adminBase; ?>dashboard.php">
    <span class="nav-icon">📊</span> Dashboard
  </a>

  <div class="nav-section">Catalog</div>
  <a class="<?php echo $active==='coupons'?'active':''; ?>" href="<?php echo $adminBase; ?>coupons.php">
    <span class="nav-icon">🎟️</span> Coupon Codes
  </a>
  <a class="<?php echo $active==='categories'?'active':''; ?>" href="<?php echo $adminBase; ?>categories.php">
    <span class="nav-icon">📁</span> Categories
  </a>
  <a class="<?php echo $active==='subcategories'?'active':''; ?>" href="<?php echo $adminBase; ?>subcategories.php">
    <span class="nav-icon">📂</span> Subcategories
  </a>
  <a class="<?php echo $active==='services'?'active':''; ?>" href="<?php echo $adminBase; ?>services.php">
    <span class="nav-icon">🛠</span> Services
  </a>

  <div class="nav-section">Users (App)</div>
  <a class="<?php echo $active==='users'?'active':''; ?>" href="<?php echo $adminBase; ?>users.php">
    <span class="nav-icon">👤</span> Users
  </a>
  <a class="<?php echo $active==='leads'?'active':''; ?>" href="<?php echo $adminBase; ?>leads.php">
    <span class="nav-icon">🛒</span> User Orders (Leads)
  </a>
  <a class="<?php echo $active==='user_orders'?'active':''; ?>" href="<?php echo $adminBase; ?>user_orders.php">
    <span class="nav-icon">📦</span> App Orders
  </a>

  <div class="nav-section">Operations</div>
  <a class="<?php echo $active==='vendors'?'active':''; ?>" href="<?php echo $adminBase; ?>vendors.php">
    <span class="nav-icon">👤</span> Vendors
  </a>
  <a class="<?php echo $active==='leads'?'active':''; ?>" href="<?php echo $adminBase; ?>leads.php">
    <span class="nav-icon">📋</span> Leads
  </a>
  <a class="<?php echo $active==='jobs'?'active':''; ?>" href="<?php echo $adminBase; ?>jobs.php">
    <span class="nav-icon">✅</span> Jobs
  </a>
  <a class="<?php echo $active==='tx'?'active':''; ?>" href="<?php echo $adminBase; ?>transactions.php">
    <span class="nav-icon">💰</span> Transactions
  </a>
  <a class="<?php echo $active==='withdrawals'?'active':''; ?>" href="<?php echo $adminBase; ?>withdrawals.php">
    <span class="nav-icon">💸</span> Withdrawals
  </a>
  <a class="<?php echo $active==='contact'?'active':''; ?>" href="<?php echo $adminBase; ?>contact_messages.php">
    <span class="nav-icon">✉️</span> Contact Enquiries
  </a>
  <a class="<?php echo $active==='otp'?'active':''; ?>" href="<?php echo $adminBase; ?>otp_logs.php">
    <span class="nav-icon">🔐</span> OTP Logs
  </a>
  <a class="<?php echo $active==='sessions'?'active':''; ?>" href="<?php echo $adminBase; ?>vendor_sessions.php">
    <span class="nav-icon">📱</span> Vendor Sessions
  </a>

  <div class="nav-section">Reports & Analytics</div>
  <a class="<?php echo $reportActive==='management'?'active':''; ?>" href="<?php echo $reportBase; ?>management_report.php">
    <span class="nav-icon">📈</span> Management Report
  </a>
  <a class="<?php echo $reportActive==='sales'?'active':''; ?>" href="<?php echo $reportBase; ?>sales_report.php">
    <span class="nav-icon">💵</span> Sales Report
  </a>
  <a class="<?php echo $reportActive==='leads'?'active':''; ?>" href="<?php echo $reportBase; ?>leads_report.php">
    <span class="nav-icon">📥</span> Leads Report
  </a>
  <a class="<?php echo $reportActive==='jobs'?'active':''; ?>" href="<?php echo $reportBase; ?>jobs_report.php">
    <span class="nav-icon">📊</span> Jobs Report
  </a>
  <a class="<?php echo $reportActive==='vendor'?'active':''; ?>" href="<?php echo $reportBase; ?>vendor_report.php">
    <span class="nav-icon">👥</span> Vendor Report
  </a>
  <a class="<?php echo $reportActive==='user'?'active':''; ?>" href="<?php echo $reportBase; ?>user_report.php">
    <span class="nav-icon">👨‍💼</span> User Report
  </a>
  <a class="<?php echo $reportActive==='user_orders_report'?'active':''; ?>" href="<?php echo $reportBase; ?>user_orders_report.php">
    <span class="nav-icon">🛒</span> User Orders Report
  </a>
  <a class="<?php echo $reportActive==='transaction'?'active':''; ?>" href="<?php echo $reportBase; ?>transaction_report.php">
    <span class="nav-icon">💳</span> Transaction Report
  </a>
  <a class="<?php echo $reportActive==='payment'?'active':''; ?>" href="<?php echo $reportBase; ?>payment_report.php">
    <span class="nav-icon">📋</span> Payment & Commission
  </a>
  <a class="<?php echo $reportActive==='job_history'?'active':''; ?>" href="<?php echo $reportBase; ?>job_history_report.php">
    <span class="nav-icon">📜</span> Job History
  </a>
  <a class="<?php echo $reportActive==='wallet'?'active':''; ?>" href="<?php echo $reportBase; ?>wallet_commission_report.php">
    <span class="nav-icon">💰</span> Wallet & Commission
  </a>
  <a class="<?php echo $reportActive==='sales_analytics'?'active':''; ?>" href="<?php echo $reportBase; ?>sales_analytics_report.php">
    <span class="nav-icon">📊</span> Sales Analytics
  </a>
  <a class="<?php echo $reportActive==='business'?'active':''; ?>" href="<?php echo $reportBase; ?>business_overview.php">
    <span class="nav-icon">🦅</span> Business Eagle View
  </a>
  <a class="<?php echo $reportActive==='withdrawal_report'?'active':''; ?>" href="<?php echo $reportBase; ?>withdrawal_report.php">
    <span class="nav-icon">💸</span> Withdrawal Report
  </a>

  <div class="nav-section" style="margin-top: auto; padding-top: 24px;">Settings & Account</div>
  <a class="<?php echo $active==='settings'?'active':''; ?>" href="<?php echo $adminBase; ?>admin_users.php">
    <span class="nav-icon">👥</span> Admin Users
  </a>
  <a href="<?php echo $adminBase; ?>logout.php"><span class="nav-icon">🚪</span> Logout</a>
</div>
<div class="main">
  <div class="topbar d-flex justify-content-between align-items-center">
    <div class="page-title"><?php echo htmlspecialchars($pageTitle ?? $active ?: 'Admin'); ?></div>
    <div class="admin-info">Signed in as <?php echo htmlspecialchars($admin['email'] ?? ''); ?></div>
  </div>
  <div class="container-fluid p-4">
