<?php
include("includes/header.php");

$qry_cat = "SELECT COUNT(*) as num FROM tbl_category";
$total_category = mysqli_fetch_array(mysqli_query($mysqli, $qry_cat));
$total_category = $total_category['num'];


$cat_qry = "SELECT * FROM tbl_category ORDER BY tbl_category.cid DESC LIMIT 5";
$cat_result2 = mysqli_query($mysqli, $cat_qry);

$cat_qry = "SELECT * FROM tbl_category";
$cat_result = mysqli_query($mysqli, $cat_qry);
if (!$cat_result) {
	die("Error in SQL query: " . mysqli_error($mysqli));
}
$categories = array();
while ($row = mysqli_fetch_array($cat_result)) {
	$categories[$row['cid']] = $row['category_name'];
}



$tbl_series = "SELECT COUNT(*) as num FROM tbl_iteams";
$tbl_series = mysqli_fetch_array(mysqli_query($mysqli, $tbl_series));
$tbl_series = $tbl_series['num'];

$books_qry = "SELECT * FROM tbl_iteams
LEFT JOIN tbl_category ON tbl_iteams.cat_id= tbl_category.cid
ORDER BY tbl_iteams.itemid DESC LIMIT 5";
$books_result = mysqli_query($mysqli, $books_qry);


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

    <div class="pagetitle">
        <h1>Dashboard</h1>
        <nav>
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                <li class="breadcrumb-item active">Dashboard</li>
            </ol>
        </nav>
    </div><!-- End Page Title -->
<?php
		if (PURCHASE == "disabled") {
		?>
			<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
				<div class="alert alert-danger alert-dismissible  in" role="alert">
					<h4 id="oh-snap!-you-got-an-error!">
						<i class="fa fa-hand-o-right"></i> This is only demo.
					</h4>
					<p style="margin-bottom: 10px">You have no right operate to <strong>ADD,</strong> <strong>EDIT</strong> and <strong>DELETE</strong> function.</p>
				</div>
			</div>

		<?php
		}
		?>
    <section class="section dashboard">
        <div class="row">

            <!-- Left side columns -->
            <div class="col-lg-12">
                <div class="row">

                    <!-- Sales Card -->
                    <div class="col-xxl-3 col-md-6">
                        <div class="card info-card sales-card">
                            <a href="manage_category.php">
                                <div class="card-body">
                                    <h5 class="card-title">Categories</h5>

                                    <div class="d-flex align-items-center">
                                        <div class="card-icon rounded-circle d-flex align-items-center justify-content-center">
                                            <i class="bi-diagram-3-fill"></i>
                                        </div>
                                        <div class="ps-3">
                                            <h6><?php _e(thousandsNumberFormat($total_category)); ?></h6>
                                        </div>
                                    </div>
                                </div>
                            </a>
                        </div>
                    </div><!-- End Sales Card -->
                   
                    <div class="col-xxl-3 col-xl-12">

                        <div class="card info-card users-card">
                            <a href="manage_album.php">
                                <div class="card-body">
                                    <h5 class="card-title">Iteams</h5>

                                    <div class="d-flex align-items-center">
                                        <div class="card-icon rounded-circle d-flex align-items-center justify-content-center">
                                            <i class="bi bi-tv-fill"></i>
                                        </div>
                                        <div class="ps-3">
                                            <h6><?php _e(thousandsNumberFormat($tbl_series)); ?></h6>
                                        </div>
                                    </div>

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


				</div>
                    </div>
                </div>
                	<!-- Top Selling -->
					<div class="col-6">
						<div class="card top-selling overflow-auto">
							<div class="card-body pb-0">
								<h5 class="card-title">Recent Drawing</h5>
								<div class="table-responsive">
									<table class="table table-borderless">
										<thead>
											<tr>
												<th scope="col">Category</th>
												<!--<th scope="col">Artist</th>-->
												<th scope="col">Title</th>
												<th scope="col">Image</th>
											</tr>
										</thead>
										<tbody>
											<?php
											while ($books_row = mysqli_fetch_array($books_result)) {
											?>
												<tr>
													<td scope="row">
														<?php
														$cat_ids = explode(',', $books_row['cat_id']);
														foreach ($cat_ids as $cat_id) {
															if (isset($categories[$cat_id])) {
																_e($categories[$cat_id] . ",");
															}
														}
														?>
													</td>
													<!--<td>< ?php _e($books_row['artist_name']); ?></td>-->
													<td><?php _e($books_row['iteam_name']); ?></td>
													<th scope="row"><img src="images/<?php _e($books_row['image']); ?>" alt=""></th>
												</tr>
											<?php
											}
											?>
										</tbody>
									</table>
								</div>
							</div>

						</div>
					</div><!-- End Top Selling -->
					<div class="col-6">
						<div class="card top-selling overflow-auto">
							<div class="card-body pb-0">
								<h5 class="card-title">Recent Category</h5>
								<div class="table-responsive">
									<table class="table table-borderless">
										<thead>
											<tr>
												<th scope="col">Category</th>
												<th scope="col">Image</th>
											</tr>
										</thead>
										<tbody>
											<?php
											while ($category = mysqli_fetch_array($cat_result2)) {
											?>
												<tr>
													<!--<td>< ?php _e($books_row['artist_name']); ?></td>-->
													<td><?php _e($category['category_name']); ?></td>
													<th scope="row"><img src="images/<?php _e($category['category_image']); ?>" alt=""></th>
												</tr>
											<?php
											}
											?>
										</tbody>
									</table>
								</div>
							</div>

						</div>
					</div><!-- End Top Selling -->
            </div>
        </div>
    </section>

</main><!-- End #main -->
<?php include("includes/footer.php"); ?>