<?php
require_once __DIR__ . '/../config.php';
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['dealer_id'])) redirect(SITE_URL . '/dealer/login.php');

$db = getDB();
$dealer = $db->prepare("SELECT * FROM dealers WHERE id=?");
$dealer->execute([$_SESSION['dealer_id']]);
$dealer = $dealer->fetch();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['logout'])) {
    unset($_SESSION['dealer_id'], $_SESSION['dealer_name']);
    redirect(SITE_URL . '/dealer/login.php');
}

$current_page = basename($_SERVER['PHP_SELF'], '.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<title><?= h($page_title ?? 'Dealer Portal') ?> | Balimeen</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Outfit:wght@700;800;900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
/* Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Inter', sans-serif; background: #f8fafc; color: #334155; display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar { width: 260px; background: #0a1f4e; color: #fff; display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar-brand { padding: 24px 20px; display: flex; flex-direction: column; align-items: center; gap: 12px; background: rgba(0,0,0,0.15); border-bottom: 1px solid rgba(255,255,255,0.05); }
.sidebar-brand img { width: 60px; height: 60px; border-radius: 12px; background: #fff; object-fit: contain; padding: 4px; }
.sidebar-brand h2 { font-family: 'Outfit', sans-serif; font-size: 1.25rem; color: #fff; margin: 0; }
.sidebar-brand span { font-size: .75rem; color: #94a3b8; font-weight: 500; letter-spacing: 1px; text-transform: uppercase; }

.nav-links { padding: 20px 0; flex: 1; overflow-y: auto; }
.nav-item { padding: 14px 24px; display: flex; align-items: center; gap: 14px; color: #94a3b8; text-decoration: none; font-size: .95rem; font-weight: 500; transition: all 0.2s; border-left: 3px solid transparent; }
.nav-item:hover { background: rgba(255,255,255,0.05); color: #fff; }
.nav-item.active { background: rgba(37,87,214,0.15); color: #fff; border-left-color: #3b82f6; font-weight: 600; }
.nav-item i { width: 20px; text-align: center; font-size: 1.1rem; }

/* Main Content */
.main-content { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.top-header { height: 80px; background: #fff; padding: 0 30px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #e2e8f0; }
.header-user { display: flex; align-items: center; gap: 12px; }
.header-user img { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; background: #f1f5f9; }
.header-user h4 { margin: 0; font-size: 1rem; color: #0f172a; }
.header-user p { margin: 0; font-size: .8rem; color: #64748b; }
.btn-outline { border: 1px solid #cbd5e1; background: transparent; color: #334155; padding: 8px 16px; border-radius: 8px; font-weight: 600; font-size: .85rem; cursor: pointer; text-decoration: none; }
.btn-outline:hover { background: #f8fafc; }

/* Content Area */
.content-area { padding: 30px; overflow-y: auto; flex: 1; }

/* Common UI */
.card { background: #fff; border-radius: 16px; padding: 24px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.02), 0 10px 15px -3px rgba(0,0,0,0.02); margin-bottom: 24px; border: 1px solid #e2e8f0; }
.table-wrap { overflow-x: auto; margin-top: 16px; border: 1px solid #e2e8f0; border-radius: 12px; }
table { width: 100%; border-collapse: collapse; text-align: left; }
th, td { padding: 16px; border-bottom: 1px solid #e2e8f0; }
th { background: #f8fafc; font-weight: 600; color: #475569; font-size: .85rem; }
tr:last-child td { border-bottom: none; }
.badge { display: inline-block; padding: 4px 10px; border-radius: 50px; font-size: .75rem; font-weight: 700; text-transform: uppercase; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-info { background: #e0f2fe; color: #075985; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.btn-primary { background: #2557d6; color: #fff; border: none; padding: 10px 20px; border-radius: 8px; font-weight: 600; cursor: pointer; text-decoration: none; display: inline-block; transition: background 0.2s;}
.btn-primary:hover { background: #1d4ed8; }
</style>
</head>
<body>

<aside class="sidebar">
  <div class="sidebar-brand">
    <img src="<?= SITE_URL ?>/assets/images/logo.jpg" alt="Balimeen">
    <div>
      <h2>Balimeen</h2>
      <span>Dealer Portal</span>
    </div>
  </div>
  <nav class="nav-links">
    <a href="index.php" class="nav-item <?= $current_page==='index'?'active':'' ?>"><i class="fas fa-th-large"></i> Dashboard</a>
    <a href="price_list.php" class="nav-item <?= $current_page==='price_list'?'active':'' ?>"><i class="fas fa-box-open"></i> Products</a>
    <a href="orders.php" class="nav-item <?= $current_page==='orders'?'active':'' ?>"><i class="fas fa-shopping-cart"></i> Orders</a>
    <a href="ledger.php" class="nav-item <?= $current_page==='ledger'?'active':'' ?>"><i class="fas fa-file-invoice-dollar"></i> Payments</a>
    <a href="shipments.php" class="nav-item <?= $current_page==='shipments'?'active':'' ?>"><i class="fas fa-truck-fast"></i> Shipments</a>
    <a href="complaints.php" class="nav-item <?= $current_page==='complaints'?'active':'' ?>"><i class="fas fa-exclamation-circle"></i> Complaints</a>
    <a href="marketing.php" class="nav-item <?= $current_page==='marketing'?'active':'' ?>"><i class="fas fa-bullhorn"></i> Marketing</a>
    <a href="profile.php" class="nav-item <?= $current_page==='profile'?'active':'' ?>"><i class="fas fa-user-circle"></i> My Profile</a>
    <a href="support.php" class="nav-item <?= $current_page==='support'?'active':'' ?>"><i class="fas fa-headset"></i> Support</a>
    <form method="POST" style="margin-top:20px;">
      <button type="submit" name="logout" style="width:100%;text-align:left;background:transparent;border:none;cursor:pointer;" class="nav-item"><i class="fas fa-sign-out-alt"></i> Logout</button>
    </form>
  </nav>
</aside>

<main class="main-content">
  <header class="top-header">
    <div class="header-user">
      <img src="https://ui-avatars.com/api/?name=<?= urlencode($dealer['name']) ?>&background=e2e8f0&color=0f172a" alt="User">
      <div>
        <h4>Welcome, <?= h($dealer['name']) ?></h4>
        <p>Dealer Code: DLR-<?= str_pad($dealer['id'], 4, '0', STR_PAD_LEFT) ?> &nbsp;|&nbsp; Credit Limit: ₹<?= number_format($dealer['credit_limit']) ?></p>
      </div>
    </div>
    <div class="user-menu">
      <a href="profile.php" class="btn-outline">View Profile</a>
    </div>
  </header>
  <div class="content-area">
