<?php
session_start();
require_once __DIR__ . '/../config.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = isset($_POST['username']) ? cleanInput($_POST['username']) : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    
    if (empty($username) || empty($password)) {
        $error = 'Kullanıcı adı ve şifre boş olamaz!';
    } else {
        try {
            $stmt = $pdo->prepare("SELECT * FROM admin_users WHERE username = ? LIMIT 1");
            $stmt->execute([$username]);
            $user = $stmt->fetch();
            
            if ($user && password_verify($password, $user['password'])) {
                $_SESSION['admin_logged_in'] = true;
                $_SESSION['admin_id'] = $user['id'];
                $_SESSION['admin_username'] = $user['username'];
                $_SESSION['login_time'] = time();
                redirect('index.php');
            } else {
                if ($username === ADMIN_USER && $password === ADMIN_PASS) {
                    $_SESSION['admin_logged_in'] = true;
                    $_SESSION['admin_id'] = 0;
                    $_SESSION['admin_username'] = $username;
                    $_SESSION['login_time'] = time();
                    redirect('index.php');
                }
                $error = 'Kullanıcı adı veya şifre hatalı!';
            }
        } catch(PDOException $e) {
            $error = 'Sistem hatası: ' . $e->getMessage();
        }
    }
}

if (isLoggedIn()) {
    redirect('index.php');
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Girişi - Max Power</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
body {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
}
.login-card {
    border: none;
    border-radius: 15px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-card .card-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 15px 15px 0 0 !important;
    padding: 2rem;
}
.btn-login {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    padding: 12px;
    font-weight: 600;
    border-radius: 8px;
}
.btn-login:hover {
    transform: translateY(-1px);
    box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
.form-control {
    padding: 12px 15px;
    border-radius: 8px;
    border: 1px solid #ddd;
}
.form-control:focus {
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
    border-color: #667eea;
}
</style>
</head>
<body>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="card login-card">
                <div class="card-header text-center text-white">
                    <i class="fas fa-shield-alt fa-3x mb-3"></i>
                    <h3 class="mb-0">Max Power Admin</h3>
                    <p class="mb-0 opacity-75">Sipariş Yönetim Paneli</p>
                </div>
                <div class="card-body p-5">
                    <?php if ($error): ?>
                        <div class="alert alert-danger">
                            <i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?>
                        </div>
                    <?php endif; ?>
                    
                    <form method="POST">
                        <div class="mb-4">
                            <label class="form-label fw-semibold">
                                <i class="fas fa-user me-2"></i>Kullanıcı Adı
                            </label>
                            <input type="text" name="username" class="form-control" required autofocus>
                        </div>
                        <div class="mb-4">
                            <label class="form-label fw-semibold">
                                <i class="fas fa-lock me-2"></i>Şifre
                            </label>
                            <input type="password" name="password" class="form-control" required>
                        </div>
                        <button type="submit" class="btn btn-primary w-100 btn-login text-white">
                            <i class="fas fa-sign-in-alt me-2"></i>Giriş Yap
                        </button>
                    </form>
                    
                    <div class="mt-4 text-center text-muted small">
                        <p class="mb-0">Varsayılan: admin / admin123</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
