<?php
session_start();
header('Content-Type: application/json');
include '../config/database.php';

$db = new Database();
$conn = $db->getConnection();

$gereja_id = $_SESSION['gereja_id'] ?? 0;
$role = $_SESSION['role'] ?? '';

// Kalo admin DPW, liat semua. Kalo gembala, filter gereja_id
if($role == 'admin_dpw'){
    $stmt = $conn->query("SELECT i.*, g.nama_gereja 
                          FROM inventaris i 
                          LEFT JOIN gereja g ON i.gereja_id = g.id 
                          ORDER BY i.id DESC");
} else {
    // INI YANG PENTING: FILTER SESUAI GEREJA_ID GEMBALA
    $stmt = $conn->prepare("SELECT * FROM inventaris WHERE gereja_id = ? ORDER BY id DESC");
    $stmt->execute([$gereja_id]);
}

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(['data' => $data]);
?>