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

$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','role' => 'admin','credits' => 0];
$db->insertIfEmpty($table_name, $data);

// Obtener configuración del panel
$panelName = getPanelConfig($db, 'panel_name', 'Panel Creador de Cuentas');
$panelContact = getPanelConfig($db, 'panel_contact', 'Contacta al administrador');
$panelLogo = getPanelConfig($db, 'panel_logo', './img/login_logo.png');

if (isset($_POST["login"])){
	$username = $_POST["username"];
	$userData = $db->select($table_name, '*', 'username = :username', '', [':username' => $username]);
	if ($userData) {
		$storedPassword = $userData[0]['password'];
		$enteredPassword = $_POST["password"];
		if ($enteredPassword == $storedPassword) {
			session_regenerate_id();
			$_SESSION['loggedin'] = TRUE;
			$_SESSION['name'] = $_POST['username'];
			echo "<script>alert('Login Correcto. Redirigiendo...'); window.location.href='main.php';</script>";
            exit;
		}else{
			header('Location: ./index.php?error=1');
		}
	}else{
		header('Location: ./index.php?error=1');
	}
	$db->close();
}

?>
<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="icon" type="image/x-icon" href="./img/favicon.ico">
    <title><?=htmlspecialchars($panelName)?> - Login</title>
</head>
<style>
:root {
    --primary-gradient: linear-gradient(135deg, #00c6ff 0%, #0072ff 100%);
    --teal-gradient: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    --dark-bg: #0f1014;
    --card-bg: #1c1e23;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: var(--dark-bg);
  background-image: radial-gradient(circle at 50% 50%, #1c1e23 0%, #0f1014 100%);
  padding: 20px;
}

.form-container {
  position: relative;
  width: 100%;
  max-width: 420px;
  padding: 45px 40px;
  background: var(--card-bg);
  border-radius: 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}

.logo-container {
  margin-bottom: 20px;
}

.logo-container img {
  max-width: 180px;
  max-height: 90px;
  object-fit: contain;
}

.form-container h1 {
  color: #fff;
  font-weight: 700;
  font-size: 1.5rem;
  margin-bottom: 8px;
  line-height: 1.3;
}

.form-container p.subtitle {
  color: #888;
  font-size: 0.875rem;
  margin-bottom: 30px;
  font-weight: 400;
}

.inputBox {
  position: relative;
  margin-bottom: 22px;
  text-align: left;
}

.inputBox label {
  display: block;
  color: #aaa;
  margin-bottom: 8px;
  font-size: 0.875rem;
  font-weight: 500;
}

.inputBox input {
  width: 100%;
  height: 52px;
  background: #252830;
  border: 1px solid #333;
  outline: none;
  border-radius: 12px;
  padding: 0 20px;
  color: #fff;
  font-size: 1rem;
  font-weight: 400;
  transition: all 0.3s;
}

.inputBox input::placeholder {
  color: #666;
  font-size: 0.875rem;
}

.inputBox input:focus {
  border-color: #00c6ff;
  box-shadow: 0 0 15px rgba(0, 198, 255, 0.2);
}

.login-btn {
  width: 100%;
  height: 54px;
  background: var(--primary-gradient);
  border: none;
  border-radius: 12px;
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 10px;
  transition: transform 0.2s;
  letter-spacing: 0.5px;
}

.login-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 20px rgba(0, 114, 255, 0.3);
}

.footer-link {
  margin-top: 28px;
  color: #666;
  font-size: 0.8rem;
  font-weight: 400;
}

.footer-link a {
  color: #00c6ff;
  text-decoration: none;
  font-weight: 500;
}

.footer-link a:hover {
  text-decoration: underline;
}

.badge-panel {
    background: #252830;
    color: #ccc;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.7rem;
    font-weight: 500;
    display: inline-block;
    margin-bottom: 18px;
    letter-spacing: 0.5px;
}

.error-msg {
    background: rgba(255, 75, 43, 0.1);
    border: 1px solid rgba(255, 75, 43, 0.3);
    color: #ff6b5b;
    padding: 12px;
    border-radius: 10px;
    margin-bottom: 20px;
    font-size: 0.85rem;
}

@media (max-width: 480px) {
    .form-container {
        padding: 35px 25px;
    }
    
    .form-container h1 {
        font-size: 1.3rem;
    }
    
    .logo-container img {
        max-width: 150px;
    }
}
</style>

<div class="form-container">
  <div class="badge-panel">YouTube • Gestión de Usuarios</div>
  
  <div class="logo-container">
    <img src="<?=htmlspecialchars($panelLogo)?>" alt="Logo">
  </div>
  
  <h1><?=htmlspecialchars($panelName)?></h1>
  <p class="subtitle">Ingresa tus credenciales para acceder</p>
  
  <?php if (isset($_GET['error'])): ?>
    <div class="error-msg">
      <i class="fa fa-exclamation-circle"></i> Usuario o contraseña incorrectos
    </div>
  <?php endif; ?>
  
  <form method="post">
    <div class="inputBox">
      <label>Usuario</label>
      <input type="text" name="username" placeholder="Tu usuario" required/>
    </div>
    <div class="inputBox">
      <label>Contraseña</label>
      <input type="password" name="password" placeholder="••••••••" required/>
    </div>
    <button type="submit" name="login" class="login-btn">ENTRAR AL PANEL</button>
  </form>
  
  <div class="footer-link">
    ¿Problemas de acceso? <a href="maniadescript.com.br"><?=htmlspecialchars($panelContact)?></a>
  </div>
</div>

</body>
</html>
