<?php
require_once 'config.php';
require_once 'includes/functions.php';
startSession();

if (isLoggedIn()) {
    header('Location: index.php');
    exit;
}

$error = '';

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

    if (empty($username) || empty($password)) {
        $error = 'Please enter username and password.';
    } else {
        $db = getDB();
        $stmt = $db->prepare("SELECT * FROM users WHERE username = ? OR email = ? LIMIT 1");
        $stmt->execute([$username, $username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id']   = $user['id'];
            $_SESSION['username']  = $user['username'];
            $_SESSION['full_name'] = $user['full_name'];
            $_SESSION['role']      = $user['role'];
            session_regenerate_id(true);
            header('Location: index.php');
            exit;
        } else {
            $error = 'Invalid username or password.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login — <?= APP_NAME ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <script>
        tailwind.config = {
            theme: { extend: { fontFamily: { sans: ['Plus Jakarta Sans', 'sans-serif'] }, colors: { brand: { 500:'#6174f5', 600:'#4f56ea', 700:'#4143d0', 950:'#1e1f52' } } } }
        }
    </script>
    <style>
        body { font-family: 'Plus Jakarta Sans', sans-serif; }
        .login-bg { background: linear-gradient(135deg, #1e1f52 0%, #2f3184 50%, #4143d0 100%); }
        .card-glass { background: rgba(255,255,255,0.97); backdrop-filter: blur(20px); border-radius: 24px; box-shadow: 0 32px 80px rgba(30,31,82,0.3); }
        .form-input { width: 100%; border: 1.5px solid #e2e8f0; border-radius: 10px; padding: 0.7rem 0.875rem 0.7rem 2.5rem; font-size: 0.9rem; font-family: inherit; transition: border-color 0.2s; outline: none; }
        .form-input:focus { border-color: #6174f5; box-shadow: 0 0 0 3px rgba(97,116,245,0.15); }
        .btn-login { background: linear-gradient(135deg, #6174f5, #4143d0); color: white; width: 100%; padding: 0.8rem; border-radius: 12px; font-weight: 700; font-size: 0.9rem; transition: all 0.2s; }
        .btn-login:hover { transform: translateY(-1px); box-shadow: 0 8px 24px rgba(97,116,245,0.45); }
        .dot-pattern { background-image: radial-gradient(circle, rgba(255,255,255,0.12) 1px, transparent 1px); background-size: 28px 28px; }
    </style>
</head>
<body class="login-bg dot-pattern min-h-screen flex items-center justify-center p-4">

    <div class="w-full max-w-sm">
        <!-- Logo -->
        <div class="text-center mb-8">
            <div class="inline-flex items-center gap-3 mb-3">
                <div class="w-12 h-12 bg-white/20 backdrop-blur rounded-2xl flex items-center justify-center shadow-lg">
                    <i class="fas fa-bolt text-white text-xl"></i>
                </div>
                <span class="text-white text-2xl font-bold tracking-tight"><?= APP_NAME ?></span>
            </div>
            <p class="text-blue-200 text-sm">Powered by unizos</p>
        </div>

        <div class="card-glass p-8">
            <h2 class="text-xl font-bold text-slate-800 mb-1">Welcome back</h2>
            <p class="text-slate-400 text-sm mb-6">Sign in to your admin account</p>

            <?php if ($error): ?>
                <div class="bg-red-50 border border-red-200 text-red-700 rounded-xl p-3.5 text-sm mb-5 flex items-center gap-2">
                    <i class="fas fa-exclamation-circle"></i> <?= sanitize($error) ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="">
                <div class="space-y-4">
                    <div>
                        <label class="block text-xs font-semibold text-slate-600 mb-1.5">Username or Email</label>
                        <div class="relative">
                            <i class="fas fa-user absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 text-sm"></i>
                            <input type="text" name="username" class="form-input" placeholder="admin" value="<?= sanitize($_POST['username'] ?? '') ?>" required autocomplete="username">
                        </div>
                    </div>
                    <div>
                        <label class="block text-xs font-semibold text-slate-600 mb-1.5">Password</label>
                        <div class="relative">
                            <i class="fas fa-lock absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 text-sm"></i>
                            <input type="password" name="password" id="password" class="form-input" placeholder="••••••••" required autocomplete="current-password">
                            <button type="button" onclick="togglePwd()" class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 text-sm">
                                <i class="fas fa-eye" id="eyeIcon"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <button type="submit" class="btn-login mt-6">
                    <i class="fas fa-arrow-right-to-bracket mr-2"></i> Sign In
                </button>
            </form>

            <div class="mt-5 pt-5 border-t border-slate-100">
                <p class="text-xs text-slate-400 text-center">
                    Default: <code class="bg-slate-100 px-1.5 py-0.5 rounded font-mono text-xs">admin</code> / <code class="bg-slate-100 px-1.5 py-0.5 rounded font-mono text-xs">Admin2123</code>
                </p>
            </div>
        </div>

        <p class="text-center text-blue-300 text-xs mt-6">© <?= date('Y') ?> <?= APP_NAME ?>. All rights reserved.</p>
    </div>

    <script>
        function togglePwd() {
            const inp = document.getElementById('password');
            const icon = document.getElementById('eyeIcon');
            if (inp.type === 'password') { inp.type = 'text'; icon.className = 'fas fa-eye-slash'; }
            else { inp.type = 'password'; icon.className = 'fas fa-eye'; }
        }
    </script>
</body>
</html>
