<?php
session_start();

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

require_once("config/conexion.php");

$id_usuario = $_SESSION["usuario_id"];

/* =========================================
USUARIO
========================================= */

$sqlUsuario = "SELECT *
FROM usuarios
WHERE id = :id
LIMIT 1";

$stmtUsuario = $conexion->prepare($sqlUsuario);

$stmtUsuario->bindParam(":id", $id_usuario);

$stmtUsuario->execute();

$usuario = $stmtUsuario->fetch(PDO::FETCH_ASSOC);

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

$sql = "
SELECT

    p.*,

    pr.goles_local AS pronostico_local,
    pr.goles_visitante AS pronostico_visitante,
    pr.puntos AS pronostico_puntos

FROM partidos p

LEFT JOIN pronosticos pr
ON pr.id_partido = p.id
AND pr.id_usuario = :id_usuario

ORDER BY p.fecha ASC
";

$stmt = $conexion->prepare($sql);

$stmt->bindParam(":id_usuario", $id_usuario);

$stmt->execute();

$partidos = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<?php

function bandera($pais){

    $banderas = [

        "México" => "🇲🇽",
        "Sudáfrica" => "🇿🇦",
        "Corea del Sur" => "🇰🇷",
        "República Checa" => "🇨🇿",
        "Canadá" => "🇨🇦",
        "Bosnia y Herzegovina" => "🇧🇦",
        "Catar" => "🇶🇦",
        "Suiza" => "🇨🇭",
        "Brasil" => "🇧🇷",
        "Marruecos" => "🇲🇦",
        "Haití" => "🇭🇹",
        "Escocia" => "🏴",
        "Estados Unidos" => "🇺🇸",
        "Paraguay" => "🇵🇾",
        "Australia" => "🇦🇺",
        "Turquía" => "🇹🇷",
        "Alemania" => "🇩🇪",
        "Curazao" => "🇨🇼",
        "Costa de Marfil" => "🇨🇮",
        "Ecuador" => "🇪🇨",
        "Países Bajos" => "🇳🇱",
        "Japón" => "🇯🇵",
        "Suecia" => "🇸🇪",
        "Túnez" => "🇹🇳",
        "Bélgica" => "🇧🇪",
        "Egipto" => "🇪🇬",
        "Irán" => "🇮🇷",
        "Nueva Zelanda" => "🇳🇿",
        "España" => "🇪🇸",
        "Cabo Verde" => "🇨🇻",
        "Arabia Saudita" => "🇸🇦",
        "Uruguay" => "🇺🇾",
        "Francia" => "🇫🇷",
        "Senegal" => "🇸🇳",
        "Irak" => "🇮🇶",
        "Noruega" => "🇳🇴",
        "Argentina" => "🇦🇷",
        "Argelia" => "🇩🇿",
        "Austria" => "🇦🇹",
        "Jordania" => "🇯🇴",
        "Portugal" => "🇵🇹",
        "RD Congo" => "🇨🇩",
        "Uzbekistán" => "🇺🇿",
        "Colombia" => "🇨🇴",
         "Inglaterra" => "🇬🇧",
        "Croacia" => "🇭🇷",
        "Ghana" => "🇬🇭",
        "Panamá" => "🇵🇦"

    ];

    return $banderas[$pais] ?? "⚽";
}
?>


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

<meta charset="UTF-8">

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

<title>Partidos | 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,.60),

    rgba(0,27,77,.80)

    ),

    url('assets/img/fondo_estadio.jpg');

    background-size:cover;

    background-attachment:fixed;

    font-family:Arial;

    color:white;

    padding-bottom:110px;
}

.container-main{

    max-width:800px;

    margin:auto;

    padding:20px;
}

.logo{

    width:220px;

    display:block;

    margin:20px auto;
}

.title{

    font-size:30px;

    font-weight:bold;

    margin-bottom:25px;
}

