<?php
include "includes/functions.php";

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

$log_check = $db->select("users", "*", "id = :id", "", [":id" => 1]);
$loggedinuser = !empty($log_check) ? $log_check[0]["username"] : NULL;
if (!empty($loggedinuser) && isset($_SESSION["name"]) && $_SESSION["name"] === $loggedinuser) {
    header("Location: dns.php");
    exit;
}

$data = ["id" => "1", "username" => "admin", "password" => "admin"];
$db->insertIfEmpty("users", $data);

$login_error = "";
if (isset($_POST["login"])) {
    $username = $_POST["username"];
    $userData = $db->select("users", "*", "username = :username", "", [":username" => $username]);
    if ($userData) {
        if ($_POST["password"] === $userData[0]["password"]) {
            session_regenerate_id();
            $_SESSION["loggedin"] = true;
            $_SESSION["name"] = $_POST["username"];
            header($_POST["username"] == "admin" ? "Location: user.php" : "Location: dns.php");
            exit;
        } else {
            $login_error = "Contraseņa incorreta!";
        }
    } else {
        $login_error = "Usuario no encontrado!";
    }
    $db->close();
}
?>
<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rgvip Panels - Login</title>

<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

<style>
:root{
    --form-background: rgba(30,0,0,.7);
    --input-background: rgba(50,0,0,.5);
    --input-border: #660000;
    --primary-red:#ff0000;
    --primary-dark-red:#990000;
    --text-light:#fff;
    --text-dark:#ccc;
    --error-color:#ff0000;
}

*{box-sizing:border-box}

body{
    margin:0;
    font-family:Poppins,sans-serif;
    min-height:100vh;
    display:flex;
    align-items:center;
    justify-content:center;
    background:linear-gradient(135deg,#000,#330000,#000);
    color:var(--text-light);
}

#particles-js{
    position:absolute;
    inset:0;
    z-index:0;
}

.login-container{
    z-index:1;
    width:100%;
    max-width:420px;
    padding:20px;
}

.right-section{
    background:var(--form-background);
    padding:25px;
    border-radius:14px;
    border:1px solid var(--primary-red);
    box-shadow:0 0 15px rgba(255,0,0,.4);
}

.right-section h2{
    text-align:center;
    color:var(--primary-red);
    margin-bottom:10px;
    text-shadow:0 0 8px var(--primary-red);
}

/* LOGO DENTRO DEL FORM */
.login-logo{
    text-align:center;
    margin-bottom:20px;
}
.login-logo img{
    max-width:120px;
    filter:drop-shadow(0 0 10px var(--primary-red));
}

.error-message{
    background:rgba(255,0,0,.1);
    border:1px solid var(--error-color);
    color:var(--error-color);
    padding:10px;
    border-radius:8px;
    text-align:center;
    margin-bottom:15px;
}

.form-group{
    position:relative;
    margin-bottom:15px;
}

.form-group label{
    display:block;
    margin-bottom:6px;
}

.form-control{
    width:100%;
    padding:12px 15px 12px 40px;
    border-radius:8px;
    border:1px solid var(--input-border);
    background:var(--input-background);
    color:var(--text-light);
}

.form-control:focus{
    outline:none;
    border-color:var(--primary-red);
    box-shadow:0 0 0 3px rgba(255,0,0,.2);
}

.input-icon{
    position:absolute;
    left:12px;
    top:40px;
    color:var(--text-dark);
}

.btn-login{
    width:100%;
    padding:12px;
    border:none;
    border-radius:8px;
    background:linear-gradient(to right,var(--primary-dark-red),var(--primary-red));
    color:#fff;
    font-weight:700;
    cursor:pointer;
    box-shadow:0 0 10px rgba(255,0,0,.5);
}
.btn-login:hover{
    transform:translateY(-2px);
}
</style>
</head>

<body>
<div id="particles-js"></div>

<div class="login-container">
    <div class="right-section">
        <h2><i class="fas fa-sign-in-alt"></i> LOGIN</h2>

        <div class="login-logo">
            <img src="./img/logo.png" alt="Logo">
        </div>

        <?php if(!empty($login_error)): ?>
        <div class="error-message"><?= htmlspecialchars($login_error) ?></div>
        <?php endif; ?>

        <form method="post">
            <div class="form-group">
                <label><i class="fas fa-user"></i> Usuario</label>
                <i class="fas fa-user input-icon"></i>
                <input type="text" name="username" class="form-control" required>
            </div>

            <div class="form-group">
                <label><i class="fas fa-lock"></i> Contraseņa</label>
                <i class="fas fa-lock input-icon"></i>
                <input type="password" name="password" class="form-control" required>
            </div>

            <button class="btn-login" name="login">
                <i class="fas fa-sign-in-alt"></i> Login
            </button>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
particlesJS('particles-js',{
    particles:{
        number:{value:60},
        color:{value:'#ff0000'},
        size:{value:3},
        move:{speed:2}
    }
});
</script>
</body>
</html>
