<?php
// Baca nama aplikasi dari database, fallback ke 'ESCOBAR BILLING' jika kosong
$sys_app_name = get_setting($conn, 'app_name') ?: 'ESCOBAR BILLING';
$page_title = $page_title ?? 'Dashboard';
$active_page = $active_page ?? 'dashboard';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?> | <?php echo $sys_app_name; ?></title>
    
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&family=JetBrains+Mono:wght@700&display=swap" rel="stylesheet">
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
    .swal2-popup {
        background: #1e293b !important;
        color: #fff !important;
        border: 1px solid rgba(255,255,255,0.1) !important;
        border-radius: 24px !important;
        backdrop-filter: blur(15px) !important;
    }
    .swal2-title, .swal2-html-container { color: #fff !important; }
    .swal2-confirm { background: linear-gradient(135deg, #06b6d4, #8b5cf6) !important; border-radius: 12px !important; }
</style>

    <style>
        :root {
            --bg-body: #0f172a;
            --bg-sidebar: #1e293b;
            --primary: #06b6d4;
            --primary-glow: rgba(6, 182, 212, 0.3);
            --success: #10b981;
            --danger: #ef4444;
            --warning: #f59e0b;
            --text-main: #f1f5f9;
            --text-muted: #94a3b8;
            --border: rgba(255, 255, 255, 0.08);
            --card-bg: #1e293b;
            
            --sidebar-width: 280px; 
            --sidebar-collapsed-width: 88px;
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        ::-webkit-scrollbar { width: 6px; }
        ::-webkit-scrollbar-track { background: var(--bg-body); }
        ::-webkit-scrollbar-thumb { background: #334155; border-radius: 10px; }
        ::-webkit-scrollbar-thumb:hover { background: var(--primary); }

        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Inter', sans-serif; }

        body {
            background-color: var(--bg-body);
            color: var(--text-main);
            display: flex;
            min-height: 100vh;
            overflow-x: hidden;
        }

        input, select {
            background: #0f172a !important;
            border: 1px solid #334155 !important;
            color: #fff !important;
            padding: 12px 16px !important;
            border-radius: 12px !important;
            outline: none !important;
            transition: 0.3s !important;
            font-size: 0.9rem !important;
        }
        input:focus, select:focus { border-color: var(--primary) !important; box-shadow: 0 0 15px var(--primary-glow) !important; }

        .btn, button {
            padding: 12px 24px;
            border-radius: 12px;
            border: none;
            font-weight: 700;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
            text-transform: uppercase;
            font-size: 0.85rem;
            letter-spacing: 0.5px;
        }

        .btn-primary { background: var(--primary) !important; color: #000 !important; }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 5px 15px var(--primary-glow); filter: brightness(1.1); }

        .sidebar {
            width: var(--sidebar-width);
            background: var(--bg-sidebar);
            border-right: 1px solid var(--border);
            display: flex;
            flex-direction: column;
            transition: var(--transition);
            position: fixed;
            height: 100vh;
            z-index: 1000;
        }

        .sidebar.collapsed { width: var(--sidebar-collapsed-width); }

        .brand {
            padding: 30px 20px;
            display: flex;
            align-items: center;
            gap: 15px;
            color: var(--primary);
            text-decoration: none;
            overflow: hidden;
        }
        .brand i { font-size: 2.2rem; min-width: 45px; display: flex; justify-content: center; text-shadow: 0 0 15px var(--primary-glow); }
        .brand span { font-size: 1.2rem; font-weight: 800; white-space: nowrap; transition: opacity 0.3s; letter-spacing: 1px; }
        .sidebar.collapsed .brand span { opacity: 0; pointer-events: none; }

        .menu-list { list-style: none; padding: 0 15px; flex: 1; overflow-y: auto; overflow-x: hidden; }
        
        .menu-label {
            font-size: 0.7rem;
            font-weight: 800;
            color: var(--text-muted);
            text-transform: uppercase;
            margin: 20px 0 10px 15px;
            letter-spacing: 1px;
            transition: opacity 0.3s;
        }
        .sidebar.collapsed .menu-label { opacity: 0; }

        .menu-item {
            display: flex; align-items: center; padding: 14px 15px; color: var(--text-muted);
            text-decoration: none; border-radius: 14px; margin-bottom: 4px; transition: 0.3s;
            white-space: nowrap;
        }
        .menu-item i { font-size: 1.5rem; min-width: 40px; transition: 0.3s; }
        .menu-item span { font-weight: 600; font-size: 0.95rem; }
        .sidebar.collapsed .menu-item span { opacity: 0; display: none; }

        .menu-item:hover { background: rgba(6, 182, 212, 0.08); color: var(--primary); }
        .menu-item:hover i { transform: scale(1.1); }
        
        .menu-item.active { 
            background: linear-gradient(90deg, rgba(6, 182, 212, 0.15) 0%, transparent 100%); 
            color: var(--primary); 
            box-shadow: inset 4px 0 0 var(--primary);
        }
        .menu-item.active i { color: var(--primary); text-shadow: 0 0 10px var(--primary-glow); }

        .user-footer {
            padding: 20px 15px; border-top: 1px solid var(--border);
            display: flex; align-items: center; gap: 12px; background: rgba(0,0,0,0.1);
        }
        .user-avatar { 
            width: 44px; height: 44px; border-radius: 12px; 
            background: linear-gradient(135deg, var(--primary), #0891b2); 
            display: flex; align-items: center; justify-content: center; 
            font-weight: 800; color: #000; min-width: 44px;
        }
        .user-details { flex: 1; overflow: hidden; transition: opacity 0.3s; }
        .user-details div:first-child { font-size: 0.9rem; font-weight: 700; color: #fff; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
        .user-details div:last-child { font-size: 0.65rem; color: var(--primary); font-weight: 800; text-transform: uppercase; }
        .sidebar.collapsed .user-details { opacity: 0; display: none; }

        .btn-logout { color: var(--text-muted); font-size: 1.4rem; transition: 0.3s; border: none; background: none; cursor: pointer; }
        .btn-logout:hover { color: var(--danger); transform: rotate(15deg); }

        .main {
            margin-left: var(--sidebar-width);
            padding: 40px;
            width: calc(100% - var(--sidebar-width));
            transition: var(--transition);
        }
        .main.expanded { margin-left: var(--sidebar-collapsed-width); width: calc(100% - var(--sidebar-collapsed-width)); }

        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; }
        
        .header-left { display: flex; align-items: center; gap: 20px; }
        
        .toggle-btn {
            background: var(--bg-card); border: 1px solid var(--border); color: var(--text-main);
            width: 45px; height: 45px; border-radius: 12px; display: flex; align-items: center; justify-content: center;
            cursor: pointer; font-size: 1.5rem; transition: 0.3s;
        }
        .toggle-btn:hover { border-color: var(--primary); color: var(--primary); background: rgba(6,182,212,0.1); }

        #real-clock { 
            font-family: 'JetBrains Mono', monospace;
            font-weight: 700; font-size: 1rem; color: var(--primary); 
            background: rgba(6, 182, 212, 0.05); padding: 10px 20px; border-radius: 12px; border: 1px solid var(--border);
            letter-spacing: 1px;
        }

        @media (max-width: 768px) {
            .sidebar { transform: translateX(-100%); width: var(--sidebar-width); }
            .sidebar.mobile-open { transform: translateX(0); }
            .main { margin-left: 0; width: 100%; padding: 20px; }
            .main.expanded { margin-left: 0; width: 100%; }
        }
    </style>
</head>
<body>
<?php include 'warning_tos.php'; ?>
    <aside class="sidebar" id="sidebar">
        <a href="index.php" class="brand">
            <i class='bx bxs-zap'></i>
            <span><?php echo $sys_app_name; ?></span>
        </a>
        
        <nav class="menu-list">
            
            <div class="menu-label">Menu Utama</div>
            
            <a href="index.php" class="menu-item <?php echo $active_page=='dashboard'?'active':''; ?>">
                <i class='bx bxs-dashboard'></i> <span>Dashboard</span>
            </a>
            
            <?php if(has_access($conn, 'perm_manage_inventory')): ?>
            <a href="inventory.php" class="menu-item <?php echo $active_page=='inventory'?'active':''; ?>">
                <i class='bx bxs-box'></i> <span>Paket & Stok</span>
            </a>
            <?php endif; ?>
            
            <?php if(has_access($conn, 'perm_manage_units')): ?>
            <a href="units.php" class="menu-item <?php echo $active_page=='units'?'active':''; ?>">
                <i class='bx bxs-tv'></i> <span>Kelola Unit</span>
            </a>
            <?php endif; ?>

<?php if(has_access($conn, 'perm_manage_units')): ?>
<a href="sticks.php" class="menu-item <?php echo $active_page=='sticks'?'active':''; ?>">
    <i class='bx bx-joystick-button'></i> <span>Management Stik</span>
</a>
<?php endif; ?>

<?php if(($_SESSION['role']??'') === 'admin'): ?>
<a href="tuya_control.php" class="menu-item <?php echo $active_page=='tuya'?'active':''; ?>">
    <i class='bx bx-plug'></i> <span>Smart Plug Tuya</span>
</a>
<?php endif; ?>

            <?php if(has_access($conn, 'perm_manage_members')): ?>
            <a href="members.php" class="menu-item <?php echo $active_page=='members'?'active':''; ?>">
                <i class='bx bxs-user-detail'></i> <span>Member VIP</span>
            </a>
            <?php endif; ?>

<?php if(has_access($conn, 'perm_view_closing') || has_access($conn, 'perm_view_history')): ?>
<div class="menu-label">Keuangan & Audit</div>
<?php endif; ?>
            
            <?php if(has_access($conn, 'perm_view_closing')): ?>
            <a href="closing.php" class="menu-item <?php echo $active_page=='closing'?'active':''; ?>">
                <i class='bx bxs-calculator'></i> <span>Closing Shift</span>
            </a>
            <?php endif; ?>
            
            <?php if(has_access($conn, 'perm_view_history')): ?>
            <a href="history.php" class="menu-item <?php echo $active_page=='history'?'active':''; ?>">
                <i class='bx bxs-coin-stack'></i> <span>Riwayat Transaksi</span>
            </a>
            <?php endif; ?>
            
            <?php if($_SESSION['role'] == 'admin'): ?>
            <div class="menu-label">Sistem Admin</div>
            <a href="users.php" class="menu-item <?php echo $active_page=='users'?'active':''; ?>">
                <i class='bx bxs-user-badge'></i> <span>Kelola Operator</span>
            </a>

            <a href="settings.php" class="menu-item <?php echo $active_page=='settings'?'active':''; ?>">
                <i class='bx bxs-cog'></i> <span>Pengaturan</span>
            </a>
            <?php endif; ?>

            <div class="menu-label">Bantuan</div>

            <a href="guide.php" class="menu-item <?php echo $active_page=='guide'?'active':''; ?>">
                <i class='bx bxs-book-content'></i> <span>Panduan Pemakaian</span>
            </a>

            <a href="about.php" class="menu-item <?php echo $active_page=='about'?'active':''; ?>">
                <i class='bx bxs-info-circle'></i> <span>Tentang Kami</span>
            </a>

        </nav>

        <div class="user-footer">
            <div class="user-avatar"><?php echo strtoupper(substr($_SESSION['username'], 0, 1)); ?></div>
            <div class="user-details">
                <div><?php echo htmlspecialchars($_SESSION['username']); ?></div>
                <div><?php echo strtoupper(htmlspecialchars($_SESSION['role'])); ?></div>
            </div>
            <form action="logout.php" method="POST" style="display: inline;">
                <button type="submit" class="btn-logout" title="Keluar">
                    <i class='bx bx-log-out-circle'></i>
                </button>
            </form>
        </div>
    </aside>

    <main class="main" id="main-content">
        <header class="header">
            <div class="header-left">
                <button class="toggle-btn" onclick="toggleSidebar()">
                    <i class='bx bx-menu-alt-left'></i>
                </button>
                <h1 style="font-weight: 800; font-size: 1.8rem; letter-spacing: -1px;"><?php echo $page_title; ?></h1>
            </div>
            <div id="real-clock">00:00:00 WIB</div>
        </header>

    <script>
        function toggleSidebar() {
            const sidebar = document.getElementById('sidebar');
            const main = document.getElementById('main-content');
            
            if (window.innerWidth <= 768) {
                sidebar.classList.toggle('mobile-open');
            } else {
                sidebar.classList.toggle('collapsed');
                main.classList.toggle('expanded');
                localStorage.setItem('sidebar-state', sidebar.classList.contains('collapsed') ? 'collapsed' : 'open');
            }
        }

        document.addEventListener('DOMContentLoaded', () => {
            const sidebarState = localStorage.getItem('sidebar-state');
            if (sidebarState === 'collapsed' && window.innerWidth > 768) {
                const sidebar = document.getElementById('sidebar');
                const main = document.getElementById('main-content');
                if(sidebar) sidebar.classList.add('collapsed');
                if(main) main.classList.add('expanded');
            }

            function updateClock() {
                const now = new Date();
                const clockEl = document.getElementById('real-clock');
                if(clockEl) {
                    clockEl.innerText = now.toLocaleTimeString('id-ID', { 
                        hour: '2-digit', 
                        minute: '2-digit', 
                        second: '2-digit',
                        hour12: false 
                    }) + " WIB";
                }
            }
            setInterval(updateClock, 1000);
            updateClock();
        });
    </script>