<?php
// Output buffering: lets module files redirect with header('Location: ...')
// even after this layout file has started printing HTML. Without this,
// any header() call after this include throws "headers already sent".
if (!ob_get_level()) {
    ob_start();
}

require_once __DIR__ . '/../includes/functions.php';
requireLogin();
$current = basename($_SERVER['PHP_SELF']);
$companyName = getSetting('company_name', 'Stock Manager Pro');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= isset($pageTitle) ? sanitize($pageTitle) . ' - ' : '' ?><?= sanitize($companyName) ?></title>
<link rel="icon" type="image/png" sizes="32x32" href="<?= APP_URL ?>/assets/img/favicon-32.png">
<link rel="icon" type="image/png" sizes="16x16" href="<?= APP_URL ?>/assets/img/favicon-16.png">
<link rel="apple-touch-icon" href="<?= APP_URL ?>/assets/img/apple-touch-icon.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Fraunces:wght@600;700&family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="<?= APP_URL ?>/assets/css/style.css">
</head>
<body>
<div class="app-shell">
    <aside class="sidebar" id="sidebar">
        <div class="sidebar-brand" style="display:flex;align-items:center;gap:10px;">
            <div class="logo-img-wrap" style="background:#fff;display:inline-flex;width:40px;height:40px;align-items:center;justify-content:center;padding:6px;border-radius:8px;line-height:0;overflow:hidden;flex-shrink:0;">
                <img src="<?= APP_URL ?>/assets/img/logo-mark.png" alt="<?= sanitize($companyName) ?>" class="logo-img" style="height:100%;width:100%;max-width:100%;max-height:100%;display:block;object-fit:contain;">
            </div>
            <div class="logo-brand-name" style="font-family:var(--font-display);font-size:18px;font-weight:700;color:#fff;letter-spacing:0.3px;line-height:1.1;">Seder Core</div>
        </div>

        <div class="nav-section">
            <div class="nav-section-label">Overview</div>
            <?php if (hasTabAccess('dashboard')): ?>
            <a href="<?= APP_URL ?>/dashboard.php" class="nav-link <?= $current=='dashboard.php'?'active':'' ?>"><?= icon('layout-dashboard') ?> Dashboard</a>
            <?php endif; ?>
        </div>

        <div class="nav-section">
            <div class="nav-section-label">Parties</div>
            <?php if (hasTabAccess('purchase_vendors')): ?>
            <a href="<?= APP_URL ?>/modules/vendors/purchase_vendors.php" class="nav-link <?= $current=='purchase_vendors.php'?'active':'' ?>"><?= icon('truck-delivery') ?> Purchase Vendors</a>
            <?php endif; ?>
            <?php if (hasTabAccess('sale_vendors')): ?>
            <a href="<?= APP_URL ?>/modules/vendors/sale_vendors.php" class="nav-link <?= $current=='sale_vendors.php'?'active':'' ?>"><?= icon('users') ?> Sale Customers</a>
            <?php endif; ?>
        </div>

        <div class="nav-section">
            <div class="nav-section-label">Stock</div>
            <?php if (hasTabAccess('items')): ?>
            <a href="<?= APP_URL ?>/modules/stock/items.php" class="nav-link <?= $current=='items.php'?'active':'' ?>"><?= icon('package') ?> Item Master</a>
            <?php endif; ?>
            <?php if (hasTabAccess('purchases')): ?>
            <a href="<?= APP_URL ?>/modules/stock/purchases.php" class="nav-link <?= $current=='purchases.php'?'active':'' ?>"><?= icon('shopping-cart-plus') ?> Purchase Entry</a>
            <?php endif; ?>
            <?php if (hasTabAccess('purchase_payment')): ?>
            <a href="<?= APP_URL ?>/modules/stock/purchase_payment.php" class="nav-link <?= $current=='purchase_payment.php'?'active':'' ?>"><?= icon('cash') ?> Purchase Payments</a>
            <?php endif; ?>
            <?php if (hasTabAccess('stock_view')): ?>
            <a href="<?= APP_URL ?>/modules/stock/stock_view.php" class="nav-link <?= $current=='stock_view.php'?'active':'' ?>"><?= icon('stack-2') ?> Stock Overview</a>
            <?php endif; ?>
        </div>

        <div class="nav-section">
            <div class="nav-section-label">Sales Flow</div>
            <?php if (hasTabAccess('quotations')): ?>
            <a href="<?= APP_URL ?>/modules/quotation/quotations.php" class="nav-link <?= $current=='quotations.php'?'active':'' ?>"><?= icon('file-description') ?> Quotations</a>
            <?php endif; ?>
            <?php if (hasTabAccess('delivery_notes')): ?>
            <a href="<?= APP_URL ?>/modules/delivery/delivery_notes.php" class="nav-link <?= $current=='delivery_notes.php'?'active':'' ?>"><?= icon('truck') ?> Delivery Notes</a>
            <?php endif; ?>
            <?php if (hasTabAccess('invoices')): ?>
            <a href="<?= APP_URL ?>/modules/invoice/invoices.php" class="nav-link <?= $current=='invoices.php'?'active':'' ?>"><?= icon('receipt') ?> Tax Invoices</a>
            <?php endif; ?>
        </div>

        <div class="nav-section">
            <div class="nav-section-label">Billing</div>
            <?php if (hasTabAccess('sale_billing')): ?>
            <a href="<?= APP_URL ?>/modules/billing/sale_billing.php" class="nav-link <?= $current=='sale_billing.php'?'active':'' ?>"><?= icon('file-invoice') ?> Sale Billing</a>
            <?php endif; ?>
            <?php if (hasTabAccess('manual_billing')): ?>
            <a href="<?= APP_URL ?>/modules/billing/manual_billing.php" class="nav-link <?= $current=='manual_billing.php'?'active':'' ?>"><?= icon('tools') ?> Manual Billing (Service)</a>
            <?php endif; ?>
            <?php if (hasTabAccess('payments')): ?>
            <a href="<?= APP_URL ?>/modules/payments/payments.php" class="nav-link <?= $current=='payments.php'?'active':'' ?>"><?= icon('cash') ?> Payments</a>
            <?php endif; ?>
        </div>

        <div class="nav-section">
            <div class="nav-section-label">Reports</div>
            <?php if (hasTabAccess('purchase_report')): ?>
            <a href="<?= APP_URL ?>/modules/reports/purchase_report.php" class="nav-link <?= $current=='purchase_report.php'?'active':'' ?>"><?= icon('report-analytics') ?> Purchase Reports</a>
            <?php endif; ?>
            <?php if (hasTabAccess('sales_report')): ?>
            <a href="<?= APP_URL ?>/modules/reports/sales_report.php" class="nav-link <?= $current=='sales_report.php'?'active':'' ?>"><?= icon('report-money') ?> Sales Reports</a>
            <?php endif; ?>
            <?php if (hasTabAccess('cashflow_report')): ?>
            <a href="<?= APP_URL ?>/modules/reports/cashflow_report.php" class="nav-link <?= $current=='cashflow_report.php'?'active':'' ?>"><?= icon('arrows-exchange') ?> Cash Flow</a>
            <?php endif; ?>
        </div>

        <div class="nav-section" style="margin-bottom: 16px;">
            <div class="nav-section-label">Accounts & Finance</div>
            <?php if (hasTabAccess('accounts')): ?>
            <a href="<?= APP_URL ?>/modules/accounts/index.php" class="nav-link <?= $current==='index.php' && strpos($_SERVER['PHP_SELF'],'accounts')!==false ?'active':'' ?>"><?= icon('building-bank') ?> Overview</a>
            <a href="<?= APP_URL ?>/modules/accounts/bank_accounts.php" class="nav-link <?= $current==='bank_accounts.php'?'active':'' ?>"><?= icon('wallet') ?> Bank Accounts</a>
            <a href="<?= APP_URL ?>/modules/accounts/expenses.php" class="nav-link <?= $current==='expenses.php'?'active':'' ?>"><?= icon('receipt') ?> Expenses</a>
            <a href="<?= APP_URL ?>/modules/accounts/salaries.php" class="nav-link <?= $current==='salaries.php'?'active':'' ?>"><?= icon('users') ?> Salaries</a>
            <a href="<?= APP_URL ?>/modules/accounts/journal.php" class="nav-link <?= $current==='journal.php'?'active':'' ?>"><?= icon('files') ?> Journal Entries</a>
            <a href="<?= APP_URL ?>/modules/accounts/reports.php" class="nav-link <?= $current==='reports.php' && strpos($_SERVER['PHP_SELF'],'accounts')!==false ?'active':'' ?>"><?= icon('report-analytics') ?> Financial Reports</a>
            <?php endif; ?>
        </div>

        <div class="nav-section" style="margin-bottom: 16px;">
            <div class="nav-section-label">System</div>
            <?php if (currentUserRole() === 'admin'): ?>
            <a href="<?= APP_URL ?>/modules/users/users.php" class="nav-link <?= $current==='users.php'?'active':'' ?>"><?= icon('users-group') ?> User Management</a>
            <?php endif; ?>
            <?php if (hasTabAccess('settings')): ?>
            <a href="<?= APP_URL ?>/modules/settings.php" class="nav-link <?= $current=='settings.php'?'active':'' ?>"><?= icon('settings') ?> Settings</a>
            <?php endif; ?>
            <a href="<?= APP_URL ?>/modules/users/user_change_password.php" class="nav-link"><?= icon('key') ?> Change Password</a>
            <a href="<?= APP_URL ?>/logout.php" class="nav-link"><?= icon('logout') ?> Logout</a>
        </div>
    </aside>

    <div class="main">
        <header class="topbar">
            <div class="flex gap-12" style="align-items:center;">
                <button class="btn btn-icon mobile-toggle no-print" onclick="document.getElementById('sidebar').classList.toggle('show')"><?= icon('menu-2') ?></button>
                <h1><?= isset($pageTitle) ? sanitize($pageTitle) : 'Dashboard' ?></h1>
            </div>
            <div class="topbar-right">
                <div class="topbar-user">
                    <div class="avatar"><?= strtoupper(substr(currentUserName(),0,1)) ?></div>
                    <span><?= sanitize(currentUserName()) ?></span>
                </div>
            </div>
        </header>
        <main class="content">
<?php
$flash = getFlash();
if ($flash):
?>
            <div class="alert alert-<?= $flash['type'] ?>">
                <?= icon($flash['type']=='success'?'circle-check':($flash['type']=='danger'?'alert-circle':'info-circle')) ?>
                <?= sanitize($flash['message']) ?>
            </div>
<?php endif; ?>