<?php
session_start();
$role = $_SESSION['role'];
?>

<h1 class="h3 mb-4 text-gray-800">Laporan Data</h1>

<div class="row">

    <!-- Laporan Jemaat -->
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-primary shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-users fa-3x text-primary mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Laporan Jemaat</div>
                <a href="api/export_jemaat.php" class="btn btn-success btn-sm mr-2">
                    <i class="fas fa-file-excel"></i> Export Excel
                </a>
                <a href="api/cetak_jemaat_pdf.php" target="_blank" class="btn btn-danger btn-sm">
                    <i class="fas fa-file-pdf"></i> Cetak PDF
                </a>
            </div>
        </div>
    </div>

    <!-- Laporan Gereja - HANYA ADMIN DPW -->
    <?php if($role == 'admin_dpw'): ?>
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-info shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-church fa-3x text-info mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Laporan Gereja</div>
                <a href="api/export_gereja.php" class="btn btn-success btn-sm mr-2">
                    <i class="fas fa-file-excel"></i> Export Excel
                </a>
                <a href="api/cetak_gereja_pdf.php" target="_blank" class="btn btn-danger btn-sm">
                    <i class="fas fa-file-pdf"></i> Cetak PDF
                </a>
            </div>
        </div>
    <?php endif; ?>

    <!-- Laporan Inventaris -->
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-warning shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-box fa-3x text-warning mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Laporan Inventaris</div>
                <a href="api/export_inventaris.php" class="btn btn-success btn-sm mr-2">
                    <i class="fas fa-file-excel"></i> Export Excel
                </a>
                <a href="api/cetak_inventaris_pdf.php" target="_blank" class="btn btn-danger btn-sm">
                    <i class="fas fa-file-pdf"></i> Cetak PDF
                </a>
            </div>
        </div>
    </div>

    <!-- Cetak Kartu Anggota Jemaat -->
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-success shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-id-card fa-3x text-success mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Kartu Anggota Jemaat</div>
                <button onclick="pilihJemaat('kartu')" class="btn btn-primary btn-sm">
                    <i class="fas fa-print"></i> Cetak Kartu
                </button>
            </div>
        </div>
    </div>

    <!-- Cetak Surat Baptis -->
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-dark shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-water fa-3x text-dark mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Surat Baptis</div>
                <button onclick="pilihJemaat('baptis')" class="btn btn-primary btn-sm">
                    <i class="fas fa-print"></i> Cetak Surat
                </button>
            </div>
        </div>
    </div>

    <!-- Cetak Surat Nikah -->
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-danger shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-ring fa-3x text-danger mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Surat Nikah</div>
                <button onclick="pilihJemaat('nikah')" class="btn btn-primary btn-sm">
                    <i class="fas fa-print"></i> Cetak Surat
                </button>
            </div>
        </div>
    </div>

    <!-- Laporan Keuangan - UDAH DIPINDAH KE SINI -->
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card border-left-success shadow h-100 py-2">
            <div class="card-body text-center">
                <i class="fas fa-money-bill-wave fa-3x text-success mb-3"></i>
                <div class="h5 mb-3 font-weight-bold text-gray-800">Laporan Keuangan</div>
                <a href="pages/laporan_keuangan_excel.php" class="btn btn-success btn-sm mr-2">
                    <i class="fa fa-file-excel"></i> Export Excel
                </a>
                <a href="pages/laporan_keuangan_bendahara.php" class="btn btn-danger btn-sm">
                    <i class="fa fa-file-pdf"></i> Cetak PDF
                </a>
            </div>
        </div>
    </div>

</div> <!-- PENUTUP ROW YANG BENER -->

<!-- Modal Pilih Jemaat -->
<div class="modal fade" id="modalPilihJemaat" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Pilih Jemaat</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <input type="text" id="cariJemaat" class="form-control mb-3" placeholder="Cari nama jemaat...">
                <div id="listJemaat" style="max-height:400px;overflow-y:auto"></div>
            </div>
        </div>
    </div>
</div>

<script>
let jenisCetak = '';

function pilihJemaat(jenis) {
    jenisCetak = jenis;
    $('#modalPilihJemaat').modal('show');
    loadJemaat();
}

function loadJemaat(keyword = '') {
    $.get('api/get_jemaat.php?cari=' + keyword, function(res) {
        let html = '<table class="table table-hover"><thead><tr><th>No</th><th>Nama</th><th>No Anggota</th><th>Aksi</th></tr></thead><tbody>';
        let no = 1;
        res.data.forEach(d => {
            html += `<tr>
                <td>${no++}</td>
                <td>${d.nama_lengkap}</td>
                <td>${d.no_anggota}</td>
                <td><button class="btn btn-sm btn-primary" onclick="cetak('${d.id}')"><i class="fas fa-print"></i> Cetak</button></td>
            </tr>`;
        });
        html += '</tbody></table>';
        $('#listJemaat').html(html);
    });
}

$('#cariJemaat').on('keyup', function() {
    loadJemaat($(this).val());
});

function cetak(id) {
    if(jenisCetak == 'kartu') window.open('api/cetak_kartu_anggota.php?id=' + id, '_blank');
    if(jenisCetak == 'baptis') window.open('api/cetak_surat_baptis.php?id=' + id, '_blank');
    if(jenisCetak == 'nikah') window.open('api/cetak_surat_nikah.php?id=' + id, '_blank');
    $('#modalPilihJemaat').modal('hide');
}
</script>