<?php
$u = \Core\Session::user();
$role = $u['role_code'] ?? '';
$isAdmin = in_array($role, ['admin', 'executive'], true);
$isPm    = in_array($role, ['admin', 'executive', 'project_manager'], true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= e(APP_NAME) ?> · <?= e($title ?? 'Dashboard') ?></title>
    <link rel="icon"           type="image/x-icon" href="<?= url('/public/static/img/favicon.ico') ?>">
    <link rel="icon" sizes="16x16" type="image/png" href="<?= url('/public/static/img/favicon-16.png') ?>">
    <link rel="icon" sizes="32x32" type="image/png" href="<?= url('/public/static/img/favicon-32.png') ?>">
    <link rel="apple-touch-icon"                    href="<?= url('/public/static/img/apple-touch-icon.png') ?>">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= url('/public/static/css/rsflow.css') ?>">
    <link rel="stylesheet" href="<?= url('/public/static/css/app.css') ?>">
    <!-- jquery-confirm (matches SiteTracker16 modal style) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.css">
    <?= $this->section('head') ?>
</head>
<body>

<div class="rs-wrapper">

    <!-- ── Sidebar ───────────────────────────────────────────────────── -->
    <aside class="rs-sidebar">
        <a href="<?= url('/Home') ?>" class="rs-sidebar-brand">
            <div class="rs-brand-mark"><img src="<?= url('/public/static/img/logo.png') ?>" alt="OrchView"></div>
            <div>
                <div class="brand-name"><?= e(APP_NAME) ?></div>
                <div class="brand-sub">Plant Ops</div>
            </div>
        </a>

        <?php
        $pi = $_SERVER['PATH_INFO'] ?? '/';
        $is = fn(string $seg): bool => strpos($pi, '/' . $seg) === 0;
        ?>

        <nav class="rs-nav">
            <ul>
                <li><a href="<?= url('/Home') ?>" class="rs-nav-link <?= $is('Home') ? 'active' : '' ?>">
                    <i class="fas fa-tachometer-alt"></i><span>Dashboard</span>
                </a></li>

                <div class="rs-nav-section">OPERATIONS</div>
                <li><a href="<?= url('/Checklists') ?>" class="rs-nav-link <?= $is('Checklists') ? 'active' : '' ?>">
                    <i class="fas fa-clipboard-list"></i><span>Fill a Checklist</span>
                </a></li>
                <li><a href="<?= url('/WorkRequests') ?>" class="rs-nav-link <?= $is('WorkRequests') ? 'active' : '' ?>">
                    <i class="fas fa-inbox"></i><span>Work Requests</span>
                </a></li>
                <li><a href="<?= url('/Rounds') ?>" class="rs-nav-link <?= $is('Rounds') ? 'active' : '' ?>">
                    <i class="fas fa-route"></i><span>Operator Rounds</span>
                </a></li>
                <li><a href="<?= url('/Permits') ?>" class="rs-nav-link <?= $is('Permits') ? 'active' : '' ?>">
                    <i class="fas fa-shield-halved"></i><span>Permits</span>
                </a></li>
                <li><a href="<?= url('/WorkOrders') ?>" class="rs-nav-link <?= $is('WorkOrders') ? 'active' : '' ?>">
                    <i class="fas fa-clipboard-check"></i><span>Work Orders</span>
                </a></li>
                <li><a href="<?= url('/Incidents') ?>" class="rs-nav-link <?= $is('Incidents') ? 'active' : '' ?>">
                    <i class="fas fa-triangle-exclamation"></i><span>Incidents</span>
                </a></li>
                <li><a href="<?= url('/Expenses') ?>" class="rs-nav-link <?= $is('Expenses') ? 'active' : '' ?>">
                    <i class="fas fa-receipt"></i><span>Expenses</span>
                </a></li>

                <div class="rs-nav-section">INVENTORY</div>
                <li><a href="<?= url('/Sites') ?>" class="rs-nav-link <?= $is('Sites') ? 'active' : '' ?>">
                    <i class="fas fa-map-marker-alt"></i><span>Sites</span>
                </a></li>
                <li><a href="<?= url('/Assets') ?>" class="rs-nav-link <?= $is('Assets') ? 'active' : '' ?>">
                    <i class="fas fa-cogs"></i><span>Assets</span>
                </a></li>
                <li><a href="<?= url('/Parts') ?>" class="rs-nav-link <?= $is('Parts') ? 'active' : '' ?>">
                    <i class="fas fa-boxes-stacked"></i><span>Spare Parts</span>
                </a></li>

                <div class="rs-nav-section">BILLING</div>
                <li><a href="<?= url('/Quotes') ?>" class="rs-nav-link <?= $is('Quotes') ? 'active' : '' ?>">
                    <i class="fas fa-file-invoice"></i><span>Quotes</span>
                </a></li>
                <li><a href="<?= url('/Invoices') ?>" class="rs-nav-link <?= $is('Invoices') ? 'active' : '' ?>">
                    <i class="fas fa-file-invoice-dollar"></i><span>Invoices</span>
                </a></li>
                <li><a href="<?= url('/Customers') ?>" class="rs-nav-link <?= $is('Customers') ? 'active' : '' ?>">
                    <i class="fas fa-building"></i><span>Customers</span>
                </a></li>
                <li><a href="<?= url('/ProductsServices') ?>" class="rs-nav-link <?= $is('ProductsServices') ? 'active' : '' ?>">
                    <i class="fas fa-boxes"></i><span>Products & Services</span>
                </a></li>

                <div class="rs-nav-section">COMPLIANCE</div>
                <li><a href="<?= url('/Hse') ?>" class="rs-nav-link <?= $is('Hse') || $is('Sds') || $is('Training') ? 'active' : '' ?>">
                    <i class="fas fa-shield-heart"></i><span>HSE Library</span>
                </a></li>

                <div class="rs-nav-section">CLIENT VIEW</div>
                <li><a href="<?= url('/ClientPortal') ?>" class="rs-nav-link <?= $is('ClientPortal') ? 'active' : '' ?>">
                    <i class="fas fa-building-user"></i><span>Client Portal</span>
                </a></li>

                <?php if ($isAdmin): ?>
                <div class="rs-nav-section">ADMIN</div>
                <li><a href="<?= url('/CompanySettings') ?>" class="rs-nav-link <?= $is('CompanySettings') ? 'active' : '' ?>">
                    <i class="fas fa-building"></i><span>Company Settings</span>
                </a></li>
                <li><a href="<?= url('/AssetTypes') ?>" class="rs-nav-link <?= $is('AssetTypes') ? 'active' : '' ?>">
                    <i class="fas fa-list-check"></i><span>Asset Types</span>
                </a></li>
                <li><a href="<?= url('/Calibration') ?>" class="rs-nav-link <?= $is('Calibration') ? 'active' : '' ?>">
                    <i class="fas fa-ruler-combined"></i><span>Calibration</span>
                </a></li>
                <li><a href="<?= url('/PmTemplates') ?>" class="rs-nav-link <?= $is('PmTemplates') ? 'active' : '' ?>">
                    <i class="fas fa-calendar-check"></i><span>PM Templates <small class="opacity-50">(Checklists)</small></span>
                </a></li>
                <li><a href="<?= url('/Users') ?>" class="rs-nav-link <?= $is('Users') ? 'active' : '' ?>">
                    <i class="fas fa-users-cog"></i><span>Users</span>
                </a></li>
                <?php elseif ($isPm): ?>
                <div class="rs-nav-section">MANAGE</div>
                <li><a href="<?= url('/PmTemplates') ?>" class="rs-nav-link <?= $is('PmTemplates') ? 'active' : '' ?>">
                    <i class="fas fa-calendar-check"></i><span>PM Templates <small class="opacity-50">(Checklists)</small></span>
                </a></li>
                <div class="rs-nav-section">TEAM</div>
                <li><a href="<?= url('/Users') ?>" class="rs-nav-link <?= $is('Users') ? 'active' : '' ?>">
                    <i class="fas fa-users"></i><span>Users</span>
                </a></li>
                <?php endif; ?>
            </ul>
        </nav>

        <?php if ($u): ?>
        <div class="rs-sidebar-footer">
            <div class="rs-user-avatar">
                <?= strtoupper(substr((string)($u['full_name'] ?: $u['username'] ?? '?'), 0, 1)) ?>
            </div>
            <div style="flex:1; min-width:0;">
                <div class="rs-user-name"><?= e($u['full_name'] ?: $u['username']) ?></div>
                <div class="rs-user-role"><?= e($u['role'] ?? '—') ?></div>
            </div>
            <a href="<?= url('/Logout') ?>" class="text-light opacity-50" title="Sign out">
                <i class="fas fa-sign-out-alt"></i>
            </a>
        </div>
        <?php endif; ?>
    </aside>

    <!-- ── Main ──────────────────────────────────────────────────────── -->
    <div class="rs-main">
        <header class="rs-topbar">
            <strong class="page-title"><?= e($title ?? 'Dashboard') ?></strong>
            <div class="topbar-right">
                <span class="date-pill"><?= e(date('D, d M Y')) ?></span>
            </div>
        </header>

        <main class="rs-content">
            <?= $content ?>
    <?= $this->include('partials/document-modal') ?>
        </main>
    </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- Vue 3 global build (Options API). Loaded on every page so any view can
     Vue.createApp({...}).mount('#foo') without extra setup. -->
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<!-- jQuery + jquery-confirm for the rsConfirm modal (used on destructive actions). -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.js"></script>
<script>
// rsConfirm — the SiteTracker16-style confirm dialog. Falls back to the native
// window.confirm if jquery-confirm hasn't loaded (offline / CDN blocked).
window.rsConfirm = function (opts) {
    opts = opts || {};
    var ok = function () { if (typeof opts.onConfirm === 'function') opts.onConfirm(); };
    if (typeof $ !== 'function' || typeof $.confirm !== 'function') {
        if (window.confirm((opts.title || 'Confirm') + '\n\n' + (opts.message || ''))) ok();
        return;
    }
    $.confirm({
        title: opts.title || 'Confirm',
        content: opts.message || 'Are you sure?',
        type: opts.danger === false ? 'blue' : 'red',
        theme: 'modern',
        icon: opts.danger === false ? 'fas fa-question-circle' : 'fas fa-exclamation-triangle',
        buttons: {
            confirm: {
                text: opts.okLabel || 'Confirm',
                btnClass: opts.danger === false ? 'btn-blue' : 'btn-red',
                action: ok,
            },
            cancel: { text: opts.cancelLabel || 'Cancel' },
        },
    });
};

// Any <form class="rs-confirm-submit"> gets intercepted and confirmed first.
// Attributes:
//   data-confirm-title, data-confirm-body, data-confirm-ok, data-confirm-danger="0"
document.addEventListener('submit', function (ev) {
    var f = ev.target;
    if (!(f instanceof HTMLFormElement) || !f.classList.contains('rs-confirm-submit')) return;
    if (f.dataset.rsConfirmed === '1') return;
    ev.preventDefault();
    window.rsConfirm({
        title:   f.dataset.confirmTitle || 'Are you sure?',
        message: f.dataset.confirmBody  || 'This action cannot be undone.',
        danger:  f.dataset.confirmDanger !== '0',
        okLabel: f.dataset.confirmOk    || 'Delete',
        onConfirm: function () { f.dataset.rsConfirmed = '1'; f.submit(); },
    });
}, true);
</script>
<?= $this->section('scripts') ?>
</body>
</html>
