<?php
session_start();
require_once '../config/auth.php'; // SETAN 1: INI WAJIB BIAR SESSION KEBACA
header('Content-Type: application/json');
error_reporting(0); // SETAN 2: MATIIN WARNING BIAR JSON BERSIH

cek_role('gembala');

$gereja_id = $_SESSION['gereja_id'];

try {
    $sql = "SELECT p.id, p.jabatan, j.nama_lengkap, j.foto, j.no_hp, g.nama_gereja 
            FROM pelayan p 
            INNER JOIN jemaat j ON p.jemaat_id = j.id 
            INNER JOIN gereja g ON p.gereja_id = g.id 
            WHERE p.gereja_id = ?
            ORDER BY p.id DESC";
    
    $stmt = $conn->prepare($sql);
    $stmt->execute([$gereja_id]);
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode(['data' => $data]);
    
} catch(Exception $e) {
    echo json_encode(['data' => [], 'error' => $e->getMessage()]);
}
?>