<?php
session_start();

if(!isset($_SESSION["usuario_id"])){
    header("Location: ../login.php");
    exit;
}

if($_SESSION["rol"] != "admin"){
    exit("Acceso denegado");
}

require_once("../config/conexion.php");

/* =========================================
TOTAL USUARIOS
========================================= */

$sqlUsuarios = "SELECT COUNT(*) total
FROM usuarios";

$stmtUsuarios = $conexion->prepare($sqlUsuarios);

$stmtUsuarios->execute();

$totalUsuarios = $stmtUsuarios->fetch(PDO::FETCH_ASSOC);

/* =========================================
TOTAL PARTIDOS
========================================= */

$sqlPartidos = "SELECT COUNT(*) total
FROM partidos";

$stmtPartidos = $conexion->prepare($sqlPartidos);

$stmtPartidos->execute();

$totalPartidos = $stmtPartidos->fetch(PDO::FETCH_ASSOC);

/* =========================================
PARTIDOS FINALIZADOS
========================================= */

$sqlFinalizados = "SELECT COUNT(*) total
FROM partidos
WHERE estatus = 'finalizado'";

$stmtFinalizados = $conexion->prepare($sqlFinalizados);

$stmtFinalizados->execute();

$totalFinalizados = $stmtFinalizados->fetch(PDO::FETCH_ASSOC);

/* =========================================
TOTAL PRONOSTICOS
========================================= */

$sqlPronosticos = "SELECT COUNT(*) total
FROM pronosticos";

$stmtPronosticos = $conexion->prepare($sqlPronosticos);

$stmtPronosticos->execute();

$totalPronosticos = $stmtPronosticos->fetch(PDO::FETCH_ASSOC);

/* =========================================
LIDER ACTUAL
========================================= */

$sqlLider = "SELECT nombre, puntos
FROM usuarios
ORDER BY puntos DESC
LIMIT 1";

$stmtLider = $conexion->prepare($sqlLider);

$stmtLider->execute();

$lider = $stmtLider->fetch(PDO::FETCH_ASSOC);

/* =========================================
ULTIMOS RESULTADOS
========================================= */

$sqlResultados = "SELECT *
FROM partidos
WHERE estatus = 'finalizado'
ORDER BY fecha DESC
LIMIT 5";

$stmtResultados = $conexion->prepare($sqlResultados);

$stmtResultados->execute();

$resultados = $stmtResultados->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html lang="es">
<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Admin | COQUINIELA</title>

<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
>

<link
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css"
rel="stylesheet"
>

<style>

body{

    margin:0;

    background:
    linear-gradient(rgba(0,27,77,.92), rgba(0,27,77,.96)),
    url('../assets/img/fondo_estadio.jpg');

    background-size:cover;

    background-attachment:fixed;

    font-family:Arial;

    color:white;

    padding:20px;

    padding-bottom:110px;
}

.logo{

    width:220px;

    display:block;

    margin:20px auto 30px auto;
}

.card-kpi{

    background:rgba(255,255,255,.08);

    border-radius:25px;

    padding:25px;

    backdrop-filter:blur(10px);

    box-shadow:0 0 25px rgba(0,0,0,.35);

    margin-bottom:20px;

    text-align:center;
}

.kpi-icon{

    font-size:42px;

    color:#F4C400;

    margin-bottom:10px;
}

.kpi-number{

    font-size:38px;

    font-weight:bold;
}

.kpi-title{

    color:#ddd;

    margin-top:5px;
}

.section-title{

    font-size:24px;

    font-weight:bold;

    margin:25px 0 15px 0;
}

.result-card{

    background:rgba(255,255,255,.08);

    border-radius:20px;

    padding:20px;

    margin-bottom:15px;

    backdrop-filter:blur(10px);
}

.result-teams{

    font-size:22px;

    font-weight:bold;
}

.result-score{

    font-size:34px;

    color:#F4C400;

    font-weight:bold;

    margin-top:10px;
}

.result-date{

    color:#ccc;

    margin-top:8px;
}

/* =========================================
BOTTOM MENU
========================================= */

