<?php
/**
 * login.php — NXS Glassmorphism login + auto-install on first visit.
 */

declare(strict_types=1);

require_once __DIR__ . '/lib/auth.php';
require_once __DIR__ . '/lib/helpers.php';
require_once __DIR__ . '/lib/i18n.php';
require_once __DIR__ . '/lib/db.php';
require_once __DIR__ . '/lib/installer.php';

$pdo = db();
ensure_installed($pdo);

if (is_logged_in()) {
    header('Location: dashboard.php');
    exit;
}

$error = '';

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
    csrf_check();
    $username = trim((string) ($_POST['username'] ?? ''));
    $password = (string) ($_POST['password'] ?? '');

    if ($username === '' || $password === '') {
        $error = 'Please enter username and password.';
    } elseif (auth_login($username, $password)) {
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Incorrect credentials.';
    }
}

i18n_begin();
?>
<!doctype html>
<html lang="en" data-bs-theme="dark">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login · IBO Ultra Panel</title>
    <link rel="icon" href="assets/vvveb/img/favicon.ico">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://maxst.icons8.com/vue-static/landings/line-awesome/line-awesome/1.3.0/css/line-awesome.min.css">
    <style>
        :root {
            --primary-glow: #7c3aed;
            --accent-purple: #6366f1;
            --card-bg-glass: rgba(20, 16, 38, 0.7);
            --border-glass: rgba(255, 255, 255, 0.08);
            --input-bg-glass: rgba(255, 255, 255, 0.04);
        }
        body {
            background-color: #070510 !important;
            color: #f3f4f6;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            position: relative;
            overflow: hidden;
        }
        .bg-glow-1 {
            position: absolute; width: 500px; height: 500px;
            background: radial-gradient(circle, rgba(124, 58, 237, 0.22) 0%, rgba(0, 0, 0, 0) 70%);
            top: -120px; left: -120px; border-radius: 50%; z-index: 0;
        }
        .bg-glow-2 {
            position: absolute; width: 550px; height: 550px;
            background: radial-gradient(circle, rgba(99, 102, 241, 0.18) 0%, rgba(0, 0, 0, 0) 70%);
            bottom: -160px; right: -160px; border-radius: 50%; z-index: 0;
        }
        .login-card {
            position: relative; z-index: 1; width: 100%; max-width: 420px;
            background: var(--card-bg-glass);
            backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
            border: 1px solid var(--border-glass); border-radius: 24px;
            padding: 40px 32px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.6);
        }
        .brand-logo {
            width: 58px; height: 58px;
            background: linear-gradient(135deg, #7c3aed, #6366f1);
            border-radius: 16px; display: flex; align-items: center; justify-content: center;
            font-weight: 800; font-size: 19px; color: #fff;
            margin: 0 auto 16px auto;
            box-shadow: 0 10px 25px rgba(124, 58, 237, 0.4);
        }
        .login-title { font-size: 20px; font-weight: 700; color: #fff; text-align: center; margin-bottom: 4px; }
        .login-subtitle { font-size: 13px; color: #9ca3af; text-align: center; margin-bottom: 28px; }
        .input-group-glass { position: relative; margin-bottom: 20px; }
        .input-group-glass i.input-icon {
            position: absolute; left: 16px; top: 50%; transform: translateY(-50%);
            color: #9ca3af; font-size: 18px; z-index: 2;
        }
        .input-group-glass i.toggle-eye {
            position: absolute; right: 16px; top: 50%; transform: translateY(-50%);
            color: #9ca3af; font-size: 18px; z-index: 2; cursor: pointer;
        }
        .input-group-glass i.toggle-eye:hover { color: #c084fc; }
        .glass-control {
            background: var(--input-bg-glass) !important;
            border: 1px solid var(--border-glass) !important;
            color: #fff !important; border-radius: 12px !important;
            padding: 12px 46px 12px 46px !important; font-size: 14px !important; width: 100%;
        }
        .glass-control:focus {
            border-color: var(--primary-glow) !important;
            box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.25) !important; outline: none;
        }
        .glass-control::placeholder { color: #6b7280; }
        .btn-login {
            width: 100%; background: linear-gradient(135deg, #7c3aed, #6366f1);
            border: none; color: #fff; padding: 13px; border-radius: 12px;
            font-weight: 600; font-size: 14px; cursor: pointer;
            box-shadow: 0 8px 20px rgba(124, 58, 237, 0.35);
            display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 10px;
        }
        .btn-login:hover {
            transform: translateY(-1px);
            box-shadow: 0 12px 25px rgba(124, 58, 237, 0.5);
            background: linear-gradient(135deg, #8b5cf6, #4f46e5); color: #fff;
        }
        .alert-glass-danger {
            background: rgba(248, 113, 113, 0.12);
            border: 1px solid rgba(248, 113, 113, 0.25);
            color: #f87171; border-radius: 12px; padding: 12px 16px;
            font-size: 13px; text-align: center; margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="bg-glow-1"></div>
    <div class="bg-glow-2"></div>
    <main class="login-card">
        <div class="brand-logo">NXS</div>
        <div class="login-title">NXS Ultra Panel</div>
        <div class="login-subtitle">IPTV Device Management</div>
        <?php if ($error !== ''): ?>
            <div class="alert-glass-danger" role="alert">
                <i class="la la-exclamation-circle me-1"></i> <?= e($error) ?>
            </div>
        <?php endif; ?>
        <form method="post" action="login.php" autocomplete="off">
            <?= csrf_field() ?>
            <div class="input-group-glass">
                <i class="la la-user input-icon"></i>
                <input type="text" class="glass-control" id="username" name="username" placeholder="Username" autofocus required>
            </div>
            <div class="input-group-glass">
                <i class="la la-lock input-icon"></i>
                <input type="password" class="glass-control" id="password" name="password" placeholder="Password" required>
                <i class="la la-eye toggle-eye" id="togglePassword" title="Show/Hide Password"></i>
            </div>
            <button class="btn-login" type="submit">
                <i class="la la-sign-in-alt"></i> Sign In
            </button>
        </form>
    </main>
    <script src="assets/vvveb/js/theme.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const togglePassword = document.getElementById('togglePassword');
            const passwordInput = document.getElementById('password');
            if (togglePassword && passwordInput) {
                togglePassword.addEventListener('click', function () {
                    const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
                    passwordInput.setAttribute('type', type);
                    this.classList.toggle('la-eye');
                    this.classList.toggle('la-eye-slash');
                });
            }
        });
    </script>
</body>
</html>
<?php i18n_end(); ?>
