<?= $this->session->flashdata('pesan'); ?>
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center w-100">
        <h3 class="card-title mb-1" style="margin-top: 5px;">Halaman Prestasi Santri</h3>
        <div class="ml-auto">
            <a href="<?= base_url('PrestasiSantri/tambah') ?>" class="btn btn-success btn-sm custom-button">
                Tambah Prestasi
            </a>
            <button class="btn btn-info btn-sm custom-button" data-toggle="modal" data-target="#modalFilterPrintPerlombaan">
                <i class="fas fa-print"></i> Print
            </button>
            <button class="btn btn-success btn-sm custom-button" data-toggle="modal" data-target="#modalFilterPdfPerlombaan">
                <i class="fas fa-print"></i> Cetak PDF
            </button>
        </div>
    </div>
    <!-- /.card-header -->
    <?php
    $kelas_diniyah = [
        1 => 'Al-Wadhih Banin',
        2 => 'Al-Wadhih Banat',
        3 => 'Al-Jurūmiyyah Banin',
        4 => 'Al-Jurūmiyyah Banat',
        5 => 'Al-Imriti Banin',
        6 => 'Al-Imriti Banat',
        7 => 'Alfiyah Ula Banin',
        8 => 'Alfiyah Ula Banat',
        9 => 'Alfiyah Tsani Banin',
        10 => 'Alfiyah Tsani Banat',
        11 => 'Alfiyah Tsalits Banin',
        12 => 'Alfiyah Tsalits Banat'
    ];
    ?>
    <div class="card-body">
        <table id="tb_santri" class="table table-bordered table-striped">
            <thead>
                <tr class="text-center">
                    <th>No</th>
                    <th>NIS</th>
                    <th>Nama Santri</th>
                    <th>Kelas Diniah</th>
                    <th>Prestasi</th>
                    <th>Perlombaan</th>
                    <th>Agenda</th>
                    <th>Tanggal</th>
                    <th>Bukti Prestasi</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1;
                foreach ($prestasisantri as $ptistr) : ?>
                    <tr class="text-center">
                        <td><?= $no++ ?></td>
                        <td><?= $ptistr->NIS ?></td>
                        <td><?= $ptistr->nama_santri ?></td>
                        <td><?= $kelas_diniyah[$ptistr->kelas_diniyah] ?? 'Kelas Diniyah Tidak Valid' ?></td>
                        <td><?= $ptistr->prestasi ?></td>
                        <td><?= $ptistr->perlombaan ?></td>
                        <td><?= $ptistr->agenda ?></td>
                        <td><?= date('d/m/Y', strtotime($ptistr->tanggal)) ?></td>
                        <td>
                            <?php if (!empty($ptistr->bukti_prestasi)): ?>
                                <a href="<?= $ptistr->bukti_prestasi ?>" target="_blank" class="btn btn-primary btn-sm">Lihat File</a>
                            <?php else: ?>
                                <span class="text-muted">Tidak Ada File</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <button data-toggle="modal" data-target="#edit<?= $ptistr->id_prestasi_santri ?>" class="btn btn-warning btn-sm"><i class="fas fa-edit"></i></button>
                            <a href="<?= base_url('PrestasiSantri/delete/' . $ptistr->id_prestasi_santri) ?>" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin menghapus data ini?')"><i class="fas fa-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Modal Filter Print Prestasi Santri -->
<div class="modal fade" id="modalFilterPrintPerlombaan" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <form method="GET" action="<?= base_url('PrestasiSantri/print') ?>" target="_blank">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modalLabel">Filter Tanggal Perlombaan</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="start_date">Dari Tanggal</label>
                        <input type="text" id="start_date" name="start_date" class="form-control" placeholder="dd/mm/yyyy" required>
                    </div>
                    <div class="form-group">
                        <label for="end_date">Sampai Tanggal</label>
                        <input type="text" id="end_date" name="end_date" class="form-control" placeholder="dd/mm/yyyy" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <!-- Tombol print semua data tanpa filter -->
                    <a href="<?= base_url('PrestasiSantri/print') ?>" target="_blank" class="btn btn-primary">
                        Print Semua
                    </a>
                    <!-- Tombol filter print berdasarkan tanggal -->
                    <button type="submit" class="btn btn-info">Print Sesuai Filter</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Modal Filter Pdf Prestasi -->
<div class="modal fade" id="modalFilterPdfPerlombaan" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <form method="GET" action="<?= base_url('PrestasiSantri/pdf') ?>" target="_blank">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modalLabel">Filter Tanggal Perlombaan</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="start_date">Dari Tanggal</label>
                        <input type="text" id="start_date" name="start_date" class="form-control" placeholder="dd/mm/yyyy" required>
                    </div>
                    <div class="form-group">
                        <label for="end_date">Sampai Tanggal</label>
                        <input type="text" id="end_date" name="end_date" class="form-control" placeholder="dd/mm/yyyy" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <!-- Tombol print semua data tanpa filter -->
                    <a href="<?= base_url('PrestasiSantri/pdf') ?>" target="_blank" class="btn btn-primary">
                        Cetak PDF Semua
                    </a>
                    <!-- Tombol filter print berdasarkan tanggal -->
                    <button type="submit" class="btn btn-info">Cetak PDF Sesuai Filter</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Modal Edit -->
