<main id="main" class="main">
    <div class="pagetitle">
        <h1>Atur Slider</h1>
        <nav>
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                <li class="breadcrumb-item">Atur Slider</li>
            </ol>
        </nav>
    </div>
    <!-- End Page Title -->
    <!-- Modal Tambah Data -->
    <!-- Modal Import XLS -->
    <!-- Tambahkan tombol untuk memanggil modal -->
    <div class="modal fade" id="tambah_data" tabindex="-1" aria-labelledby="tambahDataLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="tambahDataLabel">Tambah Data Pasaran</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form action="tambah_pasaran.php" method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="gambar_pasaran" class="form-label">Gambar Pasaran</label>
                            <input type="file" class="form-control" id="gambar_pasaran" name="gambar_pasaran"
                                accept="image/*" required>
                        </div>
                        <div class="mb-3">
                            <label for="nama_pasaran" class="form-label">Nama Pasaran</label>
                            <textarea class="form-control" id="nama_pasaran" name="nama_pasaran" rows="1"
                                required></textarea>
                        </div>
                        <div class="mb-3">
                            <label for="link_livedraw" class="form-label">Link Livedraw</label>
                            <textarea class="form-control" id="link_livedraw" name="link_livedraw" rows="1"
                                required></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <section class="section dashboard">
        <div class="card recent-sales overflow-auto">
            <div class="filter">
                <!-- Modal Dialog Scrollable -->
                <a href="#" class="icon" data-bs-toggle="modal" data-bs-target="#tambah_data">
                    <i class="bi bi-plus-lg"></i>
                </a>
            </div>
            <div class="card-body">
                <h5 class="card-title">Data Pasaran</h5>
                <!-- Notifikasi -->
                <?php if (isset($_GET['notif'])): ?>
                    <div class="alert alert-success" role="alert">
                        <?php echo htmlspecialchars($_GET['notif']); ?>
                    </div>
                <?php endif; ?>
                <table class="table table-borderless datatable">
                    <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Gambar</th>
                            <th scope="col">Nama Pasaran</th>
                            <th scope="col">Link Livedraw</th>
                            <th scope="col">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $no = 1;
                        $qry_pasaran = mysqli_query($koneksi, "SELECT * FROM data_pasaran ORDER BY id DESC");
                        while ($pasaran = mysqli_fetch_array($qry_pasaran)):
                            ?>
                            <tr>
                                <th><?php echo $no++; ?></th>
                                <td>
                                    <img src="<?php echo htmlspecialchars($pasaran['gambar_pasaran']); ?>"
                                        alt="<?php echo htmlspecialchars($pasaran['nama_pasaran']); ?>"
                                        style="max-width: 100px;">
                                </td>
                                <td><?php echo htmlspecialchars($pasaran['nama_pasaran']); ?></td>
                                <td>
                                    <a href="<?php echo htmlspecialchars($pasaran['link_livedraw']); ?>"
                                        class="btn btn-sm btn-primary" target="_blank">
                                        <i class="bi bi-play-circle"></i> Lihat Live Draw
                                    </a>
                                </td>
                                <td>
                                    <!-- Tombol Hapus -->
                                    <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal"
                                        data-bs-target="#modalHapus_<?php echo htmlspecialchars($pasaran['id']); ?>">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                    <!-- Tombol Edit -->
                                    <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal"
                                        data-bs-target="#modalEdit_<?php echo htmlspecialchars($pasaran['id']); ?>">
                                        <i class="bi bi-pen"></i> Edit
                                    </button>
                                </td>

                                <!-- Modal Konfirmasi Hapus -->
                                <div class="modal fade" id="modalHapus_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                    tabindex="-1"
                                    aria-labelledby="hapusLabel_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                    aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title"
                                                    id="hapusLabel_<?php echo htmlspecialchars($pasaran['id']); ?>">
                                                    Konfirmasi Hapus
                                                </h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"
                                                    aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body">
                                                Apakah Anda yakin ingin menghapus pasaran ini?
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary"
                                                    data-bs-dismiss="modal">Batal</button>
                                                <form action="delete_pasaran.php" method="post">
                                                    <input type="hidden" name="id"
                                                        value="<?php echo htmlspecialchars($pasaran['id']); ?>">
                                                    <button type="submit" class="btn btn-sm btn-danger">
                                                        <i class="bi bi-trash"></i> Hapus
                                                    </button>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Modal Edit -->
                                <div class="modal fade" id="modalEdit_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                    tabindex="-1"
                                    aria-labelledby="editLabel_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                    aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title"
                                                    id="editLabel_<?php echo htmlspecialchars($pasaran['id']); ?>">
                                                    Edit Pasaran
                                                </h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"
                                                    aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body">
                                                <form action="edit_pasaran.php" method="post" enctype="multipart/form-data">
                                                    <input type="hidden" name="id"
                                                        value="<?php echo htmlspecialchars($pasaran['id']); ?>">

                                                    <div class="mb-3">
                                                        <label
                                                            for="nama_pasaran_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                                            class="form-label">Nama Pasaran</label>
                                                        <input type="text" class="form-control"
                                                            id="nama_pasaran_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                                            name="nama_pasaran"
                                                            value="<?php echo htmlspecialchars($pasaran['nama_pasaran']); ?>"
                                                            required>
                                                    </div>

                                                    <div class="mb-3">
                                                        <label
                                                            for="gambar_pasaran_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                                            class="form-label">Gambar Pasaran</label>
                                                        <input type="file" class="form-control"
                                                            id="gambar_pasaran_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                                            name="gambar_pasaran">
                                                        <small>Leave blank if you don't want to change the image.</small>
                                                    </div>

                                                    <div class="mb-3">
                                                        <label
                                                            for="link_livedraw_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                                            class="form-label">Link Live Draw</label>
                                                        <input type="url" class="form-control"
                                                            id="link_livedraw_<?php echo htmlspecialchars($pasaran['id']); ?>"
                                                            name="link_livedraw"
                                                            value="<?php echo htmlspecialchars($pasaran['link_livedraw']); ?>"
                                                            required>
                                                    </div>

                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary"
                                                            data-bs-dismiss="modal">Batal</button>
                                                        <button type="submit" class="btn btn-primary">
                                                            <i class="bi bi-save"></i> Simpan
                                                        </button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>

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

</main>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        const editButtons = document.querySelectorAll(".edit-btn");
        editButtons.forEach(button => {
            button.addEventListener("click", function () {
                const id = this.getAttribute("data-id");
                const provider = this.getAttribute("data-provider");
                const gambar = this.getAttribute("data-gambar");
                const keterangan = this.getAttribute("data-keterangan");

                document.getElementById("edit_id").value = id;
                document.getElementById("edit_provider").value = provider;
                document.getElementById("edit_gambar_preview").src = gambar;
                document.getElementById("edit_keterangan").value = keterangan;
            });
        });
    });
</script>