.match-card{

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

    border-radius:25px;

    padding:25px;

    margin-bottom:20px;

    backdrop-filter:blur(10px);

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

.match-header{

    display:flex;

    justify-content:space-between;

    align-items:center;

    margin-bottom:15px;
}

.status{

    padding:8px 15px;

    border-radius:50px;

    font-size:13px;

    font-weight:bold;
}

.status-open{

    background:#198754;
}

.status-closed{

    background:#dc3545;
}

.status-final{

    background:#F4C400;

    color:#001B4D;
}

.teams{

    text-align:center;

     font-size:32px;

    font-weight:bold;

    margin-top:15px;
}

.vs{

    color:#F4C400;
}

.date{

    text-align:center;

    color:#ccc;

    margin-top:15px;
}

.result-box{

    margin-top:20px;

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

    border-radius:20px;

    padding:20px;

    text-align:center;
}

.result-title{

    font-size:14px;

    color:#ccc;
}

.result-score{

    font-size:42px;

    font-weight:bold;

    color:#F4C400;
}

.points{

    margin-top:10px;

    font-size:20px;

    font-weight:bold;
}

.btn-pronostico{

    width:100%;

    height:55px;

    border:none;

    border-radius:18px;

    margin-top:20px;

    background:#F4C400;

    color:#001B4D;

    font-size:18px;

    font-weight:bold;
}

.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;
}

</style>

</head>
<body>

<div class="container-main">

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

    <div class="title">
        ⚽ Todos los Partidos
    </div>

    <?php foreach($partidos as $partido){ ?>

        <?php

        $hora_limite = strtotime($partido["fecha"]) - 3600;

        $cerrado = false;

        if(time() >= $hora_limite){

            $cerrado = true;

        }

        ?>

        <div class="match-card">

            <div class="match-header">

                <div>

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

                </div>

                <div>

                    <?php if($partido["estatus"] == "finalizado"){ ?>

                        <span class="status status-final">
                            FINALIZADO
                        </span>

                    <?php } elseif($cerrado){ ?>

                        <span class="status status-closed">
                            CERRADO
                        </span>

                    <?php } else { ?>

                        <span class="status status-open">
                            ABIERTO
                        </span>

                    <?php } ?>

                </div>

            </div>

            <div class="teams">

    <?php echo bandera($partido["equipo_local"]); ?>

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

    <span class="vs">
        VS
    </span>

    <?php echo bandera($partido["equipo_visitante"]); ?>

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

</div>

            <div class="date">

                🕒

                <?php
                echo date(
                    "H:i",
                    strtotime($partido["fecha"])
                );
                ?>

            </div>

            <?php if($partido["estatus"] == "finalizado"){ ?>

                <div class="result-box">

                    <div class="result-title">
                        Resultado Final
                    </div>

                    <div class="result-score">

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

                        -

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

                    </div>

                </div>

            <?php } ?>

            <?php if($partido["pronostico_local"] !== null){ ?>

                <div class="result-box">

                    <div class="result-title">
                        Tu Pronóstico
                    </div>

                    <div class="result-score">

                        <?php echo $partido["pronostico_local"]; ?>

                        -

                        <?php echo $partido["pronostico_visitante"]; ?>

                    </div>

                    <?php if($partido["estatus"] == "finalizado"){ ?>

                        <div class="points">

                            🏆

                            <?php echo $partido["pronostico_puntos"]; ?>

                            pts

                        </div>

                    <?php } ?>

                </div>

            <?php } ?>

            <?php if(!$cerrado && $partido["pronostico_local"] === null){ ?>

                <a
                    href="pronosticos.php?token=<?php echo $partido["token"]; ?>"
                >

                    <button class="btn-pronostico">

                        Hacer Pronóstico

                    </button>

                </a>

            <?php } ?>

        </div>

    <?php } ?>

</div>

<!-- MENU -->

<div class="bottom-menu">

    <a href="dashboard.php">
        <i class="bi bi-house-fill"></i>
        Inicio
    </a>

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

    <a href="ranking.php">
        <i class="bi bi-trophy-fill"></i>
        Ranking
    </a>

    <?php if($usuario["rol"] == "admin"){ ?>

        <a href="admin/index.php">
            <i class="bi bi-shield-lock-fill"></i>
            Admin
        </a>

    <?php } else { ?>

        <a href="perfil.php">
            <i class="bi bi-person-fill"></i>
            Perfil
        </a>

    <?php } ?>

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

</div>

</body>
</html>