<?php
session_start();
include_once "../config.php";
require_once '../database1.php';
require_once "../check_admin.php";

class mostPurchasedProduct extends database
{
    public function index()
    { 
        $sql = " SELECT p.*, COUNT(*) AS product_count FROM tb_checkout_item ci INNER JOIN tb_product p ON p.cl_id=ci.cl_product_id WHERE ci.cl_deleted_at IS NULL GROUP BY p.cl_id ORDER BY product_count DESC LIMIT 0,100 ";
        return $this->callQuery($sql);
    }
    
}

$obj = new mostPurchasedProduct();	
$products = $obj->index();


include '../header.php';
?>
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">Most Purchased Products (last 100 items)</h2>
                                    
                                </div>
								
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
							<div class="col-lg-12">
                                                         
                                <div class="table-responsive table--no-card m-b-30 top-margin pull-left">
                                    <table class="table table-borderless table-data3" style="width: 100%">
                                        <thead>
                                            <tr>
												
                                               
                                                <th>Product Name</th>
                                                <th>Supplier</th>
                                                <th>Manufacturer</th>
                                                <th>Product ID</th>
                                                <th>Total Product Purchased</th>
											
                                            </tr>
                                        </thead>
                                        <tbody>
                                           <?php
                                            foreach($products AS $product)
                                            {

                                            ?>
                                            <tr class="action-container">


                                                    <td>
                                                            <?php echo htmlspecialchars($product['cl_name']); ?>
                                                    </td>
                                                    <td>
                                                            <?php echo htmlspecialchars($product['cl_supplier']); ?>
                                                    </td>
                                                    <td>
                                                            <?php echo htmlspecialchars($product['cl_manufacturer']); ?>
                                                    </td>
                                                    <td>
                                                            <?php echo htmlspecialchars($product['cl_product_id']); ?>
                                                    </td>
                                                    <td>
                                                            <?php echo htmlspecialchars($product['product_count']); ?>
                                                    </td>
                                            </tr>
                                            <?php
                                            }
                                            ?>
                                           
                                        </tbody>
                                    </table>
									
                                </div>
                            </div>
                            
                          
                        </div>
                        
                       <script>
						$(document).ready(function(){
							$('.active').removeClass('active');
							$('#customer-menu').addClass('active');
						}); 
						</script>               

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