<?php
session_start();
if (empty($_SESSION['SESSION_USER']) && empty($_SESSION['SESSION_ID'])) {
    header('location:../../login/');
    exit;
} else {
    require_once '../../../library/sw-config.php';
    require_once '../../login/login_session.php';
    include('../../../library/sw-function.php');

    switch (@$_GET['action']) {

        /* -------  LOAD DATA IBADAH PER ORANG ----------*/
        case 'ibadah':
            $error = [];

            // Validasi nama jamaah
            if (empty($_GET['nama'])) {
                $error[] = 'Nama tidak boleh kosong';
            } else {
                $nama = mysqli_real_escape_string($connection, $_GET['nama']);
            }

            // Ambil bulan & tahun (default: bulan & tahun saat ini)
            if (!empty($_POST['month']) && !empty($_POST['year'])) {
                $bulan = $_POST['month'];
                $tahun = $_POST['year'];
            } else {
                $bulan = date("m");
                $tahun = date("Y");
            }

            if (empty($error)) {
                echo '
                <div class="table-responsive">
                <table class="table table-bordered table-hover" id="swdatatable">
                    <thead>
                        <tr>
                            <th class="align-middle" width="20">No</th>
                            <th class="align-middle">Tanggal</th>
                            <th class="align-middle">Shalat Wajib</th>
                            <th class="align-middle">Shalat Sunnah</th>
                            <th class="align-middle">Kebiasaan Baik</th>
                            <th class="align-middle">Dzikir</th>
                            <th class="align-middle">Tilawah</th>
                            <th class="align-middle">Catatan</th>
                        </tr>
                    </thead>
                    <tbody>';

                // Query ambil data ibadah per nama dan periode
                $query = "
                    SELECT * FROM ibadah 
                    WHERE nama='$nama' 
                    AND MONTH(tanggal)='$bulan' 
                    AND YEAR(tanggal)='$tahun'
                    ORDER BY tanggal ASC";
                $result = $connection->query($query);

                if ($result && $result->num_rows > 0) {
                    $no = 1;
                    while ($row = $result->fetch_assoc()) {
                        // Decode JSON kolom ibadah
                        $shalat_wajib   = json_decode($row['shalat_wajib'], true);
                        $shalat_sunnah  = json_decode($row['shalat_sunnah'], true);
                        $kebiasaan_baik = json_decode($row['kebiasaan_baik'], true);

                        $wajib = '';
                        if (!empty($shalat_wajib)) {
                            foreach ($shalat_wajib as $sholat => $status) {
                                $wajib .= ucfirst($sholat) . ': ' . $status . '<br>';
                            }
                        } else {
                            $wajib = '-';
                        }

                        $sunnah = !empty($shalat_sunnah) ? implode(', ', $shalat_sunnah) : '-';
                        $baik   = !empty($kebiasaan_baik) ? implode(', ', $kebiasaan_baik) : '-';

                        echo '
                        <tr>
                            <td class="text-center">' . $no++ . '</td>
                            <td>' . tgl_indo($row['tanggal']) . '</td>
                            <td>' . $wajib . '</td>
                            <td>' . $sunnah . '</td>
                            <td>' . $baik . '</td>
                            <td>' . htmlspecialchars($row['dzikir']) . '</td>
                            <td>' . htmlspecialchars($row['tilawah']) . '</td>
                            <td>' . nl2br(htmlspecialchars($row['catatan'])) . '</td>
                        </tr>';
                    }
                } else {
                    echo '<tr><td colspan="8" class="text-center">Tidak ada data ibadah untuk periode ini</td></tr>';
                }

                echo '
                    </tbody>
                </table>
                </div>';

                // Statistik Ibadah (ringkasan sederhana)
                $totalData = $connection->query("
                    SELECT COUNT(*) as jml FROM ibadah 
                    WHERE nama='$nama' AND MONTH(tanggal)='$bulan' AND YEAR(tanggal)='$tahun'
                ")->fetch_assoc()['jml'];

                echo '
                <hr>
                <div class="text-center">
                    <p><b>Total Hari Ibadah Tercatat:</b> <span class="badge bg-blue">' . (int)$totalData . '</span></p>
                </div>';

                // Script DataTable
                echo '
                <script>
                    $("#swdatatable").dataTable({
                        "iDisplayLength": 31,
                        "aLengthMenu": [[31, 50, -1], [31, 50, "Semua"]],
                        "language": {
                            "lengthMenu": "Tampilkan _MENU_ data per halaman",
                            "zeroRecords": "Tidak ada catatan ibadah ditemukan",
                            "info": "Menampilkan _START_ - _END_ dari _TOTAL_ data",
                            "infoEmpty": "Tidak ada data",
                            "infoFiltered": "(disaring dari _MAX_ total data)",
                            "search": "Cari:",
                            "paginate": { "previous": "Sebelumnya", "next": "Berikutnya" }
                        }
                    });
                </script>';
            } else {
                echo '<div class="alert alert-danger">Kesalahan: ' . implode(", ", $error) . '</div>';
            }
        break;
    }
}
?>
