<?php
// ============================================================
//  Admin-area shared gate + header
// ============================================================
require_once __DIR__ . '/../includes/auth.php';
auth_require_admin();

$admin = auth_admin_user();

require_once __DIR__ . '/../includes/config.php';

$current = basename($_SERVER['PHP_SELF']);
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin &mdash; <?= htmlspecialchars($page_title ?? '') ?> &mdash; <?= htmlspecialchars(SITE_NAME) ?></title>

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500;600;700&family=Playfair+Display:ital,wght@0,400;0,500;0,600;0,700;1,400&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="../assets/css/style.css">
    <style>
    /* ── Portal header (admin + member share this style) ── */
    body.portal { padding-top: 0; background: #f6f6f4; }
    .portal-header {
        background: #0e0e0e;
        color: #fff;
        padding: .6rem 0;
        box-shadow: 0 2px 10px rgba(0,0,0,.15);
        position: sticky;
        top: 0;
        z-index: 100;
    }
    .portal-header .container {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1.5rem;
    }
    .portal-brand {
        display: flex;
        align-items: center;
        gap: .75rem;
        color: #fff;
        text-decoration: none;
        flex-shrink: 0;
    }
    .portal-brand img { height: 42px; width: 42px; object-fit: contain; }
    .portal-brand .name {
        font-family: var(--font-display);
        font-size: 1rem;
        line-height: 1.1;
    }
    .portal-brand .role-badge {
        background: var(--brand-accent);
        color: #fff;
        font-size: .65rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: .08em;
        padding: .2em .6em;
        border-radius: 3px;
        margin-left: .4rem;
    }
    .portal-user {
        display: flex;
        align-items: center;
        gap: 1.25rem;
        color: #c0c0c0;
        font-size: .85rem;
    }
    .portal-user a { color: #c0c0c0; text-decoration: none; }
    .portal-user a:hover { color: #fff; }

    /* ── Portal sub-nav (section tabs) ── */
    .portal-subnav {
        background: #fff;
        border-bottom: 1px solid var(--line);
        position: sticky;
        top: 62px;
        z-index: 99;
    }
    .portal-subnav .container {
        display: flex;
        gap: 1.75rem;
        flex-wrap: wrap;
        padding-top: .75rem;
        padding-bottom: 0;
    }
    .portal-subnav a {
        padding: .65rem 0;
        font-size: .78rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: .05em;
        text-decoration: none;
        color: var(--ink-muted);
        border-bottom: 2px solid transparent;
        transition: color .15s, border-color .15s;
        white-space: nowrap;
    }
    .portal-subnav a:hover { color: var(--ink); }
    .portal-subnav a.active {
        color: var(--brand-primary);
        border-bottom-color: var(--brand-accent);
    }

    @media (max-width: 700px) {
        .portal-subnav .container { gap: 1rem; }
        .portal-subnav a { font-size: .72rem; }
        .portal-brand .name { display: none; }
    }

    /* Admin pages get wider content area than the public site */
    body.portal main .container,
    body.portal section.section > .container {
        max-width: none;
        width: 100%;
        padding-left: 2rem;
        padding-right: 2rem;
        box-sizing: border-box;
    }
    @media (min-width: 1600px) {
        body.portal main .container,
        body.portal section.section > .container {
            padding-left: 3rem;
            padding-right: 3rem;
        }
    }
    @media (max-width: 600px) {
        body.portal main .container,
        body.portal section.section > .container {
            padding-left: 1rem;
            padding-right: 1rem;
        }
    }
    /* But keep top header + sub-nav at sensible width */
    body.portal .portal-header .container,
    body.portal .portal-subnav .container {
        max-width: 1400px;
        margin-left: auto;
        margin-right: auto;
    }
    </style>
</head>
<body class="portal">

<header class="portal-header">
    <div class="container">
        <a href="../index.php" class="portal-brand">
            <img src="../assets/img/buylocal-stamp.png" alt="Buy Local Lowveld">
            <span class="name">
                Buy Local Lowveld
                <span class="role-badge">Admin</span>
            </span>
        </a>
        <div class="portal-user">
            <span>Hi, <strong style="color:#fff;"><?= htmlspecialchars($admin['first_name']) ?></strong></span>
            <a href="../index.php">View site</a>
            <a href="logout.php">Log out</a>
        </div>
    </div>
</header>

<nav class="portal-subnav" aria-label="Admin sections">
    <div class="container">
        <?php
            // Pages considered "settings" — used for highlighting the Settings tab
            $settings_pages = [
                'settings.php', 'payment-fees.php',
                'plans.php', 'plan-edit.php',
                'categories.php', 'category-edit.php',
                'users.php', 'user-edit.php',
                'email-templates.php', 'email-template-edit.php',
                'email-queue.php', 'email-log.php',
                'zoho-log.php',
                'cron-status.php',
            ];
        ?>
        <a href="dashboard.php"      class="<?= $current==='dashboard.php'?'active':'' ?>">Dashboard</a>
        <a href="metrics.php"        class="<?= $current==='metrics.php'?'active':'' ?>">Metrics</a>
        <a href="members.php"        class="<?= in_array($current,['members.php','member-edit.php'],true)?'active':'' ?>">Members</a>
        <a href="subscriptions.php"  class="<?= in_array($current,['subscriptions.php','subscription-edit.php'],true)?'active':'' ?>">Subscriptions</a>
        <a href="listings.php"       class="<?= in_array($current,['listings.php','listing-edit.php'],true)?'active':'' ?>">Listings</a>
        <a href="journeys.php"       class="<?= in_array($current,['journeys.php','journey-templates.php','journey-template-edit.php'],true)?'active':'' ?>">Journeys</a>
        <a href="accounting.php"     class="<?= in_array($current,['accounting.php','invoices.php','bank-transactions.php','payfast-payouts.php'],true)?'active':'' ?>">Accounting</a>
        <a href="blog.php"           class="<?= in_array($current,['blog.php','blog-edit.php'],true)?'active':'' ?>">Blog</a>
        <a href="claims.php"         class="<?= $current==='claims.php'?'active':'' ?>">Claims</a>
        <?php
        // Open tasks badge — count tasks assigned to me that aren't done/cancelled
        $_admin_id = (int)($_SESSION['admin_id'] ?? 0);
        $_my_tasks = $_admin_id ? (int)db_value(
            "SELECT COUNT(*) FROM admin_tasks
              WHERE (assigned_to = :me1 OR (assigned_to IS NULL AND created_by = :me2))
                AND status IN ('open','in_progress')",
            ['me1' => $_admin_id, 'me2' => $_admin_id]
        ) : 0;
        ?>
        <a href="tasks.php"          class="<?= in_array($current,['tasks.php','task-edit.php'],true)?'active':'' ?>">
            Tasks
            <?php if ($_my_tasks > 0): ?>
                <span style="background:#dc2626;color:#fff;font-size:.65rem;padding:.05em .45em;border-radius:999px;font-weight:700;margin-left:.25rem;vertical-align:middle;"><?= $_my_tasks ?></span>
            <?php endif; ?>
        </a>
        <a href="calendar.php"       class="<?= $current==='calendar.php'?'active':'' ?>">Calendar</a>
        <a href="settings.php"       class="<?= in_array($current, $settings_pages, true)?'active':'' ?>">Settings</a>
    </div>
</nav>

<main>