<?php include('includes/connectdb.php') ?>
<?php include('includes/head.php') ?>
<body>
    <div class="page-wrapper">
        <?php include('includes/header.php') ?>

        <main class="main">
            <nav aria-label="breadcrumb" class="breadcrumb-nav">
                <div class="container">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index"><i class="icon-home"></i></a></li>
                        <li class="breadcrumb-item active" aria-current="page">Home</li>
                    </ol>
                </div><!-- End .container -->
            </nav>
            <div class="container">
                <div class="row row-sm">
				    <?php  
					   if(isset($_GET['q'])){
						   $keyword = $_GET['q'];
						   $sql = mysqli_query($con, "SELECT * FROM product WHERE 
														   section = 'supermarket' 
														   OR section = 'pharmacy'
														   AND product_name LIKE '%" . $keyword . "%' 
														   OR category_name LIKE '%" . $keyword ."%'
														   OR product_info LIKE '%" . $keyword ."%'
														   OR price = '  $keyword  '
													  LIMIT 64
					   ");
					   }else{
						 $sql = mysqli_query($con, "SELECT * FROM product WHERE section = 'supermarket' OR section = 'pharmacy' ORDER BY RAND() LIMIT 64");
					   }
					   while($row = mysqli_fetch_array($sql)):
					   $images = explode(',', $row['image_url']);
					   $product_name = $row['product_name'];
					?>
                    <div class="col-6 col-md-4 col-xl-2">
                        <div class="product">
                            <figure class="product-image-container">
                                <a href="product?pid=<?php echo $row['product_id'] ?>" class="product-image">
									<img src="uploads/<?php echo $row['section'] . '/' .  $images[0] ?>" alt="product">
								</a>
                                <a href="ajax/product-quick-view?pid=<?php echo $row['product_id']; ?>" class="btn-quickview">Quick View</a>
                            </figure>
                            <div class="product-details">
                                <h2 class="product-title">
                                    <a data-toggle="tooltip" title="<?php echo $row['product_name'] ?>" href="product?pid=<?php echo $row['product_id'] ?>"><?php echo substr($row['product_name'], 0,23) ?></a>
                                </h2>
                                <div class="price-box">
                                    <span class="product-price"><?php echo '₦' . number_format($row['price']) ?></span>
                                </div><!-- End .price-box -->
                                <div class="product-action">
									<a onclick="addToCart('<?php echo $row['section'] ?>',<?php echo $row['product_id'] ?>,<?php echo $row['price']?>,'<?php echo $product_name ?>','<?php echo $images[0] ?>')" class="paction add-cart" title="Add to Cart">
										<span class="pointer">Add to Cart</span>
									</a>
								</div><!-- End .product-action -->
                            </div><!-- End .product-details -->
                        </div><!-- End .product -->
                    </div><!-- End .col-xl-3 -->
					<?php endwhile;  ?>
                </div><!-- End .row -->
            </div><!-- End .container -->
          <div class="mb-5"></div><!-- margin -->
        </main><!-- End .main -->

        <?php include('includes/footer.php') ?>
    </div><!-- End .page-wrapper -->

    <div class="mobile-menu-overlay"></div><!-- End .mobil-menu-overlay -->

    <?php include('includes/mobile-menu.php') ?>

    <a id="scroll-top" href="#top" title="Top" role="button"><i class="icon-angle-up"></i></a>

    <!-- Plugins JS File -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/plugins.min.js"></script>
    <script src="assets/js/nouislider.min.js"></script>

    <!-- Main JS File -->
    <script src="assets/js/main.min.js"></script>
</body>

</html>
<script>
function addToCart(section, pid, price, product_name, product_image){
	
	var query = 'add';
	var quantity = 1;
	var action = 'increment';
	
	$.ajax({
		type: "POST",
		url: "process.php",
		data: {
			query_cart : 1,
			query : query,
			section : section,
			product_id : pid,
			product_name : product_name,
			product_image : product_image,
			price: price,
			quantity : quantity,
			action : action
		},
		success: function(data){
			    location.reload(true);
		}
	});
	
}
</script>