<?php
// File: api/login_ajax.php
require_once '../config/auth.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Method not allowed']);
    exit;
}

$data = json_decode(file_get_contents('php://input'), true);
$username = $data['username'] ?? '';
$password = $data['password'] ?? '';

if (login($username, $password)) {
    echo json_encode([
        'success' => true,
        'message' => 'Login berhasil',
        'is_admin' => ($_SESSION['user_level'] === 'admin')
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Username atau password salah!'
    ]);
}
?>