<?php
/**
 * Admin Header Template
 */

// Start output buffering to prevent header issues
ob_start();

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

// Installation check removed

require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../includes/functions.php';
require_once __DIR__ . '/../../includes/Auth.php';

// Require admin login
auth()->requireAdmin();

$admin = auth()->getAdmin();

// Get counts for badges
$pendingDeposits = dbCount('deposits', "status = 'pending'");
$pendingWithdrawals = dbCount('withdrawals', "status = 'pending'");
$openTickets = dbCount('support_tickets', "status IN ('open', 'in_progress')");
$pendingKyc = dbCount('users', "kyc_status = 'submitted'");

// Current page for active menu
$currentPage = basename($_SERVER['PHP_SELF'], '.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $pageTitle ?? 'Dashboard' ?> - Admin Panel</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="<?= ASSETS_URL ?>/css/style.css">
    <?php if (isset($extraCss)): ?>
        <?= $extraCss ?>
    <?php endif; ?>
</head>
<body>
    <div class="wrapper">
        <!-- Sidebar -->
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <div class="sidebar-logo">
                    <i class="fas fa-chart-line"></i>
                </div>
                <div>
                    <div class="sidebar-title"><?= APP_NAME ?></div>
                    <div class="sidebar-subtitle">Admin Panel</div>
                </div>
            </div>
            
            <nav class="sidebar-nav">
                <div class="nav-section">
                    <div class="nav-section-title">Main</div>
                    <a href="dashboard.php" class="nav-item <?= $currentPage === 'dashboard' ? 'active' : '' ?>">
                        <i class="fas fa-home"></i>
                        <span>Dashboard</span>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">User Management</div>
                    <a href="users.php" class="nav-item <?= $currentPage === 'users' ? 'active' : '' ?>">
                        <i class="fas fa-users"></i>
                        <span>All Users</span>
                    </a>
                    <a href="kyc.php" class="nav-item <?= $currentPage === 'kyc' ? 'active' : '' ?>">
                        <i class="fas fa-id-card"></i>
                        <span>KYC Verification</span>
                        <?php if ($pendingKyc > 0): ?>
                            <span class="nav-badge"><?= $pendingKyc ?></span>
                        <?php endif; ?>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">Trading</div>
                    <a href="trades.php" class="nav-item <?= $currentPage === 'trades' ? 'active' : '' ?>">
                        <i class="fas fa-exchange-alt"></i>
                        <span>All Trades</span>
                    </a>
                    <a href="assets.php" class="nav-item <?= $currentPage === 'assets' ? 'active' : '' ?>">
                        <i class="fas fa-coins"></i>
                        <span>Assets</span>
                    </a>
                    <a href="risk.php" class="nav-item <?= $currentPage === 'risk' ? 'active' : '' ?>">
                        <i class="fas fa-shield-alt"></i>
                        <span>Risk Management</span>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">Financial</div>
                    <a href="deposits.php" class="nav-item <?= $currentPage === 'deposits' ? 'active' : '' ?>">
                        <i class="fas fa-arrow-down"></i>
                        <span>Deposits</span>
                        <?php if ($pendingDeposits > 0): ?>
                            <span class="nav-badge"><?= $pendingDeposits ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="upi-payments.php" class="nav-item <?= $currentPage === 'upi-payments' ? 'active' : '' ?>">
                        <i class="fas fa-qrcode"></i>
                        <span>UPI Payments</span>
                    </a>
                    <a href="upi-settings.php" class="nav-item <?= $currentPage === 'upi-settings' ? 'active' : '' ?>">
                        <i class="fas fa-cog"></i>
                        <span>UPI Settings</span>
                    </a>
                    <a href="withdrawals.php" class="nav-item <?= $currentPage === 'withdrawals' ? 'active' : '' ?>">
                        <i class="fas fa-arrow-up"></i>
                        <span>Withdrawals</span>
                        <?php if ($pendingWithdrawals > 0): ?>
                            <span class="nav-badge"><?= $pendingWithdrawals ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="transactions.php" class="nav-item <?= $currentPage === 'transactions' ? 'active' : '' ?>">
                        <i class="fas fa-history"></i>
                        <span>Transactions</span>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">Marketing & SEO</div>
                    <a href="seo.php" class="nav-item <?= $currentPage === 'seo' ? 'active' : '' ?>">
                        <i class="fas fa-search"></i>
                        <span>SEO Management</span>
                    </a>
                    <a href="bonuses.php" class="nav-item <?= $currentPage === 'bonuses' ? 'active' : '' ?>">
                        <i class="fas fa-gift"></i>
                        <span>Bonuses</span>
                    </a>
                    <a href="promo-codes.php" class="nav-item <?= $currentPage === 'promo-codes' ? 'active' : '' ?>">
                        <i class="fas fa-tag"></i>
                        <span>Promo Codes</span>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">Support</div>
                    <a href="live-chat.php" class="nav-item <?= $currentPage === 'live-chat' ? 'active' : '' ?>">
                        <i class="fas fa-headset"></i>
                        <span>Live Support</span>
                    </a>
                    <a href="tickets.php" class="nav-item <?= $currentPage === 'tickets' ? 'active' : '' ?>">
                        <i class="fas fa-ticket-alt"></i>
                        <span>Support Tickets</span>
                        <?php if ($openTickets > 0): ?>
                            <span class="nav-badge"><?= $openTickets ?></span>
                        <?php endif; ?>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">Reports</div>
                    <a href="reports.php" class="nav-item <?= $currentPage === 'reports' ? 'active' : '' ?>">
                        <i class="fas fa-chart-bar"></i>
                        <span>Analytics</span>
                    </a>
                    <a href="activity-logs.php" class="nav-item <?= $currentPage === 'activity-logs' ? 'active' : '' ?>">
                        <i class="fas fa-list"></i>
                        <span>Activity Logs</span>
                    </a>
                </div>
                
                <div class="nav-section">
                    <div class="nav-section-title">Settings</div>
                    <a href="settings.php" class="nav-item <?= $currentPage === 'settings' ? 'active' : '' ?>">
                        <i class="fas fa-cog"></i>
                        <span>General Settings</span>
                    </a>
                    <a href="payment-methods.php" class="nav-item <?= $currentPage === 'payment-methods' ? 'active' : '' ?>">
                        <i class="fas fa-credit-card"></i>
                        <span>Payment Methods</span>
                    </a>
                    <a href="pages.php" class="nav-item <?= $currentPage === 'pages' ? 'active' : '' ?>">
                        <i class="fas fa-file-alt"></i>
                        <span>CMS Pages</span>
                    </a>
                    <a href="admins.php" class="nav-item <?= $currentPage === 'admins' ? 'active' : '' ?>">
                        <i class="fas fa-user-shield"></i>
                        <span>Admin Users</span>
                    </a>
                </div>
            </nav>
        </aside>
        
        <!-- Main Content -->
        <main class="main-content">
            <!-- Topbar -->
            <header class="topbar">
                <div class="d-flex align-items-center gap-3">
                    <button class="btn btn-secondary d-lg-none" id="sidebarToggle">
                        <i class="fas fa-bars"></i>
                    </button>
                    <h1 class="h5 m-0"><?= $pageTitle ?? 'Dashboard' ?></h1>
                </div>
                
                <div class="d-flex align-items-center gap-3">
                    <!-- Quick Stats -->
                    <div class="d-none d-md-flex align-items-center gap-3">
                        <a href="deposits.php?status=pending" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-down text-green"></i>
                            <?= $pendingDeposits ?> Pending
                        </a>
                        <a href="withdrawals.php?status=pending" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-up text-red"></i>
                            <?= $pendingWithdrawals ?> Pending
                        </a>
                    </div>
                    
                    <!-- User Dropdown -->
                    <div class="user-dropdown" id="userDropdown">
                        <div class="user-dropdown-toggle" onclick="toggleUserDropdown()">
                            <div class="user-avatar">
                                <?= strtoupper(substr($admin['full_name'] ?? $admin['username'], 0, 1)) ?>
                            </div>
                            <div class="user-info d-none d-md-block">
                                <div class="user-name"><?= htmlspecialchars($admin['full_name'] ?? $admin['username']) ?></div>
                                <div class="user-email"><?= ucfirst($admin['role']) ?></div>
                            </div>
                            <i class="fas fa-chevron-down text-muted"></i>
                        </div>
                        <div class="user-dropdown-menu">
                            <a href="profile.php" class="dropdown-item">
                                <i class="fas fa-user"></i>
                                My Profile
                            </a>
                            <a href="settings.php" class="dropdown-item">
                                <i class="fas fa-cog"></i>
                                Settings
                            </a>
                            <div class="dropdown-divider"></div>
                            <a href="<?= SITE_URL ?>" class="dropdown-item" target="_blank">
                                <i class="fas fa-external-link-alt"></i>
                                View Site
                            </a>
                            <div class="dropdown-divider"></div>
                            <a href="logout.php" class="dropdown-item text-red">
                                <i class="fas fa-sign-out-alt"></i>
                                Logout
                            </a>
                        </div>
                    </div>
                </div>
            </header>
            
            <!-- Content -->
            <div class="content">
                <?php displayFlashMessage(); ?>
