<?php
include "proses/connect.php";
date_default_timezone_set('Asia/Jakarta');
$query = mysqli_query($conn, "SELECT tb_order.*,tb_bayar.*,nama, SUM(harga*jumlah) AS harganya FROM tb_order
    LEFT JOIN tb_user ON tb_user.id = tb_order.pelayan
    LEFT JOIN tb_list_order ON tb_list_order.kode_order = tb_order.id_order
    LEFT JOIN tb_daftar_menu ON tb_daftar_menu.id = tb_list_order.menu
    LEFT JOIN tb_bayar ON tb_bayar.id_bayar = tb_order.id_order
    GROUP BY id_order ORDER BY waktu_order DESC");
while ($record = mysqli_fetch_array($query)) {
  $result[] = $record;
}
?>
<div class="col-lg-9 mt-2">
  <div class="card">
    <div class="card-header">
      Halaman Order
    </div>
    <div class="card-body">
      <div class="row">
        <div class="col d-flex justify-content-end">
          <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#ModalTambahUser">Tambah Order</button>
        </div>
      </div>

      <!-- Modal Tambah Order Baru -->
      <div class="modal fade" id="ModalTambahUser" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h1 class="modal-title fs-5" id="exampleModalLabel">Tambah Order Kue</h1>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
              <form class="needs-validation" novalidate action="proses/proses_input_order.php" method="POST">
                <div class="row">
                  <div class="col-lg-3">
                    <div class="form-floating mb-3">
                      <input type="text" class="form-control " id="uploadFoto" name="kode_order" value="<?php echo date('ymdHi') . rand(100, 999) ?>" readonly>
                      <label for="uploadFoto">Kode Order</label>
                      <div class="invalid-feedback">
                        Masukkan Kode Order
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-floating mb-3">
                      <input type="number" class="form-control" id="antrean" placeholder="Nomor antrean" name="antrean" required>
                      <label for="antrean">Antrean</label>
                      <div class="invalid-feedback">
                        Masukkan Antrean.
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-7">
                    <div class="form-floating mb-3">
                      <input type="text" class="form-control" id="pelanggan" placeholder="Nama Pelanggan" name="pelanggan" required>
                      <label for="pelanggan">Nama Pelanggan</label>
                      <div class="invalid-feedback">
                        Masukkan Nama Pelanggan.
                      </div>
                    </div>
                  </div>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary" name="input_order_validate" value="12345">Buat Order</button>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
      <!-- Akhir Modal Tambah Order Baru -->

      <?php
      if (empty($result)) {
        echo "Data menu tidak ada";
      } else {
        foreach ($result as $row) {
      ?>
          <!-- Modal Edit -->
          <div class="modal fade" id="ModalEdit<?php echo $row['id_order'] ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-xl">
              <div class="modal-content">
                <div class="modal-header">
                  <h1 class="modal-title fs-5" id="exampleModalLabel">Edit Menu</h1>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                  <form class="needs-validation" novalidate action="proses/proses_edit_order.php" method="POST">
                    <div class="row">
                      <div class="col-lg-3">
                        <div class="form-floating mb-3">
                          <input readonly type="text" class="form-control " id="uploadFoto" name="kode_order" value="<?php echo $row['id_order'] ?>" readonly>
                          <label for="uploadFoto">Kode Order</label>
                          <div class="invalid-feedback">
                            Masukkan Kode Order
                          </div>
                        </div>
                      </div>
                      <div class="col-lg-2">
                        <div class="form-floating mb-3">
                          <input type="number" class="form-control" id="antrean" placeholder="Nomor Antrean" name="antrean" required value="<?php echo $row['antrean'] ?>">
                          <label for="antrean">Antrean</label>
                          <div class="invalid-feedback">
                            Masukkan Antrean.
                          </div>
                        </div>
                      </div>
                      <div class="col-lg-7">
                        <div class="form-floating mb-3">
                          <input type="text" class="form-control" id="pelanggan" placeholder="Nama Pelanggan" name="pelanggan" required value="<?php echo $row['pelanggan'] ?>">
                          <label for="pelanggan">Nama Pelanggan</label>
                          <div class="invalid-feedback">
                            Masukkan Nama Pelanggan.
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                      <button type="submit" class="btn btn-primary" name="edit_order_validate" value="12345">Simpan</button>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
          <!--Akhir Modal Edit -->

          <!-- Modal Delete -->
          <div class="modal fade" id="ModalDelete<?php echo $row['id_order'] ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-md">
              <div class="modal-content">
                <div class="modal-header">
                  <h1 class="modal-title fs-5" id="exampleModalLabel">Delete Order</h1>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                  <form class="needs-validation" novalidate action="proses/proses_delete_order.php" method="POST">
                    <input type="hidden" value="<?php echo $row['id_order'] ?>" name="kode_order">
                    <div class="col-lg-12">
                      Apakah anda ingin menghapus order <b><?php echo $row['pelanggan']  ?></b> dengan dengan kode order <b><?php echo $row['id_order']  ?></b>
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                      <button type="submit" class="btn btn-danger" name="delete_order_validate" value="1234">Hapus</button>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
          <!--Akhir Modal Delete -->
        <?php
        }
        ?>

        <div style="max-height: 400px; overflow-y: auto; overflow-x: hidden; scrollbar-width: none;">
  <table class="table table-hover">
            <thead style="position: sticky; top: 0; z-index: 1; background-color: white;">
              <tr class="text-nowrap">
                <th scope="col">No</th>
                <th scope="col">Kode Order</th>
                <th scope="col">Pelanggan</th>
                <th scope="col">Antrean</th>
                <th scope="col">Total Harga</th>
                <th scope="col">Pelayan</th>
                <th scope="col">Status</th>
                <th scope="col">Waktu Order</th>
                <th scope="col">Aksi</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $no = 1;
              foreach ($result as $row) {
              ?>
                <tr class="text-nowrap">
                  <th scope="row"><?php echo $no++ ?></th>
                  <td><?php echo $row['id_order'] ?></td>
                  <td><?php echo $row['pelanggan'] ?></td>
                  <td><?php echo $row['antrean'] ?></td>
                  <td><?php echo number_format((int)$row['harganya'], 0, ',', '.') ?></td>
                  <td><?php echo $row['nama'] ?></td>
                  <td><?php echo (!empty($row['id_bayar'])) ? "<span class='badge text-bg-success'>dibayar</span>" : ""; ?>
                  </td>
                  <td><?php echo $row['waktu_order'] ?></td>
                  <td>
                    <div class="d-flex">
                      <a class="btn btn-info btn-sm me-1" href="./?x=orderitem&order=<?php echo $row['id_order'] . "&antrean=" . $row['antrean'] . "&pelanggan=" . $row['pelanggan'] ?>"><i class="bi bi-eye"></i></a>
                      <button class="<?php echo (!empty($row['id_bayar'])) ? "btn btn-secondary btn-sm me-1 disabled" : "btn btn-warning btn-sm me-1"; ?>"
                        data-bs-toggle="modal"
                        data-bs-target="#ModalEdit<?php echo $row['id_order'] ?>">
                        <i class="bi bi-pencil-square"></i>
                      </button>

                      <button class="<?php echo (!empty($row['id_bayar'])) ? "btn btn-secondary btn-sm me-1 disabled" : "btn btn-danger btn-sm me-1"; ?>"
                        data-bs-toggle="modal"
                        data-bs-target="#ModalDelete<?php echo $row['id_order'] ?>">
                        <i class="bi bi-trash"></i>
                      </button>
                    </div>
                  </td>
                </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        </div>
        <hr>
<h5 class="fw-bold mb-3">🔥 Rekomendasi Menu Terlaris</h5>
<div class="row">
<?php
$query_rekomendasi = mysqli_query($conn, "
    SELECT tb_daftar_menu.nama_menu, SUM(tb_list_order.jumlah) as total_terjual
    FROM tb_list_order
    JOIN tb_daftar_menu ON tb_daftar_menu.id = tb_list_order.menu
    JOIN tb_order ON tb_order.id_order = tb_list_order.kode_order
    JOIN tb_bayar ON tb_bayar.id_bayar = tb_order.id_order
    GROUP BY tb_list_order.menu
    ORDER BY total_terjual DESC
    LIMIT 3
");

$peringkat = 1;
while($data = mysqli_fetch_assoc($query_rekomendasi)){
    $medal = $peringkat == 1 ? '🥇' : ($peringkat == 2 ? '🥈' : '🥉');
    $warna = $peringkat == 1 ? 'border-warning bg-warning bg-opacity-10' : ($peringkat == 2 ? 'border-secondary bg-secondary bg-opacity-10' : 'border-danger bg-danger bg-opacity-10');
?>
    <div class="col-md-4 mb-3">
        <div class="card border-2 <?php echo $warna ?> h-100">
            <div class="card-body text-center">
                <div style="font-size: 2rem"><?php echo $medal ?></div>
                <h6 class="fw-bold mt-2"><?php echo $data['nama_menu'] ?></h6>
                <hr class="my-2">
                <span class="badge bg-primary rounded-pill px-3 py-2">
                    🛒 Terjual: <?php echo $data['total_terjual'] ?> pcs
                </span>
            </div>
        </div>
    </div>
<?php 
    $peringkat++;
} 
?>
</div>
      <?php
      }
      ?>
    </div>
  </div>
</div>