<?php
/* ============================================================================
   Member panel → Earning Report
   ============================================================================ */
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/Language.php';
require_once __DIR__ . '/includes/earning_view.php';

if (!isLoggedIn()) { redirect('login.php'); }

$user_id = $_SESSION['user_id'];

// CSV export must run before any output
earning_handle_export($db, $user_id);

$lang = new Language($db);
$current_lang = $lang->getCurrentLanguage();

$user_data = $db->select("SELECT * FROM users WHERE id = ?", [$user_id]);
$user = !empty($user_data) ? $user_data[0] : null;
if (!$user) { redirect('logout.php'); }

$data = earning_page_data($db, $user_id);

$wallet_balance  = $data['balances']['main'];
$unread_notifs   = $db->select("SELECT * FROM notifications WHERE user_id = ? AND is_read = 0 ORDER BY created_at DESC LIMIT 5", [$user_id]);
$company_logo    = getSetting('company_logo', 'assets/images/logo.png');
$company_name    = getSetting('company_name', 'WBMCZ');

$all_categories  = $db->select("SELECT * FROM service_categories WHERE status = 1 ORDER BY display_order ASC");
$sidebar_subcats = [1 => [], 5 => []];
foreach ($db->select("SELECT id, category_id, title, icon_class FROM agency_sub_categories WHERE category_id IN (1,5) AND status = 1 ORDER BY category_id, id") as $__r) {
    $sidebar_subcats[$__r['category_id']][] = $__r;
}
?>
<!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>Earning Report - <?php echo htmlspecialchars($company_name); ?></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@400;500;600;700;800;900&display=swap" rel="stylesheet">
    <style> body { font-family:'Inter',sans-serif; } .hide-scrollbar::-webkit-scrollbar { display:none; } .hide-scrollbar { -ms-overflow-style:none; scrollbar-width:none; } </style>
</head>
<?php
require_once __DIR__ . '/includes/theme.php';
$__menu = theme_user_menu($sidebar_subcats, $all_categories);
theme_open([
    'panel'  => 'user',
    'base'   => '',
    'title'  => 'Earning Report',
    'active' => 'earning',
    'user'   => $user,
    'wallet' => $wallet_balance,
    'notifs' => $unread_notifs,
    'logo'   => $company_logo,
    'brand'  => $company_name,
    'menu'   => $__menu,
]);
?>

            <main class="flex-1 overflow-y-auto p-4 md:p-6 lg:p-8">
                <div class="max-w-6xl mx-auto pb-20">

                    <div class="mb-6">
                        <h2 class="text-2xl font-bold text-slate-800 mb-1"><i class="fas fa-chart-line text-indigo-600 mr-2"></i>Earning Report</h2>
                        <p class="text-gray-500 text-sm">Every rupee you have earned — referrals, shares and bonuses — in one place.</p>
                    </div>

                    <?php echo earning_render($data, ['self' => 'earning_report.php', 'accent' => 'indigo']); ?>

                </div>
            </main>
<?php theme_close(); ?>
</html>
