<?php
include('header.php');
include('../../../includes/config.php');

$userid = $udata['phone'];
$userType = $udata['type']; 

function formatAmt($amt){
    return "₹" . number_format($amt, 2);
}

$today = date('Y-m-d');
$monthStart = date('Y-m-01');

// Initialize variables
$totalUsers = $totalDistributors = $totalRetailers = 0;
$todayRecharge = $monthRecharge = $todayCommission = $monthCommission = $wallet = 0;
$todayNsdl = $monthNsdl = $todayNsdlSuccess = $monthNsdlSuccess = $todayNsdlPending = $monthNsdlPending = 0;
$todayWalletCommission = $monthWalletCommission = 0;

// Fetch stats based on user type
if($userType == 'admin'){
    // User counts
    $totalUsers = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM users"))['cnt'];
    $totalDistributors = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM users WHERE type='distributor'"))['cnt'];
    $totalRetailers = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM users WHERE type='retailer'"))['cnt'];
    
    // Recharge stats
    $todayRecharge = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM recharge_history WHERE DATE(date)='$today'"))['amt'] ?? 0;
    $monthRecharge = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM recharge_history WHERE DATE(date)>='$monthStart'"))['amt'] ?? 0;
    $todayCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(commission) as amt FROM recharge_history WHERE DATE(date)='$today'"))['amt'] ?? 0;
    $monthCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(commission) as amt FROM recharge_history WHERE DATE(date)>='$monthStart'"))['amt'] ?? 0;
    
    // Wallet commission stats
    $todayWalletCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM wallethistory WHERE purpose LIKE '%Commission%' AND DATE(date)='$today'"))['amt'] ?? 0;
    $monthWalletCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM wallethistory WHERE purpose LIKE '%Commission%' AND DATE(date)>='$monthStart'"))['amt'] ?? 0;
    
    // NSDL stats
    $todayNsdl = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE DATE(date)='$today'"))['cnt'] ?? 0;
    $monthNsdl = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE DATE(date)>='$monthStart'"))['cnt'] ?? 0;
    $todayNsdlSuccess = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE status='SUCCESS' AND DATE(date)='$today'"))['cnt'] ?? 0;
    $monthNsdlSuccess = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE status='SUCCESS' AND DATE(date)>='$monthStart'"))['cnt'] ?? 0;
    $todayNsdlPending = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE status='Pending' AND DATE(date)='$today'"))['cnt'] ?? 0;
    $monthNsdlPending = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE status='Pending' AND DATE(date)>='$monthStart'"))['cnt'] ?? 0;
    
} elseif($userType == 'distributor'){
    $wallet = $udata['balance'];
    $totalRetailers = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM users WHERE parent='$userid' AND type='retailer'"))['cnt'];
    
    // Recharge stats for distributor's retailers
    $todayRecharge = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(r.amount) as amt FROM recharge_history r JOIN users u ON u.userid=r.userid WHERE u.parent='$userid' AND DATE(r.date)='$today'"))['amt'] ?? 0;
    $monthRecharge = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(r.amount) as amt FROM recharge_history r JOIN users u ON u.userid=r.userid WHERE u.parent='$userid' AND DATE(r.date)>='$monthStart'"))['amt'] ?? 0;
    $todayCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(r.commission) as amt FROM recharge_history r JOIN users u ON u.userid=r.userid WHERE u.parent='$userid' AND DATE(r.date)='$today'"))['amt'] ?? 0;
    $monthCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(r.commission) as amt FROM recharge_history r JOIN users u ON u.userid=r.userid WHERE u.parent='$userid' AND DATE(r.date)>='$monthStart'"))['amt'] ?? 0;
    
    // Wallet commission stats for distributor
    $todayWalletCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM wallethistory WHERE userid='$userid' AND purpose LIKE '%Commission%' AND DATE(date)='$today'"))['amt'] ?? 0;
    $monthWalletCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM wallethistory WHERE userid='$userid' AND purpose LIKE '%Commission%' AND DATE(date)>='$monthStart'"))['amt'] ?? 0;
    
    // NSDL stats for distributor's retailers
    $todayNsdl = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests n JOIN users u ON u.phone=n.phone WHERE u.parent='$userid' AND DATE(n.date)='$today'"))['cnt'] ?? 0;
    $monthNsdl = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests n JOIN users u ON u.phone=n.phone WHERE u.parent='$userid' AND DATE(n.date)>='$monthStart'"))['cnt'] ?? 0;
    $todayNsdlSuccess = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests n JOIN users u ON u.phone=n.phone WHERE u.parent='$userid' AND n.status='SUCCESS' AND DATE(n.date)='$today'"))['cnt'] ?? 0;
    $monthNsdlSuccess = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests n JOIN users u ON u.phone=n.phone WHERE u.parent='$userid' AND n.status='SUCCESS' AND DATE(n.date)>='$monthStart'"))['cnt'] ?? 0;
    $todayNsdlPending = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests n JOIN users u ON u.phone=n.phone WHERE u.parent='$userid' AND n.status='Pending' AND DATE(n.date)='$today'"))['cnt'] ?? 0;
    $monthNsdlPending = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests n JOIN users u ON u.phone=n.phone WHERE u.parent='$userid' AND n.status='Pending' AND DATE(n.date)>='$monthStart'"))['cnt'] ?? 0;
    
} elseif($userType == 'retailer'){
    $wallet = $udata['balance'];
    
    // Recharge stats for retailer
    $todayData = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt, SUM(commission) as comm FROM recharge_history WHERE userid='$userid' AND DATE(date)='$today'")) ?? ['amt'=>0,'comm'=>0];
    $monthData = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt, SUM(commission) as comm FROM recharge_history WHERE userid='$userid' AND DATE(date)>='$monthStart'")) ?? ['amt'=>0,'comm'=>0];
    $todayRecharge = $todayData['amt'] ?? 0;
    $todayCommission = $todayData['comm'] ?? 0;
    $monthRecharge = $monthData['amt'] ?? 0;
    $monthCommission = $monthData['comm'] ?? 0;
    
    // Wallet commission stats for retailer
    $todayWalletCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM wallethistory WHERE userid='$userid' AND purpose LIKE '%Commission%' AND DATE(date)='$today'"))['amt'] ?? 0;
    $monthWalletCommission = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT SUM(amount) as amt FROM wallethistory WHERE userid='$userid' AND purpose LIKE '%Commission%' AND DATE(date)>='$monthStart'"))['amt'] ?? 0;
    
    // NSDL stats for retailer
    $todayNsdl = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE phone='$userid' AND DATE(date)='$today'"))['cnt'] ?? 0;
    $monthNsdl = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE phone='$userid' AND DATE(date)>='$monthStart'"))['cnt'] ?? 0;
    $todayNsdlSuccess = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE phone='$userid' AND status='SUCCESS' AND DATE(date)='$today'"))['cnt'] ?? 0;
    $monthNsdlSuccess = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE phone='$userid' AND status='SUCCESS' AND DATE(date)>='$monthStart'"))['cnt'] ?? 0;
    $todayNsdlPending = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE phone='$userid' AND status='Pending' AND DATE(date)='$today'"))['cnt'] ?? 0;
    $monthNsdlPending = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT COUNT(*) as cnt FROM nsdl_requests WHERE phone='$userid' AND status='Pending' AND DATE(date)>='$monthStart'"))['cnt'] ?? 0;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= strtoupper($userType) ?> Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #2c3e50;
            --secondary: #3498db;
            --success: #27ae60;
            --info: #2980b9;
            --warning: #f39c12;
            --danger: #e74c3c;
            --light: #ecf0f1;
            --dark: #2c3e50;
            --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
        }

        .page-wrapper {
            min-height: 100vh;
            padding: 20px;
        }

        .dashboard-header {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 25px;
            box-shadow: var(--card-shadow);
        }

        .dashboard-header h2 {
            margin: 0;
            font-weight: 600;
            font-size: 1.8rem;
        }

        .welcome-banner {
            display: flex;
            align-items: center;
            background: white;
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 25px;
            box-shadow: var(--card-shadow);
        }

        .welcome-banner i {
            font-size: 3rem;
            color: var(--secondary);
            margin-right: 20px;
        }

        .welcome-banner h4 {
            margin: 0;
            font-weight: 600;
            color: var(--primary);
        }

        .welcome-banner p {
            margin: 5px 0 0;
            color: #666;
        }

        .section-title {
            font-weight: 600;
            color: var(--primary);
            margin: 30px 0 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid var(--secondary);
            width: 100%;
        }

        /* Card Styles */
        .lux-card {
            background: white;
            border-radius: 15px;
            padding: 25px 20px;
            text-align: center;
            box-shadow: var(--card-shadow);
            transition: var(--transition);
            margin-bottom: 20px;
            height: 100%;
            position: relative;
            overflow: hidden;
            border: none;
        }

        .lux-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
        }

        .lux-card i {
            font-size: 2.5rem;
            margin-bottom: 15px;
            display: block;
        }

        .lux-card h5 {
            font-size: 1rem;
            font-weight: 500;
            margin-bottom: 10px;
            color: #666;
        }

        .lux-card h3 {
            font-size: 1.8rem;
            font-weight: 600;
            margin: 0;
        }

        .card-tooltip {
            position: absolute;
            top: 10px;
            right: 10px;
            color: #aaa;
            cursor: help;
        }

        /* Card Type Colors */
        .card-success {
            border-left: 5px solid var(--success);
        }

        .card-success i {
            color: var(--success);
        }

        .card-pending {
            border-left: 5px solid var(--warning);
        }

        .card-pending i {
            color: var(--warning);
        }

        .card-info {
            border-left: 5px solid var(--info);
        }

        .card-info i {
            color: var(--info);
        }

        .card-commission {
            border-left: 5px solid #9b59b6;
        }

        .card-commission i {
            color: #9b59b6;
        }

        .card-danger {
            border-left: 5px solid var(--danger);
        }

        .card-danger i {
            color: var(--danger);
        }

        /* Stats Grid */
        .stats-grid {
            margin-bottom: 30px;
        }

        /* Responsive adjustments */
        @media (max-width: 768px) {
            .page-wrapper {
                padding: 10px;
            }
            
            .dashboard-header h2 {
                font-size: 1.5rem;
            }
            
            .welcome-banner {
                flex-direction: column;
                text-align: center;
            }
            
            .welcome-banner i {
                margin-right: 0;
                margin-bottom: 10px;
            }
            
            .lux-card {
                padding: 20px 15px;
            }
            
            .lux-card h3 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
<div class="page-wrapper">
    <div class="page-content">
        <div class="dashboard-header">
            <h2><?= strtoupper($userType) ?> DASHBOARD</h2>
        </div>

        <div class="welcome-banner">
            <i class="fas fa-user-circle"></i>
            <div>
                <h4>Welcome, <?= $udata['name'] ?>!</h4>
                <p>Here's your performance overview and statistics</p>
            </div>
        </div>

        <div class="row stats-grid">
            <?php if($userType=='admin'): ?>
                <!-- User Stats -->
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total number of users in the system"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-users"></i>
                        <h5>Total Users</h5>
                        <h3><?= $totalUsers ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total number of distributors"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-user-tie"></i>
                        <h5>Total Distributors</h5>
                        <h3><?= $totalDistributors ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total number of retailers"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-user-check"></i>
                        <h5>Total Retailers</h5>
                        <h3><?= $totalRetailers ?></h3>
                    </div>
                </div>
                
                <!-- Wallet Balance -->
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-success">
                        <span class="card-tooltip" title="Your current wallet balance"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-wallet"></i>
                        <h5>Wallet Balance</h5>
                        <h3><?= $udata['balance'] ?></h3>
                    </div>
                </div>
                
                <!-- Recharge Stats -->
                <h3 class="section-title">Recharge Statistics</h3>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge amount for today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-calendar-day"></i>
                        <h5>Today's Recharge</h5>
                        <h3><?= formatAmt($todayRecharge) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge commission for today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-money-bill-wave"></i>
                        <h5>Today's Recharge Commission</h5>
                        <h3><?= formatAmt($todayCommission) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge amount this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-calendar-alt"></i>
                        <h5>Monthly Recharge</h5>
                        <h3><?= formatAmt($monthRecharge) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge commission this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-hand-holding-usd"></i>
                        <h5>Monthly Recharge Commission</h5>
                        <h3><?= formatAmt($monthCommission) ?></h3>
                    </div>
                </div>
                
                <?php if($userType=='admin' || $userType=='distributor'): ?>
                <!-- Commission Stats (from Wallet History) -->
                <h3 class="section-title">Total Commission (All Sources)</h3>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-commission">
                        <span class="card-tooltip" title="Total commission from all sources today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-coins"></i>
                        <h5>Today's Total Commission</h5>
                        <h3><?= formatAmt($todayWalletCommission) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-commission">
                        <span class="card-tooltip" title="Total commission from all sources this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-chart-line"></i>
                        <h5>Monthly Total Commission</h5>
                        <h3><?= formatAmt($monthWalletCommission) ?></h3>
                    </div>
                </div>
                <?php endif; ?>
                
                <!-- NSDL Stats -->
                <h3 class="section-title">NSDL PAN Statistics</h3>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total NSDL applications submitted today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-file-import"></i>
                        <h5>Today's NSDL Applications</h5>
                        <h3><?= $todayNsdl ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-success">
                        <span class="card-tooltip" title="NSDL applications successfully processed today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-check-circle"></i>
                        <h5>Today's NSDL Success</h5>
                        <h3><?= $todayNsdlSuccess ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-pending">
                        <span class="card-tooltip" title="NSDL applications pending today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-clock"></i>
                        <h5>Today's NSDL Pending</h5>
                        <h3><?= $todayNsdlPending ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total NSDL applications submitted this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-file-import"></i>
                        <h5>Monthly NSDL Applications</h5>
                        <h3><?= $monthNsdl ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-success">
                        <span class="card-tooltip" title="NSDL applications successfully processed this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-check-circle"></i>
                        <h5>Monthly NSDL Success</h5>
                        <h3><?= $monthNsdlSuccess ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-pending">
                        <span class="card-tooltip" title="NSDL applications pending this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-clock"></i>
                        <h5>Monthly NSDL Pending</h5>
                        <h3><?= $monthNsdlPending ?></h3>
                    </div>
                </div>

            <?php else: ?>
                <!-- Wallet Balance -->
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-success">
                        <span class="card-tooltip" title="Your current wallet balance"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-wallet"></i>
                        <h5>Wallet Balance</h5>
                        <h3><?= formatAmt($wallet) ?></h3>
                    </div>
                </div>
                
                <?php if($userType=='distributor'): ?>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total retailers under your account"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-users"></i>
                        <h5>Total Retailers</h5>
                        <h3><?= $totalRetailers ?></h3>
                    </div>
                </div>
                <?php endif; ?>
                
                <!-- Recharge Stats -->
                <h3 class="section-title">Recharge Statistics</h3>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge amount for today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-calendar-day"></i>
                        <h5>Today's Recharge</h5>
                        <h3><?= formatAmt($todayRecharge) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge commission for today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-money-bill-wave"></i>
                        <h5>Today's Recharge Commission</h5>
                        <h3><?= formatAmt($todayCommission) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge amount this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-calendar-alt"></i>
                        <h5>Monthly Recharge</h5>
                        <h3><?= formatAmt($monthRecharge) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card">
                        <span class="card-tooltip" title="Total recharge commission this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-hand-holding-usd"></i>
                        <h5>Monthly Recharge Commission</h5>
                        <h3><?= formatAmt($monthCommission) ?></h3>
                    </div>
                </div>
                
                <!-- Commission Stats (from Wallet History) -->
                <h3 class="section-title">Total Commission (All Sources)</h3>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-commission">
                        <span class="card-tooltip" title="Total commission from all sources today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-coins"></i>
                        <h5>Today's Total Commission</h5>
                        <h3><?= formatAmt($todayWalletCommission) ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-commission">
                        <span class="card-tooltip" title="Total commission from all sources this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-chart-line"></i>
                        <h5>Monthly Total Commission</h5>
                        <h3><?= formatAmt($monthWalletCommission) ?></h3>
                    </div>
                </div>
                
                <!-- NSDL Stats -->
                <h3 class="section-title">NSDL PAN Statistics</h3>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total NSDL applications submitted today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-file-import"></i>
                        <h5>Today's NSDL Applications</h5>
                        <h3><?= $todayNsdl ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-success">
                        <span class="card-tooltip" title="NSDL applications successfully processed today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-check-circle"></i>
                        <h5>Today's NSDL Success</h5>
                        <h3><?= $todayNsdlSuccess ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-pending">
                        <span class="card-tooltip" title="NSDL applications pending today"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-clock"></i>
                        <h5>Today's NSDL Pending</h5>
                        <h3><?= $todayNsdlPending ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-info">
                        <span class="card-tooltip" title="Total NSDL applications submitted this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-file-import"></i>
                        <h5>Monthly NSDL Applications</h5>
                        <h3><?= $monthNsdl ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-success">
                        <span class="card-tooltip" title="NSDL applications successfully processed this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-check-circle"></i>
                        <h5>Monthly NSDL Success</h5>
                        <h3><?= $monthNsdlSuccess ?></h3>
                    </div>
                </div>
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="lux-card card-pending">
                        <span class="card-tooltip" title="NSDL applications pending this month"><i class="fas fa-info-circle"></i></span>
                        <i class="fas fa-clock"></i>
                        <h5>Monthly NSDL Pending</h5>
                        <h3><?= $monthNsdlPending ?></h3>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
    $(document).ready(function() {
        // Add animation to cards when they come into view
        $('.lux-card').each(function(i) {
            $(this).delay(i * 100).animate({opacity: 1}, 300);
        });
        
        // Tooltip functionality
        $('.card-tooltip').tooltip({
            placement: 'top',
            trigger: 'hover'
        });
    });
</script>

<?php
// Example: Fetch the notification message from DB
$notifMessage = ""; // Default
$query = "SELECT message FROM notification WHERE id='1' LIMIT 1";
$res = mysqli_query($ahk_conn, $query);
if($res && mysqli_num_rows($res) > 0){
    $row = mysqli_fetch_assoc($res);
    $notifMessage = $row['message'];
}
?>

<script>
document.addEventListener('DOMContentLoaded', function () {
    <?php if($notifMessage != ""): ?>
    Swal.fire({
        title: '<strong>📢 Notification</strong>',
        html: `<?php echo addslashes($notifMessage); ?>`,
        showCloseButton: true,
        showCancelButton: false,
        focusConfirm: true,
        showConfirmButton: false,
        timer: 10000,            // 10 seconds auto close
        timerProgressBar: true,
        showClass: {
            popup: 'animate__animated animate__fadeInDown'
        },
        hideClass: {
            popup: 'animate__animated animate__fadeOutUp'
        },
        didOpen: () => {
            const b = Swal.getHtmlContainer().querySelector('b');
            if(b) b.style.color = '#00bfff'; // Make title blue
        }
    });
    <?php endif; ?>
});
</script>

<!-- Additional JS files -->
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/js/index.js"></script>
<script src="../template/ahkweb/assets/js/app.js"></script>

<!-- Datatables -->
<script src="../template/ahkweb/assets/plugins/datatable/js/jquery.dataTables.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.bootstrap5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.buttons.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.bootstrap5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/jszip.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/pdfmake.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/vfs_fonts.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.html5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.print.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.colVis.min.js"></script>

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