<?php
session_start();
if (isset($_SESSION['admin'])) {
    header("Location: dashboard.php");
    exit;
}

require_once '../db_conexion.php';
$error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $usuario = trim($_POST['usuario']);
    $clave   = $_POST['password'];

    $stmt = $pdo->prepare("SELECT usuario, password FROM admins WHERE usuario = ?");
    $stmt->execute([$usuario]);
    $fila = $stmt->fetch();

    if ($fila) {
        if (password_verify($clave, $fila['password'])) {
            $_SESSION['admin'] = $fila['usuario'];
            header("Location: dashboard.php");
            exit;
        } else {
            $error = "Contraseña incorrecta.";
        }
    } else {
        $error = "El usuario ingresado no existe.";
    }
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Intranet - Canelita</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Arial, sans-serif; background: #3b1f0f; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
        .box { max-width: 380px; width: 90%; background: white; padding: 36px; border-radius: 12px; box-shadow: 0 8px 32px rgba(0,0,0,0.3); }
        .box h2 { text-align: center; color: #3b1f0f; margin-bottom: 6px; font-size: 1.4rem; }
        .box p  { text-align: center; color: #888; font-size: 0.88rem; margin-bottom: 24px; }
        label { display: block; font-size: 0.85rem; font-weight: 600; color: #555; margin-bottom: 5px; }
        input { width: 100%; padding: 10px 14px; border: 1px solid #ddd; border-radius: 8px; margin-bottom: 16px; font-size: 1rem; }
        input:focus { outline: none; border-color: #c8702a; }
        button { width: 100%; background: #c8702a; color: white; border: none; padding: 12px; border-radius: 8px; font-size: 1rem; font-weight: bold; cursor: pointer; }
        button:hover { background: #a85a1e; }
        .error-msg { background: #fdecea; color: #c0392b; padding: 10px; border-radius: 8px; margin-bottom: 16px; font-size: 0.88rem; text-align: center; }
        .logo { text-align: center; font-size: 2rem; margin-bottom: 8px; }
    </style>
</head>
<body>
    <div class="box">
        <div class="logo">🦆</div>
        <h2>Panel Administrativo</h2>
        <p>Canelita Duck House</p>

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

        <form action="login.php" method="POST">
            <label>Usuario</label>
            <input type="text" name="usuario" placeholder="Ej. admin1" required autofocus />
            <label>Contraseña</label>
            <input type="password" name="password" placeholder="••••••••" required />
            <button type="submit">Iniciar Sesión</button>
        </form>
    </div>
</body>
</html>