<?php
require_once '../includes/config.php';
require_once '../includes/db.php';
require_once '../includes/functions.php';

// Provera da li je korisnik već prijavljen
if (is_admin()) {
    header('Location: /admin/');
    exit;
}

$error = '';

// Obrada forme za prijavu
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    
    // Validacija
    if (empty($email) || empty($password)) {
        $error = 'Molimo unesite email i lozinku.';
    } else {
        // Provera korisnika u bazi
        $user = get_row("SELECT * FROM users WHERE email = ? AND role = 'admin'", [$email]);
        
        if ($user && password_verify($password, $user['password'])) {
            // Uspešna prijava
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['name'];
            $_SESSION['user_email'] = $user['email'];
            $_SESSION['user_role'] = $user['role'];
            
            // Preusmeravanje na dashboard
            header('Location: /admin/');
            exit;
        } else {
            $error = 'Neispravni podaci za prijavu.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="sr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Prijava | Hugong Aparati</title>
    
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    
    <style>
        body {
            background-color: #0077cc;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }
        .login-container {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            width: 100%;
            max-width: 400px;
            padding: 30px;
        }
        .logo-container {
            text-align: center;
            margin-bottom: 30px;
        }
        .logo-container img {
            max-width: 150px;
            margin-bottom: 15px;
        }
        .logo-container h2 {
            color: #0077cc;
            margin: 0;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .btn-primary {
            background-color: #0077cc;
            border-color: #0077cc;
            width: 100%;
            padding: 10px;
        }
        .footer {
            margin-top: 20px;
            text-align: center;
            font-size: 14px;
            color: #6c757d;
        }
        .alert {
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo-container">
            <img src="/assets/images/logo.png" alt="Hugong Aparati">
            <h2>Admin Panel</h2>
        </div>
        
        <h4 class="text-center mb-4">Prijava na administratorski panel</h4>
        
        <?php if (!empty($error)): ?>
        <div class="alert alert-danger">
            <?php echo $error; ?>
        </div>
        <?php endif; ?>
        
        <form action="" method="post">
            <div class="form-group">
                <label for="email">Email adresa</label>
                <input type="email" class="form-control" id="email" name="email" required autofocus>
            </div>
            
            <div class="form-group">
                <label for="password">Lozinka</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            
            <div class="form-group form-check">
                <input type="checkbox" class="form-check-input" id="remember" name="remember">
                <label class="form-check-label" for="remember">Zapamti me</label>
            </div>
            
            <button type="submit" class="btn btn-primary">Prijava</button>
        </form>
        
        <div class="mt-3 text-center">
            <a href="forgot-password.php">Zaboravili ste lozinku?</a>
        </div>
        
        <div class="footer">
            <p>&copy; <?php echo date('Y'); ?> Hugong Aparati. Sva prava zadržana.</p>
            <p><a href="/">Nazad na sajt</a></p>
        </div>
    </div>
</body>
</html>