<?php
// Template Sidebar untuk Semua Halaman Admin
// File: admin-sidebar-template.php

// Check if admin is logged in
if (!defined('ADMIN_ACCESS')) {
    die('Direct access not allowed');
}

// Get current admin data
$currentAdmin = $_SESSION['admin_data'] ?? null;
$isMasterAdmin = ($currentAdmin && isset($currentAdmin['is_master_admin']) && $currentAdmin['is_master_admin']) ||
                 (isset($_SESSION['integrated_admin_type']) && $_SESSION['integrated_admin_type'] === 'master') ||
                 ($currentAdmin && isset($currentAdmin['is_master']) && $currentAdmin['is_master']);

// Current page detection
$currentPage = basename($_SERVER['PHP_SELF'], '.php');

function isActivePage($page) {
    global $currentPage;
    return $currentPage === $page ? 'active' : '';
}

function canAccess($permission) {
    global $isMasterAdmin, $currentAdmin;
    
    // Master admin has access to everything
    if ($isMasterAdmin) return true;
    
    // For sub-admins, allow permissions based on their access
    $allowedPermissions = [
        'manage_markets',
        'manage_results', 
        'manage_prediksi',
        'manage_buku_mimpi',
        'manage_content',
        'manage_settings',
        'manage_support'
    ];
    
    return in_array($permission, $allowedPermissions);
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle ?? 'Admin Panel'; ?> - <?php echo ADMIN_TITLE ?? 'Togel Admin'; ?></title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <!-- FontAwesome Icons -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <!-- Custom Admin CSS -->
    <link href="../shared/css/admin-styles-fixed.css" rel="stylesheet">
</head>
<body>
    <!-- Sidebar -->
    <nav class="sidebar" id="sidebar">
        <!-- Sidebar Header -->
        <div class="sidebar-header">
            <a href="dashboard.php" class="sidebar-brand">
                <i class="bi bi-speedometer2"></i>
                <span class="brand-text">Admin Panel</span>
            </a>
        </div>

        <!-- Navigation Menu -->
        <div class="sidebar-nav d-flex flex-column h-100">
            <!-- Dashboard -->
            <div class="nav-item">
                <a href="dashboard.php" class="nav-link <?php echo isActivePage('dashboard'); ?>">
                    <i class="bi bi-house"></i>
                    <span>Dashboard</span>
                </a>
            </div>

            <?php if ($isMasterAdmin): ?>
            <!-- MASTER ADMIN ONLY MENUS -->
            <div class="nav-divider">
                <small class="text-muted">Master Admin</small>
            </div>
            
            <!-- Master Multi Result Input -->
            <div class="nav-item">
                <a href="master-multi-result-input.php" class="nav-link <?php echo isActivePage('master-multi-result-input'); ?>">
                    <i class="bi bi-columns-gap"></i>
                    <span>Multi Site Result Input</span>
                </a>
            </div>
            
            <!-- Master View All Results -->
            <div class="nav-item">
                <a href="master-view-all-results.php" class="nav-link <?php echo isActivePage('master-view-all-results'); ?>">
                    <i class="bi bi-eye"></i>
                    <span>View All Results</span>
                </a>
            </div>
            
            <!-- Master Buku Mimpi -->
            <div class="nav-item">
                <a href="master-buku-mimpi.php" class="nav-link <?php echo isActivePage('master-buku-mimpi'); ?>">
                    <i class="bi bi-book"></i>
                    <span>Master Buku Mimpi</span>
                </a>
            </div>
            
            <!-- Master Support Tickets -->
            <div class="nav-item">
                <a href="master-support.php" class="nav-link <?php echo isActivePage('master-support'); ?>">
                    <i class="bi bi-headset"></i>
                    <span>Support Tickets</span>
                </a>
            </div>

            <!-- Database Management -->
            <div class="nav-item">
                <a href="database.php" class="nav-link <?php echo isActivePage('database'); ?>">
                    <i class="bi bi-server"></i>
                    <span>Database Management</span>
                </a>
            </div>
            
            <?php else: ?>
            <!-- REGULAR ADMIN MENUS -->
            <div class="nav-divider">
                <small class="text-muted">Site Management</small>
            </div>

            <!-- Sites Management (Hidden for regular admin) -->
            <?php if (false): // Hidden for now ?>
            <div class="nav-item">
                <a href="sites.php" class="nav-link <?php echo isActivePage('sites'); ?>">
                    <i class="bi bi-globe"></i>
                    <span>Kelola Situs</span>
                </a>
            </div>
            <?php endif; ?>

            <!-- Sub Admins (Hidden for regular admin) -->
            <?php if (false): // Hidden for now ?>
            <div class="nav-item">
                <a href="sub-admins.php" class="nav-link <?php echo isActivePage('sub-admins'); ?>">
                    <i class="bi bi-people"></i>
                    <span>Kelola Sub Admin</span>
                </a>
            </div>
            <?php endif; ?>
            
            <!-- Markets Management -->
            <div class="nav-item">
                <a href="markets.php" class="nav-link <?php echo isActivePage('markets'); ?>">
                    <i class="bi bi-shop"></i>
                    <span>Kelola Pasaran</span>
                </a>
            </div>

            <!-- Site Markets Assignment (Hidden for regular admin) -->
            <?php if (false): // Hidden for now ?>
            <div class="nav-item">
                <a href="site-markets.php" class="nav-link <?php echo isActivePage('site-markets'); ?>">
                    <i class="bi bi-diagram-3"></i>
                    <span>Assign Pasaran ke Situs</span>
                </a>
            </div>
            <?php endif; ?>

            <!-- Live Draw Results -->
            <div class="nav-item">
                <a href="livedraw.php" class="nav-link <?php echo isActivePage('livedraw') || isActivePage('multi-result-input'); ?>">
                    <i class="bi bi-broadcast"></i>
                    <span>Live Draw</span>
                </a>
            </div>

            <!-- Predictions -->
            <div class="nav-item">
                <a href="../prediksi.php" class="nav-link" target="_blank">
                    <i class="bi bi-graph-up"></i>
                    <span>Prediksi</span>
                </a>
            </div>

            <!-- Buku Mimpi -->
            <div class="nav-item">
                <a href="buku-mimpi.php" class="nav-link <?php echo isActivePage('buku-mimpi'); ?>">
                    <i class="bi bi-book"></i>
                    <span>Buku Mimpi</span>
                </a>
            </div>

            <!-- Banners -->
            <div class="nav-item">
                <a href="banners.php" class="nav-link <?php echo isActivePage('banners'); ?>">
                    <i class="bi bi-image"></i>
                    <span>Banner</span>
                </a>
            </div>

            <!-- Website Settings -->
            <div class="nav-item">
                <a href="website-settings.php" class="nav-link <?php echo isActivePage('website-settings') || isActivePage('settings'); ?>">
                    <i class="bi bi-gear"></i>
                    <span>Pengaturan Web</span>
                </a>
            </div>

            <!-- Support -->
            <div class="nav-item">
                <a href="support.php" class="nav-link <?php echo isActivePage('support'); ?>">
                    <i class="bi bi-headset"></i>
                    <span>Support</span>
                </a>
            </div>

            <!-- Bukti Jackpot -->
            <div class="nav-item">
                <a href="bukti-jackpot.php" class="nav-link <?php echo isActivePage('bukti-jackpot'); ?>">
                    <i class="bi bi-trophy"></i>
                    <span>Bukti Jackpot</span>
                </a>
            </div>
            <?php endif; ?>
        </div>

        <!-- Spacer untuk push admin info ke bawah -->
        <div class="flex-grow-1"></div>

        <!-- Admin Info - Fixed at bottom -->
        <div class="admin-info mt-auto">
            <div class="d-flex align-items-center mb-2">
                <div class="admin-avatar">
                    <?php echo strtoupper(substr($currentAdmin['username'] ?? 'A', 0, 1)); ?>
                </div>
                <div class="flex-grow-1 ms-2">
                    <div class="fw-semibold small"><?php echo htmlspecialchars($currentAdmin['username'] ?? 'Admin'); ?></div>
                    <small class="text-muted">
                        <?php echo $isMasterAdmin ? 'Master Admin' : 'Sub Admin'; ?>
                    </small>
                </div>
            </div>
            <div class="d-grid">
                <a href="<?php echo $isMasterAdmin ? 'logout.php' : 'logout-multi.php'; ?>" class="btn btn-outline-light btn-sm">
                    <i class="bi bi-box-arrow-right me-1"></i>
                    Logout
                </a>
            </div>
        </div>
    </nav>

    <!-- Sidebar Toggle Button -->
    <button class="sidebar-toggle d-lg-none" id="sidebarToggle">
        <i class="bi bi-list"></i>
    </button>

    <!-- Main Content -->
    <div class="main-content" id="mainContent">
        <!-- Content Header -->
        <div class="content-header">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h1 class="h3 mb-0"><?php echo $pageTitle ?? 'Dashboard'; ?></h1>
                    <?php if (isset($pageDescription)): ?>
                    <p class="text-muted mb-0"><?php echo $pageDescription; ?></p>
                    <?php endif; ?>
                </div>
                <div class="d-flex align-items-center gap-2">
                    <?php if (isset($headerActions)): ?>
                        <?php echo $headerActions; ?>
                    <?php endif; ?>
                    
                    <!-- Admin Info & Logout -->
                    <div class="dropdown">
                        <button class="btn btn-outline-primary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="bi bi-person-circle me-1"></i>
                            <?php echo htmlspecialchars($currentAdmin['username'] ?? 'Admin'); ?>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><span class="dropdown-item-text">
                                <small class="text-muted">
                                    <?php echo $isMasterAdmin ? 'Master Admin' : 'Sub Admin'; ?>
                                </small>
                            </span></li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item text-danger" href="<?php echo $isMasterAdmin ? 'logout.php' : 'logout-multi.php'; ?>">
                                    <i class="bi bi-box-arrow-right me-2"></i>Logout
                                </a>
                            </li>
                        </ul>
                    </div>
                    
                    <span class="badge bg-primary">
                        <?php echo date('H:i'); ?>
                    </span>
                </div>
            </div>
        </div>

        <!-- Content Body -->
        <div class="content-body">
            <?php
            // Display any flash messages
            if (isset($_SESSION['success_message'])) {
                echo '<div class="alert alert-success alert-dismissible fade show" role="alert">';
                echo '<i class="bi bi-check-circle me-2"></i>';
                echo htmlspecialchars($_SESSION['success_message']);
                echo '<button type="button" class="btn-close" data-bs-dismiss="alert"></button>';
                echo '</div>';
                unset($_SESSION['success_message']);
            }

            if (isset($_SESSION['error_message'])) {
                echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">';
                echo '<i class="bi bi-exclamation-triangle me-2"></i>';
                echo htmlspecialchars($_SESSION['error_message']);
                echo '<button type="button" class="btn-close" data-bs-dismiss="alert"></button>';
                echo '</div>';
                unset($_SESSION['error_message']);
            }
            ?>

            <!-- Page Content Will Be Inserted Here -->