<?php
require_once '../config/koneksi.php';
require_once '../helpers/functions.php';

if (!isLogin() || !isAdmin()) redirect('../auth/login.php');

/*
|--------------------------------------------------------------------------
| QUERY DATA (OPTIMIZED)
|--------------------------------------------------------------------------
*/

// Total client
$q_user = mysqli_query($conn, "SELECT COUNT(*) as total FROM users WHERE role='client'");
$total_user = mysqli_fetch_assoc($q_user)['total'] ?? 0;

// Pending setoran
$q_setoran = mysqli_query($conn, "SELECT COUNT(*) as total FROM transactions WHERE status='pending'");
$total_pending = mysqli_fetch_assoc($q_setoran)['total'] ?? 0;

// Pending penarikan
$q_withdraw = mysqli_query($conn, "SELECT COUNT(*) as total FROM withdrawals WHERE status='pending'");
$total_withdraw_pending = mysqli_fetch_assoc($q_withdraw)['total'] ?? 0;

// Total saldo
$q_saldo = mysqli_query($conn, "SELECT SUM(saldo) as total FROM users WHERE role='client'");
$total_saldo = mysqli_fetch_assoc($q_saldo)['total'] ?? 0;

$title = "Dashboard Admin";
include '../layouts/header.php';
?>

<div class="container-fluid">
    <div id="overlay" class="overlay"></div>

    <div class="row">
        <!-- SIDEBAR -->
        <div class="col-lg-2 p-0">
            <?php include '../layouts/sidebar.php'; ?>
        </div>

        <!-- CONTENT -->
        <div class="col-lg-10 p-0">
            <?php include '../layouts/navbar.php'; ?>

            <div class="content-wrap">
                <?php showFlash(); ?>

                <div class="row g-4">

                    <!-- TOTAL CLIENT -->
                    <div class="col-md-6 col-xl-3">
                        <div class="card card-stat">
                            <div class="stat-icon">
                                <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M17 20h5V4H2v16h5m10 0v-2a4 4 0 00-4-4H11a4 4 0 00-4 4v2m10 0H7m10-10a4 4 0 11-8 0 4 4 0 018 0z"/>
                                </svg>
                            </div>
                            <h6>Total Client</h6>
                            <h2><?= $total_user; ?></h2>
                        </div>
                    </div>

                    <!-- PENDING SETORAN -->
                    <div class="col-md-6 col-xl-3">
                        <div class="card card-stat">
                            <div class="stat-icon">
                                <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M17 9V7a5 5 0 00-10 0v2M5 9h14l-1 10H6L5 9z"/>
                                </svg>
                            </div>
                            <h6>Pending Setoran</h6>
                            <h2 class="text-warning"><?= $total_pending; ?></h2>
                        </div>
                    </div>

                    <!-- PENDING PENARIKAN -->
                    <div class="col-md-6 col-xl-3">
                        <div class="card card-stat">
                            <div class="stat-icon">
                                <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M4 7h16M4 12h10M4 17h16"/>
                                </svg>
                            </div>
                            <h6>Pending Penarikan</h6>
                            <h2 class="text-danger"><?= $total_withdraw_pending; ?></h2>
                        </div>
                    </div>

                    <!-- TOTAL SALDO -->
                    <div class="col-md-6 col-xl-3">
                        <div class="card card-stat">
                            <div class="stat-icon">
                                <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 8c-2.5 0-4 1.2-4 2.5S9.5 13 12 13s4 1.2 4 2.5S14.5 18 12 18m0-10V6m0 12v-2"/>
                                </svg>
                            </div>
                            <h6>Total Saldo Client</h6>
                            <h4 class="text-success"><?= rupiah($total_saldo); ?></h4>
                        </div>
                    </div>

                </div> <!-- END ROW -->

            </div> <!-- END CONTENT WRAP -->
        </div>
    </div>
</div>

<style>
.card-stat {
    padding: 20px;
    border-radius: 20px;
    border: none;
    background: linear-gradient(135deg, #ffffff, #f8fbff);
    box-shadow: 0 10px 25px rgba(0,0,0,0.06);
}

.stat-icon {
    width: 45px;
    height: 45px;
    border-radius: 12px;
    background: #eef4ff;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
}

.card-stat h6 {
    color: #6c757d;
    font-size: 14px;
}

.card-stat h2, .card-stat h4 {
    font-weight: 700;
    margin: 0;
}
</style>

<?php include '../layouts/footer.php'; ?>