<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech
include('include/header.php');
$page_title = WEB_NAME . " | Admin Dashboard";

// ══════════════════════════════════════════════════════════════
//  PROCESS SCHEDULED EMAILS (triggered on dashboard visit)
// ══════════════════════════════════════════════════════════════
try {
    $pending_emails = $pdo->query("SELECT id FROM scheduled_emails WHERE status = 'pending' AND scheduled_at <= NOW() LIMIT 3")->fetchAll(PDO::FETCH_ASSOC);
    if (!empty($pending_emails)) {
        // Trigger the cron script in the background (non-blocking)
        $cron_url = $site_data['web_url'] . '/cron/send_scheduled_emails.php?key=jedi_cron_2024_secure';
        @file_get_contents($cron_url, false, stream_context_create(['http' => ['timeout' => 1]]));
    }
} catch (PDOException $e) {
    // Silently fail - don't break dashboard
}

// ══════════════════════════════════════════════════════════════
//  USERS STATS
// ══════════════════════════════════════════════════════════════
try {
    $u = $pdo->query("
        SELECT
            COUNT(*)                                            AS total_users,
            SUM(status = 1)                                     AS active_users,
            SUM(status = 0)                                     AS blocked_users,
            SUM(status = 2)                                     AS banned_users,
            SUM(is_admin = 1)                                   AS admin_users,
            SUM(DATE(created_at) = CURDATE())                   AS registered_today,
            SUM(DATE(updated_at) = CURDATE() AND status = 1)    AS active_today,
            SUM(main_balance)                                   AS total_user_balance,
            SUM(referral_balance)                               AS total_referral_balance,
            SUM(has_active_subscription = 1)                    AS subscribed_users
        FROM users
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $u = array_fill_keys(['total_users','active_users','blocked_users','banned_users','admin_users','registered_today','active_today','total_user_balance','total_referral_balance','subscribed_users'], 0);
}

// ══════════════════════════════════════════════════════════════
//  OTP / active_number STATS
// ══════════════════════════════════════════════════════════════
try {
    $otp = $pdo->query("
        SELECT
            COUNT(*)                                            AS total_otp,
            SUM(status = '1')                                   AS completed_otp,
            SUM(status = '2')                                   AS pending_otp,
            SUM(status = '3')                                   AS cancelled_otp,
            SUM(active_status = '2')                            AS active_numbers,
            SUM(active_status != '2')                           AS inactive_numbers,
            SUM(DATE(buy_time) = CURDATE())                     AS today_otp,
            SUM(CAST(service_price AS DECIMAL(10,2)))           AS total_otp_revenue,
            SUM(CASE WHEN DATE(buy_time) = CURDATE()
                THEN CAST(service_price AS DECIMAL(10,2)) ELSE 0 END) AS today_otp_revenue,
            SUM(CASE WHEN active_status != '2'
                THEN CAST(service_price AS DECIMAL(10,2)) ELSE 0 END) AS active_number_amount,
            SUM(CASE WHEN provider='smspva'  THEN 1 ELSE 0 END) AS s1_total,
            SUM(CASE WHEN provider='smspva'  AND status='1' THEN 1 ELSE 0 END) AS s1_completed,
            SUM(CASE WHEN (provider='smscodes' OR provider IS NULL OR provider='') THEN 1 ELSE 0 END) AS s2_total,
            SUM(CASE WHEN (provider='smscodes' OR provider IS NULL OR provider='') AND status='1' THEN 1 ELSE 0 END) AS s2_completed,
            SUM(CASE WHEN provider='5sim'    THEN 1 ELSE 0 END) AS s3_total,
            SUM(CASE WHEN provider='5sim'    AND status='1' THEN 1 ELSE 0 END) AS s3_completed,
            SUM(CASE WHEN provider='smsbus'  THEN 1 ELSE 0 END) AS s4_total,
            SUM(CASE WHEN provider='smsbus'  AND status='1' THEN 1 ELSE 0 END) AS s4_completed
        FROM active_number
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $otp = array_fill_keys(['total_otp','completed_otp','pending_otp','cancelled_otp','active_numbers','inactive_numbers','today_otp','total_otp_revenue','today_otp_revenue','active_number_amount'], 0);
}

// ══════════════════════════════════════════════════════════════
//  SMM ORDERS STATS
// ══════════════════════════════════════════════════════════════
try {
    $smm = $pdo->query("
        SELECT
            COUNT(*)                                                    AS total_smm_orders,
            SUM(status = 'Completed')                                   AS smm_completed,
            SUM(status = 'Pending')                                     AS smm_pending,
            SUM(status IN ('In progress','Processing'))                 AS smm_active,
            SUM(status = 'Cancelled')                                   AS smm_cancelled,
            SUM(status = 'Partial')                                     AS smm_partial,
            SUM(charge)                                                 AS total_smm_revenue,
            SUM(CASE WHEN status='Completed' THEN charge ELSE 0 END)    AS smm_completed_revenue,
            SUM(DATE(created_at) = CURDATE())                           AS today_smm_orders,
            SUM(CASE WHEN DATE(created_at) = CURDATE()
                THEN charge ELSE 0 END)                                 AS today_smm_revenue,
            SUM(quantity)                                               AS total_smm_quantity
        FROM smm_orders
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $smm = array_fill_keys(['total_smm_orders','smm_completed','smm_pending','smm_active','smm_cancelled','smm_partial','total_smm_revenue','smm_completed_revenue','today_smm_orders','today_smm_revenue','total_smm_quantity'], 0);
}

// ══════════════════════════════════════════════════════════════
//  SMM SERVICES STATS
// ══════════════════════════════════════════════════════════════
try {
    $smm_svc = $pdo->query("
        SELECT
            COUNT(*)                    AS total_smm_services,
            SUM(status = 1)             AS active_smm_services,
            COUNT(DISTINCT category)    AS smm_categories
        FROM smm_services
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $smm_svc = ['total_smm_services'=>0,'active_smm_services'=>0,'smm_categories'=>0];
}

// ══════════════════════════════════════════════════════════════
//  SMS SERVICES (service table)
// ══════════════════════════════════════════════════════════════
try {
    $sms_svc = $pdo->query("
        SELECT
            COUNT(*)        AS total_sms_services,
            SUM(status='1') AS active_sms_services
        FROM service
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $sms_svc = ['total_sms_services'=>0,'active_sms_services'=>0];
}

// ══════════════════════════════════════════════════════════════
//  SM ACCOUNTS (sm_services / sm_purchases)
// ══════════════════════════════════════════════════════════════
try {
    $sm = $pdo->query("
        SELECT
            COUNT(*)                        AS total_sm_accounts,
            SUM(status='available')         AS available_accounts,
            SUM(status='sold')              AS sold_accounts,
            SUM(status='pending')           AS pending_accounts,
            SUM(amount)                     AS total_sm_value
        FROM sm_services
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $sm = array_fill_keys(['total_sm_accounts','available_accounts','sold_accounts','pending_accounts','total_sm_value'], 0);
}

try {
    $sm_rev = $pdo->query("
        SELECT
            COUNT(*)        AS total_sm_purchases,
            SUM(amount)     AS total_sm_revenue
        FROM sm_purchases
        WHERE status = 'completed'
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $sm_rev = ['total_sm_purchases'=>0,'total_sm_revenue'=>0];
}

// ══════════════════════════════════════════════════════════════
//  REFERRALS
// ══════════════════════════════════════════════════════════════
try {
    $ref = $pdo->query("
        SELECT
            COUNT(*)                    AS total_referrals,
            SUM(status='active')        AS active_referrals,
            SUM(status='completed')     AS completed_referrals,
            SUM(earnings)               AS total_referral_earnings
        FROM referrals
    ")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $ref = ['total_referrals'=>0,'active_referrals'=>0,'completed_referrals'=>0,'total_referral_earnings'=>0];
}

// ══════════════════════════════════════════════════════════════
//  COMBINED REVENUE
// ══════════════════════════════════════════════════════════════
$total_platform_revenue = (float)$otp['total_otp_revenue'] + (float)$smm['total_smm_revenue'] + (float)$sm_rev['total_sm_revenue'];
$today_total_revenue    = (float)$otp['today_otp_revenue'] + (float)$smm['today_smm_revenue'];

// ══════════════════════════════════════════════════════════════
//  RECENT OTP ORDERS (last 5)
// ══════════════════════════════════════════════════════════════
try {
    $recent_otp_stmt = $pdo->query("
        SELECT an.*, u.username, u.full_name
        FROM active_number an
        LEFT JOIN users u ON an.user_id = u.id
        ORDER BY an.buy_time DESC LIMIT 5
    ");
    $recent_otp = $recent_otp_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $recent_otp = []; }

// ══════════════════════════════════════════════════════════════
//  RECENT SMM ORDERS (last 5)
// ══════════════════════════════════════════════════════════════
try {
    $recent_smm_stmt = $pdo->query("
        SELECT o.*, u.username, u.full_name, ss.name AS service_name
        FROM smm_orders o
        LEFT JOIN users u         ON o.user_id = u.id
        LEFT JOIN smm_services ss ON o.smm_service_id = ss.id
        ORDER BY o.created_at DESC LIMIT 5
    ");
    $recent_smm = $recent_smm_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $recent_smm = []; }

// ══════════════════════════════════════════════════════════════
//  RECENT USERS (last 5)
// ══════════════════════════════════════════════════════════════
try {
    $recent_users_stmt = $pdo->query("
        SELECT id, username, full_name, email, main_balance, status, created_at
        FROM users ORDER BY created_at DESC LIMIT 5
    ");
    $recent_users = $recent_users_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $recent_users = []; }
?>
<title>Dashboard - <?php echo $site_data['web_name']; ?></title>
<link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">

<style>
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.18s ease, box-shadow 0.18s ease;
        border-radius: 6px;
    }
    .stat-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0,0,0,0.10);
    }
    .revenue-sub {
        display:inline-block; font-size:0.7rem; font-weight:600;
        padding:2px 7px; border-radius:10px;
        background:#d1fae5; color:#065f46; margin-top:4px;
    }
    .section-divider {
        border: 0; border-top: 2px solid #e3e6f0;
        margin: 8px 0 20px;
    }
    .section-label {
        font-size:0.7rem; font-weight:800; letter-spacing:.12em;
        text-transform:uppercase; color:#b7b9cc; margin-bottom:12px;
        display:flex; align-items:center; gap:8px;
    }
    .section-label::after {
        content:''; flex:1; height:1px; background:#e3e6f0;
    }
    .status-dot {
        width:8px; height:8px; border-radius:50%; display:inline-block; margin-right:4px;
    }
    .table td, .table th { vertical-align:middle; font-size:0.82rem; }
    .badge-status {
        padding:3px 9px; border-radius:20px; font-size:0.7rem; font-weight:700;
        display:inline-block; white-space:nowrap;
    }
    .card-header-tabs .nav-link { font-size:0.82rem; }
    .mini-icon-box {
        width:46px; height:46px; border-radius:50%;
        display:flex; align-items:center; justify-content:center;
        flex-shrink:0;
    }
</style>

<div class="container-fluid" id="container-wrapper">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
        <ol class="breadcrumb mb-0">
            <li class="breadcrumb-item"><a href="#">Home</a></li>
            <li class="breadcrumb-item active">Dashboard</li>
        </ol>
    </div>

    <!-- ══════════════════════════════════════════════════════
         SECTION 1 — USERS
    ══════════════════════════════════════════════════════ -->
    <div class="section-label"><i class="fas fa-users mr-1"></i> Users</div>
    <div class="row mb-2">

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#4e73df;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Total Users</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($u['total_users']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success mr-1"><i class="fas fa-plus"></i> <?php echo number_format($u['registered_today']); ?></span> Registered Today
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#eef0fb;"><i class="fas fa-user-circle fa-lg text-primary"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#1cc88a;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Active Users</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($u['active_users']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success mr-1"><i class="fas fa-circle"></i> <?php echo number_format($u['active_today']); ?></span> Active Today
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e6faf3;"><i class="fas fa-user-check fa-lg text-success"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#f6c23e;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Total User Balance</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">₦<?php echo number_format((float)$u['total_user_balance'], 2); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-info mr-1"><i class="fas fa-gift"></i> ₦<?php echo number_format((float)$u['total_referral_balance'], 2); ?></span> Referral Balance
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#fef9ec;"><i class="fas fa-wallet fa-lg text-warning"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#e74a3b;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Banned / Blocked</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($u['banned_users'] + $u['blocked_users']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-danger mr-1"><?php echo number_format($u['banned_users']); ?> banned</span> &bull;
                                <span class="ml-1"><?php echo number_format($u['blocked_users']); ?> blocked</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#fde9e7;"><i class="fas fa-user-alt-slash fa-lg text-danger"></i></div></div>
                    </div>
                </div>
            </div>
        </div>
<!--
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#36b9cc;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Subscribed Users</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($u['subscribed_users']); ?></div>
                            <div class="mt-2 text-muted text-xs">Active subscription plans</div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e8f8fb;"><i class="fas fa-crown fa-lg text-info"></i></div></div>
                    </div>
                </div>
            </div>
        </div>
-->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#858796;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Referrals</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($ref['total_referrals']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success"><?php echo number_format($ref['active_referrals']); ?> active</span> &bull;
                                <span class="ml-1"><?php echo number_format($ref['completed_referrals']); ?> completed</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#f0f0f3;"><i class="fas fa-users fa-lg text-secondary"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- ══════════════════════════════════════════════════════
         SECTION 2 — OTP / SMS NUMBERS
    ══════════════════════════════════════════════════════ -->
    <div class="section-label"><i class="fas fa-mobile-alt mr-1"></i> OTP / SMS Numbers</div>
    <div class="row mb-2">

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#4e73df;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Total OTP Sells</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['total_otp']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success mr-1"><i class="fas fa-plus"></i> <?php echo number_format($otp['today_otp']); ?></span> Today
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#eef0fb;"><i class="fas fa-comments fa-lg text-primary"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#1cc88a;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">OTP Revenue</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">₦<?php echo number_format((float)$otp['total_otp_revenue'], 2); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success mr-1"><i class="fas fa-plus"></i> ₦<?php echo number_format((float)$otp['today_otp_revenue'], 2); ?></span> Today
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e6faf3;"><i class="fas fa-comments-dollar fa-lg text-success"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#f6c23e;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Active Numbers (OTP Pending)</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['active_numbers']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-warning">₦<?php echo number_format((float)$otp['active_number_amount'], 2); ?></span> Active Amount
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#fef9ec;"><i class="fas fa-phone-slash fa-lg text-warning"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#36b9cc;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">OTP Status Breakdown</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['completed_otp']); ?> <small class="text-muted" style="font-size:0.7rem;">completed</small></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-warning mr-2"><?php echo number_format($otp['pending_otp']); ?> pending</span>
                                <span class="text-danger"><?php echo number_format($otp['cancelled_otp']); ?> cancelled</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e8f8fb;"><i class="fas fa-chart-pie fa-lg text-info"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#858796;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">SMS Services Available</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($sms_svc['total_sms_services']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success"><?php echo number_format($sms_svc['active_sms_services']); ?> active</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#f0f0f3;"><i class="fas fa-sms fa-lg text-secondary"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- OTP Server Breakdown -->
    <div class="row mb-4">
        <div class="col-md-3 mb-3">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"><i class="fas fa-server mr-1"></i>Server1 — smspva</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['s1_total'] ?? 0); ?></div>
                            <small class="text-muted"><?php echo number_format($otp['s1_completed'] ?? 0); ?> completed</small>
                        </div>
                        <div class="col-auto"><i class="fas fa-server fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1"><i class="fas fa-satellite-dish mr-1"></i>Server2 — smscodes</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['s2_total'] ?? 0); ?></div>
                            <small class="text-muted"><?php echo number_format($otp['s2_completed'] ?? 0); ?> completed</small>
                        </div>
                        <div class="col-auto"><i class="fas fa-satellite-dish fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1"><i class="fas fa-signal mr-1"></i>Server3 — 5sim</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['s3_total'] ?? 0); ?></div>
                            <small class="text-muted"><?php echo number_format($otp['s3_completed'] ?? 0); ?> completed</small>
                        </div>
                        <div class="col-auto"><i class="fas fa-signal fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1"><i class="fas fa-bus mr-1"></i>Server4 — smsbus</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($otp['s4_total'] ?? 0); ?></div>
                            <small class="text-muted"><?php echo number_format($otp['s4_completed'] ?? 0); ?> completed</small>
                        </div>
                        <div class="col-auto"><i class="fas fa-bus fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- ══════════════════════════════════════════════════════
         SECTION 3 — SMM ORDERS
    ══════════════════════════════════════════════════════ -->
    <div class="section-label"><i class="fas fa-share-alt mr-1"></i> SMM Panel</div>
    <div class="row mb-2">

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#4e73df;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Total SMM Orders</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($smm['total_smm_orders']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success mr-1"><i class="fas fa-plus"></i> <?php echo number_format($smm['today_smm_orders']); ?></span> Today
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#eef0fb;"><i class="fas fa-shopping-cart fa-lg text-primary"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#1cc88a;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">SMM Revenue</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">₦<?php echo number_format((float)$smm['total_smm_revenue'], 2); ?></div>
                            <div class="revenue-sub"><i class="fas fa-check-circle mr-1"></i>₦<?php echo number_format((float)$smm['smm_completed_revenue'], 2); ?> completed</div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e6faf3;"><i class="fas fa-dollar-sign fa-lg text-success"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#f6c23e;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">SMM Order Status</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($smm['smm_completed']); ?> <small class="text-muted" style="font-size:0.7rem;">completed</small></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-warning mr-2"><?php echo number_format($smm['smm_pending']); ?> pending</span>
                                <span class="text-primary mr-2"><?php echo number_format($smm['smm_active']); ?> active</span>
                                <span class="text-danger"><?php echo number_format($smm['smm_cancelled']); ?> cancelled</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#fef9ec;"><i class="fas fa-tasks fa-lg text-warning"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#36b9cc;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">SMM Services</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($smm_svc['total_smm_services']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success"><?php echo number_format($smm_svc['active_smm_services']); ?> active</span>
                                &bull; <span class="ml-1"><?php echo number_format($smm_svc['smm_categories']); ?> categories</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e8f8fb;"><i class="fas fa-layer-group fa-lg text-info"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- ══════════════════════════════════════════════════════
         SECTION 4 — SM ACCOUNTS + PLATFORM TOTAL
    ══════════════════════════════════════════════════════ -->
    <div class="section-label"><i class="fas fa-at mr-1"></i> Social Media Accounts &amp; Platform Overview</div>
    <div class="row mb-2">

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#4e73df;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">SM Accounts Listed</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($sm['total_sm_accounts']); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success mr-2"><?php echo number_format($sm['available_accounts']); ?> available</span>
                                <span class="text-danger"><?php echo number_format($sm['sold_accounts']); ?> sold</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#eef0fb;"><i class="fas fa-at fa-lg text-primary"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#1cc88a;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">SM Account Revenue</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">$<?php echo number_format((float)$sm_rev['total_sm_revenue'], 2); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span class="text-success"><?php echo number_format($sm_rev['total_sm_purchases']); ?> completed purchases</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#e6faf3;"><i class="fas fa-store fa-lg text-success"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#f6c23e;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Total Platform Revenue (₦)</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">₦<?php echo number_format($total_platform_revenue, 2); ?></div>
                            <div class="revenue-sub"><i class="fas fa-calendar-day mr-1"></i>₦<?php echo number_format($today_total_revenue, 2); ?> Today</div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#fef9ec;"><i class="fas fa-chart-line fa-lg text-warning"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card h-100" style="border-left-color:#e74a3b;">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1">Referral Earnings Paid</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">₦<?php echo number_format((float)$ref['total_referral_earnings'], 2); ?></div>
                            <div class="mt-2 text-muted text-xs">
                                <span><?php echo number_format($ref['total_referrals']); ?> total referrals</span>
                            </div>
                        </div>
                        <div class="col-auto"><div class="mini-icon-box" style="background:#fde9e7;"><i class="fas fa-hand-holding-usd fa-lg text-danger"></i></div></div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- ══════════════════════════════════════════════════════
         SECTION 5 — RECENT ACTIVITY TABLES (tabbed)
    ══════════════════════════════════════════════════════ -->
    <div class="row mt-2">
        <div class="col-lg-12">
            <div class="card mb-4">
                <div class="card-header py-0">
                    <ul class="nav nav-tabs card-header-tabs" id="activityTabs" role="tablist">
                        <li class="nav-item">
                            <a class="nav-link active" id="tab-users" data-toggle="tab" href="#pane-users" role="tab">
                                <i class="fas fa-users mr-1"></i> Recent Users
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" id="tab-otp" data-toggle="tab" href="#pane-otp" role="tab">
                                <i class="fas fa-mobile-alt mr-1"></i> Recent OTP Orders
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" id="tab-smm" data-toggle="tab" href="#pane-smm" role="tab">
                                <i class="fas fa-share-alt mr-1"></i> Recent SMM Orders
                            </a>
                        </li>
                    </ul>
                </div>
                <div class="card-body p-0">
                    <div class="tab-content">

                        <!-- Recent Users -->
                        <div class="tab-pane fade show active" id="pane-users" role="tabpanel">
                            <div class="table-responsive">
                                <table class="table table-hover mb-0">
                                    <thead class="thead-light">
                                        <tr>
                                            <th>User</th>
                                            <th>Email</th>
                                            <th>Balance</th>
                                            <th>Status</th>
                                            <th>Joined</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recent_users as $ru):
                                            $st_color = $ru['status']==1 ? '#d1fae5' : ($ru['status']==0 ? '#fee2e2' : '#fef3c7');
                                            $st_text  = $ru['status']==1 ? 'Active'  : ($ru['status']==0 ? 'Blocked' : 'Banned');
                                            $st_c     = $ru['status']==1 ? '#065f46' : ($ru['status']==0 ? '#991b1b' : '#92400e');
                                        ?>
                                        <tr>
                                            <td>
                                                <strong><?php echo htmlspecialchars($ru['full_name'] ?: $ru['username']); ?></strong><br>
                                                <small class="text-muted">@<?php echo htmlspecialchars($ru['username']); ?></small>
                                            </td>
                                            <td><small><?php echo htmlspecialchars($ru['email']); ?></small></td>
                                            <td><strong class="text-success">₦<?php echo number_format((float)$ru['main_balance'], 2); ?></strong></td>
                                            <td><span class="badge-status" style="background:<?php echo $st_color; ?>;color:<?php echo $st_c; ?>;"><?php echo $st_text; ?></span></td>
                                            <td><small><?php echo date('M d, Y', strtotime($ru['created_at'])); ?></small></td>
                                        </tr>
                                        <?php endforeach; ?>
                                        <?php if (empty($recent_users)): ?>
                                        <tr><td colspan="5" class="text-center text-muted py-3">No users found</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <!-- Recent OTP Orders -->
                        <div class="tab-pane fade" id="pane-otp" role="tabpanel">
                            <div class="table-responsive">
                                <table class="table table-hover mb-0">
                                    <thead class="thead-light">
                                        <tr>
                                            <th>User</th>
                                            <th>Number</th>
                                            <th>Service</th>
                                            <th>Price</th>
                                            <th>Status</th>
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recent_otp as $ro):
                                            if ($ro['status']=='1')      { $sc='#d1fae5'; $tc='#065f46'; $sl='Completed'; }
                                            elseif ($ro['status']=='2')  { $sc='#fef3c7'; $tc='#92400e'; $sl='Pending';   }
                                            else                         { $sc='#fee2e2'; $tc='#991b1b'; $sl='Cancelled'; }
                                        ?>
                                        <tr>
                                            <td>
                                                <strong><?php echo htmlspecialchars($ro['full_name'] ?: $ro['username'] ?: 'User #'.$ro['user_id']); ?></strong>
                                            </td>
                                            <td><code style="font-size:0.78rem;">+<?php echo htmlspecialchars($ro['number']); ?></code></td>
                                            <td><?php echo htmlspecialchars($ro['service_name']); ?></td>
                                            <td><strong class="text-success">₦<?php echo number_format((float)$ro['service_price'], 2); ?></strong></td>
                                            <td><span class="badge-status" style="background:<?php echo $sc; ?>;color:<?php echo $tc; ?>;"><?php echo $sl; ?></span></td>
                                            <td><small><?php echo date('M d, Y H:i', strtotime($ro['buy_time'])); ?></small></td>
                                        </tr>
                                        <?php endforeach; ?>
                                        <?php if (empty($recent_otp)): ?>
                                        <tr><td colspan="6" class="text-center text-muted py-3">No OTP orders found</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <!-- Recent SMM Orders -->
                        <div class="tab-pane fade" id="pane-smm" role="tabpanel">
                            <div class="table-responsive">
                                <table class="table table-hover mb-0">
                                    <thead class="thead-light">
                                        <tr>
                                            <th>Order ID</th>
                                            <th>User</th>
                                            <th>Service</th>
                                            <th>Qty</th>
                                            <th>Charge</th>
                                            <th>Status</th>
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $smm_status_map = [
                                            'Completed'   => ['#d1fae5','#065f46'],
                                            'Pending'     => ['#fef3c7','#92400e'],
                                            'In progress' => ['#dbeafe','#1e40af'],
                                            'Processing'  => ['#ede9fe','#5b21b6'],
                                            'Partial'     => ['#fff7ed','#c2410c'],
                                            'Cancelled'   => ['#fee2e2','#991b1b'],
                                            'Refunded'    => ['#f3f4f6','#374151'],
                                        ];
                                        foreach ($recent_smm as $rs):
                                            [$sc,$tc] = $smm_status_map[$rs['status']] ?? ['#f3f4f6','#374151'];
                                        ?>
                                        <tr>
                                            <td><code>#<?php echo (int)$rs['id']; ?></code></td>
                                            <td>
                                                <strong><?php echo htmlspecialchars($rs['full_name'] ?: $rs['username'] ?: 'User #'.$rs['user_id']); ?></strong>
                                            </td>
                                            <td style="max-width:180px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">
                                                <small title="<?php echo htmlspecialchars($rs['service_name'] ?? ''); ?>"><?php echo htmlspecialchars($rs['service_name'] ?? 'N/A'); ?></small>
                                            </td>
                                            <td><?php echo number_format($rs['quantity']); ?></td>
                                            <td><strong class="text-success">₦<?php echo number_format((float)$rs['charge'], 2); ?></strong></td>
                                            <td><span class="badge-status" style="background:<?php echo $sc; ?>;color:<?php echo $tc; ?>;"><?php echo htmlspecialchars($rs['status']); ?></span></td>
                                            <td><small><?php echo date('M d, Y H:i', strtotime($rs['created_at'])); ?></small></td>
                                        </tr>
                                        <?php endforeach; ?>
                                        <?php if (empty($recent_smm)): ?>
                                        <tr><td colspan="7" class="text-center text-muted py-3">No SMM orders found</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                    </div><!-- /.tab-content -->
                </div>
            </div>
        </div>
    </div>
    <!-- end recent activity -->

</div>
<!-- end container -->

<?php include('include/footer.php'); ?>