<?php
include('includes/functions.php');
include('includes/json_utils.php');
$table_name = "user";

$superusername = getmodsbyid('1') ?? 'superadmin';
$superpassword = getmodsbyid('2') ?? 'superadmin';
$weblink = getmodsbyid('3') ?? 'https://t.me/RTXshowcase';
$brandname = getmodsbyid('4') ?? 'RTX Rebrands';

$icon32x32 = getmodsbyid('8') ?? 'img/ficon/favicon-32x32.png';
$icon16x16 = getmodsbyid('9') ?? 'img/ficon/favicon-16x16.png';
$iconaplle = getmodsbyid('10') ?? 'img/ficon/apple-touch-icon.png';
$iconmain = getmodsbyid('11') ?? 'img/ficon/site.webmanifest';


$log_check = $db->select($table_name, '*', 'id = :id', '', [':id' => 1]);
$loggedinuser = !empty($log_check) ? $log_check[0]['username'] : null;

if (!empty($loggedinuser) && isset($_SESSION['name']) && $_SESSION['name'] === $loggedinuser) {
    header("Location: main.php");
    exit;
}

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

if (isset($_POST["login"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];

    // ✅ Check superadmin login first
    if ($username === $superusername && $password === $superpassword) {
        session_regenerate_id();
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $username;
        $_SESSION['passwordxyz'] = $password;
        header('Location: admin.php');
        exit;
    }

    // 🔁 Then check DB user login
    $userData = $db->select($table_name, '*', 'username = :username', '', [':username' => $username]);
    if ($userData && $password === $userData[0]['password']) {
        session_regenerate_id();
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $username;
        $_SESSION['passwordxyz'] = $password;
        if ($username === 'admin') {
            header('Location: user.php');
        } else {
            header('Location: main.php');
        }
        exit;
    } else {
        // ❌ invalid login
        header('Location: ./api/index.php');
        exit;
    }
    $db->close();
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="author" content="<?php echo $brandname; ?>">
  
  <link rel="apple-touch-icon" sizes="180x180" href="<?php echo $iconaplle ?>">
    <link rel="icon" type="image/png" sizes="32x32" href="<?php echo $icon32x32 ?>">
    <link rel="icon" type="image/png" sizes="16x16" href="<?php echo $icon16x16 ?>">
    <link rel="manifest" href="<?php echo $iconmain ?>">
    
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/css.css">
    <title><?php echo $brandname; ?></title>
</head>
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  /* Fondo cinemático azul oscuro */
  background: linear-gradient(135deg, #0A2540 0%, #000000 100%);
}

.form {
  position: relative;
  width: 350px;
  padding: 40px 40px 60px;
  /* Efecto Glassmorphism (Cristal) */
  background: rgba(10, 37, 64, 0.4);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.form h2 {
  color: #ffffff;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 4px;
  margin-bottom: 10px;
}

.form .input {
  text-align: left;
  margin-top: 30px;
}

.form .input .inputBox {
  margin-top: 20px;
}

.form .input .inputBox label {
  display: block;
  color: #b0c4de;
  margin-bottom: 5px;
  font-size: 14px;
  letter-spacing: 1px;
}

.form .input .inputBox input {
  width: 100%;
  height: 50px;
  /* Cajas de texto modernas */
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  outline: none;
  border-radius: 25px;
  padding: 5px 20px;
  color: #fff;
  font-size: 16px;
  transition: all 0.3s ease;
}

.form .input .inputBox input:focus {
  border-color: #007BFF;
  box-shadow: 0 0 10px rgba(0, 123, 255, 0.3);
  background: rgba(0, 0, 0, 0.5);
}

.form .input .inputBox input[type="submit"] {
  margin-top: 20px;
  background: #007BFF;
  border: none;
  color: white;
  font-weight: 600;
  letter-spacing: 2px;
  cursor: pointer;
  box-shadow: 0 5px 15px rgba(0, 123, 255, 0.3);
}

.form .input .inputBox input[type="submit"]:hover {
  background: #0056B3;
  box-shadow: 0 5px 20px rgba(0, 123, 255, 0.6);
  transform: translateY(-2px);
}

.form .input .inputBox input[type="submit"]:active {
  transform: translateY(0);
  background: #004494;
}

.form .input .inputBox input::placeholder {
  color: rgba(255, 255, 255, 0.3);
  font-size: 14px;
}

.forget {
  margin-top: 30px;
  color: #b0c4de;
  font-size: 14px;
}

.forget a {
  color: #00E5FF;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.forget a:hover {
  color: #ffffff;
  text-decoration: underline;
}
  
</style>

<br><br>
<div class="form">
  <h2>YOUTIVI AUTO-DEMO V2</h2>
  <form method="post">
    <div class="input">
      <div class="inputBox">
        <label>Username</label>
        <input type="text" name="username" placeholder="Username"/>
      </div>
      <div class="inputBox">
        <label>Password</label>
        <input type="password" name="password" placeholder="&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;"/>
      </div>
      <div class="inputBox">
            <input type="submit" name="login" value="LOGIN"/>
      </div>
    </div>
  </form>
  <p class="forget"><a href="<?php echo $weblink; ?>"><?php echo $brandname; ?></a></p>
</div>
<br><br>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>