<?php
// ============================================================
//  Member-area shared layout
// ============================================================
require_once __DIR__ . '/../includes/auth.php';
auth_require_login();

$member = auth_user();
if (!$member) {
    auth_logout();
    header('Location: ../login.php');
    exit;
}

require_once __DIR__ . '/../includes/config.php';
$current = basename($_SERVER['PHP_SELF']);

// Find the member's own listing (used for "View public page" link)
$own_listing = db_row(
    'SELECT id FROM listings WHERE member_id = :m LIMIT 1',
    ['m' => $member['id']]
);
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars($page_title ?? 'Member Area') ?> &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>
    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-primary);
        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-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;
        align-items: center;
    }
    .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);
    }
    .portal-subnav .spacer { margin-left: auto; }

    @media (max-width: 700px) {
        .portal-subnav .container { gap: 1rem; }
        .portal-subnav a { font-size: .72rem; }
        .portal-brand .name { display: none; }
    }
    </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">Member</span>
            </span>
        </a>
        <div class="portal-user">
            <span>Hi, <strong style="color:#fff;"><?= htmlspecialchars($member['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="Member sections">
    <div class="container">
        <a href="welcome.php"         class="<?= $current==='welcome.php'?'active':'' ?>">Dashboard</a>
        <a href="edit-business.php"   class="<?= $current==='edit-business.php'?'active':'' ?>">Edit Business</a>
        <a href="complete-profile.php" class="<?= $current==='complete-profile.php'?'active':'' ?>">My Listing</a>
        <a href="view-statement.php"  class="<?= $current==='view-statement.php'?'active':'' ?>">Statement</a>
        <a href="invoices.php"        class="<?= in_array($current,['invoices.php','invoice.php'],true)?'active':'' ?>">Invoices</a>
        <a href="view-transactions.php" class="<?= $current==='view-transactions.php'?'active':'' ?>">Transactions</a>
        <a href="calendar.php"        class="<?= $current==='calendar.php'?'active':'' ?>">Calendar</a>
        <a href="cart.php"            class="<?= $current==='cart.php'?'active':'' ?>">Cart</a>
        <?php if ($own_listing): ?>
            <a href="../directory-item.php?id=<?= $own_listing['id'] ?>" class="spacer" target="_blank">
                View public page ↗
            </a>
        <?php endif; ?>
    </div>
</nav>

<main>