<?= $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 Berita</h3>
        <div class="ml-auto">
            <a href="<?= base_url('Berita/tambah') ?>" class="btn btn-success btn-sm custom-button">
                Tambah Berita
            </a>
        </div>
    </div>
    <!-- /.card-header -->
    <div class="card-body">
        <table id="tb_santri" class="table table-bordered table-striped">
            <thead>
                <tr class="text-center">
                    <th>Gambar</th>
                    <th>Judul Berita</th>
                    <th>Deskripsi Singkat</th>
                    <th>Tanggal</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($berita as $bta) : ?>
                    <tr class="text-center">
                        <td><img src="<?= base_url('assets/img/berita/' . $bta->gambar) ?>" width="100"></td>
                        <td><?= $bta->judul ?></td>
                        <td><?= character_limiter($bta->deskripsi, 50) ?></td>
                        <td><?= date('d/m/Y H:i:s', strtotime($bta->tanggal)) ?></td>
                        <td>
                            <button data-toggle="modal" data-target="#edit<?= $bta->id_berita ?>" class="btn btn-warning btn-sm"><i class="fas fa-edit"></i></button>
                            <!-- <button data-toggle="modal" data-target="#view<?= $bta->id_berita ?>" class="btn btn-success btn-sm"><i class="fas fa-newspaper"></i></button> -->
                            <a href="<?= base_url('Berita/delete/' . $bta->id_berita) ?>" 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 View -->
<?php foreach ($berita as $bta) { ?>
    <div class="modal fade" id="view<?= $bta->id_berita ?>" 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">Detail Berita</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 action="<?= base_url('Berita/view/') ?>" method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="id_berita" value="<?= $bta->id_berita ?>" data-default="<?= $bta->id_berita ?>">
                        <div class="form-group">
                            <label>Gambar</label><br>

                            <!-- Tampilkan gambar lama -->
                            <img src="<?= base_url('assets/img/berita/' . $bta->gambar) ?>" width="150" class="mb-2" alt="Gambar Sebelumnya">

                        </div>
                        <div class="form-group">
                            <label>Judul Berita</label>
                            <textarea name="judul" class="form-control" readonly><?= $bta->judul ?></textarea>
                            <?= form_error('judul', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Deskripsi Singkat</label>
                            <textarea name="deskripsi" class="form-control" readonly><?= $bta->deskripsi ?></textarea>
                            <?= form_error('deskripsi', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Tanggal</label>
                            <input type="datetime-local" name="tanggal" class="form-control" value="<?= $bta->tanggal ?>" readonly>
                            <?= form_error('tanggal', '<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 } ?>

<!-- Modal Edit -->
<?php foreach ($berita as $bta) { ?>
    <div class="modal fade" id="edit<?= $bta->id_berita ?>" 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 Berita</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 action="<?= base_url('Berita/edit/' . $bta->id_berita) ?>" method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="id_berita" value="<?= $bta->id_berita ?>" data-default="<?= $bta->id_berita ?>">
                        <div class="form-group">
                            <label>Gambar</label><br>

                            <!-- Tampilkan gambar lama -->
                            <img src="<?= base_url('assets/img/berita/' . $bta->gambar) ?>" width="150" class="mb-2" alt="Gambar Sebelumnya">

                            <!-- Input file untuk upload gambar baru -->
                            <input type="file" class="form-control" id="gambar" name="gambar">
                        </div>
                        <div class="form-group">
                            <label>Judul Berita</label>
                            <textarea name="judul" class="form-control"><?= $bta->judul ?></textarea>
                            <?= form_error('judul', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Deskripsi Singkat</label>
                            <textarea name="deskripsi" class="form-control"><?= $bta->deskripsi ?></textarea>
                            <?= form_error('deskripsi', '<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 H:i:s', strtotime($bta->tanggal)) ?>">
                            <?= form_error('tanggal', '<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", {
        enableTime: true,
        dateFormat: "d/m/Y H:i:S", // hasil: 2025/06/15 14:30
        time_24hr: true,
        enableSeconds: true
    });
</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
        });
    });
    document.addEventListener("DOMContentLoaded", function() {
        document.querySelectorAll(".reset-btn").forEach(button => {
            button.addEventListener("click", function() {
                var id = this.getAttribute("data-id");
                var form = document.getElementById("formEdit" + id);

                if (form) {
                    form.reset(); // Reset semua input di form

                    // Handle input yang memiliki value dari PHP agar benar-benar kosong
                    form.querySelectorAll("input:not([readonly]), textarea").forEach(input => {
                        input.value = "";
                    });

                    // **Kembalikan nilai id_santri agar tidak kosong!**
                    var idKegiatanSantriInput = form.querySelector("input[name='id_kegiatan']");
                    if (idKegiatanSantriInput) {
                        idKegiatanSantriInput.value = idKegiatanSantriInput.getAttribute("data-default");
                    }
                }
            });
        });
    });
</script>