<script type="text/javascript">
  var displayList = false;
  var page_name = 'tt';
  var quickView = false;
  var request = window.location.href;
  var display = "grid";
</script>

<?php // BEGIN PHP
includeContainer('_library_page_lib.php', 1);
$current_lang = $_SESSION['lang']['code'];
$current_lang_short_code = $_SESSION['lang']['shortcode'];


$root_cat_object = new Categorie($db);

if (GETPOST('cat', 'alpha')) {
	$root_category_id = GETPOST('cat', 'alpha');
}
if (GETPOST('extcat', 'alpha')) {
	$root_category_extid = GETPOST('extcat', 'alpha');
}

$tag = GETPOST('tag', 'alpha');
$list = GETPOST('list', 'alpha');
$search_words = GETPOST('search_query', 'alpha');
$cat = GETPOST('cat', 'alpha');
$extcat = GETPOST('extcat', 'alpha');

$isHomePage = !GETPOST('tag', 'alpha') && !GETPOST('cat', 'alpha') && !GETPOST('list', 'alpha') && !GETPOST('search_query', 'alpha') && !GETPOST('extcat', 'alpha');

if (!empty($search_words)) {
    $keywords = explode(" ", $search_words);
    $request = '';
    $order = 'ORDER BY CASE ';

    foreach ($keywords as $key => $value) {
        if ($key != 0) {
            $request .= " AND ";
        }
        $value = $db->escape($db->escapeforlike($value));

        // Build the search conditions for labels, notes, and vendor names
        $request .= "(ol.label LIKE '%" . $value . "%' OR 
                    ol.note LIKE '" . $value . " %'  OR 
                    ol.note LIKE '% " . $value . " %'  OR 
                    ol.note LIKE '% " . $value . "' OR
                    ol.note LIKE '%>" . $value . " %' OR
                    ol.note LIKE '% " . $value . "<%' OR
                    s.nom LIKE '%" . $value . "%')";

        // Define the order of results based on matching criteria
        // Level 1: Full match in the label
        $order .= "WHEN ol.label LIKE '" . $value . " %' THEN 1 ";
        $order .= "WHEN ol.label LIKE '% " . $value . " %' THEN 1 ";
        $order .= "WHEN ol.label LIKE '% " . $value . "' THEN 1 ";

        // Level 2: Partial match in the label or vendor name
        $order .= "WHEN ol.label LIKE '%" . $value . "%' THEN 2 ";
        $order .= "WHEN s.nom LIKE '%" . $value . "%' THEN 2 ";

        // Level 3: Full Match in the notes(Long description) field
        $order .= "WHEN ol.note LIKE '" . $value . " %' THEN 3 ";
        $order .= "WHEN ol.note LIKE '% " . $value . " %' THEN 3 ";
        $order .= "WHEN ol.note LIKE '% " . $value . "' THEN 3 ";
        $order .= "WHEN ol.note LIKE '%>" . $value . " %' THEN 3 ";
        $order .= "WHEN ol.note LIKE '% " . $value . "<%' THEN 3 ";

        //$order .= "WHEN ol.description LIKE '%" . $value . "%' THEN 4 ";
    }
    
    // Finalize filter and order
    $filter = "($request) AND (o.tosell = 1) ";
    $order .= " END, o.datec DESC";  // Complete the order clause
} else {
    $filter = 'o.tosell = 1';
}

$page_no = GETPOST('p', 'alpha');
if(empty($page_no)) {
	$page_no = 1;
}

$limit = GETPOST('n', 'alpha');
if(empty($limit)) {
	$limit = 11;
}else{
	$limit = $limit -1;
}

$orderby = GETPOST('orderby', 'alpha');
switch ($orderby) {
	case 'price':
		$sortfield = 'price_ttc';
		break;
	case 'name':
		$sortfield = 'label';
		break;
	case 'reference':
		$sortfield = 'ref';
		break;
	default:
		$sortfield = 'datec';
		break;
}

$orderway = GETPOST('orderway', 'alpha');
switch ($orderway) {
	case 'asc':
		$sortorder = "ASC";
		break;
	case 'desc':
		$sortorder = "DESC";
		break;
	default:
		$sortorder = "DESC";
		break;
}

if ($page_no == 1) {
	$offset = 0;
}else{
	$offset = ( $limit * ($page_no-1) )+($page_no - 1);
}

