<?php
// api/employees.php
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    header('Access-Control-Allow-Origin: https://drarnya.itcprogram.com');
    header('Access-Control-Allow-Methods: GET, OPTIONS');
    header('Access-Control-Allow-Headers: Authorization, Content-Type');
    http_response_code(200);
    exit;
}
header('Access-Control-Allow-Origin: https://drarnya.itcprogram.com');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
header('Content-Type: application/json');

require_once '../config/database.php';
require_once 'auth.php';

$user = validateToken();
if (!$user) {
    http_response_code(401);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

$conn = getConnection();
$action = $_GET['action'] ?? '';

if ($action === 'list_active') {
    $sql = "SELECT e.*, u.name as unit_name 
            FROM employees e 
            LEFT JOIN units u ON e.unit_id = u.id 
            WHERE e.employment_status = 'aktif' 
            ORDER BY e.full_name";
    $result = $conn->query($sql);
    $data = [];
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
    echo json_encode($data);
} else {
    http_response_code(400);
    echo json_encode(['error' => 'Action tidak dikenal']);
}