<?php
session_start();

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

require_once("config/conexion.php");

$id_usuario = $_SESSION["usuario_id"];

/* =========================================
OBTENER 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);

if(!$usuario){

    header("Location: logout.php");

    exit;
}

/* =========================================
CAMBIAR PASSWORD
========================================= */

if(isset($_POST["cambiar_password"])){

    $password = password_hash(
        $_POST["password"],
        PASSWORD_DEFAULT
    );

    $sqlUpdate = "UPDATE usuarios
    SET password = :password
    WHERE id = :id";

    $stmtUpdate = $conexion->prepare($sqlUpdate);

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

    $stmtUpdate->execute();

    header("Location: perfil.php?pass=1");

    exit;
}

/* =========================================
SUBIR FOTO
========================================= */

if(isset($_POST["subir_foto"])){

    if(isset($_FILES["foto"]) && $_FILES["foto"]["name"] != ""){

        $extension = pathinfo(
            $_FILES["foto"]["name"],
            PATHINFO_EXTENSION
        );

        $foto = md5(
            uniqid().time()
        ).".".$extension;

        move_uploaded_file(
            $_FILES["foto"]["tmp_name"],
            "assets/img/usuarios/".$foto
        );

        $sqlFoto = "UPDATE usuarios
        SET foto = :foto
        WHERE id = :id";

        $stmtFoto = $conexion->prepare($sqlFoto);

        $stmtFoto->bindParam(":foto", $foto);

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

        $stmtFoto->execute();

    }

    header("Location: perfil.php?foto=1");

    exit;
}

/* =========================================
FOTO
========================================= */

$foto = "assets/img/usuarios/default.png";

if($usuario["foto"] != ""){

    $foto = "assets/img/usuarios/".$usuario["foto"];

}

?>

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

<meta charset="UTF-8">

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

<title>Perfil | 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,.55),

    rgba(0,27,77,.72)

    ),

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

    background-size:cover;

    background-attachment:fixed;

    font-family:Arial;

    color:white;

    padding-bottom:100px;
}

.container-main{

    max-width:700px;

    margin:auto;

    padding:20px;
}

.logo{

    width:220px;

    display:block;

    margin:20px auto;
}

.card-main{

    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,.4);
}

.profile-photo{

    width:130px;

    height:130px;

    border-radius:50%;

    object-fit:cover;

    border:5px solid #F4C400;

    display:block;

    margin:auto;

    margin-bottom:20px;
}

.user-name{

    text-align:center;

    font-size:28px;

    font-weight:bold;
}

.user-info{

    text-align:center;

    color:#ccc;

    margin-top:10px;
}

.user-points{

    text-align:center;

    color:#F4C400;

    font-size:40px;

    font-weight:bold;

    margin-top:15px;
}

.form-control{

    height:55px;

    border:none;
}

.btn-save{

    width:100%;

    height:55px;

    border:none;

    border-radius:15px;

    background:#F4C400;

    color:#001B4D;

    font-weight:bold;

    font-size:18px;
}

.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"
    >

    <!-- PERFIL -->

    <div class="card-main">

        <img
            src="<?php echo $foto; ?>"
            class="profile-photo"
        >

        <div class="user-name">

            <?php echo $usuario["nombre"]; ?>

        </div>

        <div class="user-info">

            @<?php echo $usuario["usuario"]; ?>

            <br>

            💰 Pagó:
            $<?php echo number_format($usuario["pagado"],2); ?>

        </div>

        <div class="user-points">

            <?php echo $usuario["puntos"]; ?>

            pts

        </div>

    </div>

    <!-- FOTO -->

    <div class="card-main">

        <h4 class="mb-4">
            📸 Cambiar Foto
        </h4>

        <?php if(isset($_GET["foto"])){ ?>

            <div class="alert alert-success">
                Foto actualizada correctamente
            </div>

        <?php } ?>

        <form
            method="POST"
            enctype="multipart/form-data"
        >

            <input
                type="file"
                name="foto"
                class="form-control mb-3"
                accept="image/*"
                required
            >

            <button
                class="btn-save"
                name="subir_foto"
            >

                Subir Foto

            </button>

        </form>

    </div>

    <!-- PASSWORD -->

    <div class="card-main">

        <h4 class="mb-4">
            🔒 Cambiar Contraseña
        </h4>

        <?php if(isset($_GET["pass"])){ ?>

            <div class="alert alert-success">
                Contraseña actualizada
            </div>

        <?php } ?>

        <form method="POST">

            <input
                type="password"
                name="password"
                class="form-control mb-3"
                placeholder="Nueva contraseña"
                required
            >

            <button
                class="btn-save"
                name="cambiar_password"
            >

                Guardar Contraseña

            </button>

        </form>

    </div>

</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>

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

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

</div>

</body>
</html>