<?php 
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../config/config_database.php';

// Ambil data user yang login dari session atau default ke superadmin
$user_id = $_SESSION['user_id'] ?? 1;

// Ambil pengaturan perusahaan untuk title dinamis
$company_name = 'ANUBILL'; // Default fallback
$company_info = null;

try {
    if (isset($mysqli)) {
        // Ambil pengaturan perusahaan
        $company_query = "SELECT * FROM pengaturan_perusahaan WHERE id_pengaturan = 1 LIMIT 1";
        $company_result = $mysqli->query($company_query);
        if ($company_result && $company_result->num_rows > 0) {
            $company_info = $company_result->fetch_assoc();
            $company_name = $company_info['nama_perusahaan'] ?? 'ANUBILL';
        }
        
        // Ambil data user
        $stmt = $mysqli->prepare("SELECT * FROM users WHERE id_user = ?");
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $result = $stmt->get_result();
        $current_user = $result->fetch_assoc();
    } else {
        $current_user = null;
    }
} catch (Exception $e) {
    // Jika ada error, gunakan default
    $company_name = 'ANUBILL';
    $current_user = null;
}

// Jika user tidak ditemukan, set default
if (!$current_user) {
    $current_user = ['nama_lengkap' => 'Guest'];
}

// Function untuk memformat nama perusahaan menjadi brand yang sesuai
function formatCompanyBrand($name) {
    // Jika nama terlalu panjang, singkat untuk mobile
    if (strlen($name) > 25) {
        $words = explode(' ', $name);
        if (count($words) > 1) {
            // Ambil inisial dari setiap kata
            $initials = '';
            foreach ($words as $word) {
                $initials .= strtoupper(substr($word, 0, 1));
            }
            return $initials;
        } else {
            // Jika satu kata panjang, potong
            return strtoupper(substr($name, 0, 8));
        }
    }
    return strtoupper($name);
}

