<?= $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 Data Wali Santri</h3>
        <!-- <div class="ml-auto">
            <a href="<?= base_url('santri/print') ?>" class="btn btn-info btn-sm custom-button"><i class="fas fa-print"></i> Print </a>
            <a href="<?= base_url('santri/pdf') ?>" class="btn btn-success btn-sm custom-button"><i class="fas fa-file"></i> Cetak PDF </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>No</th>
                    <th>Nama Wali Santri</th>
                    <th>Nama Santri</th>
                    <th>No Telepon</th>
                    <th>Alamat</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1;
                foreach ($santri as $str) : ?>
                    <tr class="text-center">
                        <td><?= $no++ ?></td>
                        <td><?= $str->nama_wali_santri ?></td>
                        <td><?= $str->nama_santri ?></td>
                        <td><?= $str->no_telepon ?></td>
                        <td><?= $str->alamat ?></td>
                        <td>
                            <button data-toggle="modal" data-target="#view<?= $str->id_santri ?>" class="btn btn-success btn-sm"><i class="fas fa-user"></i></button>
                            <button data-toggle="modal" data-target="#edit<?= $str->id_santri ?>" class="btn btn-warning btn-sm"><i class="fas fa-edit"></i></button>
                            <!-- <a href="<?= base_url('WaliSantri/delete/' . $str->id_santri) ?>" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin menghapus data ini?')"><i class="fas fa-trash"></i></a> -->
                            <a href="<?= base_url('walisantri/chatadmin/' . $str->id_wali_santri) ?>" class="btn btn-sm btn-info"><i class="fas fa-message"></i></a>
                        </td>
                    </tr>
                <?php endforeach ?>
            </tbody>
        </table>
    </div>
</div>

<?php foreach ($santri as $str) { ?>
    <!-- Modal View-->
    <div class="modal fade" id="view<?= $str->id_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">Detail Wali 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 action="<?= base_url('WaliSantri/view') ?>" method="POST">
                        <!-- Tambahkan input hidden untuk id_santri -->
                        <div class="form-group">
                            <label>Email</label>
                            <input type="text" name="email" class="form-control" value="<?= $str->email ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>Password</label>
                            <input type="text" name="password" class="form-control" value="<?= $str->password ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>Nama Wali Santri</label>
                            <input type="text" name="nama_wali_santri" class="form-control" value="<?= $str->nama_wali_santri ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>Nama Santri</label>
                            <input type="text" name="nama_santri" class="form-control" value="<?= $str->nama_santri ?>" readonly>
                        </div>
                        <!-- <div class="form-group">
                            <label>NIS</label>
                            <input type="text" name="NIS" class="form-control" value="<?= $str->NIS ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>No KTP</label>
                            <input type="text" name="no_ktp" class="form-control" value="<?= $str->no_ktp ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>Tempat Lahir</label>
                            <input type="text" name="tempat_lahir" class="form-control" value="<?= $str->tempat_lahir ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>Tanggal Lahir</label>
                            <input type="date" name="tanggal_lahir" class="form-control" value="<?= $str->tanggal_lahir ?>" readonly>
                        </div> -->
                        <div class="form-group">
                            <label>No Telepon</label>
                            <input type="text" name="no_telepon" class="form-control" value="<?= $str->no_telepon ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>Alamat</label>
                            <textarea name="alamat" class="form-control" readonly><?= $str->alamat ?></textarea>
                        </div>
                        <div class="form-group">
                            <label>Pekerjaan</label>
                            <input type="text" name="pekerjaan" class="form-control" value="<?= $str->pekerjaan ?>" readonly>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-danger" data-dismiss="modal">Tutup</button>
                        </div>
                    </form>

                    <!-- <script>
                        $(document).ready(function() {
                            $('.datepicker').datepicker({
                                format: 'yyyy-mm-dd',
                                autoclose: true,
                                todayHighlight: true
                            });
                        });
                    </script> -->
                </div>
            </div>
        </div>
    </div>
<?php } ?>


<!-- Modal Edit-->
<?php foreach ($santri as $str) { ?>
    <div class="modal fade" id="edit<?= $str->id_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 Wali 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="formEdit<?= $str->id_santri ?>" action="<?= base_url('walisantri/edit/' . $str->id_wali_santri) ?>" method="POST">
                        <?php if (is_object($str)) : ?>
                            <input type="hidden" name="id_santri" value="<?= $str->id_santri ?>" data-default="<?= $str->id_santri ?>">
                        <?php endif; ?>
                        <div class="form-group">
                            <label>Email</label>
                            <input type="text" name="email" class="form-control form-control-user" value="<?= $str->email ?>">
                            <?= form_error('email', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Password</label>
                            <input type="text" name="password" class="form-control" value="<?= $str->password ?>">
                            <?= form_error('password', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Nama Wali Santri</label>
                            <input type="text" name="nama_wali_santri" class="form-control" value="<?= $str->nama_wali_santri ?>">
                            <?= form_error('nama_wali_santri', '<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="<?= $str->nama_santri ?>" readonly>
                        </div>
                        <!-- <div class="form-group">
                            <label>NIS</label>
                            <input type="text" name="NIS" class="form-control" value="<?= $str->NIS ?>" readonly>
                        </div>
                        <div class="form-group">
                            <label>No KTP</label>
                            <input type="text" name="no_ktp" class="form-control" value="<?= $str->no_ktp ?>">
                            <?= form_error('no_ktp', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Tempat Lahir</label>
                            <input type="text" name="tempat_lahir" class="form-control" value="<?= $str->tempat_lahir ?>">
                            <?= form_error('tempat_lahir', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Tanggal Lahir</label>
                            <input type="date" name="tanggal_lahir" class="form-control" value="<?= $str->tanggal_lahir ?>">
                            <?= form_error('tanggal_lahir', '<div class="text-small text-danger">', '</div>'); ?>
                        </div> -->
                        <div class="form-group">
                            <label>No Telepon</label>
                            <input type="text" name="no_telepon" class="form-control" value="<?= $str->no_telepon ?>">
                            <?= form_error('no_telepon', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Alamat</label>
                            <textarea name="alamat" class="form-control"><?= $str->alamat ?></textarea>
                            <?= form_error('alamat', '<div class="text-small text-danger">', '</div>'); ?>
                        </div>
                        <div class="form-group">
                            <label>Pekerjaan</label>
                            <input type="text" name="pekerjaan" class="form-control" value="<?= $str->pekerjaan ?>">
                            <?= form_error('pekerjaan', '<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 } ?>

<!-- CSS untuk mengubah background dan border <th> dan <td> -->
<style>
    /* 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 */
    }

    /* 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;
    }

    .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 */
    }
</style>

<!-- DataTables Initialization -->
<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 = "";
                    });

                    // Jika ada datepicker, kosongkan juga
                    var dateInput = form.querySelector(".datepicker");
                    if (dateInput) {
                        dateInput.value = "";
                    }

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