<?php
require '../includes/db.php';
require 'includes/admin_auth.php';

// Fetch stats
$total_users = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$total_payin = $pdo->query("SELECT SUM(amount) FROM transactions WHERE type = 'payin' AND status = 'success'")->fetchColumn();
$total_payout = $pdo->query("SELECT SUM(amount) FROM withdrawal_requests WHERE status = 'success'")->fetchColumn();

?>

<!DOCTYPE html>
<html>
<head>
    <title>Portal - KiliCowPay</title>
    <meta charset="UTF-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            background: #f4f7fa;
            font-family: 'Segoe UI', sans-serif;
        }
        .sidebar {
            height: 100vh;
            background-color: #343a40;
            padding: 20px;
            color: #fff;
        }
        .sidebar h4 {
            margin-bottom: 30px;
            color: #ffc107;
        }
        .sidebar a {
            color: #ccc;
            display: block;
            padding: 10px 0;
            text-decoration: none;
        }
        .sidebar a:hover {
            color: #fff;
            background: #495057;
            border-radius: 5px;
            padding-left: 10px;
        }
        .main-content {
            padding: 30px;
        }
        .card {
            border: none;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        }
        .dashboard-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .logout-btn {
            background: #dc3545;
            border: none;
            color: white;
            padding: 8px 15px;
            border-radius: 5px;
        }
        .logout-btn:hover {
            background: #c82333;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 sidebar">
                <h4><i class="bi bi-speedometer2"></i> Admin Panel</h4>
                <a href="users.php"><i class="bi bi-people-fill"></i> Users</a>
                <a href="transactions.php"><i class="bi bi-cash-stack"></i> Pay-ins</a>
                <a href="payouts.php"><i class="bi bi-arrow-down-circle"></i> Payouts</a>
                <a href="manual_payin.php" class="nav-link"><i class="bi bi-person-plus"></i> Manual Pay-In</a>
                <a href="#"><i class="bi bi-credit-card"></i> Merchant Keys</a>
                <a href="support_admin.php"><i class="bi bi-credit-card"></i>Support</a>
                <a href="logout.php"><i class="bi bi-box-arrow-left"></i> Logout</a>
            </div>

            <!-- Main Content -->
            <div class="col-md-9 main-content">
                <div class="dashboard-header mb-4">
                    <h2>Welcome, Admin</h2>
                    <a href="logout.php" class="logout-btn">Logout</a>
                </div>

                <div class="row g-4">
                    <div class="col-md-4">
                        <div class="card bg-primary text-white p-3">
                            <div class="card-body">
                                <h5 class="card-title"><i class="bi bi-people-fill"></i> Total Users</h5>
                                <p class="fs-3 fw-bold"><?= $total_users ?></p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card bg-success text-white p-3">
                            <div class="card-body">
                                <h5 class="card-title"><i class="bi bi-cash-stack"></i> Total Pay-Ins</h5>
                                <p hidden class="fs-3 fw-bold">₹<?= number_format($total_payin, 2) ?></p>
                                <p class="fs-3 fw-bold">₹<?= number_format($total_payin ?? 0, 2) ?></p>

                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card bg-warning text-dark p-3">
                            <div class="card-body">
                                <h5 class="card-title"><i class="bi bi-arrow-down-circle"></i> Total Payouts</h5>
                                <p hidden class="fs-3 fw-bold">₹<?= number_format($total_payout, 2) ?></p>
                                <p class="fs-3 fw-bold">₹<?= number_format($total_payout ?? 0, 2) ?></p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- You can add recent transactions or other content here -->

            </div>
        </div>
    </div>
</body>
</html>
