<main id="main" class="main">
    <div class="pagetitle">
        <h1>Atur Result Pasaran</h1>
        <nav>
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                <li class="breadcrumb-item">Atur Result Pasaran</li>
            </ol>
        </nav>
    </div>
    <!-- End Page Title -->

    <?php include "koneksi.php"; ?>

    <!-- Modal Tambah Data -->

    <!-- Modal -->


    <section class="section dashboard">
        <div class="card recent-sales overflow-auto">
            <!-- Tombol untuk Menampilkan Modal -->

            <!-- Modal Tambah Data -->
            <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_result_pasaran.php" method="post">
                            <div class="modal-body">
                                <!-- Pilihan Nama Pasaran -->
                                <div class="mb-3">
                                    <label for="nama_pasaran" class="form-label">Nama Pasaran</label>
                                    <select class="form-control" id="nama_pasaran" name="nama_pasaran" required>
                                        <option value="">-- Pilih Pasaran --</option>
                                        <?php
                                        $qry_pasaran = mysqli_query($koneksi, "SELECT nama_pasaran FROM data_pasaran ORDER BY nama_pasaran ASC");
                                        while ($pasaran = mysqli_fetch_array($qry_pasaran)) {
                                            echo '<option value="' . htmlspecialchars($pasaran['nama_pasaran']) . '">' . htmlspecialchars($pasaran['nama_pasaran']) . '</option>';
                                        }
                                        ?>
                                    </select>
                                </div>

                                <!-- Input Result Pasaran -->
                                <div class="mb-3">
                                    <label for="result_pasaran" class="form-label">Result Pasaran</label>
                                    <input type="text" class="form-control" id="result_pasaran" name="result_pasaran"
                                        required>
                                </div>

                                <!-- Input Tanggal Result -->
                                <div class="mb-3">
                                    <label for="tanggal_result" class="form-label">Tanggal Result</label>
                                    <input type="date" class="form-control" id="tanggal_result" name="tanggal_result"
                                        required>
                                </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>

            <!-- Tombol Bulk Import -->
            <div class="filter">
                <!-- Modal Dialog Bulk Upload -->
                <a href="#" class="icon" data-bs-toggle="modal" data-bs-target="#bulkImportModal"
                    style="margin-right:50px;">
                    <i class="bi bi-upload"></i> Bulk Upload
                </a>
            </div>
            <div class="filter">
                <!-- Tombol Tambah Data -->
                <a href="#" class="icon" data-bs-toggle="modal" data-bs-target="#tambah_data">
                    <i class="bi bi-plus-lg"></i>
                </a>
            </div>



            <!-- Modal Bulk Import -->
            <div class="modal fade" id="bulkImportModal" tabindex="-1" aria-labelledby="bulkImportLabel"
                aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="bulkImportLabel">Bulk Import Result Pasaran</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form action="bulk_result_pasaran.php" method="post" enctype="multipart/form-data">
                                <div class="mb-3">
                                    <label for="nama_pasaran" class="form-label">Pilih Pasaran</label>
                                    <select name="nama_pasaran" class="form-select" required>
                                        <option value="">-- Pilih Pasaran --</option>
                                        <?php
                                        // Ambil daftar pasaran dari database
                                        $qry_pasaran = mysqli_query($koneksi, "SELECT nama_pasaran FROM data_pasaran");
                                        while ($row = mysqli_fetch_assoc($qry_pasaran)) {
                                            echo "<option value='" . htmlspecialchars($row['nama_pasaran']) . "'>" . htmlspecialchars($row['nama_pasaran']) . "</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label for="file_txt" class="form-label">Upload File .txt</label>
                                    <input type="file" name="file_txt" class="form-control" accept=".txt" required>
                                </div>
                                <button type="submit" class="btn btn-success">Import</button>
                            </form>
                        </div>
                    </div>
                </div>
            </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">Nama Pasaran</th>
                            <th scope="col">Result Pasaran</th>
                            <th scope="col">Tanggal Result</th>
                            <th scope="col">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        include "koneksi.php";
                        $no = 1;
                        $qry_result = mysqli_query($koneksi, "SELECT * FROM result_pasaran ORDER BY id DESC");
                        while ($result = mysqli_fetch_array($qry_result)):
                            ?>
                            <tr>
                                <th><?php echo $no++; ?></th>
                                <td><?php echo htmlspecialchars($result['nama_pasaran']); ?></td>
                                <td><?php echo htmlspecialchars($result['result_pasaran']); ?></td>
                                <td><?php echo htmlspecialchars($result['tanggal_result']); ?></td>
                                <td>
                                    <!-- Tombol Hapus -->
                                    <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal"
                                        data-bs-target="#modalHapus_<?php echo htmlspecialchars($result['id']); ?>">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </td>
                            </tr>

                            <!-- Modal Konfirmasi Hapus -->
                            <div class="modal fade" id="modalHapus_<?php echo htmlspecialchars($result['id']); ?>"
                                tabindex="-1" aria-labelledby="hapusLabel_<?php echo htmlspecialchars($result['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($result['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 hasil pasaran
                                            <strong><?php echo htmlspecialchars($result['nama_pasaran']); ?></strong>?
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary"
                                                data-bs-dismiss="modal">Batal</button>
                                            <form action="delete_result_pasaran.php" method="post">
                                                <input type="hidden" name="id"
                                                    value="<?php echo htmlspecialchars($result['id']); ?>">
                                                <button type="submit" class="btn btn-sm btn-danger">
                                                    <i class="bi bi-trash"></i> Hapus
                                                </button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endwhile; ?>
                    </tbody>
                </table>

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

</main>