<?php
// Menangani preflight request OPTIONS
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    header('Access-Control-Allow-Origin: https://drarnya.itcprogram.com');
    header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Authorization, Content-Type');
    header('Access-Control-Max-Age: 86400');
    http_response_code(200);
    exit;
}

// Header untuk semua response
header('Access-Control-Allow-Origin: https://drarnya.itcprogram.com');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
header('Content-Type: application/json');

// Lanjutkan dengan kode asli...

// Lanjutkan dengan kode login seperti biasa
require_once '../config/database.php';
require_once '../config/master_auth.php';

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

$result = master_login_app($username, $password, 'perawat');
if ($result['success']) {
    $payload = [
        'user_id'  => $result['user']['employee_id'],
        'name'     => $result['user']['nama'],
        'role'     => $result['user']['role'],
        'exp'      => time() + 86400
    ];
    $token = base64_encode(json_encode($payload));
    echo json_encode(['success' => true, 'token' => $token]);
} else {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => $result['message']]);
}