<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="csrf-token" content="<?= csrf_hash() ?>"/>
    <title><?= $title ?? 'Fieldly' ?> — Fieldly</title>
    <!-- Bootstrap 5 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet"/>
    <!-- Fieldly styles -->
    <link href="/fieldly/public/assets/css/fieldly.css" rel="stylesheet"/>
</head>
<body class="fieldly-body">

<!-- Sidebar -->
<nav class="fieldly-sidebar" id="sidebar">
    <div class="sidebar-brand">
        <div class="brand-icon">F</div>
        <div class="brand-text">
            <div class="brand-name">Fieldly</div>
            <div class="brand-sub"><?= esc($settings['branding']['company_name'] ?? 'Job Management') ?></div>
        </div>
    </div>

    <ul class="sidebar-nav">
        <?php $path = current_url(true)->getPath(); ?>
        <li><a href="/fieldly/public/index.php/dashboard"  class="<?= str_starts_with($path,'/dashboard')||$path==='/'?'active':'' ?>"><i class="bi bi-grid-1x2-fill"></i> Dashboard</a></li>
        <?php if (in_array('view_activity', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/activity"   class="<?= str_starts_with($path,'/activity')?'active':'' ?>"><i class="bi bi-activity"></i> Activity</a></li>
        <?php endif; ?>
        <?php if (in_array('view_customers', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/customers"  class="<?= str_starts_with($path,'/customers')?'active':'' ?>"><i class="bi bi-people-fill"></i> Customers</a></li>
        <?php endif; ?>
        <?php if (in_array('view_jobs', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/jobs"       class="<?= str_starts_with($path,'/jobs')?'active':'' ?>"><i class="bi bi-briefcase-fill"></i> Jobs <?php $uj=db_connect()->table('jobs j')->join('job_statuses js','js.id=j.status_id')->where('j.company_id',session('company_id'))->where('j.scheduled_date',date('Y-m-d'))->where('js.is_complete',0)->where('j.deleted_at IS NULL',null,false)->countAllResults(); if($uj>0): ?><span class="badge bg-warning text-dark ms-auto"><?= $uj ?></span><?php endif; ?></a></li>
        <?php endif; ?>
        <?php if (in_array('view_calendar', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/calendar"   class="<?= str_starts_with($path,'/calendar')?'active':'' ?>"><i class="bi bi-calendar-week-fill"></i> Calendar</a></li>
        <?php endif; ?>
        <?php if (in_array('view_quotes', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/quotes"     class="<?= str_starts_with($path,'/quotes')?'active':'' ?>"><i class="bi bi-file-earmark-text-fill"></i> Quotes</a></li>
        <?php endif; ?>
        <?php if (in_array('view_invoices', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/invoices"   class="<?= str_starts_with($path,'/invoices')?'active':'' ?>"><i class="bi bi-receipt"></i> Invoices <?php $oi=db_connect()->table('invoices')->where('company_id',session('company_id'))->where('status','overdue')->where('deleted_at IS NULL',null,false)->countAllResults(); if($oi>0): ?><span class="badge bg-danger ms-auto"><?= $oi ?></span><?php endif; ?></a></li>
        <?php endif; ?>
        <?php if (in_array('view_time', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/time"       class="<?= str_starts_with($path,'/time')?'active':'' ?>"><i class="bi bi-clock-fill"></i> Time</a></li>
        <?php endif; ?>
        <?php if (in_array('view_reports', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/reports"    class="<?= str_starts_with($path,'/reports')?'active':'' ?>"><i class="bi bi-bar-chart-fill"></i> Reports</a></li>
        <?php endif; ?>
        <?php if (in_array('view_global_settings', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/global"     class="<?= str_starts_with($path,'/global')?'active':'' ?>"><i class="bi bi-globe2"></i> Global</a></li>
        <?php endif; ?>
        <?php if (in_array('view_settings', $permissions)): ?>
        <li><a href="/fieldly/public/index.php/settings"   class="<?= str_starts_with($path,'/settings')?'active':'' ?>"><i class="bi bi-gear-fill"></i> Settings</a></li>
        <?php endif; ?>
        <li><a href="/fieldly/public/index.php/bin"        class="<?= str_starts_with($path,'/bin')?'active':'' ?>"><i class="bi bi-trash3-fill"></i> Recycle Bin</a></li>
    </ul>

    <div class="sidebar-user">
        <div class="user-avatar" style="background:<?= esc($currentUser['color']??'#4f46e5') ?>"><?= esc($currentUser['initials']??'?') ?></div>
        <div class="user-info">
            <div class="user-name"><?= esc(($currentUser['first_name']??'').' '.($currentUser['last_name']??'')) ?></div>
            <div class="user-role"><?= esc($currentUser['role_name']??'User') ?></div>
        </div>
        <a href="/fieldly/public/index.php/logout" class="user-logout" title="Logout"><i class="bi bi-box-arrow-right"></i></a>
    </div>
</nav>

<!-- Main content -->
<div class="fieldly-content">
    <!-- Top bar -->
    <header class="fieldly-topbar">
        <button class="sidebar-toggle d-lg-none" onclick="document.getElementById('sidebar').classList.toggle('open')">
            <i class="bi bi-list"></i>
        </button>
        <h1 class="page-title"><?= $title ?? '' ?></h1>
        <div class="topbar-right">
            <?php if (isset($topbarActions)): ?>
                <?= $topbarActions ?>
            <?php endif; ?>
        </div>
    </header>

    <!-- Flash messages -->
    <div class="flash-container px-4 pt-3">
        <?php if (session()->getFlashdata('success')): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="bi bi-check-circle-fill me-2"></i><?= esc(session()->getFlashdata('success')) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>
        <?php if (session()->getFlashdata('error')): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-circle-fill me-2"></i><?= esc(session()->getFlashdata('error')) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>
        <?php if (session()->getFlashdata('errors')): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-circle-fill me-2"></i>
                <ul class="mb-0 mt-1">
                    <?php foreach ((array)session()->getFlashdata('errors') as $e): ?>
                        <li><?= esc($e) ?></li>
                    <?php endforeach; ?>
                </ul>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>
    </div>

    <!-- Page content -->
    <main class="fieldly-main px-4 pb-5">
        <?= $this->renderSection('content') ?>
    </main>
</div>

<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- Fieldly JS -->
<script src="/assets/js/fieldly.js"></script>
<?= $this->renderSection('scripts') ?>
</body>
</html>
