<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="google" content="notranslate">
    <meta http-equiv="Content-Language" content="id">
    <title><?php echo isset($title) ? $title . ' - ' : ''; ?>Sistem RT</title>
    
    <!-- PWA -->
    <link rel="manifest" href="<?= base_url('manifest.json'); ?>">
    <meta name="theme-color" content="#009688">
    <link rel="apple-touch-icon" href="<?= base_url('assets/img/icon-192.png'); ?>">

    <!-- Bootstrap CSS -->
    <!-- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> -->
       <link rel="stylesheet" href="<?= base_url('assets/css/bootstrap.min.css'); ?>">
    <!-- Bootstrap Icons -->
    <!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css"> -->
     <link rel="stylesheet" href="<?= base_url('assets/bootstrap-icons@1.10.0/bootstrap-icons.css'); ?>">
    <!-- DataTables -->
    <!-- <link rel="stylesheet" href="https://cdn.datatables.net/1.13.0/css/dataTables.bootstrap5.min.css"> -->
      <link rel="stylesheet" href="<?= base_url('assets/css/dataTables.bootstrap5.min.css'); ?>">
    
    <!-- Custom CSS -->
    <style>
        :root {
            --primary-color: #009688;
            --sidebar-width: 220px; /* disamakan dengan sidebar.php */
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
        }
        
        /* ===== SIDEBAR ===== */
        .sidebar {
            position: fixed;
            top: 0;
            left: 0;
            height: 100vh;
            width: var(--sidebar-width);
            background: linear-gradient(180deg, #009688 0%, #0d9488 100%);
            padding: 0;
            z-index: 1000;
            overflow-y: auto;
            overflow-x: hidden;
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
            scrollbar-width: thin;
            scrollbar-color: rgba(255,255,255,.15) transparent;
        }
        .sidebar::-webkit-scrollbar { width: 4px; }
        .sidebar::-webkit-scrollbar-track { background: transparent; }
        .sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 4px; }
        
        .sidebar-brand {
            padding: 1.5rem 1rem;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            color: white;
        }
        
        .sidebar-brand i {
            display: block;
            margin-bottom: 0.5rem;
        }
        
        .sidebar-brand h4 {
            margin: 0;
            font-weight: 600;
            color: white;
        }
        
        .sidebar-menu {
            padding: 1rem 0;
        }
        
        .sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 0.65rem 1.25rem;
            margin: 0.15rem 0.5rem;
            border-radius: 8px;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            font-size: 0.85rem;
            white-space: nowrap;
        }
        
        .sidebar .nav-link:hover {
            background-color: rgba(255,255,255,0.1);
            color: white;
        }
        
        .sidebar .nav-link.active {
            background-color: rgba(255,255,255,0.2);
            color: white;
            font-weight: 700;
        }
        
        .sidebar .nav-link i {
            margin-right: 0.65rem;
            font-size: 1rem;
            width: 18px;
            text-align: center;
            flex-shrink: 0;
        }

        /* Mobile sidebar toggle */
        @media (max-width: 991.98px) {
            .sidebar {
                transform: translateX(-100%);
                transition: transform .25s ease;
                box-shadow: none;
            }
            .sidebar.show {
                transform: translateX(0);
                box-shadow: 4px 0 24px rgba(0,0,0,.18);
            }
        }

        /* ===== MAIN CONTENT ===== */
        .main-content {
            margin-left: var(--sidebar-width);
            min-height: 100vh;
            padding: 1.5rem;
            width: calc(100% - var(--sidebar-width));
            box-sizing: border-box;
            transition: margin-left .25s ease, width .25s ease;
        }

        @media (max-width: 991.98px) {
            .main-content {
                margin-left: 0 !important;
                width: 100% !important;
                padding: 1rem;
            }
        }
        
        /* ===== TOPBAR ===== */
        .topbar {
            background: white;
            padding: 1rem 1.5rem;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            margin-bottom: 1.5rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        /* Hamburger button (mobile) */
        .topbar-hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 1.4rem;
            color: #374151;
            cursor: pointer;
            padding: 4px 8px;
            border-radius: 6px;
            transition: background .15s;
        }
        .topbar-hamburger:hover { background: #f1f5f9; }

        @media (max-width: 991.98px) {
            .topbar-hamburger { display: flex; align-items: center; }
        }

        /* Sidebar overlay */
        .sidebar-overlay {
            display: none;
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,.35);
            z-index: 999;
        }
        .sidebar-overlay.show { display: block; }
        
        /* ===== CARD ===== */
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            margin-bottom: 1.5rem;
        }
        
        .card-header {
            background-color: white;
            border-bottom: 1px solid #e9ecef;
            padding: 1rem 1.5rem;
            font-weight: 600;
        }
        
        /* ===== BUTTON ===== */
        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .btn-primary:hover {
            background-color: #0d9488;
            border-color: #0d9488;
        }
        
        /* ===== TABLE ===== */
        .table-responsive {
            border-radius: 10px;
            overflow: hidden;
        }
        
        .table thead th {
            background-color: #f8f9fa;
            font-weight: 600;
            border-bottom: 2px solid #dee2e6;
            white-space: nowrap;
        }
        
        .table tbody tr:hover {
            background-color: #f8f9fa;
        }
        
        /* ===== BADGE & ALERT ===== */
        .badge {
            padding: 0.35rem 0.65rem;
            font-weight: 500;
        }
        
        .alert {
            border-radius: 10px;
            border: none;
        }

        /* ===== TOPBAR AVATAR & DROPDOWN ===== */
        .topbar-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            overflow: hidden;
            background: linear-gradient(135deg, #14a897, #0e7b6e);
            display: flex;
            align-items: center;
            justify-content: center;
            border: 2px solid #e9ecef;
            box-shadow: 0 2px 6px rgba(0,0,0,0.12);
            transition: box-shadow 0.2s, border-color 0.2s;
            text-decoration: none;
            flex-shrink: 0;
            cursor: pointer;
        }

        .topbar-avatar:hover {
            box-shadow: 0 4px 12px rgba(20,168,151,0.35);
            border-color: #14a897;
        }

        .topbar-avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }

        .topbar-user-wrap {
            position: relative;
        }

        .topbar-dropdown {
            display: none;
            position: absolute;
            top: calc(100% + 10px);
            right: 0;
            min-width: 200px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0,0,0,0.12);
            z-index: 9999;
            overflow: hidden;
            border: 1px solid #e9ecef;
        }

        .topbar-dropdown.show {
            display: block;
            animation: dropFade 0.15s ease;
        }

        @keyframes dropFade {
            from { opacity: 0; transform: translateY(-6px); }
            to   { opacity: 1; transform: translateY(0); }
        }

        .topbar-dropdown-header {
            padding: 12px 16px;
            border-bottom: 1px solid #f1f5f9;
            background: #f8fffe;
        }

        .topbar-dropdown-header .name {
            font-weight: 600;
            font-size: 0.9rem;
            color: #1e293b;
            line-height: 1.3;
        }

        .topbar-dropdown-header .role {
            font-size: 0.75rem;
            color: #14a897;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .topbar-dropdown a {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 10px 16px;
            color: #374151;
            text-decoration: none;
            font-size: 0.875rem;
            transition: background 0.15s;
        }

        .topbar-dropdown a:hover {
            background: #f4fcfb;
            color: #14a897;
        }

        .topbar-dropdown a.text-danger:hover {
            background: #fff5f5;
            color: #dc2626 !important;
        }

        .topbar-dropdown .divider {
            height: 1px;
            background: #f1f5f9;
            margin: 2px 0;
        }
    </style>
