<?php
requireAdminAuth();
if (!isset($admin)) {
    $admin = getCurrentAdmin();
}

// FIX: If full_name isn't defined, construct it from first_name and last_name
if (!isset($admin['full_name'])) {
    $firstName = $admin['first_name'] ?? '';
    $lastName = $admin['last_name'] ?? '';
    $admin['full_name'] = trim($firstName . ' ' . $lastName);
    
    // Fallback if both name fields happen to be empty
    if (empty($admin['full_name'])) {
        $admin['full_name'] = $admin['username'] ?? 'Admin';
    }
}

$pendingTickets = getPendingTickets();
?>
<div class="header">
    <div class="header-left">
        <button class="header-toggle" onclick="toggleSidebar()">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" /></svg>
        </button>
        <h1 class="header-title">
            <?php 
            $pageTitles = [
                'dashboard' => 'Admin Dashboard',
                'users' => 'User Management',
                'transactions' => 'All Transactions',
                'crypto-wallets' => 'Crypto Wallets',
                'tickets' => 'Support Tickets',
                'kyc' => 'KYC Verification',
                'settings' => 'Global Settings',
                'addminmang' => 'Staff Management',
                'subaddminmang' => 'Sub-Admin Panel',
                'activity-logs' => 'Activity Logs',
            ];
            $currentPage = basename($_SERVER['PHP_SELF'], '.php');
            echo $pageTitles[$currentPage] ?? 'Admin Panel';
            ?>
        </h1>
    </div>
    
    <div class="header-right">
        <a href="tickets.php" class="header-icon-btn" style="text-decoration: none;">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" /></svg>
            <?php if ($pendingTickets > 0): ?>
                <span class="badge"><?php echo $pendingTickets; ?></span>
            <?php endif; ?>
        </a>
        
        <div class="header-user" style="cursor: default;">
            <div class="header-user-avatar" style="background: linear-gradient(135deg, var(--primary), var(--primary-dark));">
                <?php echo !empty($admin['full_name']) ? strtoupper(substr($admin['full_name'], 0, 1)) : 'A'; ?>
            </div>
            <div class="header-user-info">
                <div class="header-user-name"><?php echo e($admin['full_name']); ?></div>
                <div class="header-user-role"><?php echo ucfirst(str_replace('_', ' ', e($admin['role']))); ?></div>
            </div>
        </div>
    </div>
</div>
