<?php
session_start();
include '../config/database.php';
$db = new Database(); $conn = $db->getConnection();

$role = $_SESSION['role'];
$where = "";
$nama_gereja = "SEMUA GEREJA";
if($role == 'data_gembala'){
    $where = "WHERE i.gereja_id = ".$_SESSION['gereja_id'];
    $nama_gereja = $_SESSION['nama_gereja'];
}

$stmt = $conn->prepare("SELECT i.*, g.nama_gereja FROM inventaris i 
                        LEFT JOIN gereja g ON i.gereja_id=g.id $where ORDER BY i.nama_barang ASC");
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

$filename = "Laporan_Inventaris_".date('Ymd').".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
?>
<table border="1">
    <tr>
        <td colspan="7" align="center" style="font-size:16px;font-weight:bold">
            LAPORAN INVENTARIS GEREJA<br>
            DEWAN PENGURUS WILAYAH GBI KABUPATEN KEEROM<br>
            <?php echo $nama_gereja; ?>
        </td>
    </tr>
    <tr><td colspan="7">&nbsp;</td></tr>
    <tr style="background:#CCCCCC;font-weight:bold">
        <th>No</th><th>Nama Barang</th><th>Kategori</th><th>Jumlah</th><th>Kondisi</th><th>Tgl Beli</th><th>Gereja</th>
    </tr>
    <?php $no=1; foreach($data as $d): ?>
    <tr>
        <td align="center"><?php echo $no++; ?></td>
        <td><?php echo $d['nama_barang']; ?></td>
        <td><?php echo $d['kategori']; ?></td>
        <td align="center"><?php echo $d['jumlah']; ?></td>
        <td><?php echo $d['kondisi']; ?></td>
        <td><?php echo $d['tgl_beli'] ? date('d-m-Y', strtotime($d['tgl_beli'])) : '-'; ?></td>
        <td><?php echo $d['nama_gereja']; ?></td>
    </tr>
    <?php endforeach; ?>
    <tr><td colspan="7">&nbsp;</td></tr>
    <tr><td colspan="7">&nbsp;</td></tr>
    <tr>
        <td colspan="4">&nbsp;</td>
        <td colspan="3" align="center">
            Keerom, <?php echo date('d F Y'); ?><br>
            <?php if($role=='gembala'): ?>
            Gembala,<br><br><br><br><b><u><?php echo $_SESSION['nama']; ?></u></b>
            <?php else: ?>
            Ketua DPW GBI Kab. Keerom,<br><br><br><br><b><u>Pdt. [Nama Ketua DPW]</u></b>
            <?php endif; ?>
        </td>
    </tr>
</table>