<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/Language.php';

// Redirect to login if not logged in or NOT an admin
if (!isLoggedIn() || !isAdmin()) { redirect('login.php'); }

$lang = new Language($db);
$current_lang = $lang->getCurrentLanguage();
$user_id = $_SESSION['user_id'];

// UI Permissions map for Sidebar
$can_users = hasPermission('manage_users');
$can_sellers = hasPermission('manage_sellers');
$can_staff = hasPermission('manage_staff_roles');
$can_services = hasPermission('manage_services');
$can_orders = hasPermission('manage_orders');
$can_transactions = hasPermission('manage_transactions');
$can_tickets = hasPermission('manage_tickets');
$can_settings = hasPermission('manage_settings');

// ---------------------------------------------------------
// GET FILTERS
// ---------------------------------------------------------
$sel_cust = $_GET['customer_id'] ?? 'all';
$sel_seller = $_GET['seller_id'] ?? 'all';
$sel_staff = $_GET['staff_id'] ?? 'all';
$sel_admin = $_GET['admin_id'] ?? 'all';

// Dropdown Lists for Selection
$drp_customers = $db->select("SELECT id, name, mobile FROM users WHERE user_type = 'customer' ORDER BY name ASC");
$drp_sellers = $db->select("SELECT u.id, s.business_name, u.name FROM users u JOIN sellers s ON u.id = s.user_id WHERE u.user_type = 'seller' ORDER BY s.business_name ASC");
$drp_staff = $db->select("SELECT id, name, email FROM users WHERE user_type = 'admin' AND id != 1 ORDER BY name ASC");
$drp_admins = $db->select("SELECT id, name FROM users WHERE user_type = 'admin' AND id = 1"); // Super Admins

// ---------------------------------------------------------
// 1. GLOBAL METRICS & ALL ORDERS
// ---------------------------------------------------------
$global_revenue = $db->select("SELECT SUM(amount) as total FROM orders WHERE status = 'Completed'")[0]['total'] ?? 0.00;
$global_orders = count($db->select("SELECT id FROM orders"));
$total_tickets = count($db->select("SELECT id FROM support_tickets"));