<?php foreach ($prestasisantri as $ptistr) {
    $list_kelas_diniyah = [
        1 => 'Al-Wadhih Banin',
        2 => 'Al-Wadhih Banat',
        3 => 'Al-Jurūmiyyah Banin',
        4 => 'Al-Jurūmiyyah Banat',
        5 => 'Al-Imriti Banin',
        6 => 'Al-Imriti Banat',
        7 => 'Alfiyah Ula Banin',
        8 => 'Alfiyah Ula Banat',
        9 => 'Alfiyah Tsani Banin',
        10 => 'Alfiyah Tsani Banat',
        11 => 'Alfiyah Tsalits Banin',
        12 => 'Alfiyah Tsalits Banat'
    ];

    $kelas_terpilih = isset($ptistr->kelas_diniyah) ? $ptistr->kelas_diniyah : set_value('kelas_diniyah');
?>
    <div class="modal fade" id="edit<?= $ptistr->id_prestasi_santri ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Edit Prestasi Santri</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="editPrestasiSantriForm<?= $ptistr->id_prestasi_santri ?>" action="<?= base_url('PrestasiSantri/edit/' . $ptistr->id_prestasi_santri) ?>" method="POST">
                        <input type="hidden" name="id_kegiatan" value="<?= $ptistr->id_prestasi_santri ?>" data-default="<?= $ptistr->id_prestasi_santri ?>">
                        <div class="form-group">
                            <label>NIS</label>
                            <input type="text" name="NIS" class="form-control" value="<?= $ptistr->NIS ?>">
                            <?= form_error('NIS', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Nama Santri</label>
                            <input type="text" name="nama_santri" class="form-control" value="<?= $ptistr->nama_santri ?>">
                            <?= form_error('nama_santri', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Kelas Diniyah</label>
                            <select class="custom-select" name="kelas_diniyah">
                                <option value="">Pilih Kelas Diniah</option>
                                <?php foreach ($list_kelas_diniyah as $key => $label): ?>
                                    <option value="<?= $key ?>" <?= ($kelas_terpilih == $key) ? 'selected' : '' ?>>
                                        <?= $label ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <?= form_error('kelas_diniyah', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Prestasi</label>
                            <input type="text" name="prestasi" class="form-control" value="<?= $ptistr->prestasi ?>">
                            <?= form_error('prestasi', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Perlombaan</label>
                            <input type="text" name="perlombaan" class="form-control" value="<?= $ptistr->perlombaan ?>">
                            <?= form_error('Perlombaan', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Agenda</label>
                            <input type="text" name="agenda" class="form-control" value="<?= $ptistr->agenda ?>">
                            <?= form_error('Agenda', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Tanggal</label>
                            <input type="text" id="tanggal" name="tanggal" class="form-control"
                                value="<?= date('d/m/Y', strtotime($ptistr->tanggal)) ?>">
                            <?= form_error('Tanggal', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Bukti Prestasi</label>
                            <input type="text" name="bukti_prestasi" class="form-control" value="<?= $ptistr->bukti_prestasi ?>">
                            <?= form_error('bukti_prestasi', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-save"></i> Simpan </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php } ?>

<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
    flatpickr("#tanggal", {
        dateFormat: "d/m/Y" // Menampilkan dalam format dd/mm/yyyy
    });
    flatpickr("#start_date", {
        dateFormat: "d/m/Y" // Menampilkan dalam format dd/mm/yyyy
    });
    flatpickr("#end_date", {
        dateFormat: "d/m/Y" // Menampilkan dalam format dd/mm/yyyy
    });
</script>

<style>
    /* CSS untuk mengubah background dan border <th> dan <td> */
    /* Ubah warna border tabel menjadi hijau */
    table.table-bordered {
        border: 2px solid green;
        /* Border luar tabel */
    }

    /* Ubah warna background dan border header tabel */
    table.table-bordered th {
        background-color: #e0f7e0;
        /* Hijau muda untuk header */
        border: 1px solid green;
        /* Warna border hijau */
        color: #333;
        /* Warna teks */
    }

    /* Ubah warna background dan border sel tabel */
    table.table-bordered td {
        background-color: #f0fff0;
        /* Hijau sangat muda untuk sel */
        border: 1px solid green;
        /* Warna border hijau */
        color: #333;
        /* Warna teks */
    }

    .form-control:not([size]):not([multiple]) {
        background-image: none !important;
        /* Menghilangkan panah bawaan Bootstrap */
    }

    .custom-button {
        font-family: inherit;
        /* Mengikuti font dari parent */
        font-size: 0.9rem;
        /* Sesuaikan dengan font pada h3 */
        font-weight: bold;
        /* Jika judul pakai bold, tombol juga dibuat bold */
        text-transform: capitalize;
        /* Mengikuti format teks di judul */
        padding: 6px 13px;
        /* Sesuaikan padding agar proporsional */
    }

    .custom-btn {
        font-family: inherit;
        /* Mengikuti font dari parent */
        font-size: 0.9rem;
        /* Sesuaikan dengan font pada h3 */
        font-weight: bold;
        /* Jika judul pakai bold, tombol juga dibuat bold */
        text-transform: capitalize;
        /* Mengikuti format teks di judul */
        padding: 4px 10px;
        /* Sesuaikan padding agar proporsional */
    }

    /* Override panah sorting ganda */
    /* Hapus panah sorting bawaan template atau Bootstrap */
    table.dataTable thead .sorting:after,
    table.dataTable thead .sorting_asc:after,
    table.dataTable thead .sorting_desc:after {
        display: none !important;
    }
</style>

<script>
    $(document).ready(function() {
        $('#tb_santri').DataTable({
            "paging": true, // Enable pagination
            "lengthChange": true, // Allow user to change number of rows shown
            "searching": true, // Enable search
            "ordering": true, // Enable column sorting
            "info": true, // Show table info
            "autoWidth": false, // Disable auto column width adjustment
        });
    });
</script>