</head>
<body>
    
    <?php if(!function_exists('is_admin')) { $this->load->helper('authorization'); } ?>

    <?php
    if (!function_exists('tgl_indo_now')) {
        function tgl_indo_now() {
            $hari  = ['Sunday'=>'Minggu','Monday'=>'Senin','Tuesday'=>'Selasa',
                      'Wednesday'=>'Rabu','Thursday'=>'Kamis','Friday'=>'Jumat','Saturday'=>'Sabtu'];
            $bulan = ['January'=>'Januari','February'=>'Februari','March'=>'Maret',
                      'April'=>'April','May'=>'Mei','June'=>'Juni','July'=>'Juli',
                      'August'=>'Agustus','September'=>'September','October'=>'Oktober',
                      'November'=>'November','December'=>'Desember'];
            $str = date('l, d F Y');
            $str = str_replace(array_keys($hari),  array_values($hari),  $str);
            $str = str_replace(array_keys($bulan), array_values($bulan), $str);
            return $str;
        }
    }

    $foto_session  = $this->session->userdata('foto');
    $foto_path_hd  = 'uploads/foto_profil/' . $foto_session;
    $has_foto_hd   = !empty($foto_session) && file_exists(FCPATH . $foto_path_hd);
    ?>

    <!-- Sidebar Overlay (mobile) -->
    <div class="sidebar-overlay" id="sidebarOverlay" onclick="closeSidebar()"></div>
    
    <!-- Sidebar -->
    <?php $this->load->view('templates/sidebar'); ?>
    
    <!-- Main Content -->
    <div class="main-content">
        <div class="topbar">
            <div class="d-flex align-items-center gap-3">
                <!-- Hamburger (mobile only) -->
                <button class="topbar-hamburger" onclick="toggleSidebar()" id="sidebarToggle">
                    <i class="bi bi-list"></i>
                </button>
                <div>
                    <h5 class="mb-0"><?php echo isset($title) ? $title : 'Dashboard'; ?></h5>
                    <small class="text-muted"><?php echo tgl_indo_now(); ?></small>
                </div>
            </div>

            <!-- Kanan: info user + avatar -->
            <div class="d-flex align-items-center gap-3">
                <div class="text-end d-none d-sm-block">
                    <div class="fw-bold" style="font-size:0.9rem;">
                        <?php echo htmlspecialchars($this->session->userdata('nama')); ?>
                    </div>
                    <small class="text-muted text-uppercase" style="font-size:0.72rem; letter-spacing:0.5px;">
                        <?php echo $this->session->userdata('role'); ?>
                    </small>
                </div>

                <!-- Avatar + Dropdown -->
                <div class="topbar-user-wrap" id="topbarUserWrap">
                    <div class="topbar-avatar" id="topbarAvatar" role="button" title="Akun Saya">
                        <?php if($has_foto_hd): ?>
                            <img src="<?php echo base_url($foto_path_hd) . '?v=' . filemtime(FCPATH . $foto_path_hd); ?>"
                                 alt="Foto Profil">
                        <?php else: ?>
                            <i class="bi bi-person-fill text-white" style="font-size:1.2rem;"></i>
                        <?php endif; ?>
                    </div>

                    <div class="topbar-dropdown" id="topbarDropdown">
                        <div class="topbar-dropdown-header d-flex align-items-center gap-2">
                            <div style="width:36px;height:36px;border-radius:50%;overflow:hidden;
                                        background:linear-gradient(135deg,#14a897,#0e7b6e);
                                        display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                                <?php if($has_foto_hd): ?>
                                    <img src="<?php echo base_url($foto_path_hd) . '?v=' . filemtime(FCPATH . $foto_path_hd); ?>"
                                         style="width:100%;height:100%;object-fit:cover;" alt="">
                                <?php else: ?>
                                    <i class="bi bi-person-fill text-white" style="font-size:1rem;"></i>
                                <?php endif; ?>
                            </div>
                            <div>
                                <div class="name">
                                    <?php echo htmlspecialchars($this->session->userdata('nama')); ?>
                                </div>
                                <div class="role">
                                    <?php echo $this->session->userdata('role'); ?>
                                </div>
                            </div>
                        </div>

                        <a href="<?php echo base_url('profil'); ?>">
                            <i class="bi bi-person-circle" style="color:#14a897;width:16px;"></i>
                            Profil Saya
                        </a>
                        <a href="<?php echo base_url('profil'); ?>#gantiPassword">
                            <i class="bi bi-shield-lock" style="color:#6b7280;width:16px;"></i>
                            Ganti Password
                        </a>
                        <div class="divider"></div>
                        <a href="<?php echo base_url('auth/logout'); ?>" class="text-danger"
                           data-confirm="Yakin ingin keluar?" data-title="Logout" data-type="danger">
                            <i class="bi bi-box-arrow-right" style="width:16px;"></i>
                            Keluar
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Flash Messages -->
        <?php if($this->session->flashdata('success')): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="bi bi-check-circle-fill me-2"></i>
                <?php echo $this->session->flashdata('success'); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>
        
        <?php if($this->session->flashdata('error')): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-triangle-fill me-2"></i>
                <?php echo $this->session->flashdata('error'); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <?php $this->load->view('templates/mobile_bottom_nav'); ?>

        <script>
        // ===== Topbar Avatar Dropdown =====
        const topbarAvatar   = document.getElementById('topbarAvatar');
        const topbarDropdown = document.getElementById('topbarDropdown');

        if (topbarAvatar && topbarDropdown) {
            topbarAvatar.addEventListener('click', function(e) {
                e.stopPropagation();
                topbarDropdown.classList.toggle('show');
            });
            document.addEventListener('click', function(e) {
                if (!topbarAvatar.contains(e.target) && !topbarDropdown.contains(e.target)) {
                    topbarDropdown.classList.remove('show');
                }
            });
        }

        // ===== Sidebar Toggle (mobile) =====
        function toggleSidebar() {
            const sidebar = document.getElementById('mainSidebar');
            const overlay = document.getElementById('sidebarOverlay');
            sidebar.classList.toggle('show');
            overlay.classList.toggle('show');
        }

        function closeSidebar() {
            const sidebar = document.getElementById('mainSidebar');
            const overlay = document.getElementById('sidebarOverlay');
            sidebar.classList.remove('show');
            overlay.classList.remove('show');
        }

        // Tutup sidebar jika klik nav-link di mobile
        document.querySelectorAll('.sidebar .nav-link:not([data-bs-toggle])').forEach(function(link) {
            link.addEventListener('click', function() {
                if (window.innerWidth <= 991) {
                    closeSidebar();
                }
            });
        });
        </script>