$brand_name = formatCompanyBrand($company_name);
$page_title = $company_name . ' - INTERNET BILLING MANAGEMENT SYSTEM';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($page_title) ?></title>
    
    <!-- Meta tags untuk SEO -->
    <meta name="description" content="<?= htmlspecialchars($company_name) ?> - Sistem Manajemen Billing Internet yang Terintegrasi">
    <meta name="keywords" content="billing internet, rt/rw net, mikrotik, ftth, voucher hotspot">
    <meta name="author" content="<?= htmlspecialchars($company_name) ?>">
    
    <!-- Favicon jika ada -->
    <?php if ($company_info && !empty($company_info['logo_perusahaan'])): ?>
    <link rel="icon" type="image/png" href="<?= htmlspecialchars($company_info['logo_perusahaan']) ?>">
    <?php endif; ?>
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Helvetica, Arial, sans-serif;
            background-color: #f7f7f7;
            overflow-x: hidden;
        }

        /* Header/Top Navigation */
        .top-nav {
            background-color: #2A3F54;
            color: white;
            padding: 0;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
            height: 55px;
            border-bottom: 1px solid #1D2939;
            display: flex;
            align-items: center;
        }

        .nav-toggle {
            background: none;
            border: none;
            color: white;
            font-size: 18px;
            padding: 15px 20px;
            cursor: pointer;
            height: 55px;
            flex-shrink: 0;
        }

        .nav-toggle:hover {
            background-color: #1D2939;
        }

        /* Dynamic Brand Container */
        #dynamic-nav-brand-container {
            flex-grow: 1;
            display: flex;
            align-items: center;
            padding: 0 20px;
        }

        .nav-brand {
            color: white;
            font-size: 20px;
            font-weight: bold;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 10px;
            white-space: nowrap;
            overflow: hidden;
        }

        .nav-brand:hover {
            color: #E7E7E7;
            text-decoration: none;
        }

        .company-logo {
            width: 32px;
            height: 32px;
            border-radius: 4px;
            object-fit: cover;
        }

        .brand-text {
            display: flex;
            flex-direction: column;
            line-height: 1.2;
        }

        .brand-name {
            font-size: 18px;
            font-weight: bold;
        }

        .brand-subtitle {
            font-size: 10px;
            color: #B8C7CE;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .nav-profile {
            padding: 8px 20px;
            position: relative;
            flex-shrink: 0;
            margin-left: auto;
        }

        .profile-dropdown {
            background: none;
            border: none;
            color: white;
            padding: 8px 0;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .profile-dropdown:hover {
            color: #E7E7E7;
        }

        .profile-img {
            width: 35px;
            height: 35px;
            border-radius: 50%;
            background-color: #5A738E;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* Sidebar */
        .sidebar {
            background-color: #2A3F54;
            position: fixed;
            top: 55px;
            left: 0;
            width: 230px;
            height: calc(100vh - 55px);
            overflow-y: auto;
            transition: all 0.3s ease;
            z-index: 999;
            display: flex;
            flex-direction: column;
        }

        .sidebar.collapsed {
            width: 70px;
        }

        .sidebar-menu {
            list-style: none;
            padding: 0;
            margin: 0;
            flex-grow: 1;
        }

        .sidebar-menu li {
            border-bottom: 1px solid #425668;
            position: relative;
        }

        .sidebar-menu li:last-child {
            border-bottom: none;
        }

        .sidebar-menu a {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            color: #E7E7E7;
            text-decoration: none;
            transition: all 0.3s ease;
            white-space: nowrap;
        }

        .sidebar-menu a:hover {
            background-color: #1D2939;
            color: white;
        }

        .sidebar-menu a.active {
            background-color: #1ABB9C;
            color: white;
        }

        .sidebar-menu .menu-icon {
            font-size: 18px;
            width: 30px;
            text-align: center;
            margin-right: 15px;
        }

        .sidebar-menu .menu-text {
            font-size: 14px;
            font-weight: 400;
        }

        .sidebar-menu .menu-arrow {
            margin-left: auto;
            transition: transform 0.3s ease;
            font-size: 12px;
        }

        .sidebar-menu .menu-arrow.rotated {
            transform: rotate(180deg);
        }

        .sidebar.collapsed .menu-text,
        .sidebar.collapsed .menu-arrow {
            display: none;
        }

        .sidebar.collapsed .menu-icon {
            margin-right: 0;
        }

        .sidebar.collapsed .sidebar-menu a {
            justify-content: center;
            padding: 15px 10px;
        }

        /* Submenu */
        .submenu {
            list-style: none;
            padding: 0;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease;
            background-color: #1D2939;
        }

        .submenu.show {
            max-height: 500px;
        }

        .submenu a {
            padding-left: 50px;
            padding-top: 10px;
            padding-bottom: 10px;
            font-size: 13px;
        }

        .submenu a:hover {
            background-color: #1ABB9C;
        }

        .sidebar.collapsed .submenu {
            display: none;
        }

        /* Sidebar Footer */
        .sidebar-footer {
            background-color: #1D2939;
            padding: 10px 0;
            display: flex;
            justify-content: space-around;
            border-top: 1px solid #425668;
        }

        .sidebar.collapsed .sidebar-footer {
            display: none;
        }

        .sidebar-footer a {
            color: #E7E7E7;
            padding: 10px;
            text-align: center;
            width: 25%;
            position: relative;
            transition: all 0.3s ease;
        }

        .sidebar-footer a:hover {
            color: white;
            background-color: #1ABB9C;
        }

        /* Main Content */
        .main-content {
            margin-left: 230px;
            margin-top: 55px;
            padding: 20px;
            transition: margin-left 0.3s ease;
            min-height: calc(100vh - 55px - 60px);
        }

        .main-content.expanded {
            margin-left: 70px;
        }

        .page-title {
            background-color: white;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .page-title h1 {
            font-size: 24px;
            color: #2A3F54;
            margin: 0;
        }

        .content-card {
            background-color: white;
            padding: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        /* Footer */
        .footer {
            background-color: white;
            padding: 20px;
            border-top: 1px solid #E5E5E5;
            margin-left: 230px;
            transition: margin-left 0.3s ease;
            color: #73879C;
            font-size: 12px;
        }

        .footer.expanded {
            margin-left: 70px;
        }

        .footer-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .footer-links a {
            color: #73879C;
            text-decoration: none;
            margin-left: 20px;
        }

        .footer-links a:hover {
            color: #1ABB9C;
        }

        /* Dropdown menu */
        .dropdown-menu {
            background-color: #2A3F54;
            border: none;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        }

        .dropdown-menu .dropdown-item {
            color: #E7E7E7;
            padding: 10px 20px;
        }

        .dropdown-menu .dropdown-item:hover {
            background-color: #1D2939;
            color: white;
        }

        /* Mobile Responsive Styles */
        @media (max-width: 768px) {
            /* Header adjustments */
            .top-nav {
                height: 50px;
            }

            .nav-toggle {
                padding: 13px 15px;
                font-size: 16px;
                height: 50px;
            }

            #dynamic-nav-brand-container {
                padding: 0 10px;
            }

            .nav-brand {
                font-size: 16px;
            }

            .brand-name {
                font-size: 16px;
            }

            .brand-subtitle {
                font-size: 9px;
            }

            .company-logo {
                width: 24px;
                height: 24px;
            }

            .nav-profile {
                padding: 8px 10px;
            }

            .profile-dropdown span {
                display: none;
            }

            /* Sidebar behavior for mobile */
            .sidebar {
                width: 250px;
                transform: translateX(-100%);
                transition: transform 0.3s ease;
                top: 50px;
                height: calc(100vh - 50px);
            }

            .sidebar.show {
                transform: translateX(0);
            }

            .sidebar.collapsed {
                width: 250px;
                transform: translateX(-100%);
            }

            .sidebar.show.collapsed {
                transform: translateX(0);
            }

            /* Show menu text on mobile */
            .sidebar .menu-text {
                display: block !important;
            }

            /* Main content adjustments */
            .main-content {
                margin-left: 0 !important;
                margin-top: 50px;
                padding: 15px;
            }

            .footer {
                margin-left: 0 !important;
                padding: 15px;
            }

            /* Hide arrows on mobile */
            .menu-arrow {
                display: none !important;
            }

            /* Adjust card padding */
            .content-card {
                padding: 15px;
            }

            /* Page title adjustments */
            .page-title {
                padding: 15px;
            }

            .page-title h1 {
                font-size: 20px;
            }
        }

        /* Tablet and desktop behavior */
        @media (min-width: 769px) {
            .sidebar {
                transform: translateX(0) !important;
            }
            
            .main-content {
                margin-left: 230px;
            }
            
            .sidebar.collapsed + .main-content {
                margin-left: 70px;
            }
            
            .footer {
                margin-left: 230px;
            }
            
            .sidebar.collapsed ~ .footer {
                margin-left: 70px;
            }
        }

        /* Loading animation for brand update */
        .brand-loading {
            opacity: 0.7;
            transition: opacity 0.3s ease;
        }

        /* Company info tooltip */
        .company-info-tooltip {
            position: absolute;
            background: #1D2939;
            color: white;
            padding: 10px;
            border-radius: 4px;
            font-size: 12px;
            white-space: nowrap;
            z-index: 1001;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s ease;
            top: 100%;
            left: 0;
            margin-top: 5px;
        }

        .nav-brand:hover .company-info-tooltip {
            opacity: 1;
        }
    </style>
</head>
<body>
    <!-- Header/Top Navigation -->
    <nav class="top-nav">
        <button class="nav-toggle" id="navToggle">
            <i class="fas fa-bars"></i>
        </button>
        
        <div id="dynamic-nav-brand-container"></div>

        <div class="nav-profile">
            <div class="dropdown">
                <button class="profile-dropdown" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <div class="profile-img">
                        <i class="fas fa-user"></i>
                    </div>
                    <span><?= htmlspecialchars($current_user['nama_lengkap']) ?></span>
                    <i class="fas fa-chevron-down"></i>
                </button>
                <ul class="dropdown-menu dropdown-menu-end">
                    <li><a class="dropdown-item" href="../profile/profile.php">
                        <i class="fas fa-user me-2"></i>Profile
                    </a></li>
                    <li><a class="dropdown-item" href="../settings/pengaturan_perusahaan.php">
                        <i class="fas fa-cog me-2"></i>Pengaturan
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item" href="../auth/logout.php">
                        <i class="fas fa-sign-out-alt me-2"></i>Logout
                    </a></li>
                </ul>
            </div>
        </div>
    </nav>

    <script>
        // Mobile sidebar toggle functionality
        (function() {
            document.addEventListener('DOMContentLoaded', function() {
                const navToggle = document.getElementById('navToggle');
                const sidebar = document.getElementById('sidebar');
                
                // Check if mobile device
                function isMobile() {
                    return window.innerWidth <= 768;
                }
                
                navToggle.addEventListener('click', function() {
                    if (isMobile()) {
                        if (sidebar) sidebar.classList.toggle('show');
                    } else {
                        if (sidebar) sidebar.classList.toggle('collapsed');
                        const mainContent = document.getElementById('mainContent');
                        const footer = document.getElementById('footer');
                        if (mainContent) mainContent.classList.toggle('expanded');
                        if (footer) footer.classList.toggle('expanded');
                    }
                });
                
                // Auto-close sidebar when clicking outside on mobile
                document.addEventListener('click', function(e) {
                    if (isMobile() && sidebar && sidebar.classList.contains('show') && 
                        !sidebar.contains(e.target) && 
                        e.target !== navToggle && 
                        !navToggle.contains(e.target)) {
                        sidebar.classList.remove('show');
                    }
                });

                // Handle window resize
                window.addEventListener('resize', function() {
                    if (!isMobile() && sidebar) {
                        sidebar.classList.remove('show');
                    }
                });
            });

            // Function to update company brand dynamically (for AJAX updates)
            window.updateCompanyBrand = function(companyData) {
                const navBrand = document.getElementById('navBrand');
                if (navBrand && companyData) {
                    // Add loading state
                    navBrand.classList.add('brand-loading');
                    
                    // Update brand text
                    const brandName = navBrand.querySelector('.brand-name');
                    if (brandName && companyData.nama_perusahaan) {
                        const formattedName = formatCompanyName(companyData.nama_perusahaan);
                        brandName.textContent = formattedName;
                    }
                    
                    // Update logo if provided
                    if (companyData.logo_perusahaan) {
                        let logo = navBrand.querySelector('.company-logo');
                        if (!logo) {
                            logo = document.createElement('img');
                            logo.className = 'company-logo';
                            navBrand.insertBefore(logo, navBrand.firstChild);
                        }
                        logo.src = companyData.logo_perusahaan;
                        logo.alt = companyData.nama_perusahaan || 'Company Logo';
                    }
                    
                    // Update page title
                    document.title = (companyData.nama_perusahaan || 'ANUBILL') + ' - INTERNET BILLING MANAGEMENT SYSTEM';
                    
                    // Remove loading state
                    setTimeout(() => {
                        navBrand.classList.remove('brand-loading');
                    }, 300);
                }
            };

            // Helper function to format company name
            function formatCompanyName(name) {
                if (name.length > 25) {
                    const words = name.split(' ');
                    if (words.length > 1) {
                        return words.map(word => word.charAt(0).toUpperCase()).join('');
                    } else {
                        return name.substring(0, 8).toUpperCase();
                    }
                }
                return name.toUpperCase();
            }
        })();
    </script>

    <!-- Bootstrap JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
</body>
</html>