<?php
require_once __DIR__ . '/config.php';
require_admin_login();

try {
    // ==========================
    // EXISTING COUNTS
    // ==========================
    $total_users = (int)$pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
    $blocked_users = (int)$pdo->query("SELECT COUNT(*) FROM users WHERE is_blocked=1")->fetchColumn();

    $total_recharges = (int)$pdo->query("SELECT COUNT(*) FROM recharges")->fetchColumn();
    $pending_recharges = (int)$pdo->query("SELECT COUNT(*) FROM recharges WHERE status='pending'")->fetchColumn();

    $total_orders = (int)$pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
    $pending_orders = (int)$pdo->query("SELECT COUNT(*) FROM orders WHERE status='pending'")->fetchColumn();
    $completed_orders = (int)$pdo->query("SELECT COUNT(*) FROM orders WHERE status='completed'")->fetchColumn();

    $total_services = (int)$pdo->query("SELECT COUNT(*) FROM services")->fetchColumn();
    $total_subcategories = (int)$pdo->query("SELECT COUNT(*) FROM subcategories")->fetchColumn();


    // ==========================
    // NEW DASHBOARD FEATURES
    // ==========================

    // Total Balance (All Users)
    $total_balance = (float)$pdo->query(
        "SELECT COALESCE(SUM(balance),0) FROM users"
    )->fetchColumn();

    // Today Recharge Amount
    $today_recharge = (float)$pdo->query(
        "SELECT COALESCE(SUM(amount),0)
         FROM recharges
         WHERE DATE(created_at) = CURDATE()
         AND status='approved'"
    )->fetchColumn();

    // Today Completed Orders
    $today_completed_orders = (int)$pdo->query(
        "SELECT COUNT(*)
         FROM orders
         WHERE status='completed'
         AND DATE(created_at) = CURDATE()"
    )->fetchColumn();

} catch (Throwable $e) {
    die("A database error occurred: " . $e->getMessage());
}

require_once 'header.php';
?>

<style>
    /* সম্পূর্ণ সাইটের প্রাইমারি কালার সেটআপ */
    :root {
        --main-purple: #230133;
    }

    .text-gray-800, .h3 {
        color: var(--main-purple) !important;
        font-weight: 700;
    }

    /* ড্যাশবোর্ড কার্ডের কাস্টম ডিজাইন */
    .card-purple-theme {
        border-left: 5px solid var(--main-purple) !important;
        transition: transform 0.2s;
    }

    .card-purple-theme:hover {
        transform: translateY(-5px);
    }

    .theme-text-color {
        color: var(--main-purple) !important;
    }

    .icon-box {
        color: #e2d9e6 !important; /* হালকা পার্পল আইকন ব্যাকগ্রাউন্ড */
    }

    /* বুটস্ট্র্যাপের ডিফল্ট বর্ডার কালার ওভাররাইড */
    .border-primary, .border-danger, .border-success, .border-warning, .border-info {
        border-color: var(--main-purple) !important;
    }

    .text-primary, .text-danger, .text-success, .text-warning, .text-info {
        color: var(--main-purple) !important;
    }
</style>

<div class="container-fluid">
    <h1 class="h3 mb-4">অ্যাডমিন ড্যাশবোর্ড</h1>

    <div class="row g-4">

        <div class="col-xl-4 col-md-6 mb-4">
            <div class="card card-purple-theme h-100 shadow-sm border-0">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col me-2">
                            <div class="text-xs fw-bold text-uppercase mb-1 theme-text-color">
                                মোট ইউজার (Total Users)
                            </div>
                            <div class="h4 mb-0 fw-bold text-dark">
                                <?php echo $total_users; ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="bi bi-people-fill fs-1 icon-box"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4 col-md-6 mb-4">
            <div class="card card-purple-theme h-100 shadow-sm border-0">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col me-2">
                            <div class="text-xs fw-bold text-uppercase mb-1 theme-text-color">
                                ব্লকড ইউজার (Blocked)
                            </div>
                            <div class="h4 mb-0 fw-bold text-dark">
                                <?php echo $blocked_users; ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="bi bi-person-fill-slash fs-1 icon-box"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4 col-md-6 mb-4">
            <div class="card card-purple-theme h-100 shadow-sm border-0">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col me-2">
                            <div class="text-xs fw-bold text-uppercase mb-1 theme-text-color">
                                মোট ইউজার ব্যালেন্স
                            </div>
                            <div class="h4 mb-0 fw-bold text-dark">
                                ৳ <?php echo number_format($total_balance, 2); ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="bi bi-wallet2 fs-1 icon-box"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4 col-md-6 mb-4">
            <div class="card card-purple-theme h-100 shadow-sm border-0" style="border-left-color: #ffd700 !important;">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col me-2">
                            <div class="text-xs fw-bold text-uppercase mb-1 theme-text-color">
                                আজকের রিচার্জ (Approved)
                            </div>
                            <div class="h4 mb-0 fw-bold text-dark">
                                ৳ <?php echo number_format($today_recharge, 2); ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="bi bi-cash-coin fs-1 icon-box"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4 col-md-6 mb-4">
            <div class="card card-purple-theme h-100 shadow-sm border-0">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col me-2">
                            <div class="text-xs fw-bold text-uppercase mb-1 theme-text-color">
                                আজকের কমপ্লিট অর্ডার
                            </div>
                            <div class="h4 mb-0 fw-bold text-dark">
                                <?php echo $today_completed_orders; ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="bi bi-check-circle-fill fs-1 icon-box"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4 col-md-6 mb-4">
            <div class="card card-purple-theme h-100 shadow-sm border-0">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col me-2">
                            <div class="text-xs fw-bold text-uppercase mb-1 theme-text-color">
                                পেন্ডিং রিচার্জ রিকোয়েস্ট
                            </div>
                            <div class="h4 mb-0 fw-bold text-dark">
                                <?php echo $pending_recharges; ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="bi bi-clock-history fs-1 icon-box"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<?php
require_once 'footer.php';
?>