<?php
require_once '../includes/admin_auth.php';

if (!$adminAuth->isLoggedIn()) {
    header("Location: login.php");
    exit();
}

$admin_id = $_SESSION['admin_id'];
$admin_username = $_SESSION['admin_username'];
$admin_role = $_SESSION['admin_role'];

// Get admin info
$db = Database::getInstance()->connect();
$stmt = $db->prepare("SELECT * FROM admin_users WHERE id = ?");
$stmt->execute([$admin_id]);
$admin = $stmt->fetch();

// Get notification count
$stmt = $db->prepare("SELECT COUNT(*) as count FROM support_tickets WHERE status = 'open'");
$stmt->execute();
$notifications = $stmt->fetch()['count'];

// Get pending transactions
$stmt = $db->prepare("SELECT COUNT(*) as count FROM transactions WHERE status = 'pending'");
$stmt->execute();
$pending_tx = $stmt->fetch()['count'];

// Get total users
$stmt = $db->prepare("SELECT COUNT(*) as count FROM users");
$stmt->execute();
$total_users = $stmt->fetch()['count'];

// Helper function for time ago
function time_ago($datetime) {
    $time = strtotime($datetime);
    $now = time();
    $diff = $now - $time;
    
    if ($diff < 60) return 'just now';
    if ($diff < 3600) return floor($diff/60) . ' minutes ago';
    if ($diff < 86400) return floor($diff/3600) . ' hours ago';
    if ($diff < 604800) return floor($diff/86400) . ' days ago';
    return date('M d, Y', $time);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo isset($page_title) ? $page_title . ' - ' : ''; ?>Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        .nav-link.active {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        .dropdown:hover .dropdown-menu {
            display: block;
        }
    </style>
</head>
<body class="bg-gray-100">
    <div class="flex">
        <!-- Sidebar -->
        <div id="sidebar" class="w-64 bg-gray-900 text-white min-h-screen hidden md:block fixed md:relative z-50 h-full overflow-y-auto">
            <div class="p-6">
                <!-- Logo -->
                <div class="flex justify-between items-center mb-8">
                    <div class="flex items-center space-x-3">
                        <i class="fas fa-shield-alt text-2xl text-purple-500"></i>
                        <div>
                            <h1 class="text-xl font-bold"><?php echo SITE_NAME; ?></h1>
                            <p class="text-gray-400 text-sm">Admin Panel</p>
                        </div>
                    </div>
                    <button id="sidebarClose" class="md:hidden text-gray-400 hover:text-white focus:outline-none">
                        <i class="fas fa-times text-xl"></i>
                    </button>
                </div>

                <!-- Admin Info -->
                <div class="bg-gray-800 rounded-lg p-4 mb-6">
                    <div class="flex items-center space-x-3">
                        <div class="w-12 h-12 bg-gradient-to-r from-purple-600 to-blue-600 rounded-full flex items-center justify-center">
                            <i class="fas fa-user-shield text-xl"></i>
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="font-semibold truncate"><?php echo htmlspecialchars($admin_username); ?></p>
                            <p class="text-gray-400 text-sm"><?php echo ucfirst($admin_role); ?></p>
                            <p class="text-green-400 text-xs">Online</p>
                        </div>
                    </div>
                    <div class="mt-4 pt-4 border-t border-gray-700">
                        <a href="logout.php" class="block text-center text-red-400 hover:text-red-300 text-sm">
                            <i class="fas fa-sign-out-alt mr-2"></i>Logout
                        </a>
                    </div>
                </div>

                <!-- Navigation -->
                <nav class="space-y-1">
                    <a href="index.php" class="nav-link flex items-center space-x-3 p-3 rounded-lg hover:bg-gray-800 <?php echo basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active' : ''; ?>">
                        <i class="fas fa-tachometer-alt"></i>
                        <span>Dashboard</span>
                    </a>
                    
                    <!-- Users Management -->
                    <div class="dropdown relative">
                        <a href="#" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-800">
                            <div class="flex items-center space-x-3">
                                <i class="fas fa-users"></i>
                                <span>Users</span>
                            </div>
                            <i class="fas fa-chevron-down text-xs"></i>
                        </a>
                        <div class="dropdown-menu w-full bg-gray-800 rounded-lg hidden mt-1">
                            <a href="users.php" class="block px-4 py-3 hover:bg-gray-700">All Users</a>
                            <a href="user-create.php" class="block px-4 py-3 hover:bg-gray-700">Create User</a>
                            <a href="users.php?status=pending" class="block px-4 py-3 hover:bg-gray-700">Pending Users</a>
                            <a href="users.php?status=active" class="block px-4 py-3 hover:bg-gray-700">Active Users</a>
                        </div>
                    </div>
                    
                    <!-- Transactions -->
                    <div class="dropdown relative">
                        <a href="#" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-800">
                            <div class="flex items-center space-x-3">
                                <i class="fas fa-exchange-alt"></i>
                                <span>Transactions</span>
                            </div>
                            <i class="fas fa-chevron-down text-xs"></i>
                        </a>
                        <div class="dropdown-menu w-full bg-gray-800 rounded-lg hidden mt-1">
                            <a href="transactions.php" class="block px-4 py-3 hover:bg-gray-700">All Transactions</a>
                            <a href="deposits.php" class="block px-4 py-3 hover:bg-gray-700">Deposits</a>
                            <a href="withdrawals.php" class="block px-4 py-3 hover:bg-gray-700">Withdrawals</a>
                            <a href="transactions.php?status=pending" class="block px-4 py-3 hover:bg-gray-700">Pending</a>
                        </div>
                    </div>
                    
                    <!-- Investments -->
                    <a href="investments.php" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-gray-800 <?php echo basename($_SERVER['PHP_SELF']) == 'investments.php' ? 'active' : ''; ?>">
                        <i class="fas fa-chart-pie"></i>
                        <span>Investments</span>
                    </a>
                    
                    <!-- Investment Plans -->
                    <a href="plans.php" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-gray-800 <?php echo basename($_SERVER['PHP_SELF']) == 'plans.php' ? 'active' : ''; ?>">
                        <i class="fas fa-list-alt"></i>
                        <span>Investment Plans</span>
                    </a>
                    
                    <!-- Support Tickets -->
                    <a href="support.php" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-gray-800 <?php echo basename($_SERVER['PHP_SELF']) == 'support.php' ? 'active' : ''; ?>">
                        <i class="fas fa-headset"></i>
                        <span>Support Tickets
                            <?php if ($notifications > 0): ?>
                            <span class="ml-2 bg-red-500 text-white text-xs rounded-full px-2 py-1"><?php echo $notifications; ?></span>
                            <?php endif; ?>
                        </span>
                    </a>
                    
                    <!-- Financial Management -->
                    <div class="dropdown relative">
                        <a href="#" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-800">
                            <div class="flex items-center space-x-3">
                                <i class="fas fa-money-bill-wave"></i>
                                <span>Financial</span>
                            </div>
                            <i class="fas fa-chevron-down text-xs"></i>
                        </a>
                        <div class="dropdown-menu w-full bg-gray-800 rounded-lg hidden mt-1">
                            <a href="reports.php" class="block px-4 py-3 hover:bg-gray-700">Financial Reports</a>
                            <a href="reports.php?type=daily" class="block px-4 py-3 hover:bg-gray-700">Daily Reports</a>
                            <a href="reports.php?type=monthly" class="block px-4 py-3 hover:bg-gray-700">Monthly Reports</a>
                            <a href="referral-system.php" class="block px-4 py-3 hover:bg-gray-700">Referral System</a>
                        </div>
                    </div>
                    
                    <!-- System -->
                    <div class="dropdown relative">
                        <a href="#" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-800">
                            <div class="flex items-center space-x-3">
                                <i class="fas fa-cog"></i>
                                <span>System</span>
                            </div>
                            <i class="fas fa-chevron-down text-xs"></i>
                        </a>
                        <div class="dropdown-menu w-full bg-gray-800 rounded-lg hidden mt-1">
                            <a href="settings.php" class="block px-4 py-3 hover:bg-gray-700">Settings</a>
                            <a href="email-templates.php" class="block px-4 py-3 hover:bg-gray-700">Email Templates</a>
                            <a href="notifications.php" class="block px-4 py-3 hover:bg-gray-700">Notifications</a>
                            <a href="backup.php" class="block px-4 py-3 hover:bg-gray-700">Backup</a>
                        </div>
                    </div>
                    
                    <!-- Admin Users -->
                    <?php if ($admin_role == 'superadmin'): ?>
                    <a href="admin-users.php" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-gray-800">
                        <i class="fas fa-user-shield"></i>
                        <span>Admin Users</span>
                    </a>
                    <?php endif; ?>
                </nav>
                
                <!-- Quick Stats -->
                <div class="mt-8 pt-6 border-t border-gray-700">
                    <div class="space-y-3">
                        <div class="flex justify-between items-center">
                            <span class="text-gray-400 text-sm">Total Users:</span>
                            <span class="font-semibold"><?php echo number_format($total_users); ?></span>
                        </div>
                        <div class="flex justify-between items-center">
                            <span class="text-gray-400 text-sm">Pending TX:</span>
                            <span class="font-semibold text-yellow-500"><?php echo $pending_tx; ?></span>
                        </div>
                        <div class="flex justify-between items-center">
                            <span class="text-gray-400 text-sm">Open Tickets:</span>
                            <span class="font-semibold text-red-500"><?php echo $notifications; ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Main Content -->
        <div class="flex-1 p-4 md:p-6 min-w-0">
            <!-- Top Bar -->
            <div class="bg-white shadow-sm p-4 mb-6 rounded-xl">
                <div class="flex flex-col md:flex-row md:justify-between md:items-center gap-4">
                    <div class="flex items-center">
                        <!-- Mobile Menu Button -->
                        <button id="sidebarToggle" class="md:hidden mr-4 text-gray-600 hover:text-gray-900 focus:outline-none">
                            <i class="fas fa-bars text-xl"></i>
                        </button>
                        <div>
                            <h1 class="text-2xl font-bold text-gray-800"><?php echo $page_title ?? 'Admin Dashboard'; ?></h1>
                            <p class="text-gray-600"><?php echo date('l, F j, Y'); ?></p>
                        </div>
                    </div>
                    
                    <div class="flex items-center space-x-4">
                        <!-- Quick Actions -->
                        <div class="hidden md:flex items-center space-x-2">
                            <a href="deposits.php?status=pending" class="bg-green-100 text-green-700 px-3 py-1 rounded-lg text-sm font-medium hover:bg-green-200">
                                <i class="fas fa-money-bill-wave mr-1"></i> Approve Deposits
                            </a>
                            <a href="withdrawals.php?status=pending" class="bg-blue-100 text-blue-700 px-3 py-1 rounded-lg text-sm font-medium hover:bg-blue-200">
                                <i class="fas fa-wallet mr-1"></i> Process Withdrawals
                            </a>
                            <a href="support.php?status=open" class="bg-red-100 text-red-700 px-3 py-1 rounded-lg text-sm font-medium hover:bg-red-200">
                                <i class="fas fa-headset mr-1"></i> Answer Tickets
                            </a>
                        </div>
                        
                        <!-- Notification Bell -->
                        <div x-data="{ open: false }" class="relative">
                            <button @click="open = !open" class="relative p-2 text-gray-600 hover:text-gray-800 focus:outline-none">
                                <i class="fas fa-bell text-xl"></i>
                                <span class="notification-count absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center" style="<?php echo $notifications > 0 ? '' : 'display: none;'; ?>"><?php echo $notifications; ?></span>
                            </button>
                            <div x-show="open" @click.away="open = false" x-transition class="absolute right-0 mt-2 w-80 bg-white shadow-xl rounded-lg hidden z-50" style="max-height: 400px; overflow-y: auto;">
                                <div class="p-4 border-b">
                                    <h4 class="font-bold">Notifications</h4>
                                </div>
                                <div id="admin-notification-list" class="space-y-1 p-2">
                                    <?php
                                    $stmt = $db->prepare("SELECT * FROM support_tickets WHERE status = 'open' ORDER BY created_at DESC LIMIT 5");
                                    $stmt->execute();
                                    $tickets = $stmt->fetchAll();
                                    
                                    if (count($tickets) > 0): 
                                        foreach ($tickets as $ticket):
                                    ?>
                                    <a href="ticket-view.php?id=<?php echo $ticket['id']; ?>" class="block p-3 rounded-lg hover:bg-gray-100">
                                        <div class="flex items-start space-x-3">
                                            <i class="fas fa-ticket-alt text-blue-500 mt-1"></i>
                                            <div>
                                                <p class="text-sm font-semibold">New Ticket: #<?php echo $ticket['ticket_id']; ?></p>
                                                <p class="text-sm text-gray-600 truncate"><?php echo htmlspecialchars($ticket['subject']); ?></p>
                                                <p class="text-xs text-gray-400 mt-1"><?php echo time_ago($ticket['created_at']); ?></p>
                                            </div>
                                        </div>
                                    </a>
                                    <?php endforeach; else: ?>
                                    <p class="text-gray-500 text-sm text-center p-4">No new notifications</p>
                                    <?php endif; ?>
                                </div>
                                <div class="mt-3 pt-3 border-t text-center">
                                    <a href="support.php" class="text-sm text-purple-600 hover:text-purple-800">View All Tickets</a>
                                    </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Content Area -->
            <div>
            <!-- Content will be inserted here -->