<?php
use App\Models\Settings;

// Get system branding
$appConfig = new Settings();
$appName = $appConfig->get('company_name', 'Finance Manager');
$logo = $appConfig->get('logo');
$logoUrl = $logo ? $this->baseUrl('/' . ltrim($logo, '/')) : null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($appName) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="<?= $this->baseUrl('/css/style.css') ?>">
    <style>
        /* Mobile-first sidebar offcanvas */
        @media (max-width: 991.98px) {
            .sidebar-desktop { display: none !important; }
        }
        @media (min-width: 992px) {
            .sidebar-mobile-toggle { display: none; }
            .sidebar-offcanvas { display: none; }
        }
        .offcanvas-sidebar {
            width: 280px !important;
            background: linear-gradient(180deg, var(--darker) 0%, var(--dark) 100%);
            color: #94a3b8;
            border-right: 1px solid rgba(212,175,55,0.2);
        }
        .offcanvas-sidebar .nav-link {
            color: #94a3b8;
        }
        .offcanvas-sidebar .nav-link.active,
        .offcanvas-sidebar .nav-link:hover {
            background: linear-gradient(90deg, rgba(212,175,55,0.15) 0%, transparent 100%);
            color: var(--gold-light);
            border-left: 3px solid var(--gold);
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row g-0">
        <!-- Desktop Sidebar (visible on lg+) -->
        <div class="col-auto px-0 sidebar sidebar-desktop">
            <div class="d-flex flex-column p-3">
                <!-- Brand -->
                <a href="<?= $this->baseUrl('/dashboard') ?>" class="navbar-brand mb-4 d-flex align-items-center gap-2 text-decoration-none">
                    <?php if ($logoUrl): ?>
                        <img src="<?= $logoUrl ?>" alt="Logo" style="height:35px; max-width:100px;"
                             onerror="this.style.display='none'; this.nextElementSibling.style.display='inline';">
                        <span style="display:none; font-size:1.5rem;">💰</span>
                    <?php else: ?>
                        <span style="font-size:1.5rem;">💰</span>
                    <?php endif; ?>
                    <span style="color: var(--gold-light);"><?= htmlspecialchars($appName) ?></span>
                </a>
                <!-- Navigation (same as before) -->
                <ul class="nav nav-pills flex-column mb-auto">
                    <li><a href="<?= $this->baseUrl('/dashboard') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/dashboard') !== false ? 'active' : '' ?>"><i class="bi bi-speedometer2"></i> Dashboard</a></li>
                    <li><a href="<?= $this->baseUrl('/clients') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/clients') !== false ? 'active' : '' ?>"><i class="bi bi-people"></i> Clients</a></li>
                    <li><a href="<?= $this->baseUrl('/forex/trades') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/forex') !== false ? 'active' : '' ?>"><i class="bi bi-currency-exchange"></i> Forex</a></li>
                    <li><a href="<?= $this->baseUrl('/rent/properties') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/rent') !== false ? 'active' : '' ?>"><i class="bi bi-house"></i> Rent</a></li>
                    <li><a href="<?= $this->baseUrl('/mpesa') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/mpesa') !== false ? 'active' : '' ?>"><i class="bi bi-phone"></i> M‑Pesa</a></li>
                    <li><a href="<?= $this->baseUrl('/daily-float') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/daily-float') !== false ? 'active' : '' ?>"><i class="bi bi-cash-stack"></i> Daily Float</a></li>
                    <li><a href="<?= $this->baseUrl('/reports') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/reports') === 0 ? 'active' : '' ?>"><i class="bi bi-file-earmark-bar-graph"></i> Reports</a></li>
                    <li><a href="<?= $this->baseUrl('/users') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/users') !== false ? 'active' : '' ?>"><i class="bi bi-person-gear"></i> Users</a></li>
                    <li><a href="<?= $this->baseUrl('/settings') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/settings') !== false ? 'active' : '' ?>"><i class="bi bi-gear"></i> Settings</a></li>
                </ul>
                <hr class="text-secondary">
                <!-- User dropdown -->
                <div class="dropdown">
                    <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
                        <i class="bi bi-person-circle me-2"></i><span><?= $_SESSION['user_name'] ?? 'User' ?></span>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-dark">
                        <li><a class="dropdown-item" href="<?= $this->baseUrl('/profile') ?>">Profile</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="<?= $this->baseUrl('/logout') ?>">Logout</a></li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- Mobile Sidebar Offcanvas -->
        <div class="offcanvas offcanvas-start offcanvas-sidebar" tabindex="-1" id="mobileSidebar">
            <div class="offcanvas-header">
                <h5 class="offcanvas-title" style="color:var(--gold-light);"><?= htmlspecialchars($appName) ?></h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas"></button>
            </div>
            <div class="offcanvas-body">
                <ul class="nav nav-pills flex-column">
                    <li><a href="<?= $this->baseUrl('/dashboard') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/dashboard') !== false ? 'active' : '' ?>"><i class="bi bi-speedometer2"></i> Dashboard</a></li>
                    <li><a href="<?= $this->baseUrl('/clients') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/clients') !== false ? 'active' : '' ?>"><i class="bi bi-people"></i> Clients</a></li>
                    <li><a href="<?= $this->baseUrl('/forex/trades') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/forex') !== false ? 'active' : '' ?>"><i class="bi bi-currency-exchange"></i> Forex</a></li>
                    <li><a href="<?= $this->baseUrl('/rent/properties') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/rent') !== false ? 'active' : '' ?>"><i class="bi bi-house"></i> Rent</a></li>
                    <li><a href="<?= $this->baseUrl('/mpesa') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/mpesa') !== false ? 'active' : '' ?>"><i class="bi bi-phone"></i> M‑Pesa</a></li>
                    <li><a href="<?= $this->baseUrl('/daily-float') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/daily-float') !== false ? 'active' : '' ?>"><i class="bi bi-cash-stack"></i> Daily Float</a></li>
                    <li><a href="<?= $this->baseUrl('/reports') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/reports') === 0 ? 'active' : '' ?>"><i class="bi bi-file-earmark-bar-graph"></i> Reports</a></li>
                    <li><a href="<?= $this->baseUrl('/users') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/users') !== false ? 'active' : '' ?>"><i class="bi bi-person-gear"></i> Users</a></li>
                    <li><a href="<?= $this->baseUrl('/settings') ?>" class="nav-link <?= strpos($_SERVER['REQUEST_URI'], '/settings') !== false ? 'active' : '' ?>"><i class="bi bi-gear"></i> Settings</a></li>
                </ul>
                <hr class="text-secondary">
                <div class="dropdown">
                    <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
                        <i class="bi bi-person-circle me-2"></i><span><?= $_SESSION['user_name'] ?? 'User' ?></span>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-dark">
                        <li><a class="dropdown-item" href="<?= $this->baseUrl('/profile') ?>">Profile</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="<?= $this->baseUrl('/logout') ?>">Logout</a></li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- Main content -->
        <div class="col p-0">
            <div class="top-bar d-flex justify-content-between align-items-center">
                <div class="d-flex align-items-center gap-2">
                    <!-- Mobile hamburger button -->
                    <button class="btn btn-outline-secondary sidebar-mobile-toggle" type="button" data-bs-toggle="offcanvas" data-bs-target="#mobileSidebar">
                        <i class="bi bi-list"></i>
                    </button>
                    <h4 class="mb-0"><?= $pageTitle ?? 'Dashboard' ?></h4>
                </div>
                <div>
                    <span class="text-muted"><?= date('l, F j, Y') ?></span>
                </div>
            </div>
            <div class="main-content"></div>