<?php
// admin/index.php
// 1. Ruta de archivos: Subimos un nivel para encontrar config e includes
require '../config/db.php'; 

if (session_status() === PHP_SESSION_NONE) { session_start(); }

// 2. Seguridad: Verificar que sea admin
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../index.php");
    exit;
}

try {
    // Estadísticas Reales
    $total_users = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
    $total_orders = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
    
    // Volumen Total de Ventas
    $total_volume = $pdo->query("SELECT SUM(total_price) FROM orders WHERE status = 'completed'")->fetchColumn() ?? 0;

    // GANANCIAS REALES (Comisión del 10%)
    $platform_earnings = $pdo->query("SELECT SUM(amount) FROM admin_earnings")->fetchColumn() ?? 0;

    // Disputas Abiertas
    $open_disputes = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'dispute'")->fetchColumn();

} catch (PDOException $e) {
    $db_error = $e->getMessage();
    $platform_earnings = 0; $total_users = 0; $total_orders = 0; $open_disputes = 0; $total_volume = 0;
}

include '../includes/header.php';
?>

<style>
    :root {
        --admin-bg: #0d1117;
        --admin-card: #161b22;
        --admin-accent: #00ff88; 
        --admin-border: #30363d;
    }
    body { background-color: var(--admin-bg); color: white; font-family: 'Inter', sans-serif; }
    .admin-container { max-width: 1200px; margin: 40px auto; padding: 0 20px; }

    .admin-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; flex-wrap: wrap; gap: 20px; }
    .admin-title { font-family: 'Syncopate', sans-serif; font-size: 24px; font-weight: 900; margin: 0; letter-spacing: -1px; }
    .admin-title span { color: var(--admin-accent); }

    .admin-nav-top { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
    .btn-nav { background: rgba(255,255,255,0.03); color: #c9d1d9; border: 1px solid var(--admin-border); padding: 12px 20px; border-radius: 12px; text-decoration: none; font-size: 11px; font-weight: 800; transition: 0.3s; display: flex; align-items: center; gap: 10px; text-transform: uppercase; }
    .btn-nav:hover { background: rgba(0, 255, 136, 0.05); border-color: var(--admin-accent); color: white; transform: translateY(-2px); }
    
    .btn-accent { background: var(--admin-accent); color: black !important; border: none; }
    .btn-red { background: rgba(255, 77, 77, 0.05); color: #ff4d4d !important; border: 1px solid rgba(255, 77, 77, 0.2); }
    .btn-red:hover { background: #ff4d4d; color: white !important; border-color: #ff4d4d; }

    .stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; margin-bottom: 40px; }
    .stat-card { background: var(--admin-card); border: 1px solid var(--admin-border); border-radius: 20px; padding: 30px; position: relative; overflow: hidden; transition: 0.3s; }
    
    .stat-label { display: block; color: #8b949e; font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: 1.5px; margin-bottom: 10px; }
    .stat-value { font-family: 'Syncopate', sans-serif; font-size: 22px; font-weight: 800; display: block; }
    .value-accent { color: var(--admin-accent); }
    .stat-card i.bg-icon { position: absolute; right: -10px; bottom: -10px; font-size: 60px; opacity: 0.03; color: white; transform: rotate(-15deg); }

    .admin-tip { background: rgba(0, 255, 136, 0.02); border: 1px solid rgba(0, 255, 136, 0.1); border-radius: 15px; padding: 20px; text-align: center; font-size: 13px; color: #8b949e; margin-bottom: 50px; display: flex; align-items: center; justify-content: center; gap: 15px; }
</style>

<div class="admin-container">
    <div class="admin-header">
        <h1 class="admin-title">VVAULT<span>.CORE</span></h1>
        <div class="admin-nav-top">
            <a href="admin_finances.php" class="btn-nav btn-accent">
                <i class="fa-solid fa-chart-line"></i> Earnings
            </a>
            <a href="admin_servers.php" class="btn-nav">
                <i class="fa-solid fa-server"></i> Servers
            </a>
            <a href="admin/products.php" class="btn-nav">
                <i class="fa-solid fa-box"></i> Products
            </a>
            <a href="admin/categories.php" class="btn-nav">
                <i class="fa-solid fa-list"></i> Categories
            </a>
            <a href="admin/users.php" class="btn-nav">
                <i class="fa-solid fa-users"></i> Users
            </a>
            <a href="admin/disputes.php" class="btn-nav <?php echo ($open_disputes > 0) ? 'btn-red' : ''; ?>">
                <i class="fa-solid fa-gavel"></i> Disputes (<?php echo $open_disputes; ?>)
            </a>
            <a href="admin/addons.php" class="btn-nav">
                <i class="fa-solid fa-plug"></i> Addons
            </a>
        </div>
    </div>

    <div class="stats-grid">
        <div class="stat-card">
            <span class="stat-label">Net Earnings (10%)</span>
            <span class="stat-value value-accent">$<?php echo number_format($platform_earnings, 2); ?></span>
            <i class="fa-solid fa-sack-dollar bg-icon"></i>
        </div>
        <div class="stat-card">
            <span class="stat-label">Total Volume</span>
            <span class="stat-value">$<?php echo number_format($total_volume, 2); ?></span>
            <i class="fa-solid fa-money-bill-transfer bg-icon"></i>
        </div>
        <div class="stat-card">
            <span class="stat-label">Total Orders</span>
            <span class="stat-value"><?php echo $total_orders; ?></span>
            <i class="fa-solid fa-cart-shopping bg-icon"></i>
        </div>
        <div class="stat-card">
            <span class="stat-label">Network Users</span>
            <span class="stat-value"><?php echo $total_users; ?></span>
            <i class="fa-solid fa-user-group bg-icon"></i>
        </div>
    </div>

    <div class="admin-tip">
        <div style="width: 10px; height: 10px; background: var(--admin-accent); border-radius: 50%; box-shadow: 0 0 10px var(--admin-accent);"></div>
        <span>System Status: <strong>Online</strong>. Commission: <strong>10%</strong>.</span>
        <?php if($open_disputes > 0): ?>
            <span style="color: #ff4d4d; margin-left: 20px;">
                <i class="fa-solid fa-triangle-exclamation"></i> Action Required: <?php echo $open_disputes; ?> Disputes.
            </span>
        <?php endif; ?>
    </div>
</div>

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