$all_orders = $db->select("
    SELECT o.*, 
           cu.name as customer_name, cu.mobile as customer_mobile,
           su.name as seller_name, s.business_name 
    FROM orders o 
    LEFT JOIN users cu ON o.user_id = cu.id 
    LEFT JOIN users su ON o.seller_id = su.id 
    LEFT JOIN sellers s ON o.seller_id = s.user_id 
    ORDER BY o.id DESC LIMIT 1000
");

// ---------------------------------------------------------
// 2. CUSTOMER METRICS
// ---------------------------------------------------------
$customer_count = count($db->select("SELECT id FROM users WHERE user_type = 'customer'"));
$active_customers = count($db->select("SELECT id FROM users WHERE user_type = 'customer' AND status = 1"));
$customer_wallet_sum = $db->select("SELECT SUM(wallet_balance) as total FROM users WHERE user_type = 'customer'")[0]['total'] ?? 0.00;

if ($sel_cust === 'all') {
    $customer_report_data = $db->select("
        SELECT u.id, u.name, u.mobile, u.email, u.wallet_balance, u.created_at, u.status,
               COUNT(o.id) as total_orders, SUM(CASE WHEN o.status = 'Completed' THEN o.amount ELSE 0 END) as total_spent 
        FROM users u LEFT JOIN orders o ON u.id = o.user_id 
        WHERE u.user_type = 'customer' GROUP BY u.id ORDER BY total_spent DESC LIMIT 50
    ");
} else {
    $cust_specific = $db->select("SELECT * FROM users WHERE id = ? AND user_type = 'customer'", [$sel_cust])[0] ?? null;
    $cust_orders = $db->select("
        SELECT o.*, su.name as seller_name, s.business_name 
        FROM orders o 
        LEFT JOIN users su ON o.seller_id = su.id 
        LEFT JOIN sellers s ON o.seller_id = s.user_id 
        WHERE o.user_id = ? ORDER BY o.id DESC
    ", [$sel_cust]);
    $cust_txns = $db->select("SELECT * FROM transactions WHERE user_id = ? ORDER BY id DESC LIMIT 50", [$sel_cust]);
    $cust_total_spent = $db->select("SELECT SUM(amount) as total FROM orders WHERE user_id = ? AND status='Completed'", [$sel_cust])[0]['total'] ?? 0;
}

// ---------------------------------------------------------
// 3. SELLER METRICS
// ---------------------------------------------------------
$seller_count = count($db->select("SELECT id FROM users WHERE user_type = 'seller'"));
$active_sellers = count($db->select("SELECT id FROM users WHERE user_type = 'seller' AND status = 1"));
$seller_wallet_sum = $db->select("SELECT SUM(wallet_balance) as total FROM users WHERE user_type = 'seller'")[0]['total'] ?? 0.00;

if ($sel_seller === 'all') {
    $seller_report_data = $db->select("
        SELECT s.business_name, u.id, u.name, u.mobile, u.wallet_balance, u.created_at, u.status,
               COUNT(o.id) as total_sales, SUM(CASE WHEN o.status = 'Completed' THEN o.amount ELSE 0 END) as total_revenue 
        FROM users u JOIN sellers s ON u.id = s.user_id LEFT JOIN orders o ON u.id = o.seller_id 
        WHERE u.user_type = 'seller' GROUP BY u.id ORDER BY total_revenue DESC LIMIT 50
    ");
} else {
    $seller_specific = $db->select("SELECT u.*, s.business_name, s.full_address FROM users u JOIN sellers s ON u.id = s.user_id WHERE u.id = ? AND u.user_type = 'seller'", [$sel_seller])[0] ?? null;
    $seller_orders = $db->select("SELECT o.*, cu.name as customer_name FROM orders o LEFT JOIN users cu ON o.user_id = cu.id WHERE o.seller_id = ? ORDER BY o.id DESC", [$sel_seller]);
    $seller_txns = $db->select("SELECT * FROM transactions WHERE user_id = ? ORDER BY id DESC LIMIT 50", [$sel_seller]);
    $seller_total_rev = $db->select("SELECT SUM(amount) as total FROM orders WHERE seller_id = ? AND status='Completed'", [$sel_seller])[0]['total'] ?? 0;
}

// ---------------------------------------------------------
// 4. STAFF METRICS
// ---------------------------------------------------------
$staff_count = count($drp_staff);
if ($sel_staff === 'all') {
    $staff_list = $db->select("SELECT u.id, u.name, u.email, u.mobile, u.created_at, u.status, r.name as role_name FROM users u LEFT JOIN roles r ON u.role_id = r.id WHERE u.user_type = 'admin' AND u.id != 1");
} else {
    $staff_specific = $db->select("SELECT u.*, r.name as role_name FROM users u LEFT JOIN roles r ON u.role_id = r.id WHERE u.id = ?", [$sel_staff])[0] ?? null;
    $staff_logs = []; 
}

// ---------------------------------------------------------
// 5. SUPER ADMIN METRICS
// ---------------------------------------------------------
if ($sel_admin === 'all') {
    $admin_revenue = $db->select("SELECT SUM(amount) as total FROM transactions WHERE description LIKE '%Admin Fee%' OR (description LIKE '%Sold:%' AND user_id = 1)")[0]['total'] ?? 0.00;
    $system_profit = $global_revenue * 0.10; 
} else {
    $admin_specific = $db->select("SELECT * FROM users WHERE id = ?", [$sel_admin])[0] ?? null;
    $admin_txns = $db->select("SELECT * FROM transactions WHERE user_id = ? ORDER BY id DESC LIMIT 100", [$sel_admin]);
    $admin_total_rev = $db->select("SELECT SUM(amount) as total FROM transactions WHERE user_id = ? AND type = 'credit'", [$sel_admin])[0]['total'] ?? 0;
}

$company_logo = getSetting('company_logo', '../assets/images/logo.png');
?>
<!DOCTYPE html>
<html lang="<?php echo $current_lang; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Report Management - Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style> body { font-family: 'Inter', sans-serif; } .hide-scrollbar::-webkit-scrollbar { display: none; } </style>
</head>
<body class="bg-gray-50" x-data="{ 
    sidebarOpen: window.innerWidth >= 768,
    currentTab: new URLSearchParams(window.location.search).get('tab') || window.location.hash.substring(1) || 'all'
}" @hashchange.window="currentTab = window.location.hash.substring(1) || 'all'">

    <div class="flex h-screen overflow-hidden">
        
        <?php include __DIR__ . '/includes/sidebar.php'; ?>
        <div class="flex-1 flex flex-col min-w-0 bg-gray-100 overflow-hidden">
            <header class="h-20 bg-white text-slate-800 flex items-center justify-between px-4 shadow-sm border-b border-gray-200 z-40 relative">
                <div class="flex items-center"><button @click="sidebarOpen = !sidebarOpen" class="p-2 mr-3 text-slate-500 hover:text-emerald-600 rounded-lg"><i class="fas fa-bars text-xl"></i></button><h2 class="text-xl font-bold hidden md:block text-slate-800">Advanced Reports</h2></div>
                <div class="text-sm font-bold text-gray-500"><i class="far fa-clock mr-1"></i> <?php echo date('d M Y, h:i A'); ?></div>
            </header>

            <main class="flex-1 overflow-y-auto p-4 md:p-6 lg:p-8">

                <div x-show="currentTab === 'all'" style="display:none;" x-transition.opacity>
                    <div class="mb-6 flex flex-col md:flex-row md:items-end justify-between gap-4">
                        <div>
                            <h2 class="text-2xl font-extrabold text-gray-800">Global Platform Overview</h2>
                            <p class="text-gray-500 text-sm mt-1">Complete aggregate data across all user types, sellers, and detailed order history.</p>
                        </div>
                        <button onclick="window.print()" class="bg-slate-800 hover:bg-slate-900 text-white px-4 py-2 rounded-lg font-bold shadow-sm transition-colors flex items-center w-max">
                            <i class="fas fa-print mr-2"></i> Print Report
                        </button>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
                        <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 flex items-center justify-between">
                            <div>
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Total Revenue</p>
                                <h3 class="text-2xl font-black text-emerald-600">₹<?php echo number_format($global_revenue, 2); ?></h3>
                            </div>
                            <div class="w-12 h-12 bg-emerald-50 rounded-full flex items-center justify-center text-emerald-500 text-xl"><i class="fas fa-wallet"></i></div>
                        </div>
                        <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 flex items-center justify-between">
                            <div>
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Total Orders</p>
                                <h3 class="text-2xl font-black text-blue-600"><?php echo number_format($global_orders); ?></h3>
                            </div>
                            <div class="w-12 h-12 bg-blue-50 rounded-full flex items-center justify-center text-blue-500 text-xl"><i class="fas fa-shopping-cart"></i></div>
                        </div>
                        <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 flex items-center justify-between">
                            <div>
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Total Users</p>
                                <h3 class="text-2xl font-black text-purple-600"><?php echo number_format($customer_count + $seller_count); ?></h3>
                            </div>
                            <div class="w-12 h-12 bg-purple-50 rounded-full flex items-center justify-center text-purple-500 text-xl"><i class="fas fa-users"></i></div>
                        </div>
                        <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 flex items-center justify-between">
                            <div>
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Support Tickets</p>
                                <h3 class="text-2xl font-black text-rose-600"><?php echo number_format($total_tickets); ?></h3>
                            </div>
                            <div class="w-12 h-12 bg-rose-50 rounded-full flex items-center justify-center text-rose-500 text-xl"><i class="fas fa-headset"></i></div>
                        </div>
                    </div>

                    <div class="bg-white rounded-2xl shadow-sm border border-gray-200 p-6 mb-8">
                        <h3 class="font-bold text-gray-800 text-lg mb-6 border-b pb-2">User Demographics</h3>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                            <div>
                                <div class="flex items-center justify-between mb-2">
                                    <div class="flex items-center"><div class="w-3 h-3 rounded-full bg-blue-500 mr-3"></div><span class="font-medium text-gray-700">Customers</span></div>
                                    <span class="font-bold text-gray-900"><?php echo $customer_count; ?></span>
                                </div>
                                <div class="w-full bg-gray-100 rounded-full h-2"><div class="bg-blue-500 h-2 rounded-full" style="width: <?php echo ($customer_count + $seller_count > 0) ? ($customer_count / ($customer_count + $seller_count)) * 100 : 0; ?>%"></div></div>
                            </div>
                            <div>
                                <div class="flex items-center justify-between mb-2">
                                    <div class="flex items-center"><div class="w-3 h-3 rounded-full bg-teal-500 mr-3"></div><span class="font-medium text-gray-700">Sellers (B2B)</span></div>
                                    <span class="font-bold text-gray-900"><?php echo $seller_count; ?></span>
                                </div>
                                <div class="w-full bg-gray-100 rounded-full h-2"><div class="bg-teal-500 h-2 rounded-full" style="width: <?php echo ($customer_count + $seller_count > 0) ? ($seller_count / ($customer_count + $seller_count)) * 100 : 0; ?>%"></div></div>
                            </div>
                            <div>
                                <div class="flex items-center justify-between mb-2">
                                    <div class="flex items-center"><div class="w-3 h-3 rounded-full bg-indigo-500 mr-3"></div><span class="font-medium text-gray-700">Admin/Staff</span></div>
                                    <span class="font-bold text-gray-900"><?php echo $staff_count + 1; ?></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                        <div class="px-6 py-4 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
                            <h3 class="font-bold text-gray-800 text-lg">Comprehensive Global Orders</h3>
                        </div>
                        <div class="overflow-x-auto max-h-[600px] overflow-y-auto">
                            <table class="w-full text-left">
                                <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase tracking-wider sticky top-0 shadow-sm z-10">
                                    <tr>
                                        <th class="p-4 pl-6">Order ID & Date</th>
                                        <th class="p-4">Customer Details</th>
                                        <th class="p-4">Sold By (Seller)</th>
                                        <th class="p-4">Service</th>
                                        <th class="p-4">Amount</th>
                                        <th class="p-4 pr-6 text-right">Status</th>
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-gray-50">
                                    <?php if(!empty($all_orders)): foreach($all_orders as $ord): ?>
                                    <tr class="hover:bg-gray-50 transition-colors">
                                        <td class="p-4 pl-6">
                                            <p class="font-mono text-xs font-bold text-gray-700">#ORD-<?php echo $ord['id']; ?></p>
                                            <p class="text-[10px] text-gray-400 mt-1"><?php echo date('d M Y, h:i A', strtotime($ord['created_at'])); ?></p>
                                        </td>
                                        <td class="p-4">
                                            <p class="font-bold text-gray-800 text-sm"><?php echo htmlspecialchars($ord['customer_name'] ?? 'Unknown'); ?></p>
                                            <p class="text-xs text-gray-500"><?php echo htmlspecialchars($ord['customer_mobile'] ?? ''); ?></p>
                                        </td>
                                        <td class="p-4">
                                            <?php if($ord['seller_id'] == 0 || empty($ord['seller_name'])): ?>
                                                <span class="px-2 py-1 bg-indigo-50 text-indigo-600 text-[10px] font-bold uppercase rounded border border-indigo-100">Direct / System</span>
                                            <?php else: ?>
                                                <p class="font-bold text-teal-700 text-sm"><?php echo htmlspecialchars($ord['business_name'] ?? 'B2B Partner'); ?></p>
                                                <p class="text-xs text-gray-500"><?php echo htmlspecialchars($ord['seller_name'] ?? ''); ?></p>
                                            <?php endif; ?>
                                        </td>
                                        <td class="p-4 text-sm text-gray-700 font-medium max-w-[200px] truncate" title="<?php echo htmlspecialchars($ord['service_name'] ?? ''); ?>">
                                            <?php echo htmlspecialchars($ord['service_name'] ?? ''); ?>
                                        </td>
                                        <td class="p-4 font-black text-emerald-600">₹<?php echo number_format($ord['amount'], 2); ?></td>
                                        <td class="p-4 pr-6 text-right">
                                            <?php 
                                                $sc = 'bg-gray-100 text-gray-700';
                                                if($ord['status'] == 'Completed') $sc = 'bg-emerald-100 text-emerald-700';
                                                if($ord['status'] == 'Pending' || $ord['status'] == 'Processing') $sc = 'bg-yellow-100 text-yellow-700';
                                            ?>
                                            <span class="px-2 py-1 text-[10px] font-bold uppercase tracking-wider rounded <?php echo $sc; ?>"><?php echo htmlspecialchars($ord['status'] ?? ''); ?></span>
                                        </td>
                                    </tr>
                                    <?php endforeach; else: ?>
                                    <tr><td colspan="6" class="p-8 text-center text-gray-500">No orders found in the system.</td></tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div x-show="currentTab === 'customer'" style="display:none;" x-transition.opacity>
                    <div class="mb-6 flex flex-col md:flex-row md:items-end justify-between gap-4">
                        <div>
                            <h2 class="text-2xl font-extrabold text-gray-800">Customer Insights</h2>
                            <p class="text-gray-500 text-sm mt-1">Analytics based on standard user activities and balances.</p>
                        </div>
                        <div class="flex items-center space-x-3">
                            <form method="GET" action="reports.php" class="flex items-center bg-white border border-gray-200 rounded-lg p-1 shadow-sm">
                                <input type="hidden" name="tab" value="customer">
                                <select name="customer_id" class="text-sm bg-transparent border-none focus:ring-0 text-gray-700 font-medium px-2 py-1 outline-none w-48">
                                    <option value="all">All Customers</option>
                                    <?php foreach($drp_customers as $dc): ?>
                                        <option value="<?php echo $dc['id']; ?>" <?php echo $sel_cust == $dc['id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($dc['name'] ?? ''); ?> (<?php echo htmlspecialchars($dc['mobile'] ?? ''); ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white px-3 py-1.5 rounded text-xs font-bold transition-colors">Filter</button>
                            </form>
                            <button onclick="window.print()" class="bg-slate-800 hover:bg-slate-900 text-white px-4 py-2 rounded-lg font-bold shadow-sm transition-colors"><i class="fas fa-print"></i></button>
                        </div>
                    </div>

                    <?php if($sel_cust === 'all'): ?>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
                            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Total Customers</p>
                                <h3 class="text-2xl font-black text-blue-600"><?php echo number_format($customer_count); ?></h3>
                            </div>
                            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Active Customers</p>
                                <h3 class="text-2xl font-black text-emerald-600"><?php echo number_format($active_customers); ?></h3>
                            </div>
                            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Total Wallet Funds</p>
                                <h3 class="text-2xl font-black text-purple-600">₹<?php echo number_format($customer_wallet_sum, 2); ?></h3>
                            </div>
                        </div>

                        <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                            <div class="px-6 py-4 border-b border-gray-100 bg-gray-50">
                                <h3 class="font-bold text-gray-800 text-lg">Top Customers (By Spending)</h3>
                            </div>
                            <div class="overflow-x-auto max-h-[500px] overflow-y-auto">
                                <table class="w-full text-left">
                                    <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase tracking-wider sticky top-0 shadow-sm z-10">
                                        <tr>
                                            <th class="p-4 pl-6">Customer Details</th>
                                            <th class="p-4">Join Date</th>
                                            <th class="p-4 text-center">Wallet Balance</th>
                                            <th class="p-4 text-center">Total Orders</th>
                                            <th class="p-4 pr-6 text-right">Total Spent</th>
                                        </tr>
                                    </thead>
                                    <tbody class="divide-y divide-gray-50">
                                        <?php if(!empty($customer_report_data)): foreach($customer_report_data as $tc): ?>
                                        <tr class="hover:bg-gray-50">
                                            <td class="p-4 pl-6">
                                                <a href="?tab=customer&customer_id=<?php echo $tc['id']; ?>" class="font-bold text-blue-600 hover:underline text-sm"><?php echo htmlspecialchars($tc['name'] ?? ''); ?></a>
                                                <?php if($tc['status'] != 1): ?><span class="ml-2 text-[9px] bg-rose-100 text-rose-600 px-1.5 py-0.5 rounded uppercase">Inactive</span><?php endif; ?>
                                                <p class="text-xs text-gray-500"><?php echo htmlspecialchars($tc['mobile'] ?? ''); ?></p>
                                            </td>
                                            <td class="p-4 text-sm text-gray-600"><?php echo date('d M Y', strtotime($tc['created_at'])); ?></td>
                                            <td class="p-4 text-center font-bold text-emerald-600">₹<?php echo number_format($tc['wallet_balance'], 2); ?></td>
                                            <td class="p-4 text-center font-black text-gray-700"><?php echo number_format($tc['total_orders']); ?></td>
                                            <td class="p-4 pr-6 text-right font-black text-purple-600">₹<?php echo number_format($tc['total_spent'], 2); ?></td>
                                        </tr>
                                        <?php endforeach; else: ?>
                                        <tr><td colspan="5" class="p-6 text-center text-gray-500">No customer data available.</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                    <?php else: ?>
                        <?php if($cust_specific): ?>
                            <div class="bg-blue-50 border border-blue-200 rounded-2xl p-6 mb-6 flex flex-col md:flex-row justify-between items-center">
                                <div>
                                    <h3 class="text-2xl font-black text-blue-900"><?php echo htmlspecialchars($cust_specific['name'] ?? ''); ?></h3>
                                    <p class="text-blue-700 font-medium mt-1"><i class="fas fa-phone-alt mr-2"></i><?php echo htmlspecialchars($cust_specific['mobile'] ?? ''); ?> <span class="mx-2">|</span> <i class="fas fa-envelope mr-2"></i><?php echo htmlspecialchars($cust_specific['email'] ?? 'No Email'); ?></p>
                                </div>
                                <div class="text-right mt-4 md:mt-0 bg-white px-6 py-3 rounded-xl border border-blue-100 shadow-sm">
                                    <p class="text-xs font-bold text-gray-500 uppercase">Current Wallet Balance</p>
                                    <h3 class="text-2xl font-black text-emerald-600">₹<?php echo number_format($cust_specific['wallet_balance'], 2); ?></h3>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                                <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                                    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
                                        <h3 class="font-bold text-gray-800">Order History</h3>
                                        <span class="text-sm font-bold text-purple-600">Total Spent: ₹<?php echo number_format($cust_total_spent, 2); ?></span>
                                    </div>
                                    <div class="overflow-x-auto max-h-[400px] overflow-y-auto">
                                        <table class="w-full text-left">
                                            <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase sticky top-0 shadow-sm">
                                                <tr>
                                                    <th class="p-3 pl-6">Order</th>
                                                    <th class="p-3">Purchased From</th>
                                                    <th class="p-3">Service</th>
                                                    <th class="p-3">Amount</th>
                                                    <th class="p-3 pr-6 text-right">Status</th>
                                                </tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-50 text-sm">
                                                <?php if(!empty($cust_orders)): foreach($cust_orders as $o): ?>
                                                <tr class="hover:bg-gray-50">
                                                    <td class="p-3 pl-6 font-mono text-xs text-gray-500">#<?php echo $o['id']; ?></td>
                                                    <td class="p-3">
                                                        <?php if($o['seller_id'] == 0 || empty($o['seller_name'])): ?>
                                                            <span class="px-2 py-1 bg-indigo-50 text-indigo-600 text-[10px] font-bold uppercase rounded border border-indigo-100">Direct / System</span>
                                                        <?php else: ?>
                                                            <p class="font-bold text-teal-700 text-sm"><?php echo htmlspecialchars($o['business_name'] ?? 'B2B Partner'); ?></p>
                                                            <p class="text-[10px] text-gray-500"><?php echo htmlspecialchars($o['seller_name'] ?? ''); ?></p>
                                                        <?php endif; ?>
                                                    </td>
                                                    <td class="p-3 font-medium text-gray-700 truncate max-w-[120px]" title="<?php echo htmlspecialchars($o['service_name'] ?? ''); ?>"><?php echo htmlspecialchars($o['service_name'] ?? ''); ?></td>
                                                    <td class="p-3 font-bold text-gray-900">₹<?php echo number_format($o['amount'], 2); ?></td>
                                                    <td class="p-3 pr-6 text-right"><span class="text-[10px] font-bold uppercase text-gray-600"><?php echo htmlspecialchars($o['status'] ?? ''); ?></span></td>
                                                </tr>
                                                <?php endforeach; else: ?>
                                                <tr><td colspan="5" class="p-4 text-center text-gray-500">No orders found.</td></tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>

                                <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                                    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50"><h3 class="font-bold text-gray-800">Recent Transactions</h3></div>
                                    <div class="overflow-x-auto max-h-[400px] overflow-y-auto">
                                        <table class="w-full text-left">
                                            <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase sticky top-0 shadow-sm">
                                                <tr><th class="p-3 pl-6">Date</th><th class="p-3">Desc</th><th class="p-3 pr-6 text-right">Amount</th></tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-50 text-sm">
                                                <?php if(!empty($cust_txns)): foreach($cust_txns as $tx): ?>
                                                <tr class="hover:bg-gray-50">
                                                    <td class="p-3 pl-6 text-xs text-gray-500"><?php echo date('d M, y', strtotime($tx['created_at'])); ?></td>
                                                    <td class="p-3 text-gray-600 truncate max-w-[150px]" title="<?php echo htmlspecialchars($tx['description'] ?? ''); ?>"><?php echo htmlspecialchars($tx['description'] ?? ''); ?></td>
                                                    <td class="p-3 pr-6 text-right font-bold <?php echo $tx['type'] == 'credit' ? 'text-emerald-600' : 'text-rose-600'; ?>">
                                                        <?php echo $tx['type'] == 'credit' ? '+' : '-'; ?>₹<?php echo number_format($tx['amount'], 2); ?>
                                                    </td>
                                                </tr>
                                                <?php endforeach; else: ?>
                                                <tr><td colspan="3" class="p-4 text-center text-gray-500">No transactions found.</td></tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="bg-white p-12 text-center rounded-2xl border border-gray-200">
                                <p class="text-rose-500 font-bold text-lg"><i class="fas fa-exclamation-triangle mr-2"></i>Customer not found.</p>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>

                <div x-show="currentTab === 'seller'" style="display:none;" x-transition.opacity>
                    <div class="mb-6 flex flex-col md:flex-row md:items-end justify-between gap-4">
                        <div>
                            <h2 class="text-2xl font-extrabold text-gray-800">Seller (B2B) Performance</h2>
                            <p class="text-gray-500 text-sm mt-1">Analytics on retail partners, distributors, and franchise sales.</p>
                        </div>
                        <div class="flex items-center space-x-3">
                            <form method="GET" action="reports.php" class="flex items-center bg-white border border-gray-200 rounded-lg p-1 shadow-sm">
                                <input type="hidden" name="tab" value="seller">
                                <select name="seller_id" class="text-sm bg-transparent border-none focus:ring-0 text-gray-700 font-medium px-2 py-1 outline-none w-48">
                                    <option value="all">All Sellers</option>
                                    <?php foreach($drp_sellers as $ds): ?>
                                        <option value="<?php echo $ds['id']; ?>" <?php echo $sel_seller == $ds['id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($ds['business_name'] ?? 'B2B Partner'); ?> (<?php echo htmlspecialchars($ds['name'] ?? ''); ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <button type="submit" class="bg-teal-600 hover:bg-teal-700 text-white px-3 py-1.5 rounded text-xs font-bold transition-colors">Filter</button>
                            </form>
                            <button onclick="window.print()" class="bg-slate-800 hover:bg-slate-900 text-white px-4 py-2 rounded-lg font-bold shadow-sm transition-colors"><i class="fas fa-print"></i></button>
                        </div>
                    </div>

                    <?php if($sel_seller === 'all'): ?>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
                            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Registered Sellers</p>
                                <h3 class="text-2xl font-black text-teal-600"><?php echo number_format($seller_count); ?></h3>
                            </div>
                            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">Active Sellers</p>
                                <h3 class="text-2xl font-black text-emerald-600"><?php echo number_format($active_sellers); ?></h3>
                            </div>
                            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-1">B2B Wallet Funds</p>
                                <h3 class="text-2xl font-black text-purple-600">₹<?php echo number_format($seller_wallet_sum, 2); ?></h3>
                            </div>
                        </div>

                        <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                            <div class="px-6 py-4 border-b border-gray-100 bg-gray-50">
                                <h3 class="font-bold text-gray-800 text-lg">Top Sellers (By Revenue)</h3>
                            </div>
                            <div class="overflow-x-auto max-h-[500px] overflow-y-auto">
                                <table class="w-full text-left">
                                    <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase tracking-wider sticky top-0 shadow-sm z-10">
                                        <tr>
                                            <th class="p-4 pl-6">Business & Owner</th>
                                            <th class="p-4">Join Date</th>
                                            <th class="p-4 text-center">Wallet Balance</th>
                                            <th class="p-4 text-center">Total Sales</th>
                                            <th class="p-4 pr-6 text-right">Generated Revenue</th>
                                        </tr>
                                    </thead>
                                    <tbody class="divide-y divide-gray-50">
                                        <?php if(!empty($seller_report_data)): foreach($seller_report_data as $ts): ?>
                                        <tr class="hover:bg-gray-50">
                                            <td class="p-4 pl-6">
                                                <a href="?tab=seller&seller_id=<?php echo $ts['id']; ?>" class="font-bold text-teal-700 hover:underline text-sm"><?php echo htmlspecialchars($ts['business_name'] ?? 'B2B Partner'); ?></a>
                                                <?php if($ts['status'] != 1): ?><span class="ml-2 text-[9px] bg-rose-100 text-rose-600 px-1.5 py-0.5 rounded uppercase">Inactive</span><?php endif; ?>
                                                <p class="text-xs text-gray-500 mt-1"><?php echo htmlspecialchars($ts['name'] ?? ''); ?> (<?php echo htmlspecialchars($ts['mobile'] ?? ''); ?>)</p>
                                            </td>
                                            <td class="p-4 text-sm text-gray-600"><?php echo date('d M Y', strtotime($ts['created_at'])); ?></td>
                                            <td class="p-4 text-center font-bold text-teal-600">₹<?php echo number_format($ts['wallet_balance'], 2); ?></td>
                                            <td class="p-4 text-center font-black text-blue-600"><?php echo number_format($ts['total_sales']); ?></td>
                                            <td class="p-4 pr-6 text-right font-black text-emerald-600">₹<?php echo number_format($ts['total_revenue'], 2); ?></td>
                                        </tr>
                                        <?php endforeach; else: ?>
                                        <tr><td colspan="5" class="p-6 text-center text-gray-500">No seller data available.</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    <?php else: ?>
                        <?php if($seller_specific): ?>
                            <div class="bg-teal-50 border border-teal-200 rounded-2xl p-6 mb-6 flex flex-col md:flex-row justify-between items-center">
                                <div>
                                    <h3 class="text-2xl font-black text-teal-900"><?php echo htmlspecialchars($seller_specific['business_name'] ?? 'B2B Partner'); ?></h3>
                                    <p class="text-teal-700 font-medium mt-1"><i class="fas fa-user-tie mr-2"></i><?php echo htmlspecialchars($seller_specific['name'] ?? ''); ?> <span class="mx-2">|</span> <i class="fas fa-phone-alt mr-2"></i><?php echo htmlspecialchars($seller_specific['mobile'] ?? ''); ?></p>
                                    <p class="text-xs text-teal-600 mt-1"><i class="fas fa-map-marker-alt mr-2"></i><?php echo htmlspecialchars($seller_specific['full_address'] ?? 'No address provided'); ?></p>
                                </div>
                                <div class="text-right mt-4 md:mt-0 bg-white px-6 py-3 rounded-xl border border-teal-100 shadow-sm">
                                    <p class="text-xs font-bold text-gray-500 uppercase">Current B2B Wallet</p>
                                    <h3 class="text-2xl font-black text-emerald-600">₹<?php echo number_format($seller_specific['wallet_balance'], 2); ?></h3>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                                <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                                    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
                                        <h3 class="font-bold text-gray-800">Sales History</h3>
                                        <span class="text-sm font-bold text-emerald-600">Generated: ₹<?php echo number_format($seller_total_rev, 2); ?></span>
                                    </div>
                                    <div class="overflow-x-auto max-h-[400px] overflow-y-auto">
                                        <table class="w-full text-left">
                                            <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase sticky top-0 shadow-sm">
                                                <tr><th class="p-3 pl-6">Order</th><th class="p-3">Customer</th><th class="p-3">Service</th><th class="p-3 pr-6 text-right">Amount</th></tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-50 text-sm">
                                                <?php if(!empty($seller_orders)): foreach($seller_orders as $o): ?>
                                                <tr class="hover:bg-gray-50">
                                                    <td class="p-3 pl-6 font-mono text-xs text-gray-500">#<?php echo $o['id']; ?></td>
                                                    <td class="p-3 font-medium text-gray-800"><?php echo htmlspecialchars($o['customer_name'] ?? 'Direct'); ?></td>
                                                    <td class="p-3 text-gray-600 truncate max-w-[100px]" title="<?php echo htmlspecialchars($o['service_name'] ?? ''); ?>"><?php echo htmlspecialchars($o['service_name'] ?? ''); ?></td>
                                                    <td class="p-3 pr-6 text-right font-bold text-emerald-600">₹<?php echo number_format($o['amount'], 2); ?></td>
                                                </tr>
                                                <?php endforeach; else: ?>
                                                <tr><td colspan="4" class="p-4 text-center text-gray-500">No sales found.</td></tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>

                                <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                                    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50"><h3 class="font-bold text-gray-800">Ledger Details</h3></div>
                                    <div class="overflow-x-auto max-h-[400px] overflow-y-auto">
                                        <table class="w-full text-left">
                                            <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase sticky top-0 shadow-sm">
                                                <tr><th class="p-3 pl-6">Date</th><th class="p-3">Activity</th><th class="p-3 pr-6 text-right">Amount</th></tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-50 text-sm">
                                                <?php if(!empty($seller_txns)): foreach($seller_txns as $tx): ?>
                                                <tr class="hover:bg-gray-50">
                                                    <td class="p-3 pl-6 text-xs text-gray-500"><?php echo date('d M, y', strtotime($tx['created_at'])); ?></td>
                                                    <td class="p-3 text-gray-600 truncate max-w-[150px]" title="<?php echo htmlspecialchars($tx['description'] ?? ''); ?>"><?php echo htmlspecialchars($tx['description'] ?? ''); ?></td>
                                                    <td class="p-3 pr-6 text-right font-bold <?php echo $tx['type'] == 'credit' ? 'text-emerald-600' : 'text-rose-600'; ?>">
                                                        <?php echo $tx['type'] == 'credit' ? '+' : '-'; ?>₹<?php echo number_format($tx['amount'], 2); ?>
                                                    </td>
                                                </tr>
                                                <?php endforeach; else: ?>
                                                <tr><td colspan="3" class="p-4 text-center text-gray-500">No transactions found.</td></tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="bg-white p-12 text-center rounded-2xl border border-gray-200">
                                <p class="text-rose-500 font-bold text-lg"><i class="fas fa-exclamation-triangle mr-2"></i>Seller not found.</p>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>

                <div x-show="currentTab === 'staff'" style="display:none;" x-transition.opacity>
                    <div class="mb-6 flex flex-col md:flex-row md:items-end justify-between gap-4">
                        <div>
                            <h2 class="text-2xl font-extrabold text-gray-800">Staff & Admin Overview</h2>
                            <p class="text-gray-500 text-sm mt-1">Management of internal team roles and system access.</p>
                        </div>
                        <div class="flex items-center space-x-3">
                            <form method="GET" action="reports.php" class="flex items-center bg-white border border-gray-200 rounded-lg p-1 shadow-sm">
                                <input type="hidden" name="tab" value="staff">
                                <select name="staff_id" class="text-sm bg-transparent border-none focus:ring-0 text-gray-700 font-medium px-2 py-1 outline-none w-48">
                                    <option value="all">All Staff</option>
                                    <?php foreach($drp_staff as $st): ?>
                                        <option value="<?php echo $st['id']; ?>" <?php echo $sel_staff == $st['id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($st['name'] ?? ''); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white px-3 py-1.5 rounded text-xs font-bold transition-colors">Filter</button>
                            </form>
                            <button onclick="window.print()" class="bg-slate-800 hover:bg-slate-900 text-white px-4 py-2 rounded-lg font-bold shadow-sm transition-colors"><i class="fas fa-print"></i></button>
                        </div>
                    </div>

                    <?php if($sel_staff === 'all'): ?>
                        <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                            <div class="px-6 py-4 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
                                <h3 class="font-bold text-gray-800 text-lg">System Administrators (<?php echo $staff_count; ?>)</h3>
                                <a href="staff.php" class="text-sm font-bold text-emerald-600 hover:text-emerald-800">Manage Staff</a>
                            </div>
                            <div class="overflow-x-auto max-h-[500px] overflow-y-auto">
                                <table class="w-full text-left">
                                    <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase tracking-wider sticky top-0 shadow-sm z-10">
                                        <tr>
                                            <th class="p-4 pl-6">Admin Name</th>
                                            <th class="p-4">Contact Info</th>
                                            <th class="p-4">Added On</th>
                                            <th class="p-4 pr-6 text-right">Assigned Role</th>
                                        </tr>
                                    </thead>
                                    <tbody class="divide-y divide-gray-50">
                                        <?php if(!empty($staff_list)): foreach($staff_list as $stf): ?>
                                        <tr class="hover:bg-gray-50">
                                            <td class="p-4 pl-6">
                                                <a href="?tab=staff&staff_id=<?php echo $stf['id']; ?>" class="font-bold text-indigo-700 hover:underline text-sm"><?php echo htmlspecialchars($stf['name'] ?? ''); ?></a>
                                                <?php if($stf['status'] != 1): ?><span class="ml-2 text-[9px] bg-rose-100 text-rose-600 px-1.5 py-0.5 rounded uppercase">Suspended</span><?php endif; ?>
                                            </td>
                                            <td class="p-4 text-sm text-gray-600">
                                                <p><?php echo htmlspecialchars($stf['email'] ?? ''); ?></p>
                                                <p class="text-xs text-gray-400"><?php echo htmlspecialchars($stf['mobile'] ?? ''); ?></p>
                                            </td>
                                            <td class="p-4 text-sm text-gray-500"><?php echo date('d M Y', strtotime($stf['created_at'])); ?></td>
                                            <td class="p-4 pr-6 text-right">
                                                <span class="px-3 py-1 bg-indigo-100 text-indigo-700 font-bold text-[10px] uppercase rounded-full tracking-wider">
                                                    <?php echo htmlspecialchars($stf['role_name'] ?? 'Custom Role'); ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <?php endforeach; else: ?>
                                        <tr><td colspan="4" class="p-6 text-center text-gray-500">No staff members found.</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    <?php else: ?>
                        <?php if($staff_specific): ?>
                            <div class="bg-indigo-50 border border-indigo-200 rounded-2xl p-6 mb-6 flex flex-col md:flex-row justify-between items-center">
                                <div>
                                    <h3 class="text-2xl font-black text-indigo-900"><?php echo htmlspecialchars($staff_specific['name'] ?? ''); ?></h3>
                                    <p class="text-indigo-700 font-medium mt-1"><i class="fas fa-envelope mr-2"></i><?php echo htmlspecialchars($staff_specific['email'] ?? ''); ?> <span class="mx-2">|</span> <i class="fas fa-phone-alt mr-2"></i><?php echo htmlspecialchars($staff_specific['mobile'] ?? ''); ?></p>
                                </div>
                                <div class="text-right mt-4 md:mt-0 bg-white px-6 py-3 rounded-xl border border-indigo-100 shadow-sm">
                                    <p class="text-xs font-bold text-gray-500 uppercase">Assigned Role</p>
                                    <h3 class="text-xl font-black text-indigo-600"><?php echo htmlspecialchars($staff_specific['role_name'] ?? 'Custom Role'); ?></h3>
                                </div>
                            </div>
                            <div class="bg-white p-12 text-center rounded-2xl border border-gray-200">
                                <p class="text-gray-500 font-medium"><i class="fas fa-history mr-2 text-indigo-400"></i> Detailed activity logs for individual staff members will be introduced in the next update.</p>
                            </div>
                        <?php else: ?>
                            <div class="bg-white p-12 text-center rounded-2xl border border-gray-200">
                                <p class="text-rose-500 font-bold text-lg"><i class="fas fa-exclamation-triangle mr-2"></i>Staff member not found.</p>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>

                <div x-show="currentTab === 'admin'" style="display:none;" x-transition.opacity>
                    <div class="mb-6 flex flex-col md:flex-row md:items-end justify-between gap-4">
                        <div>
                            <h2 class="text-2xl font-extrabold text-gray-800">Master Revenue Report</h2>
                            <p class="text-gray-500 text-sm mt-1">Financial overview of admin commissions, profit margins, and system health.</p>
                        </div>
                        <div class="flex items-center space-x-3">
                            <form method="GET" action="reports.php" class="flex items-center bg-white border border-gray-200 rounded-lg p-1 shadow-sm">
                                <input type="hidden" name="tab" value="admin">
                                <select name="admin_id" class="text-sm bg-transparent border-none focus:ring-0 text-gray-700 font-medium px-2 py-1 outline-none w-48">
                                    <option value="all">Global System Data</option>
                                    <?php foreach($drp_admins as $adm): ?>
                                        <option value="<?php echo $adm['id']; ?>" <?php echo $sel_admin == $adm['id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($adm['name'] ?? ''); ?> (Wallet)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <button type="submit" class="bg-emerald-600 hover:bg-emerald-700 text-white px-3 py-1.5 rounded text-xs font-bold transition-colors">Filter</button>
                            </form>
                            <button onclick="window.print()" class="bg-slate-800 hover:bg-slate-900 text-white px-4 py-2 rounded-lg font-bold shadow-sm transition-colors"><i class="fas fa-print"></i></button>
                        </div>
                    </div>

                    <?php if($sel_admin === 'all'): ?>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div class="bg-gradient-to-br from-slate-800 to-slate-900 p-8 rounded-2xl shadow-lg border border-slate-700 text-white relative overflow-hidden">
                                <div class="absolute -right-10 -top-10 w-40 h-40 bg-white/5 rounded-full blur-2xl"></div>
                                <div class="relative z-10">
                                    <p class="text-sm font-bold text-emerald-400 uppercase tracking-wider mb-2">Estimated System Profit</p>
                                    <h3 class="text-4xl font-black mb-2">₹<?php echo number_format($system_profit, 2); ?></h3>
                                    <p class="text-xs text-slate-400 border-t border-white/10 pt-4 mt-4">Based on general 10% margin calculations from total platform revenue.</p>
                                </div>
                            </div>

                            <div class="bg-white p-8 rounded-2xl shadow-sm border border-gray-200">
                                <p class="text-sm font-bold text-gray-500 uppercase tracking-wider mb-2">Direct Admin Fees Collected</p>
                                <h3 class="text-4xl font-black text-emerald-600 mb-2">₹<?php echo number_format($admin_revenue, 2); ?></h3>
                                <p class="text-xs text-gray-400 border-t border-gray-100 pt-4 mt-4">Total amount collected explicitly as 'Admin Commission' from B2B service margins.</p>
                            </div>
                        </div>
                    <?php else: ?>
                        <?php if($admin_specific): ?>
                            <div class="bg-emerald-50 border border-emerald-200 rounded-2xl p-6 mb-6 flex flex-col md:flex-row justify-between items-center">
                                <div>
                                    <h3 class="text-2xl font-black text-emerald-900">Master Wallet: <?php echo htmlspecialchars($admin_specific['name'] ?? ''); ?></h3>
                                    <p class="text-emerald-700 font-medium mt-1"><i class="fas fa-envelope mr-2"></i><?php echo htmlspecialchars($admin_specific['email'] ?? ''); ?></p>
                                </div>
                                <div class="text-right mt-4 md:mt-0 bg-white px-6 py-3 rounded-xl border border-emerald-100 shadow-sm">
                                    <p class="text-xs font-bold text-gray-500 uppercase">Available Admin Balance</p>
                                    <h3 class="text-3xl font-black text-emerald-600">₹<?php echo number_format($admin_specific['wallet_balance'], 2); ?></h3>
                                </div>
                            </div>

                            <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                                <div class="px-6 py-4 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
                                    <h3 class="font-bold text-gray-800">Admin Ledger</h3>
                                    <span class="text-sm font-bold text-emerald-600">Lifetime Credits: ₹<?php echo number_format($admin_total_rev, 2); ?></span>
                                </div>
                                <div class="overflow-x-auto max-h-[500px] overflow-y-auto">
                                    <table class="w-full text-left">
                                        <thead class="bg-white border-b border-gray-100 text-xs text-gray-500 uppercase sticky top-0 shadow-sm">
                                            <tr><th class="p-4 pl-6">Date</th><th class="p-4">Description</th><th class="p-4 pr-6 text-right">Amount</th></tr>
                                        </thead>
                                        <tbody class="divide-y divide-gray-50 text-sm">
                                            <?php if(!empty($admin_txns)): foreach($admin_txns as $tx): ?>
                                            <tr class="hover:bg-gray-50">
                                                <td class="p-4 pl-6 text-xs text-gray-500"><?php echo date('d M Y, h:i A', strtotime($tx['created_at'])); ?></td>
                                                <td class="p-4 font-medium text-gray-700"><?php echo htmlspecialchars($tx['description'] ?? ''); ?></td>
                                                <td class="p-4 pr-6 text-right font-black <?php echo $tx['type'] == 'credit' ? 'text-emerald-600' : 'text-rose-600'; ?>">
                                                    <?php echo $tx['type'] == 'credit' ? '+' : '-'; ?>₹<?php echo number_format($tx['amount'], 2); ?>
                                                </td>
                                            </tr>
                                            <?php endforeach; else: ?>
                                            <tr><td colspan="3" class="p-6 text-center text-gray-500">No admin transactions found.</td></tr>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="bg-white p-12 text-center rounded-2xl border border-gray-200">
                                <p class="text-rose-500 font-bold text-lg"><i class="fas fa-exclamation-triangle mr-2"></i>Admin wallet not found.</p>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>

            </main>
        </div>
    </div>
</body>
</html>