<?= $this->include('admin/layout/header') ?>
<?= $this->include('admin/layout/navbar') ?>
<?= $this->include('admin/layout/topbar') ?>

<div class="pcoded-main-container">
    <div class="pcoded-content">

        <div class="card">
            <div class="card-header">
                <h5>Data Bencana</h5>

                <button class="btn btn-primary float-right" data-toggle="modal" data-target="#modalTambah">
                    Tambah Data
                </button>
            </div>

            <div class="card-body">

                <?php if (session()->getFlashdata('success')) : ?>
                    <div class="alert alert-success">
                        <?= session()->getFlashdata('success') ?>
                    </div>
                <?php endif; ?>

                <div class="table-responsive">

                    <table class="table table-bordered table-hover table-striped">

                        <thead class="text-center">

                            <tr>
                                <th>No</th>
                                <th>Jenis Data</th>
                                <th>Tahun</th>
                                <th>Sumber</th>
                                <th>Keterangan</th>
                                <th>File</th>
                                <th>Aksi</th>
                            </tr>

                        </thead>

                        <tbody>

                            <?php $no = 1;
                            foreach ($data_bencana as $row): ?>

                                <tr>

                                    <td class="text-center align-middle">
                                        <?= $no++ ?>
                                    </td>

                                    <td class="align-middle">
                                        <?= ucwords(str_replace('_', ' ', $row['jenis_data'])) ?>
                                    </td>

                                    <td class="text-center align-middle">
                                        <?= $row['tahun'] ?>
                                    </td>

                                    <td class="align-middle">
                                        <?= $row['sumber'] ?>
                                    </td>

                                    <td>
                                        <?= nl2br($row['keterangan']) ?>
                                    </td>

                                    <td class="text-center align-middle">

                                        <?php if ($row['file_data']) : ?>

                                            <a href="<?= base_url('uploads/data_bencana/' . $row['file_data']) ?>" target="_blank">
                                                Lihat File
                                            </a>

                                        <?php else : ?>

                                            -

                                        <?php endif; ?>

                                    </td>

                                    <td class="text-center align-middle">

                                        <button class="btn btn-warning btn-sm"
                                            data-toggle="modal"
                                            data-target="#edit<?= $row['id_data'] ?>">
                                            Edit
                                        </button>

                                        <button class="btn btn-danger btn-sm"
                                            data-toggle="modal"
                                            data-target="#hapus<?= $row['id_data'] ?>">
                                            Hapus
                                        </button>

                                    </td>

                                </tr>


                                <!-- EDIT MODAL -->
                                <div class="modal fade" id="edit<?= $row['id_data'] ?>">
                                    <div class="modal-dialog">
                                        <div class="modal-content">

                                            <form action="<?= base_url('admin/data/update/' . $row['id_data']) ?>" method="post" enctype="multipart/form-data">

                                                <div class="modal-header">
                                                    <h5>Edit Data Bencana</h5>
                                                </div>

                                                <div class="modal-body">

                                                    <label>Jenis Data</label>
                                                    <select name="jenis_data" class="form-control mb-2">
                                                        <option value="kepadatan_penduduk" <?= $row['jenis_data'] == 'kepadatan_penduduk' ? 'selected' : '' ?>>
                                                            Kepadatan Penduduk
                                                        </option>
                                                        <option value="kejadian_banjir" <?= $row['jenis_data'] == 'kejadian_banjir' ? 'selected' : '' ?>>
                                                            Kejadian Banjir
                                                        </option>
                                                    </select>

                                                    <label>Tahun</label>
                                                    <input type="number" name="tahun" value="<?= $row['tahun'] ?>" class="form-control mb-2">

                                                    <label>Sumber Data</label>
                                                    <input type="text" name="sumber" value="<?= $row['sumber'] ?>" class="form-control mb-2">

                                                    <label>Keterangan</label>
                                                    <textarea name="keterangan" class="form-control mb-2"><?= $row['keterangan'] ?></textarea>

                                                    <label>Upload File Baru (opsional)</label>
                                                    <input type="file" name="file_data" class="form-control">

                                                    <?php if ($row['file_data']) : ?>
                                                        <small>
                                                            File saat ini:
                                                            <a href="<?= base_url('uploads/data_bencana/' . $row['file_data']) ?>" target="_blank">
                                                                Lihat File
                                                            </a>
                                                        </small>
                                                    <?php endif; ?>

                                                </div>

                                                <div class="modal-footer">
                                                    <button class="btn btn-success">Update</button>
                                                </div>

                                            </form>

                                        </div>
                                    </div>
                                </div>

                                <!-- HAPUS MODAL -->
                                <div class="modal fade" id="hapus<?= $row['id_data'] ?>">
                                    <div class="modal-dialog">
                                        <div class="modal-content">

                                            <div class="modal-body text-center">

                                                <h5>Hapus Data Ini?</h5>
                                                <p>
                                                    Data <b><?= $row['jenis_data'] ?></b> tahun <b><?= $row['tahun'] ?></b> akan dihapus.
                                                </p>

                                                <a href="<?= base_url('admin/data/delete/' . $row['id_data']) ?>" class="btn btn-danger">
                                                    Ya, Hapus
                                                </a>

                                                <button class="btn btn-secondary" data-dismiss="modal">
                                                    Batal
                                                </button>

                                            </div>

                                        </div>
                                    </div>
                                </div>

                            <?php endforeach; ?>
                        </tbody>
                    </table>

                </div>
            </div>

        </div>
    </div>

    <!-- TAMBAH MODAL -->
    <div class="modal fade" id="modalTambah">
        <div class="modal-dialog">
            <div class="modal-content">

                <form action="<?= base_url('admin/data/store') ?>" method="post" enctype="multipart/form-data">

                    <div class="modal-header">
                        <h5>Tambah Data Bencana</h5>
                    </div>

                    <div class="modal-body">

                        <label>Jenis Data</label>
                        <select name="jenis_data" class="form-control mb-2">
                            <option value="kepadatan_penduduk">Kepadatan Penduduk</option>
                            <option value="kejadian_banjir">Kejadian Banjir</option>
                        </select>

                        <label>Tahun</label>
                        <input type="number" name="tahun" class="form-control mb-2" required>

                        <label>Sumber Data</label>
                        <input type="text" name="sumber" class="form-control mb-2" placeholder="Contoh: BNPB / BPS">

                        <label>Keterangan</label>
                        <textarea name="keterangan" class="form-control mb-2" placeholder="Deskripsi data..."></textarea>

                        <label>Upload File (PDF / JPG / PNG)</label>
                        <input type="file" name="file_data" class="form-control" accept=".pdf,.jpg,.png">

                    </div>

                    <div class="modal-footer">
                        <button class="btn btn-primary">Simpan</button>
                    </div>

                </form>

            </div>
        </div>
    </div>

    <?= $this->include('admin/layout/footer') ?>