$id = null;
$type  = Categorie::TYPE_PRODUCT;
$is_for_sell = 1;

if (empty($tag)) {
	if (isset($root_category_id) || isset($root_category_extid)) {
	    if (isset($root_category_id)) {
	        $result = $root_cat_object->fetch($root_category_id,"");
	    } else {
	        $result = $root_cat_object->fetch('', '', Categorie::TYPE_PRODUCT, $root_category_extid);
	    }
		
		if ($result <= 0) {
			redirectToContainer('404');
		}
		$root_category_id = $root_cat_object->id;
		$root_category_label = $root_cat_object->label;
	} else {
	    if ($isHomePage && getDolGlobalInt('MARKETPLACE_ROOT_CATEGORY_ID_BIS')) {
    	    // To force ID of category for v20 products only on Home page
    		$result = $root_cat_object->fetch(getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID_BIS"));
	    } else {
    		$result = $root_cat_object->fetch(getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID"));
	    }
		if ($result <= 0) {
			redirectToContainer('404');
		}
		$root_category_label = $root_cat_object->label;
		$root_category_id = $root_cat_object->id;
	}
} else {
    if ($tag == "Specials") {
        $result = $root_cat_object->fetch(getDolGlobalInt("MARKETPLACE_SPECIAL_CATEGORY_ID"));
    } else {
        $result = $root_cat_object->fetch("", $tag);
    }
	
	if ($result <= 0) {
		dol_print_error($db, $root_cat_object->error); exit;
	}
	$root_category_label = $root_cat_object->label;
	$root_category_id = $root_cat_object->id;
}
$root_category_type = $root_cat_object->type;

// If Goodies category, redirect to specific page
if ($root_category_label == 'Goodies') {
    redirectToContainer('goodies');
}


if($root_category_id == getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID") && empty($tag) && empty($search_words) && $list == "new") {
    $delay_for_new = (int) getDolGlobalInt('MARKETPLACE_DELAY_FOR_NEW', 30);
    $dateTime = new \DateTime();
    $interval = new \DateInterval('P'.$delay_for_new.'D');
    $dateTime->sub($interval);
    $request = "o.datec > '" . $dateTime->format('Y-m-d H:i:s') . "'";
    $filter = "(".$request . ") AND (o.tosell=1)";
}

// END PHP ?>

<?php
// Count SQL
/*$countSql = "SELECT c.fk_product ";
$countSql .= "FROM llx_categorie_product as c, ";
$countSql .= "llx_product as o, ";
$countSql .= "llx_product_lang as ol ";
$countSql .= "WHERE o.entity IN (1) AND c.fk_categorie = ".$root_category_id." AND c.fk_product = o.rowid AND ol.fk_product = o.rowid ";
$countSql .= "AND ol.lang = '".$db->escape($current_lang)."' AND ";
$countSql .= $filter;

$resql = $db->query($countSql);
$count_prods = $resql->num_rows;

// PRODUCT SQL
$sql = "SELECT c.fk_product as id, o.ref, o.ref_ext, ol.label, ol.description, o.datec, o.price_ttc ";
$sql .= "FROM llx_categorie_product as c, ";
$sql .= "llx_product as o, ";
$sql .= "llx_product_lang as ol ";
$sql .= "WHERE o.entity IN (1) AND c.fk_categorie = ".$root_category_id." AND c.fk_product = o.rowid AND ol.fk_product = o.rowid ";
$sql .= "AND ol.lang = '".$db->escape($current_lang)."' AND ";
$sql .= $filter;*/

// Count SQL
$countSql = "SELECT c.fk_product ";
$countSql .= "FROM llx_categorie_product as c ";
$countSql .= "INNER JOIN llx_product as o ON c.fk_product = o.rowid ";
$countSql .= "INNER JOIN llx_product_lang as ol ON ol.fk_product = o.rowid ";
$countSql .= "LEFT JOIN llx_product_fournisseur_price as pfp ON pfp.fk_product = o.rowid ";
$countSql .= "LEFT JOIN llx_societe as s ON pfp.fk_soc = s.rowid ";
$countSql .= "WHERE o.entity IN (1) AND c.fk_categorie = " . ((int) $root_category_id) . " AND ";
$countSql .= "ol.lang = '" . $db->escape($current_lang) . "' AND ";
$countSql .= $filter;
$sql .= " GROUP BY c.fk_product "; // Added GROUP BY clause to handle multiple supplier prices

$resql = $db->query($countSql);
$count_prods = $resql->num_rows;

// PRODUCT SQL
$sql = "SELECT c.fk_product as id, o.ref, o.ref_ext, ol.label, ol.description, o.datec, o.price_ttc ";
$sql .= "FROM llx_categorie_product as c ";
$sql .= "INNER JOIN llx_product as o ON c.fk_product = o.rowid ";
$sql .= "INNER JOIN llx_product_lang as ol ON ol.fk_product = o.rowid ";
$sql .= "LEFT JOIN llx_product_fournisseur_price as pfp ON pfp.fk_product = o.rowid ";
$sql .= "LEFT JOIN llx_societe as s ON pfp.fk_soc = s.rowid ";
$sql .= "WHERE o.entity IN (1) AND c.fk_categorie = " . ((int) $root_category_id) . " AND ";
$sql .= "ol.lang = '" . $db->escape($current_lang) . "' AND ";
$sql .= $filter;
$sql .= " GROUP BY c.fk_product, o.ref, o.ref_ext, ol.label, ol.description, o.datec, o.price_ttc, s.nom"; // Added GROUP BY clause to handle multiple supplier prices

if ($sortfield == 'datec' && $sortorder == 'DESC' && !empty($search_words)) {
    $sql .= " " . $order;
} else {
    // Home page: random order
    if ($isHomePage) {
        $sql .= " ORDER BY RAND() ";
    } else {
        $sql .= $db->order($sortfield, $sortorder);
    }
}

if ($limit > 0 || $offset > 0) {
	$sql .= $db->plimit($limit + 1, $offset);
}

$total_no_of_pages = ceil($count_prods / ($limit + 1));
$second_last = $total_no_of_pages - 1; // total pages minus 1
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$test = "";
?>
<?php if ($count_prods > 0 && (strlen($search_words) == 0 || strlen($search_words) > 1)): ?>
	<?php if (GETPOST('cat', 'alpha') != '' || GETPOST('extcat', 'alpha') != ''): ?>
	    <section id="category_info_section" contenteditable="true">
    		<div class="content_scene_cat">
    		    <?php
        		    $__PAGE__TITLE__ = dol_string_nohtmltag($root_cat_object->label);
                    $__PAGE__DESC__ = dol_string_nohtmltag($root_cat_object->description);
    		    ?>
    			<!-- Category image -->
    			
    			<div class="content_scene_cat_bg">
    				<div class="cat_desc">
    					<?php
    					    if ($root_cat_object->id == getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID")) {
                                print '<span class="category-name">' . dol_string_nohtmltag($weblangs->trans("newProducts")) . '</span>';
                            } else {
                                print '<span class="category-name">' . dol_string_nohtmltag($root_cat_object->label) . '</span>';
                            }
                        ?>
    					<div class="rte">
    						<p>
    						<?php 
    						    if ($root_cat_object->id != getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID")) {
    						        print dol_string_nohtmltag($root_cat_object->description);
    						    }
    						?>
						    </p>
    					</div>
    				</div>
    			</div>
    			
    		</div>
		</section>
	<?php endif ?>

	<?php if (GETPOST('tag', 'alpha') != "" || GETPOST('cat', 'alpha') != "" || GETPOST('search_query', 'alpha') != "" || GETPOST('extcat', 'alpha') != ''): ?>
	<section id="search_info_section" contenteditable="true">
		<h1 class="page-heading product-listing">
			<?php if (GETPOST('cat', 'alpha') != '' || GETPOST('extcat', 'alpha') != ''): ?>
				<span class="cat-name"><?php if ($root_cat_object->id != getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID")) {print $root_cat_object->label;} ?></span>
			<?php else: ?>
				<span class="cat-name"><?php print $weblangs->trans("Search"); ?>
				<?php if (!empty($tag)): ?>
					"<?php print $tag; ?>"</span>
				<?php endif ?>
				<?php if (!empty($keywords)): ?>
					<?php foreach ($keywords as $key => $value): ?>
						"<?php print $value; ?>"</span>
					<?php endforeach ?>
				<?php endif ?>
			<?php endif ?>
			<span class="heading-counter"><?php print $weblangs->trans("nbrOfProducts", $count_prods); ?></span>
			
		</h1>
	</section>
	<?php endif ?>

	<?php if (GETPOST('tag', 'alpha') != "" || GETPOST('cat', 'alpha') != "" || GETPOST('search_query', 'alpha') != "" || GETPOST('extcat', 'alpha') != ''): ?>
		<div class="content_sortPagiBar clearfix">
		<div class="sortPagiBar clearfix">
		    
			<ul class="display hidden-xs">
    			<li id="grid" class="selected">
    				<a rel="nofollow" href="#" title="Grid">
    				<i class="icon-th-large"></i><?php print $weblangs->trans("grid"); ?> </a>
    			</li>
    			<li id="list">
    				<a rel="nofollow" href="#" title="List">
    				<i class="icon-th-list"></i><?php print $weblangs->trans("list"); ?> </a>
    			</li>
			</ul>
			<form id="productsSortForm" action="" class="productsSortForm">
			<div class="select selector1">
				<div class="" id="uniform-selectProductSort">
				<!--<span style="width: 180.4px; user-select: none;">Price: Lowest first</span>-->
				<select id="selectProductSort" class="selectProductSort form-control" style="width: 220px;">
					<!--<option value="">--</option>-->
					<option value="date:desc" <?php (($orderway == "desc" && $orderby == "date") || empty($orderway)) ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("dateNewestFirst"); ?></option>
					<option value="price:asc" <?php ($orderway == "asc" && $orderby == "price") ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("priceLowestFirst"); ?></option>
					<option value="price:desc" <?php ($orderway == "desc" && $orderby == "price") ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("priceHighestFirst"); ?></option>
					<option value="name:asc" <?php ($orderway == "asc" && $orderby == "name") ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("productNameAToZ"); ?></option>
					<option value="name:desc" <?php ($orderway == "desc" && $orderby == "name") ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("productNameZToA"); ?></option>
					<option value="reference:asc" <?php ($orderway == "asc" && $orderby == "reference") ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("referenceLowestFirst"); ?></option>
					<option value="reference:desc" <?php ($orderway == "desc" && $orderby == "reference") ? print 'selected="selected"' : '' ; ?>><?php print $weblangs->trans("referenceHighestFirst"); ?></option>
				</select>
				</div>
			</div>
			</form>
			<!-- /Sort products -->
			<!-- nbr product/page -->
			<form action="" method="get" class="nbrItemPage">
			<div class="clearfix selector1">
				&nbsp; 
				<div class="" id="uniform-nb_item" style="width: 57.4px;">
				<!--<span style="width: 47.4px; user-select: none;">12</span>-->
				<select name="n" id="nb_item" class="form-control">
					<option value="12" <?php (($limit + 1) == 12) ? print 'selected="selected"' : '' ; ?>>12</option>
					<option value="24" <?php (($limit + 1) == 24) ? print 'selected="selected"' : '' ; ?>>24</option>
					<option value="60" <?php (($limit + 1) == 60) ? print 'selected="selected"' : '' ; ?>>60</option>
				</select>
				</div>
				<span class="opacitymedium"><?php print $weblangs->trans("productPerPage"); ?></span>
			</div>
			</form>
			<!-- /nbr product/page -->
		</div>
		

		<div class="top-pagination-content clearfix">
            <?php if ($total_no_of_pages > 1): ?>
                <!-- show all -->
                <div id="pagination" class="pagination clearfix">
                    <form class="showall" action="" method="get">
                        <div>
                            <button type="submit" class="btn btn-default button exclusive-medium">
                                <span><?php print $weblangs->trans("showAll"); ?></span>
                            </button>
                            <input name="n" id="nb_item" class="hidden" value="<?php print $count_prods; ?>">
                        </div>
                    </form>
        
                    <!-- show all -->
                    <ul class="pagination">
                        <li id="pagination_previous" class="pagination_previous" <?php if($page_no <= 1){ echo "disabled"; } ?>>
                            <a <?php if($page_no > 1){ echo "href='?p=$previous_page'"; } ?>>
                                <i class="icon-chevron-left"></i>
                                <b><?php print $weblangs->trans("previous"); ?></b>
                            </a>
                        </li>
        
                        <?php
                        $start_page = max(1, $page_no - 2);
                        $end_page = min($total_no_of_pages, $page_no + 2);
        
                        if ($total_no_of_pages <= 10) {
                            // Affiche toutes les pages si <= 10
                            for ($counter = 1; $counter <= $total_no_of_pages; $counter++) {
                                if ($counter == $page_no) {
                                    echo "<li class='active current'><span><span>$counter</span></span></li>";  
                                } else {
                                    echo "<li><a href='?p=$counter'><span>$counter</span></a></li>";
                                }
                            }
                        } else {
                            if ($start_page > 1) {
                                echo "<li><a href='?p=1'><span>1</span></a></li>";
                                if ($start_page > 2) {
                                    echo "<li><span><span><a>...</a></span></span></li>";
                                }
                            }
                            for ($counter = $start_page; $counter <= $end_page; $counter++) {
                                if ($counter == $page_no) {
                                    echo "<li class='active'><span><span>$counter</span></span></li>";
                                } else {
                                    echo "<li><a href='?p=$counter'><span>$counter</span></a></li>";
                                }
                            }
                            if ($end_page < $total_no_of_pages) {
                                if ($end_page < $total_no_of_pages - 1) {
                                    echo "<li><span><span><a>...</a></span></span></li>";
                                }
                                echo "<li><a href='?p=$total_no_of_pages'><span>$total_no_of_pages</span></a></li>";
                            }
                        }
                        ?>
        
                        <li class="pagination_next <?php if($page_no >= $total_no_of_pages){ echo 'disabled'; } ?>">
                            <a <?php if($page_no < $total_no_of_pages) { echo "href='?p=$next_page'"; } ?>>
                                <b><?php print $weblangs->trans("next"); ?></b>
                                <i class="icon-chevron-right"></i>
                            </a>
                        </li>
                    </ul>
                </div>
            <?php endif ?>
        
            <div class="product-count">
                <?php print $weblangs->trans("showing"); ?> 
                <?php print $offset + 1; ?> - 
                <?php if(($offset + $limit +1) > $count_prods) { print $count_prods; } else { print $offset + $limit +1; } ?> 
                <?php print $weblangs->trans("of"); ?> 
                <?php print $count_prods; ?> 
                <?php print $weblangs->trans("items"); ?>
            </div>
        </div>

		
		</div>
	<?php endif ?>

	<ul id="homefeatured" class="product_list grid row homefeatured tab-pane">
		<?php

		//$prods = $root_cat_object->getObjectsInCateg(Categorie::TYPE_PRODUCT, 0,$limit, $offset,$sortfield,$sortorder,$filter,'',$current_lang);
		//foreach ($prods as $key => $product){
		
		if ($resql = $db->query($sql)) {
		    $key = 0;
			while ($product = $resql->fetch_object()) {
			    $product->element = "product";
    		    // check discount and calculate discounted price
    		    $discount = marketplace_check_discount($product->id);
    		    $discounted_price = marketplace_calculate_discounted_price($product->price_ttc, $discount);
    		    //$discount = function_exists('marketplace_check_discount') ? marketplace_check_discount($product->id) : 0;
    		    //$discounted_price = function_exists('marketplace_calculate_discounted_price') ? marketplace_calculate_discounted_price($product->price_ttc, $discount) : $product->price_ttc;
    
    			$thumb = getImagePublicURLOfObject($product, 1, '_small');
    			// Add some css class for each position
    			switch ($key) {
        			case '0':
        				$position_class = "first-in-line first-item-of-tablet-line first-item-of-mobile-line";
        				break;
        			case '1':
        				$position_class = "last-item-of-mobile-line";
        				break;
        			case '2':
        				$position_class = "last-item-of-tablet-line first-item-of-mobile-line";
        				break;
        			case '3':
        				$position_class = "last-in-line first-item-of-tablet-line last-item-of-mobile-line";
        				break;
        			case '4':
        				$position_class = "first-in-line first-item-of-mobile-line";
        				break;
        			case '5':
        				$position_class = "last-item-of-tablet-line last-item-of-mobile-line";
        				break;
        			case '6':
        				$position_class = "first-item-of-tablet-line first-item-of-mobile-line";
        				break;
        			case '7':
        				$position_class = "last-in-line last-item-of-mobile-line";
        				break;
        			case '8':
        				$position_class = "first-in-line last-item-of-tablet-line first-item-of-mobile-line";
        				break;
        			case '9':
        				$position_class = "first-item-of-tablet-line last-item-of-mobile-line";
        				break;
        			case '10':
        				$position_class = "first-item-of-mobile-line";
        				break;
        			case '11':
        				$position_class = "last-in-line last-item-of-tablet-line last-item-of-mobile-line";
        				break;
        			case '12':
        				$position_class = "first-in-line last-line first-item-of-tablet-line first-item-of-mobile-line";
        				break;
        			case '13':
        				$position_class = "last-line last-item-of-mobile-line";
        				break;
        			case '14':
        				$position_class = "last-line last-item-of-tablet-line first-item-of-mobile-line last-mobile-line";
        				break;
        			case '15':
        				$position_class = "last-in-line last-line first-item-of-tablet-line last-item-of-mobile-line last-mobile-line";
        				break;
    			}
        		?>
        		<li class="ajax_block_product col-xs-12 col-sm-4 col-md-3 <?php print $position_class; ?>">
        		<div class="product-container">
        		    
        			<div class="left-block">
        			<div class="product-image-container">
            		    <?php
            		    $date_creation = $db->jdate($product->datec);
            		    if ($date_creation >= strtotime('-'.getDolGlobalString('MARKETPLACE_DELAY_FOR_NEW', 30).' days')) { ?>
            		    <!-- for ban New -->
            		    <a class="new-box" href="/">
    								<span class="new-label"><?php print $langs->trans("New"); ?></span>
    					</a>
            		    <?php } ?>

        			    
        				<a class="product_img_link" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>" title="<?php print $product->label; ?>">
        					<img class="replace-2x img-responsive" src="<?php print $thumb; ?>" alt="" title="<?php print $product->label; ?>" width="250" height="250" />
        				</a>
        				<div class="content_price">
            				<?php if ($product->price_ttc > 0): ?>
            				    <?php if ($discounted_price != $product->price_ttc): ?>
                                	<span class="price product-price original-price"> <?php print price($product->price_ttc); ?> € </span>
                                	<span class="price product-price"> <?php print price($discounted_price); ?> € </span>
                                <?php else: ?>
            					    <span class="price product-price"> <?php print price($product->price_ttc); ?> € </span>
            					<?php endif ?>
            				<?php else: ?>
                			    <span class="price product-price"> FREE </span>
                			<?php endif ?>
        				</div>
        			</div>
        			</div>
        			<div class="right-block">
        			<h5 class="list-grid-product-title">
        				<a class="product-name truncate2" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>" title="<?php print $product->label; ?>"> <?php print $product->label; ?> </a>
        			</h5>
        			<p class="product-desc truncate2"> 
        				<?php 
        				if (!empty($product->description)){
        					print dol_string_nohtmltag($product->description); 
        				}
        				?> 
        			</p>
        			<div class="content_price">
        			    <?php if ($product->price_ttc > 0): ?>
            			    <?php if ($discounted_price != $product->price_ttc): ?>
                            	<span class="price product-price original-price"> <?php print price($product->price_ttc); ?> € </span>
                            	<span class="price product-price"> <?php print price($discounted_price); ?> € </span>
                            <?php else: ?>
            				    <span class="price product-price"> <?php print price($product->price_ttc); ?> € </span>
            				<?php endif ?>
            			<?php else: ?>
            			    <span class="price product-price"> FREE </span>
            			<?php endif ?>
        			</div>
        			<div class="button-container">
        			    <form class="addtocart" method="post">
        			        <input type="hidden" name="product_id" value="<?php print $product->id; ?>">
        			        <input type="hidden" name="product_thumb" value="<?php print getImagePublicURLOfObject($product,1); ?>">
        			        <input type="hidden" name="product_quantity" value="1">
        			        <?php if ($product->price_ttc > 0): ?>
        			            <button type="submit" class="button ajax_add_to_cart_button"><span><?php print $weblangs->trans("addToCart"); ?></span></button>
        			        <?php endif ?>
            				<a class="button lnk_view btn btn-default" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>" title="View">
            				    <span><?php print $weblangs->trans("More"); ?></span>
            				</a>
        				</form>
        			</div>
        			<div class="product-flags"></div>
        			<div class="functional-buttons clearfix"></div>
        			</div>
        		</div>
        		<!-- .product-container> -->
        		</li>
		<?php 
			    $key++;
			} 
		}
		?>
	</ul>


	<?php if (GETPOST('tag', 'alpha') != "" || GETPOST('cat', 'alpha') != "" || GETPOST('search_query', 'alpha') != "" || GETPOST('extcat', 'alpha') != ''): ?>
		<div class="content_sortPagiBar">
		    <div class="bottom-pagination-content clearfix">
                <?php if ($total_no_of_pages > 1): ?>
                    <!-- show all -->
                    <div id="pagination" class="pagination clearfix">
                        <form class="showall" action="" method="get">
                            <div>
                                <button type="submit" class="btn btn-default button exclusive-medium">
                                    <span><?php print $weblangs->trans("showAll"); ?></span>
                                </button>
                                <input name="n" id="nb_item" class="hidden" value="<?php print $count_prods; ?>">
                            </div>
                        </form>
            
                        <!-- show all -->
                        <ul class="pagination">
                            <li id="pagination_previous" class="pagination_previous" <?php if($page_no <= 1){ echo "disabled"; } ?>>
                                <a <?php if($page_no > 1){ echo "href='?p=$previous_page'"; } ?>>
                                    <i class="icon-chevron-left"></i>
                                    <b><?php print $weblangs->trans("previous"); ?></b>
                                </a>
                            </li>
            
                            <?php
                            $start_page = max(1, $page_no - 2);  // Deux pages avant la page actuelle
                            $end_page = min($total_no_of_pages, $page_no + 2);  // Deux pages après la page actuelle
            
                            if ($total_no_of_pages <= 10) {
                                // Affiche toutes les pages si <= 10
                                for ($counter = 1; $counter <= $total_no_of_pages; $counter++) {
                                    if ($counter == $page_no) {
                                        echo "<li class='active current'><span><span>$counter</span></span></li>";  
                                    } else {
                                        echo "<li><a href='?p=$counter'><span>$counter</span></a></li>";
                                    }
                                }
                            } else {
                                // Affiche deux pages avant et après la page actuelle
                                if ($start_page > 1) {
                                    echo "<li><a href='?p=1'><span>1</span></a></li>";
                                    if ($start_page > 2) {
                                        echo "<li><span><span><a>...</a></span></span></li>";
                                    }
                                }
                                for ($counter = $start_page; $counter <= $end_page; $counter++) {
                                    if ($counter == $page_no) {
                                        echo "<li class='active'><span><span>$counter</span></span></li>";
                                    } else {
                                        echo "<li><a href='?p=$counter'><span>$counter</span></a></li>";
                                    }
                                }
                                if ($end_page < $total_no_of_pages) {
                                    if ($end_page < $total_no_of_pages - 1) {
                                        echo "<li><span><span><a>...</a></span></span></li>";
                                    }
                                    echo "<li><a href='?p=$total_no_of_pages'><span>$total_no_of_pages</span></a></li>";
                                }
                            }
                            ?>
            
                            <li class="pagination_next <?php if($page_no >= $total_no_of_pages){ echo 'disabled'; } ?>">
                                <a <?php if($page_no < $total_no_of_pages) { echo "href='?p=$next_page'"; } ?>>
                                    <b><?php print $weblangs->trans("next"); ?></b>
                                    <i class="icon-chevron-right"></i>
                                </a>
                            </li>
                        </ul>
                    </div>
                <?php endif ?>
            
                <div class="product-count">
                    <?php print $weblangs->trans("showing"); ?> 
                    <?php print $offset + 1; ?> - 
                    <?php if(($offset + $limit +1) > $count_prods) { print $count_prods; } else { print $offset + $limit +1; } ?> 
                    <?php print $weblangs->trans("of"); ?> 
                    <?php print $count_prods; ?> 
                    <?php print $weblangs->trans("items"); ?>
                </div>
            </div>
        
		</div>
	<?php endif ?>
<?php endif ?>

<?php if ($count_prods == 0 || strlen($search_words) == 1): ?>
		<h1 class="page-heading  product-listing">
			<?php print $weblangs->trans("search"); ?> <span class="heading-counter"><?php print $weblangs->trans("noResultsHaveBeenFound"); ?></span>
		</h1>
		<p class="alert alert-warning"><?php print $weblangs->trans("noResultsWereFound"); ?> </p>
<?php endif ?>