.bottom-menu{

    position:fixed;

    bottom:0;

    left:0;

    width:100%;

    height:75px;

    background:#001B4D;

    display:flex;

    justify-content:space-around;

    align-items:center;

    box-shadow:0 -5px 20px rgba(0,0,0,.4);
}

.bottom-menu a{

    color:white;

    text-decoration:none;

    font-size:13px;

    text-align:center;
}

.bottom-menu i{

    font-size:24px;

    display:block;
}

/* =========================================
RESPONSIVE
========================================= */

@media(min-width:768px){

    .container-custom{

        max-width:900px;

        margin:auto;
    }
}

</style>

</head>
<body>

<div class="container-custom">

    <img
        src="../assets/img/logo.png"
        class="logo"
    >

    <div class="row">

        <div class="col-6">

            <div class="card-kpi">

                <div class="kpi-icon">
                    <i class="bi bi-people-fill"></i>
                </div>

                <div class="kpi-number">
                    <?php echo $totalUsuarios["total"]; ?>
                </div>

                <div class="kpi-title">
                    Usuarios
                </div>

            </div>

        </div>

        <div class="col-6">

            <div class="card-kpi">

                <div class="kpi-icon">
                    <i class="bi bi-calendar-event-fill"></i>
                </div>

                <div class="kpi-number">
                    <?php echo $totalPartidos["total"]; ?>
                </div>

                <div class="kpi-title">
                    Partidos
                </div>

            </div>

        </div>

        <div class="col-6">

            <div class="card-kpi">

                <div class="kpi-icon">
                    <i class="bi bi-check-circle-fill"></i>
                </div>

                <div class="kpi-number">
                    <?php echo $totalFinalizados["total"]; ?>
                </div>

                <div class="kpi-title">
                    Finalizados
                </div>

            </div>

        </div>

        <div class="col-6">

            <div class="card-kpi">

                <div class="kpi-icon">
                    <i class="bi bi-controller"></i>
                </div>

                <div class="kpi-number">
                    <?php echo $totalPronosticos["total"]; ?>
                </div>

                <div class="kpi-title">
                    Pronósticos
                </div>

            </div>

        </div>

    </div>

    <!-- LIDER -->

    <div class="section-title">
        🏆 Líder Actual
    </div>

    <div class="card-kpi">

        <div class="kpi-icon">
            <i class="bi bi-trophy-fill"></i>
        </div>

        <div class="kpi-number">
            <?php echo $lider["puntos"] ?? 0; ?>
        </div>

        <div class="kpi-title">

            <?php echo $lider["nombre"] ?? "Sin datos"; ?>

        </div>

    </div>

    <!-- RESULTADOS -->

    <div class="section-title">
        ⚽ Últimos Resultados
    </div>

    <?php foreach($resultados as $resultado){ ?>

        <div class="result-card">

            <div class="result-teams">

                <?php echo $resultado["equipo_local"]; ?>

                VS

                <?php echo $resultado["equipo_visitante"]; ?>

            </div>

            <div class="result-score">

                <?php echo $resultado["goles_local"]; ?>

                -

                <?php echo $resultado["goles_visitante"]; ?>

            </div>

            <div class="result-date">

                <?php
                echo date(
                    "d/m/Y H:i",
                    strtotime($resultado["fecha"])
                );
                ?>

            </div>

        </div>

    <?php } ?>

</div>

<!-- MENU -->

<div class="bottom-menu">

    <a href="index.php">
        <i class="bi bi-speedometer2"></i>
        Admin
    </a>

    <a href="partidos.php">
        <i class="bi bi-calendar-event-fill"></i>
        Partidos
    </a>

    <a href="resultados.php">
        <i class="bi bi-trophy-fill"></i>
        Resultados
    </a>

    <a href="usuarios.php">
        <i class="bi bi-people-fill"></i>
        Usuarios
    </a>

    <a href="../dashboard.php">
        <i class="bi bi-phone-fill"></i>
        App
    </a>

    <a href="../logout.php">
        <i class="bi bi-box-arrow-right"></i>
        Salir
    </a>

</div>

</body>
</html>