<?php 

require_once("header.php");
?>





        <div class="main-panel">
        <div class="content bg-light">
            <div class="panel-header bg-primary-gradient">
                <div class="py-5 page-inner">
                    <div class="d-flex align-items-left align-items-md-center flex-column flex-md-row">
                        <div>
                            <h2 class="pb-2 text-white fw-bold">Dashboard</h2>
                            <h5 class="mb-2 text-white op-7">Welcome, Admin ( <?php echo $fullname ?>! )</h5> 
                        </div>
                                
                                
                                <div class="py-2 ml-md-auto py-md-0">
                                    
                                <!--<a href="mdeposits.php" class="mr-2 btn btn-success btn-border">Deposits</a>-->
                                
                                
                                <!--<a href="mwithdrawals.php" class="mr-2 btn btn-danger btn-border">Withdrawals</a>-->
                                
                                
                                <a href="adminverify.php" class="mr-2 btn btn-secondary btn-border">Users</a>
                                 
                            </div> 
                            
                            <!--==================================-->
                            
                          <div class="py-2 ml-md-auto py-md-0">
    <?php
    
        // Count unread messages (Inbox)
        $stmt = $pdo->prepare("SELECT COUNT(id) FROM message WHERE status = :status");
        $stmt->execute(['status' => 1]);
        $inboxCount = $stmt->fetchColumn();

        // Count sent messages (Outbox)
        $stmt->execute(['status' => 2]);
        $outboxCount = $stmt->fetchColumn();
    
    ?>

    <a href="inbox.php" class="mr-2 btn btn-warning btn-border">
        <?= htmlspecialchars($inboxCount) ?> INBOX
    </a>

    <a #="outbox.php" class="mr-2 btn btn-warning btn-border">
        <?= htmlspecialchars($outboxCount) ?> OUTBOX
    </a>
</div>
       
                    </div>
                </div>
            </div>
            <div>
    </div>            <div>
    </div>            <div class="page-inner mt--5">
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
                <!-- Beginning of  Dashboard Stats  -->
                <div class="row row-card-no-pd bg-light shadow-lg mt--2">
                   
                    <div class="col-sm-6 col-md-3">
                        <div class="card card-stats card-round bg-light full-height">
                            <div class="card-body ">
                                <div class="row">
                                    <div class="col-4">
                                        <div class="text-center icon-big">
                                            <i class="fa fa-download text-info"></i>
                                        </div>
                                    </div>
                                    <div class="col-8 col-stats">
                                        <div class="numbers">
                                            <p class="card-category">Total Deposit</p>
                                                   
                                                   
                                                   
                                      <?php

    $stmt = $pdo->query("SELECT SUM(amount) as total FROM deposit");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $totalAmount = $result['total'] ?? 0;

    echo "$" . number_format($totalAmount, 2);

?>

                                                   
                                                </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    
                    
                    
                    <?php

    // Pending Deposit(s)
    $stmt = $pdo->query("SELECT SUM(deposit) as total FROM logins");
    $deposit = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Total Withdrawal
    $stmt = $pdo->query("SELECT SUM(amount) as total FROM withdraw");
    $totalWithdraw = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Pending Withdrawal
    $stmt = $pdo->query("SELECT SUM(withdraw) as total FROM logins");
    $pendingWithdraw = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Total Users
    $stmt = $pdo->query("SELECT COUNT(id) as total FROM logins WHERE ip > 0");
    $totalUsers = ($stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0) - 0; // Assuming the first user is an admin

    // Active Users
    $stmt = $pdo->query("SELECT COUNT(id) as active FROM logins WHERE status = 1");
    $activeUsers = $stmt->fetch(PDO::FETCH_ASSOC)['active'] ?? 0;

    // Blocked Users (assuming status = 0 means blocked)
    $stmt = $pdo->query("SELECT COUNT(id) as blocked FROM logins WHERE status = 6");
    $blockedUsers = $stmt->fetch(PDO::FETCH_ASSOC)['blocked'] ?? 0;


?>

