<?php
require_once __DIR__ . '/includes/auth.php';

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

$hata = '';

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

    if ($username === '' || $password === '') {
        $hata = 'Kullanıcı adı ve şifre zorunludur.';
    } else {
        $stmt = $pdo->prepare('SELECT id, username, password_hash, full_name, role, is_active FROM users WHERE username = :username AND is_active = 1 LIMIT 1');
        $stmt->execute(['username' => $username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password_hash'])) {
            login_user($user);
            header('Location: index.php');
            exit;
        }

        $hata = 'Kullanıcı adı veya şifre hatalı.';
    }
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>HAVA 75 - Giriş</title>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            min-height: 100vh;
            background: #000;
            color: #00ffff;
            font-family: 'Courier New', Courier, monospace;
            overflow-x: hidden;
        }
        .stars {
            position: fixed;
            inset: 0;
            background: url("https://www.transparenttextures.com/patterns/stardust.png");
            opacity: 0.6;
            z-index: 0;
            pointer-events: none;
        }
        .login-wrap {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 25px;
            position: relative;
            z-index: 1;
            box-sizing: border-box;
        }
        .login-box {
            width: 100%;
            max-width: 430px;
            border: 1px solid #00ffff;
            box-shadow: 0 0 25px rgba(0,255,255,0.35);
            background: rgba(0,0,0,0.85);
            padding: 35px 28px;
            text-align: center;
            box-sizing: border-box;
        }
        h1 {
            margin: 0 0 8px 0;
            letter-spacing: 12px;
            font-size: 34px;
            text-shadow: 0 0 15px #00ffff;
        }
        .subtitle {
            font-size: 12px;
            letter-spacing: 3px;
            opacity: .8;
            margin-bottom: 28px;
        }
        .cyber-input {
            width: 100%;
            box-sizing: border-box;
            padding: 14px;
            margin: 9px 0;
            border: 1px solid #00ffff;
            background: rgba(0,255,255,0.08);
            color: #00ffff;
            outline: none;
            font-family: inherit;
            font-weight: bold;
        }
        .cyber-input::placeholder { color: rgba(0,255,255,0.55); }
        .btn-cyber {
            width: 100%;
            margin-top: 12px;
            padding: 13px 18px;
            border: 1px solid #00ffff;
            background: transparent;
            color: #00ffff;
            cursor: pointer;
            text-transform: uppercase;
            letter-spacing: 2px;
            font-family: inherit;
            font-weight: bold;
        }
        .btn-cyber:hover {
            background: #00ffff;
            color: #000;
            box-shadow: 0 0 20px #00ffff;
        }
        .error {
            border: 1px solid #ff3b3b;
            color: #ff6b6b;
            padding: 10px;
            margin-bottom: 16px;
            font-size: 13px;
            background: rgba(255,0,0,0.08);
        }
        .note {
            margin-top: 18px;
            font-size: 11px;
            opacity: .7;
            line-height: 1.5;
        }
        @media (max-width: 600px) {
            h1 { font-size: 28px; letter-spacing: 8px; }
            .login-box { padding: 30px 20px; }
        }
    </style>
</head>
<body>
<div class="stars"></div>
<div class="login-wrap">
    <form class="login-box" method="post" autocomplete="off">
        <h1>HAVA 75</h1>
        <div class="subtitle">PERSONEL GİRİŞ PANELİ</div>

        <?php if ($hata): ?>
            <div class="error"><?php echo htmlspecialchars($hata, ENT_QUOTES, 'UTF-8'); ?></div>
        <?php endif; ?>

        <input class="cyber-input" type="text" name="username" placeholder="Kullanıcı adı" required autofocus>
        <input class="cyber-input" type="password" name="password" placeholder="Şifre" required>
        <button class="btn-cyber" type="submit">SİSTEME GİR</button>

        <div class="note">Bu alan yalnızca HAVA 75 personeli için hazırlanmıştır.</div>
    </form>
</div>
</body>
</html>
