<?php include 'header.php'; ?>
<script type="text/javascript">
   // Function to generate a random number between 0 and 99, formatted to two digits
   function RandomFourDigits() {
      return Math.floor(1000 + Math.random() * 9000);
   }

   // Function to generate a random number between 0 and 999999, formatted to six digits
   function RandomSixDigits() {
      let num = Math.floor(Math.random() * 1000000);
      return num.toString().padStart(6, '0');
   }

   function randomValue() {
      document.getElementById('winner_prize').value = RandomFourDigits();
      document.getElementById('prize2').value = RandomFourDigits();
      document.getElementById('draw_id').value = RandomSixDigits();
      document.getElementById('prize3').value = RandomFourDigits();
      document.getElementById('spesial1').value = RandomFourDigits();
      document.getElementById('spesial2').value = RandomFourDigits();
      document.getElementById('spesial3').value = RandomFourDigits();
      document.getElementById('spesial4').value = RandomFourDigits();
      document.getElementById('spesial5').value = RandomFourDigits();
      document.getElementById('spesial6').value = RandomFourDigits();
      document.getElementById('consolation1').value = RandomFourDigits();
      document.getElementById('consolation2').value = RandomFourDigits();
      document.getElementById('consolation3').value = RandomFourDigits();
      document.getElementById('consolation4').value = RandomFourDigits();
      document.getElementById('consolation5').value = RandomFourDigits();
      document.getElementById('consolation6').value = RandomFourDigits();
   }