<div class="col-sm-6 col-md-3">
    <div class="card card-stats card-round bg-light full-height">
        <div class="card-body ">
            <div class="row">
                <div class="col-4">
                    <div class="text-center icon-big">
                        <i class="fa fa-download text-warning"></i>
                    </div>
                </div>
                <div class="col-8 col-stats">
                    <div class="numbers">
                        <p class="card-category">Pending Deposit(s)</p>
                        $<?php echo number_format($deposit, 2); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-6 col-md-3">
    <div class="card card-stats card-round bg-light full-height">
        <div class="card-body">
            <div class="row">
                <div class="col-4">
                    <div class="text-center icon-big">
                        <i class="fa fa-upload text-info"></i>
                    </div>
                </div>
                <div class="col-8 col-stats">
                    <div class="numbers">
                        <p class="card-category">Total Withdrawal</p>
                        $<?php echo number_format($totalWithdraw, 2); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-6 col-md-3">
    <div class="card card-stats card-round bg-light full-height">
        <div class="card-body">
            <div class="row">
                <div class="col-4">
                    <div class="text-center icon-big">
                        <i class="fa fa-upload text-secondary"></i>
                    </div>
                </div>
                <div class="col-8 col-stats">
                    <div class="numbers">
                        <p class="card-category">Pending Withdrawal</p>
                        $<?php echo number_format($pendingWithdraw, 2); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-6 col-md-3">
    <div class="card card-stats card-round bg-light">
        <div class="card-body ">
            <div class="row">
                <div class="col-4">
                    <div class="text-center icon-big">
                        <i class="fa fa-users text-success"></i>
                    </div>
                </div>
                <div class="col-8 col-stats">
                    <div class="numbers">
                        <p class="card-category">Total Users</p>
                        <h4 class="card-title text-dark"><?php echo $totalUsers; ?></h4>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-6 col-md-3">
    <div class="card card-stats card-round bg-light">
        <div class="card-body ">
            <div class="row">
                <div class="col-4">
                    <div class="text-center icon-big">
                        <i class="fa fa-user-circle-o text-danger"></i>
                    </div>
                </div>
                <div class="col-8 col-stats">
                    <div class="numbers">
                        <p class="card-category">Blocked Users</p>
                        <h4 class="card-title text-dark"><?php echo $blockedUsers; ?></h4>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-6 col-md-3">
    <div class="card card-stats card-round bg-light">
        <div class="card-body ">
            <div class="row">
                <div class="col-4">
                    <div class="text-center icon-big">
                        <i class="fa fa-user-circle-o text-success"></i>
                    </div>
                </div>
                <div class="col-8 col-stats">
                    <div class="numbers">
                        <p class="card-category">Active Users</p>
                        <h4 class="card-title text-dark"><?php echo $activeUsers; ?></h4>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

                </div>
                
                
                
                
                
                <?php

    // Fetching deposit sum
    $stmt = $pdo->query("SELECT SUM(amount) as total FROM deposit");
    $deposit = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Fetching pending deposit sum
    $stmt = $pdo->query("SELECT SUM(deposit) as total FROM logins");
    $pendingDeposit = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Fetching total withdrawal sum
    $stmt = $pdo->query("SELECT SUM(amount) as total FROM withdraw");
    $withdrawal = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Fetching pending withdrawal sum
    $stmt = $pdo->query("SELECT SUM(withdraw) as total FROM logins");
    $pendingWithdrawal = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

    // Hardcoded values
    $totalTransactions = 123332;
    $xyz = 855;


?>

<div class="row">
    <div class="col-md-12">
        <div class="overflow-auto">
            <canvas id="myChart" height="100%" class="text-dark"></canvas>
        </div>
        
        <script>
            var ctx = document.getElementById('myChart').getContext('2d');
            var myChart = new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: ['Deposit', 'Pending Deposit', 'Withdrawal', 'Pending Withdrawal', 'Total Transactions', 'XYZ'],
                    datasets: [{
                        label: "# System Statistics in $",
                        data: [
                            <?php echo $deposit; ?>, 
                            <?php echo $pendingDeposit; ?>, 
                            <?php echo $withdrawal; ?>, 
                            <?php echo $pendingWithdrawal; ?>, 
                            <?php echo $totalTransactions; ?>, 
                            <?php echo $xyz; ?>
                        ],
                        backgroundColor: [
                            'rgba(255, 99, 132, 0.2)',
                            'rgba(54, 162, 235, 0.2)',
                            'rgba(255, 206, 86, 0.2)',
                            'rgba(75, 192, 192, 0.2)',
                            'rgba(153, 102, 255, 0.2)',
                            'rgba(255, 159, 64, 0.2)'
                        ],
                        borderColor: [
                            'rgba(255, 99, 132, 1)',
                            'rgba(54, 162, 235, 1)',
                            'rgba(255, 206, 86, 1)',
                            'rgba(75, 192, 192, 1)',
                            'rgba(153, 102, 255, 1)',
                            'rgba(255, 159, 64, 1)'
                        ],
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    scales: {
                        y: {
                            beginAtZero: true
                        }
                    }
                }
            });
        </script>
    </div>
</div>

            </div>
        </div>
                 
                 
                
                    
                    
                    <?php require ("footer.php") ?>