<main id="main" class="main">

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

  <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>
        <a href="export.php" class="icon">
          Download Data
        </a>
        <div class="modal fade" id="tambah_data" tabindex="-1">
          <div class="modal-dialog">
            <div class="modal-content">
              <form action="prs_tambah_pemenang.php" method="post">
                <div class="modal-header">
                  <h5 class="modal-title">Tambah Data</h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                  <div class="form-floating mb-3">
                    <input type="text" class="form-control" id="kode_pemenang" name="kode_pemenang"
                      value="<?php echo generateRandomString(); ?>" readonly>
                    <label for="kode_pemenang">Kode Pemenang</label>
                  </div>
                  <div class="form-floating mb-3">
                    <input type="text" class="form-control" id="nama_pemenang" name="nama_pemenang"
                      placeholder="Nama Pemenang">
                    <label for="nama_pemenang">Nama Pemenang</label>
                  </div>
                  <div class="form-floating mb-3">
                    <input type="text" class="form-control" id="user_game" name="user_game" placeholder="User game">
                    <label for="user_game">User Game</label>
                  </div>
                  <div class="form-floating mb-3">
                    <input type="text" class="form-control" id="whatsapp_pemenang" name="whatsapp_pemenang"
                      placeholder="WhatsApp Pemenang">
                    <label for="whatsapp_pemenang">WhatsApp Pemenang</label>
                  </div>
                  <div class="form-floating mb-3">
                    <select class="form-select" id="id_voucher_pemenang" name="id_voucher_pemenang"
                      aria-label="id_voucher_pemenang">
                      <option selected>Pilih Voucher</option>
                      <?php
                      $qry_voucher = mysqli_query($koneksi, "SELECT * FROM voucher WHERE status_voucher = 'Belum Digunakan'");
                      while ($dt_voucher = mysqli_fetch_array($qry_voucher)) {
                        $id_voucher = $dt_voucher['id_voucher'];
                        $kode_voucher = $dt_voucher['kode_voucher'];
                        $id_hadiah_voucher = $dt_voucher['id_hadiah_voucher'];
                        ?>
                        <option value="<?php echo $id_voucher; ?>"><?php echo $kode_voucher; ?></option>
                      <?php } ?>
                    </select>
                    <label for="id_voucher_pemenang">Voucher Pemenang</label>
                  </div>
                  <div class="form-floating mb-3">
                    <select class="form-select" id="id_hadiah_pemenang" name="id_hadiah_pemenang"
                      aria-label="id_hadiah_pemenang">
                      <option selected>Pilih Hadiah</option>
                      <?php
                      $qry_hadiah = mysqli_query($koneksi, "SELECT * FROM hadiah WHERE status_hadiah = 'Aktif'");
                      while ($dt_hadiah = mysqli_fetch_array($qry_hadiah)) {
                        $id_hadiah = $dt_hadiah['id_hadiah'];
                        $nama_hadiah = $dt_hadiah['nama_hadiah'];
                        ?>
                        <option value="<?php echo $id_hadiah; ?>"><?php echo $nama_hadiah; ?></option>
                      <?php } ?>
                    </select>
                    <label for="id_hadiah_pemenang">Hadiah Pemenang</label>
                  </div>
                  <div class="form-floating mb-3">
                    <select class="form-select" id="status_pemenang" name="status_pemenang"
                      aria-label="status_pemenang">
                      <option selected>Status Pemenang</option>
                      <option value="Dibatalkan">Dibatalkan</option>
                      <option value="Proses">Proses</option>
                      <option value="Selesai">Selesai</option>
                    </select>
                    <label for="status_pemenang">Status Pemenang</label>
                  </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">Tambah Data</button>
                </div>
              </form>
            </div>
          </div>
        </div><!-- End Modal Dialog Scrollable-->
      </div>

      <div class="card-body">
        <h5 class="card-title">Hadiah</h5>
        <?php
        if (isset($_GET['notif'])) {
          if ($_GET['notif'] == "berhasil_tambah_data") {
            echo '
                    <div class="alert alert-success" role="alert">
                        Berhasil menambahkan data.
                    </div>
                ';
          } else if ($_GET['notif'] == "berhasil_ubah_data") {
            echo '
                    <div class="alert alert-success" role="alert">
                        Berhasil mengubah data.
                    </div>
                ';
          } else if ($_GET['notif'] == "berhasil_hapus_data") {
            echo '
                    <div class="alert alert-success" role="alert">
                        Berhasil menghapus data.
                    </div>
                ';
          }
        }
        ?>

        <table class="table table-borderless datatable">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Kode</th>
              <th scope="col">Nama</th>
              <th scope="col">User Game</th>
              <th scope="col">WhatsApp</th>
              <th scope="col">Browser User</th>
              <th scope="col">Voucher</th>
              <th scope="col">Hadiah</th>
              <th scope="col">Tanggal</th>
              <th scope="col">Status</th>
              <th scope="col">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $no = 1;
            $qry_pemenang = mysqli_query($koneksi, "SELECT * FROM pemenang");
            while ($dt_pemenang = mysqli_fetch_array($qry_pemenang)) {
              $id_pemenang = $dt_pemenang['id_pemenang'];
              $kode_pemenang = $dt_pemenang['kode_pemenang'];
              $nama_pemenang = $dt_pemenang['nama_pemenang'];
              $user_game = $dt_pemenang['user_game'];
              $whatsapp_pemenang = $dt_pemenang['whatsapp_pemenang'];
              $ip_user = $dt_pemenang['ip_user'];
              $browser_user = $dt_pemenang['browser_user'];
              $os_user = $dt_pemenang['os_user'];
              $id_voucher_pemenang = $dt_pemenang['id_voucher_pemenang'];
              $id_hadiah_pemenang = $dt_pemenang['id_hadiah_pemenang'];
              $tanggal_pemenang = $dt_pemenang['tanggal_pemenang'];
              $status_pemenang = $dt_pemenang['status_pemenang'];

              $qry_voucher = mysqli_query($koneksi, "SELECT * FROM voucher WHERE id_voucher = '$id_voucher_pemenang'");
              $dt_voucher = mysqli_fetch_array($qry_voucher);
              $kode_voucher = $dt_voucher['kode_voucher'];

              $qry_hadiah = mysqli_query($koneksi, "SELECT * FROM hadiah WHERE id_hadiah = '$id_hadiah_pemenang'");
              $dt_hadiah = mysqli_fetch_array($qry_hadiah);
              $nama_hadiah = $dt_hadiah['nama_hadiah'];
              ?>
              <tr>
                <th><?php echo $no++; ?></th>
                <td><?php echo $kode_pemenang; ?></td>
                <td><?php echo $nama_pemenang; ?></td>
                <td><?php echo $user_game; ?></td>
                <td><?php echo $whatsapp_pemenang; ?></td>
                <td><button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal"
                    data-bs-target="#lihat_<?php echo $id_pemenang; ?>"><i class="bi bi-eye-fill"></i></button></td>
                <td><?php echo $kode_voucher; ?></td>
                <td><?php echo $nama_hadiah; ?></td>
                <td><?php echo $tanggal_pemenang; ?></td>
                <td>
                  <?php
                  if ($status_pemenang == "Dibatalkan") {
                    echo '<span class="badge bg-danger">Dibatalkan</span>';
                  } else if ($status_pemenang == "Proses") {
                    echo '<span class="badge bg-warning">Proses</span>';
                  } else {
                    echo '<span class="badge bg-success">Selesai</span>';
                  }
                  ?>
                </td>
                <td>
                  <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal"
                    data-bs-target="#ubah_<?php echo $id_pemenang; ?>"><i class="bi bi-pencil-square"></i></button>
                  <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal"
                    data-bs-target="#hapus_<?php echo $id_pemenang; ?>"><i class="bi bi-trash"></i></button>

                  <div class="modal fade" id="ubah_<?php echo $id_pemenang; ?>" tabindex="-1">
                    <div class="modal-dialog">
                      <div class="modal-content">
                        <form action="prs_ubah_pemenang.php" method="post">
                          <div class="modal-header">
                            <h5 class="modal-title">Ubah Data</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                          </div>
                          <div class="modal-body">
                            <input type="hidden" name="id_pemenang" value="<?php echo $id_pemenang; ?>">
                            <input type="hidden" name="id_voucher_pemenang_lama"
                              value="<?php echo $id_voucher_pemenang; ?>">
                            <div class="form-floating mb-3">
                              <input type="text" class="form-control" id="kode_pemenang" name="kode_pemenang"
                                value="<?php echo $kode_pemenang; ?>" readonly>
                              <label for="kode_pemenang">Kode Pemenang</label>
                            </div>
                            <div class="form-floating mb-3">
                              <input type="text" class="form-control" id="nama_pemenang" name="nama_pemenang"
                                value="<?php echo $nama_pemenang; ?>">
                              <label for="nama_pemenang">Nama Pemenang</label>
                            </div>
                            <div class="form-floating mb-3">
                              <input type="text" class="form-control" id="whatsapp_pemenang" name="whatsapp_pemenang"
                                value="<?php echo $whatsapp_pemenang; ?>">
                              <label for="whatsapp_pemenang">WhatsApp Pemenang</label>
                            </div>
                            <div class="form-floating mb-3">
                              <select class="form-select" id="id_voucher_pemenang" name="id_voucher_pemenang"
                                aria-label="id_voucher_pemenang">
                                <?php
                                $qry_voucher = mysqli_query($koneksi, "SELECT * FROM voucher WHERE id_voucher = '$id_voucher_pemenang'");
                                $kode_voucher = $dt_voucher['kode_voucher'];
                                ?>
                                <option value="<?php echo $id_voucher_pemenang; ?>"><?php echo $kode_voucher; ?></option>
                                <?php
                                $qry_voucher = mysqli_query($koneksi, "SELECT * FROM voucher WHERE status_voucher = 'Belum Digunakan'");
                                while ($dt_voucher = mysqli_fetch_array($qry_voucher)) {
                                  $id_voucher = $dt_voucher['id_voucher'];
                                  $kode_voucher = $dt_voucher['kode_voucher'];
                                  ?>
                                  <option value="<?php echo $id_voucher; ?>"><?php echo $kode_voucher; ?></option>
                                <?php } ?>
                              </select>
                              <label for="id_voucher_pemenang">Voucher Pemenang</label>
                            </div>
                            <div class="form-floating mb-3">
                              <select class="form-select" id="id_hadiah_pemenang" name="id_hadiah_pemenang"
                                aria-label="id_hadiah_pemenang">
                                <?php
                                $qry_hadiah = mysqli_query($koneksi, "SELECT * FROM hadiah WHERE id_hadiah = '$id_hadiah_pemenang'");
                                $nama_hadiah = $dt_hadiah['nama_hadiah'];
                                ?>
                                <option value="<?php echo $id_hadiah_pemenang; ?>"><?php echo $nama_hadiah; ?></option>
                                <?php
                                $qry_hadiah = mysqli_query($koneksi, "SELECT * FROM hadiah WHERE status_hadiah = 'Aktif'");
                                while ($dt_hadiah = mysqli_fetch_array($qry_hadiah)) {
                                  $id_hadiah = $dt_hadiah['id_hadiah'];
                                  $nama_hadiah = $dt_hadiah['nama_hadiah'];
                                  ?>
                                  <option value="<?php echo $id_hadiah; ?>"><?php echo $nama_hadiah; ?></option>
                                <?php } ?>
                              </select>
                              <label for="id_hadiah_pemenang">Hadiah Pemenang</label>
                            </div>
                            <div class="form-floating mb-3">
                              <select class="form-select" id="status_pemenang" name="status_pemenang"
                                aria-label="status_pemenang">
                                <option value="<?php echo $status_pemenang; ?>"><?php echo $status_pemenang; ?></option>
                                <?php
                                if ($status_pemenang == "Dibatalkan") {
                                  echo '
                                      <option value="Proses">Proses</option>
                                      <option value="Selesai">Selesai</option>
                                    ';
                                } else if ($status_pemenang == "Proses") {
                                  echo '
                                      <option value="Dibatalkan">Dibatalkan</option>
                                      <option value="Selesai">Selesai</option>
                                    ';
                                } else {
                                  echo '
                                      <option value="Dibatalkan">Dibatalkan</option>
                                      <option value="Selesai">Selesai</option>
                                    ';
                                }
                                ?>
                              </select>
                              <label for="status_pemenang">Status Pemenang</label>
                            </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">Ubah Data</button>
                          </div>
                        </form>
                      </div>
                    </div>
                  </div><!-- End Modal Dialog Scrollable-->
                  <div class="modal fade" id="hapus_<?php echo $id_pemenang; ?>" tabindex="-1">
                    <div class="modal-dialog modal-dialog-centered">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title">Hapus Data</h5>
                          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                          Yakin ingin menghapus pemenang <strong><?php echo $nama_pemenang; ?></strong> ?
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                          <a href="prs_hapus_pemenang.php?id_pemenang=<?php echo $id_pemenang; ?>&id_voucher_pemenang=<?php echo $id_voucher_pemenang; ?>&id_hadiah_pemenang=<?php echo $id_hadiah_pemenang; ?>"
                            class="btn btn-primary">Hapus Data</a>
                        </div>
                      </div>
                    </div>
                  </div><!-- End Vertically centered Modal-->
                  <div class="modal fade" id="lihat_<?php echo $id_pemenang; ?>" tabindex="-1">
                    <div class="modal-dialog modal-dialog-centered">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title">Detail User Browser Data</h5>
                          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                          <p><b>IP User : &nbsp;</b><?php echo $ip_user; ?></p>
                          <p><b>Browser User : &nbsp;</b><?php echo $browser_user; ?></p>
                          <p><b>OS User : &nbsp;</b><?php echo $os_user; ?></p>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">OK</button>
                        </div>
                      </div>
                    </div>
                  </div>
                </td>
              </tr>
            <?php } ?>
          </tbody>
        </table>

      </div>

    </div>
  </section>

</main><!-- End #main -->