<?php
session_start();
require("../mainconfig.php");

if (isset($_SESSION['user'])) {
	$sess_username = $_SESSION['user']['username'];
	$check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$sess_username'");
	$data_user = mysqli_fetch_assoc($check_user);
	if (mysqli_num_rows($check_user) == 0) {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['status'] == "Suspended") {
		header("Location: ".$cfg_baseurl."logout.php");
	}

	include("../lib/header.php");
?>
<div class="page-body"> 
<?php include('../lib/partial/breadcrumb.php') ?>
   <!-- Container-fluid starts-->
   <div class="container-fluid">
            <div class="row justify-content-center">
              <div class="col-xxl-6">
            <div class="card">
  <div class="card-body">
      <h4 class="card-title">Top 5 Pengguna</h4>
    <div class="table-responsive">
      <table class="table table-hover">
          <thead>
										<tr class="text-uppercase">
											<th scope="col">No</th>
											<th>Nama Pengguna</th>
											<th>Jumlah Pesanan</th>
										</tr>
									</thead>
									<tbody>
								<?php
								$no = 1;
                                $query_list = mysqli_query($db, "select A.* from top_user A inner join (SELECT username,max(jumlah) as maxRev FROM top_user group by username) B on A.username=B.username and A.jumlah=B.maxRev ORDER BY jumlah DESC LIMIT 5"); // edit
                                        $no = 1;
											while ($data_hof = mysqli_fetch_assoc($query_list)) {
									?>
									<tr class="table-<?php echo $label; ?>">
										<td><?php echo $no; ?></td>
										<td><?php echo $data_hof['username']; ?></td>
										<td>Rp <?php echo number_format($data_hof['jumlah'],0,',','.'); ?> (<?php echo number_format($data_hof['total'],0,',','.'); ?>)</td>
									</tr>
									<?php
									$no++;
									}
									?>
                                    </tbody>
      </table>
    </div>
  </div>
</div>
          </div>
          <div class="col-xxl-6">
    <div class="card">
        <div class="card-body">
            <h4 class="card-title">Top 5 Layanan</h4>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr class="text-uppercase">
                            <th>No</th>
                            <th>Layanan</th>
                            <th>Jumlah</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $no = 1;
                        $query_list = mysqli_query($db, "SELECT service, COUNT(*) AS jumlah FROM orders GROUP BY service ORDER BY jumlah DESC LIMIT 5"); // Ganti nama_tabel_pesanan dengan nama tabel pesanan Anda
                        while ($data_hof = mysqli_fetch_assoc($query_list)) {
                        ?>
                        <tr class="table-<?php echo $label; ?>">
                            <td><?php echo $no; ?></td>
                            <td><?php echo $data_hof['service']; ?></td>
                            <td><?php echo $data_hof['jumlah']; ?></td>
                        </tr>
                        <?php
                            $no++;
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
              </div>
   </div>
   <!-- Container-fluid Ends-->
</div>
<?php
	include("../lib/footer.php");
} else {
	header("Location: ".$cfg_baseurl);
}
?>