</script>
<div class="content-wrapper">
   <section class="content-header">
      <h1>
         Transaksi
         <small>Data Transaksi</small>
      </h1>
      <ol class="breadcrumb">
         <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
         <li class="active">Dashboard</li>
      </ol>
   </section>
   <section class="content">
      <div class="row">
         <section class="col-lg-12">
            <div class="box box-info">
               <div class="box-header">
                  <h3 class="box-title">Atur Result Keluaran</h3>
                  <div class="btn-group pull-right">
                     <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#exampleModal"
                        style="margin-right: 5px;">
                        <i class="fa fa-plus" onclick="generateRandomNumber()"></i> &nbsp Tambah Result
                     </button>
                     <!-- Tombol untuk membuka modal -->
                     <button type="button" class="btn btn-danger btn-sm" data-toggle="modal"
                        data-target="#modalPilihKota">
                        <i class="fa fa-plus"></i> &nbsp; Buat Result Otomatis
                     </button>
                     <!-- Modal Pilih Kota -->
                     <div class="modal fade" id="modalPilihKota" tabindex="-1" role="dialog"
                        aria-labelledby="judulModal" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                           <div class="modal-content">

                              <div class="modal-header">
                                 <h5 class="modal-title" id="judulModal">Pilih Kota untuk Result Otomatis</h5>
                                 <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                                    <span aria-hidden="true">&times;</span>
                                 </button>
                              </div>

                              <div class="modal-body text-center">
                                 <p>Silakan pilih kota untuk generate result otomatis:</p>
                                 <a href="result-otomatis.php?kota=talaga" class="btn btn-primary btn-block mb-2">Result
                                    Tokyo</a>
                              </div>

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


                  </div>
                  <hr>
                  <?php
                  if (isset($_GET['alert'])) {
                     if ($_GET['alert'] == 'gagal') {
                        ?>
                        <div class="alert alert-warning alert-dismissible">
                           <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                           <h4><i class="icon fa fa-warning"></i> Peringatan !</h4>
                           Ekstensi Tidak Diperbolehkan
                        </div>
                        <?php
                     } elseif ($_GET['alert'] == "berhasil") {
                        ?>
                        <div class="alert alert-success alert-dismissible">
                           <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                           <h4><i class="icon fa fa-check"></i> Success</h4>
                           Berhasil Disimpan
                        </div>
                        <?php
                     } elseif ($_GET['alert'] == "berhasiluptanggal") {
                        ?>
                        <div class="alert alert-success alert-dismissible">
                           <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                           <h4><i class="icon fa fa-check"></i> Success</h4>
                           Berhasil Uptanggal
                        </div>
                        <?php
                     }
                  }
                  ?>
               </div>
               <div class="box-body">
                  <!-- Modal -->
                  <!-- Form with ID 'ini-id' -->
                  <script>
                     function updateFormAction() {
                        var form = document.getElementById('crud-form');
                        var select = document.getElementById('draw_type');
                        var selectedOption = select.options[select.selectedIndex];
                        var selectedId = selectedOption.id;
                        var drawType = $('#draw_type').val();

                        // Tentukan tindakan formulir berdasarkan ID yang dipilih
                        switch (selectedId) {
                           case 'option_talaga':
                              form.action = 'result_act_talaga.php';
                              break;
                           case 'option_bogor':
                              form.action = 'result_act_bogor.php';
                              break;
                           case 'option_evening':
                              form.action = 'result_act_evening.php';
                              break;
                           case 'option_night':
                              form.action = 'result_act_night.php';
                              break;
                           default:
                              form.action = '#'; // Default action or error handling
                              break;
                        }

                        // AJAX request untuk mengambil data berdasarkan draw_type
                        $.ajax({
                           url: 'anjing.php', // Ganti dengan URL file PHP Anda
                           type: 'POST',
                           data: { draw_type: drawType },
                           dataType: 'json',
                           success: function (response) {
                              if (response.success) {
                                 $('#draw_time').val(response.draw_time);
                                 $('#draw_end').val(response.draw_end);
                              } else {
                                 alert('Data tidak ditemukan.');
                              }
                           },
                           error: function (jqXHR, textStatus, errorThrown) {
                              alert('Terjadi kesalahan saat mengambil data.\n' +
                                 'Status: ' + textStatus + '\n' +
                                 'Error: ' + errorThrown + '\n' +
                                 'Response: ' + jqXHR.responseText);
                           }
                        });
                     }
                  </script>
                  <form id="crud-form" method="post" enctype="multipart/form-data">
                     <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
                        aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                           <div class="modal-content">
                              <div class="modal-header">
                                 <h4 class="modal-title" id="exampleModalLabel">Tambah Transaksi</h4>
                                 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                 </button>
                              </div>
                              <div class="modal-body">
                                 <!-- Example of select to choose action -->
                                 <input type="hidden" id="draw_id" name="draw_id" readonly>
                                 <div class="form-group">
                                    <label for="draw_type">Draw Type:</label>
                                    <select id="draw_type" name="draw_type" class="form-control"
                                       onchange="updateFormAction(); randomValue()">
                                       <option id="" value="">Pilih Draw Type</option>
                                       <option id="option_talaga" value="talaga">LOTTERY</option>
                                    </select>
                                 </div>
                                 <!-- Rest of the form fields -->
                                 <div class="form-group">
                                    <label>Tanggal Ingin Dikeluarkan</label>
                                    <input type="text" name="tanggal" required="required"
                                       class="form-control datepicker2" autocomplete="off">
                                 </div>
                                 <div class="form-group">
                                    <label>Winner Prize</label>
                                    <input type="text" name="winner_prize" id="winner_prize" value=""
                                       required="required" class="form-control">
                                 </div>
                                 <div class="form-group">
                                    <label>Prize 2</label>
                                    <input type="text" name="prize2" id="prize2" required="required"
                                       class="form-control">
                                 </div>
                                 <div class="form-group">
                                    <label>Prize 3</label>
                                    <input type="text" name="prize3" id="prize3" required="required"
                                       class="form-control">
                                 </div>
                                 <div class="form-group">
                                    <label>Starter</label>
                                    <input type="text" name="spesial1" id="spesial1" required="required"
                                       class="form-control">
                                 </div>
                                 <input type="hidden" name="spesial2" id="spesial2">
                                 <input type="hidden" name="spesial3" id="spesial3">
                                 <input type="hidden" name="spesial4" id="spesial4">
                                 <input type="hidden" name="spesial5" id="spesial5">
                                 <input type="hidden" name="spesial6" id="spesial6">

                                 <!-- Consolation1 sampai Consolation6 (disembunyikan) -->
                                 <input type="hidden" name="consolation1" id="consolation1">
                                 <input type="hidden" name="consolation2" id="consolation2">
                                 <input type="hidden" name="consolation3" id="consolation3">
                                 <input type="hidden" name="consolation4" id="consolation4">
                                 <input type="hidden" name="consolation5" id="consolation5">
                                 <input type="hidden" name="consolation6" id="consolation6">
                                 <div class="form-group">
                                    <label>Draw Time</label>
                                    <input type="text" name="draw_time" id="draw_time" value="" required="required"
                                       class="form-control" readonly>
                                 </div>
                                 <div class="form-group">
                                    <label>Draw End</label>
                                    <input type="text" name="draw_end" id="draw_end" value="" required="required"
                                       class="form-control" readonly>
                                 </div>
                                 <input type="button" value="Random Number!" class="btn-warning"
                                    onclick="randomValue();" />
                                 <!-- Add other hidden fields as needed -->
                              </div>
                              <div class="modal-footer">
                                 <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                                 <button type="submit" class="btn btn-primary">Simpan</button>
                              </div>
                           </div>
                        </div>
                     </div>
                  </form>
                  <!-- Include jQuery and Bootstrap JS for modal functionality -->
                  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
                  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
                  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
                  <!-- JavaScript to change form action based on select -->
                  <div class="table-responsive">
                     <table class="table table-bordered table-striped" id="table-datatable">
                        <thead>
                           <tr>
                              <th width="1%">NO</th>
                              <th class="text-center">Tanggal</th>
                              <th class="text-center">Draw Type</th>
                              <th class="text-center">Winner Prize</th>
                              <th class="text-center">Prize 2</th>
                              <th class="text-center">Prize 3</th>
                              <th class="text-center">OPSI</th>
                           </tr>
                        </thead>
                        <tbody>
                           <?php
                           function convertTanggalToStandard($tanggal)
                           {
                              if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $tanggal)) {
                                 return $tanggal;
                              }
                              $parsed = strtotime($tanggal);
                              return $parsed !== false ? date('Y-m-d', $parsed) : '1970-01-01';
                           }

                           $talaga_data = [];
                           $qry_all = mysqli_query($koneksi, "SELECT * FROM result_talaga ORDER BY id DESC");
                           while ($row = mysqli_fetch_assoc($qry_all)) {
                              $row['_sort_tanggal'] = convertTanggalToStandard($row['tanggal']);
                              $talaga_data[] = $row;
                           }

                           usort($talaga_data, function ($a, $b) {
                              return strtotime($b['_sort_tanggal']) - strtotime($a['_sort_tanggal']);
                           });

                           $no = 1;
                           foreach ($talaga_data as $data) {
                              $draw_id = $data['draw_id'];
                              ?>
                              <tr>
                                 <td class="text-center"><?= $no++; ?></td>
                                 <td class="text-center"><?= date('D, j M Y', strtotime($data['_sort_tanggal'])); ?></td>
                                 <td class="text-center">LOTTERY</td>
                                 <td class="text-center"><?= $data['winner_prize']; ?></td>
                                 <td class="text-center"><?= $data['prize2']; ?></td>
                                 <td class="text-center"><?= $data['prize3']; ?></td>
                                 <td class="text-center">
                                    <!-- Tombol Edit -->
                                    <button type="button" class="btn btn-warning btn-sm" data-toggle="modal"
                                       data-target="#editModal" data-id="<?= $draw_id; ?>"
                                       data-type="<?= $data['draw_type']; ?>" data-tanggal="<?= $data['tanggal']; ?>"
                                       data-winner="<?= $data['winner_prize']; ?>" data-prize2="<?= $data['prize2']; ?>"
                                       data-prize3="<?= $data['prize3']; ?>">
                                       <i class="fa fa-cog"></i>
                                    </button>

                                    <!-- Tombol Hapus -->
                                    <button type="button" class="btn btn-danger btn-sm" data-toggle="modal"
                                       data-target="#hapus_<?= $draw_id; ?>">
                                       <i class="fa fa-trash"></i>
                                    </button>

                                    <!-- Modal Hapus -->
                                    <div class="modal fade" id="hapus_<?= $draw_id; ?>" tabindex="-1" role="dialog">
                                       <div class="modal-dialog" role="document">
                                          <div class="modal-content">
                                             <div class="modal-header">
                                                <h4 class="modal-title">Konfirmasi</h4>
                                                <button type="button" class="close"
                                                   data-dismiss="modal"><span>&times;</span></button>
                                             </div>
                                             <div class="modal-body">
                                                <p>Yakin ingin menghapus data ini?</p>
                                                <form method="POST" action="result_hapus.php"
                                                   id="deleteForm_<?= $draw_id; ?>">
                                                   <input type="hidden" name="draw_id" value="<?= $draw_id; ?>">
                                                   <input type="hidden" name="draw_type"
                                                      value="<?= $data['draw_type']; ?>">
                                                </form>
                                             </div>
                                             <div class="modal-footer">
                                                <button class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                                                <button class="btn btn-danger"
                                                   onclick="document.getElementById('deleteForm_<?= $draw_id; ?>').submit();">Hapus</button>
                                             </div>
                                          </div>
                                       </div>
                                    </div>
                                 </td>
                              </tr>
                           <?php } ?>
                        </tbody>
                     </table>
                  </div>



                  <!-- Modal Edit Global -->
                  <div class="modal fade" id="editModal" tabindex="-1" role="dialog">
                     <div class="modal-dialog" role="document">
                        <form action="result_update.php" method="post">
                           <div class="modal-content">
                              <div class="modal-header">
                                 <h5 class="modal-title">Edit Transaksi</h5>
                                 <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                              </div>
                              <div class="modal-body">
                                 <input type="hidden" name="draw_id" id="edit_draw_id">
                                 <div class="form-group">
                                    <label>Draw Type</label>
                                    <input type="text" name="draw_type" id="edit_draw_type" class="form-control"
                                       readonly>
                                 </div>
                                 <div class="form-group">
                                    <label>Tanggal</label>
                                    <input type="text" name="tanggal" id="edit_tanggal"
                                       class="form-control datepicker2">
                                 </div>
                                 <div class="form-group">
                                    <label>Winner Prize</label>
                                    <input type="text" name="winner_prize" id="edit_winner_prize" class="form-control">
                                 </div>
                                 <div class="form-group">
                                    <label>Prize 2</label>
                                    <input type="text" name="prize2" id="edit_prize2" class="form-control">
                                 </div>
                                 <div class="form-group">
                                    <label>Prize 3</label>
                                    <input type="text" name="prize3" id="edit_prize3" class="form-control">
                                 </div>
                              </div>
                              <div class="modal-footer">
                                 <button class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                                 <button class="btn btn-primary" type="submit">Simpan</button>
                              </div>
                           </div>
                        </form>
                     </div>
                  </div>

                  <script>
                     $('#editModal').on('show.bs.modal', function (event) {
                        var button = $(event.relatedTarget);
                        $('#edit_draw_id').val(button.data('id'));
                        $('#edit_draw_type').val(button.data('type'));
                        $('#edit_tanggal').val(button.data('tanggal'));
                        $('#edit_winner_prize').val(button.data('winner'));
                        $('#edit_prize2').val(button.data('prize2'));
                        $('#edit_prize3').val(button.data('prize3'));
                     });
                  </script>

               </div>
            </div>
         </section>
      </div>
   </section>
</div>
<?php include 'footer.php'; ?>