<?php
if (isset($tpl['product_arr']) && !empty($tpl['product_arr']))
{
	$pagination_mode = isset($tpl['pagination_mode']) ? $tpl['pagination_mode'] : (isset($tpl['option_arr']['o_pagination_mode']) ? $tpl['option_arr']['o_pagination_mode'] : 'pagination');
	$cols_desktop = isset($tpl['option_arr']['o_grid_cols_desktop']) ? (int) $tpl['option_arr']['o_grid_cols_desktop'] : 4;
	$cols_laptop = isset($tpl['option_arr']['o_grid_cols_laptop']) ? (int) $tpl['option_arr']['o_grid_cols_laptop'] : 4;
	$cols_tablet = isset($tpl['option_arr']['o_grid_cols_tablet']) ? (int) $tpl['option_arr']['o_grid_cols_tablet'] : 3;
	$cols_mobile = isset($tpl['option_arr']['o_grid_cols_mobile']) ? (int) $tpl['option_arr']['o_grid_cols_mobile'] : 1;
	$col_lg = $cols_desktop > 0 ? floor(12 / $cols_desktop) : 3;
	$col_md = $cols_laptop > 0 ? floor(12 / $cols_laptop) : 3;
	$col_sm = $cols_tablet > 0 ? floor(12 / $cols_tablet) : 4;
	$col_xs = $cols_mobile > 0 ? floor(12 / $cols_mobile) : 12;
	$grid_classes = "col-xs-{$col_xs} col-sm-{$col_sm} col-md-{$col_md} col-lg-{$col_lg}";
    $products_in_fav_arr = array();
    $cookie_value = isset($_COOKIE[$controller->defaultCookie]) && !empty($_COOKIE[$controller->defaultCookie]) ? base64_decode(stripslashes($_COOKIE[$controller->defaultCookie])) : '';
    $favs = unserialize($cookie_value);
    if ($favs) {
        foreach ($favs as $fav => $whatever)
        {
            $item = unserialize($fav);
            $hash = md5(serialize($item));
            $products_in_fav_arr[] = $hash;
        }
    }
	?>
	<div class="container-fluid">
		<ul class="list-unstyled row text-center pjScProducts"<?php if (isset($tpl['paginator'])): ?> data-page="<?php echo (int) $tpl['paginator']['page']; ?>" data-pages="<?php echo (int) $tpl['paginator']['pages']; ?>" data-pagination-mode="<?php echo pjSanitize::html($pagination_mode); ?>"<?php endif; ?>>
			<?php
			foreach($tpl['product_arr'] as $product)
			{
				$slug = NULL;
				if ((int) $tpl['option_arr']['o_seo_url'] === 1)
				{
					# Seo friendly URLs ---------
					$category_id = NULL;
					if (!empty($product['category_ids']))
					{
						$category_id = max($product['category_ids']);
					}
		
					$category_slug = array();
					if (!is_null($category_id))
					{
						$arr = array();
						pjUtil::getBreadcrumbTree($arr, $tpl['category_arr'], $category_id);
						krsort($arr);
						$arr = array_values($arr);
											
						foreach ($arr as $k => $category)
						{
							$category_slug[] = pjAppController::friendlyURL($category['data']['name']);
						}
					}
				
					$slug = sprintf("%s-%u.html", pjAppController::friendlyURL($product['name']), $product['id']);
					if (!empty($category_slug))
					{
						$slug = join("/", $category_slug) . '/' . $slug;
					}
					$href = $tpl['option_arr']['o_install_url'] . '/' . $slug;
				} else {
					# Non-Seo friendly URLs ---------------------
					$href = $tpl['option_arr']['o_install_url'] . '/' . ((!empty($tpl['option_arr']['o_page_prefix'])) ? $tpl['option_arr']['o_page_prefix'] . '-' : '') . 'product/' . $product['id'];
				}
				$cnt_product_images = isset($tpl['product_image_arr'][$product['id']]) ? count($tpl['product_image_arr'][$product['id']]) : 0;
				$src = PJ_INSTALL_URL . PJ_IMG_PATH . 'frontend/noimg.png';
				if (isset($tpl['product_image_arr'][$product['id']][0]['medium_path']) && is_file(PJ_INSTALL_PATH . $tpl['product_image_arr'][$product['id']][0]['medium_path']))
				{
				    $mediumPath = $tpl['product_image_arr'][$product['id']][0]['medium_path'];
				    $src = PJ_INSTALL_URL . $mediumPath . '?v=' . filemtime(PJ_INSTALL_PATH . $mediumPath);
				}
				if (!empty($tpl['option_arr']['o_medium_width'])) {
					$max_width = (int) $tpl['option_arr']['o_medium_width'];
				} else {
					list($max_width, ) = explode(',', PJ_GALLERY_MEDIUM);
				}
				
				$stock_id = $product['stockId'];
				$stock_arr = array();
				$is_available = false;
				if ($product['status'] != 3) {
				    if (!empty($product['stockId_attr']))
				    {
				        $attrs = explode(",", $product['stockId_attr']);
				        foreach ($attrs as $attr)
				        {
				            list($_attr_id, $_attr_parent_id, $_stock_id, $_qty) = explode("_", $attr);
				            if ($product['is_digital'] == 0 && (int)$_qty > 0 && (!isset($tpl['order_arr'][$_stock_id]) || (isset($tpl['order_arr'][$_stock_id]) && ((int)$_qty - (int)$tpl['order_arr'][$_stock_id]) > 0))) {
				                $stock_arr[$_stock_id][] = $attr;
				                $stock_id = $_stock_id;
				            }
				        }
				        if ($stock_arr) {
				            $stock_arr = reset($stock_arr);
				            $is_available = true;
				        }
				    } elseif ($product['stockQty'] - (int) @$tpl['order_arr'][$product['stockId']] > 0 && !empty($product['stockId']) && $product['is_digital'] == 0) {
				        $is_available = true;
				    }
				}
				if ($product['is_digital'] == 1) {
				    $is_available = true;
				}
				?>
				<li class="<?php echo $grid_classes; ?> pjScProduct">
					<div class="pjScProductImages" data-id="<?php echo $product['id']; ?>" style="max-width: <?php echo $max_width;?>px;">
						<?php if ($cnt_product_images > 1) { ?>
							<div class="pjScProductDefaultGalleryImage">
								<a href="<?php echo $href; ?>" class="scSelectorProduct" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>">
        			        		<img class="img-responsive pjScProductImage" src="<?php echo $src; ?>" alt="<?php echo pjSanitize::html($product['name']); ?>" />
        			        	</a>
							</div>
    						<div class="owl-carousel owl-theme owl-carousel-hidden" id="owl-carousel-<?php echo $product['id']; ?>">
    							<?php foreach ($tpl['product_image_arr'][$product['id']] as $pimg) { 
    							    $gi_src = PJ_INSTALL_URL . PJ_IMG_PATH . 'frontend/noimg.png';
    							    if (is_file(PJ_INSTALL_PATH . $pimg['medium_path']))
    							    {
    							        $gi_src = PJ_INSTALL_URL . $pimg['medium_path'] . '?v=' . filemtime(PJ_INSTALL_PATH . $pimg['medium_path']);
    							    }
    							    ?>
            			        	<a href="<?php echo $href; ?>" class="scSelectorProduct" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>">
            			        		<img class="owl-lazy pjScProductImage" data-src="<?php echo $gi_src; ?>" alt="<?php echo pjSanitize::html($product['name']); ?>">
            			        	</a>
        			        	<?php } ?>
        			        </div>
        			    <?php } else { ?>
        			    	<a href="<?php echo $href; ?>" class="scSelectorProduct" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>">
    			        		<img class="img-responsive pjScProductImage" src="<?php echo $src; ?>" alt="<?php echo pjSanitize::html($product['name']); ?>" />
    			        	</a>
        			    <?php } ?>
        			    <?php if ($is_available) { 
        			         $product_fav_arr = array();
        			         $product_fav_arr['product_id'] = $product['id'];
        			         $product_fav_arr['is_digital'] = (int) $product['is_digital'];
        			        ?>
        			    	<form action="" method="post" class="scSelectorProductForm" id="scSelectorProductForm_<?php echo $product['id'];?>" style="display: inline; vertical-align: top">
    		     				<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>" />
    							<input type="hidden" name="is_digital" value="<?php echo (int) $product['is_digital']; ?>" />
    							<?php
    							if ($stock_arr)
    							{
    							    foreach ($stock_arr as $attr)
    								{
    									list($attr_id, $attr_parent_id, $stock_id, $qty) = explode("_", $attr);
    									$product_fav_arr['attr'][$attr_parent_id] = $attr_id;
    									?>
    									<input type="hidden" name="attr[<?php echo $attr_parent_id; ?>]" class="scSelectorAttr" value="<?php echo $attr_id; ?>" />
    									<?php
    								}
    							}
    							if (!empty($product['m_extras']))
    							{
    								foreach ($product['m_extras'] as $ek => $ev)
    								{
    								    $product_fav_arr['extra'][$ek] = $ev;
    									?><input type="hidden" name="extra[<?php echo $ek; ?>]" value="<?php echo $ev; ?>" /><?php
    								}
    							}
    							?>
    							<input type="hidden" name="qty" value="1" />
    							<input type="hidden" name="stock_id" value="<?php echo $stock_id; ?>"/>
    		     			</form>
    		     			<?php 
    		     			$product_fav_arr['qty'] = 1;
    		     			$product_fav_arr['stock_id'] = $stock_id;
    		     			$product_fav_hash = md5(serialize(pjUtil::stripFav($product_fav_arr)));
    		     			?>
                            <a href="javascript:void(0);" class="btn scButtonAdd2Favs <?php echo in_array($product_fav_hash, $products_in_fav_arr) ? 'scSelectorRemoveFavFromProductsList' : 'scSelectorAddFavFromProductsList';?>" data-hash="<?php echo $product_fav_hash;?>" data-id="<?php echo $product['id'];?>"><span class="glyphicon <?php echo in_array($product_fav_hash, $products_in_fav_arr) ? 'glyphicon-heart' : 'glyphicon-heart-empty';?>" id="scProductGlyphiconFavs_<?php echo $product['id'];?>"></span></a>
    			        <?php } 
    			        if (isset($tpl['product_attr_arr'][$product['id']])) {
    			            $product_attr_arr = array();
    			            foreach ($tpl['product_attr_arr'][$product['id']] as $p_attr) {
    			                $p_attr_child_arr = array();
    			                if (isset($p_attr['child'])) {
    			                    foreach ($p_attr['child'] as $p_attr_child) {
    			                        $p_attr_child_arr[] = pjSanitize::html($p_attr_child['name']);
    			                    }
    			                }
    			                $product_attr_arr[] = pjSanitize::html($p_attr['name']).': '.implode('; ', $p_attr_child_arr);
    			            }
    			            ?>
    			     		<div class="pjScProductAttributes"><?php echo implode('<br/>', $product_attr_arr);?></div>
    			     		<?php 
    			         }
    			        ?>
			        </div>
			        <div class="pjScProductNamePrice">
    			     	<p class="pjScProductName">
    			     		<a href="<?php echo $href; ?>" class="scSelectorProduct" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>"><?php echo pjSanitize::html($product['name']); ?></a>
    			     	</p>
    			     	<?php
    			     	if($product['is_digital'] == 0)
    			     	{ 
    				     	?>
    			     		<p class="pjScProductPrice"><?php if ((float) $product['min_price'] < (float) $product['max_price']){ __('front_price_from'); }?> <?php echo pjCurrency::formatPrice($product['min_price']); ?></p>
    			     		<?php
    			     	}else{
    			     		?>
         					<p class="pjScProductPrice"><?php echo pjCurrency::formatPrice($product['price']); ?></p>
         					<?php
    			     	} ?>
    			     </div>
			     	<?php
			     	$is_demo_listing = !empty($tpl['demo_product_ids_arr'][$product['id']]);
			     	$allow_demo_purchases = pjUtil::areDemoPurchasesAllowed($tpl['option_arr']);
			     	if ($is_available)
			     	{
			     		?>
			     		<div aria-label="Default button group" role="group" class="btn-group">
				     		<?php
				     		if ($is_demo_listing && !$allow_demo_purchases) {
				     			?><span class="scProductOutOfStock" style="display:inline-block;margin-right:8px;"><?php echo pjSanitize::html(pjUtil::getDemoProductOrderBlockedMessage()); ?></span>
				        		<a href="<?php echo $href; ?>" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>" class="btn btn-default scSelectorButton scSelectorProduct pjScBtnSecondary" type="button"><?php __('front_view_details'); ?></a><?php
				     		} elseif ((int) $tpl['option_arr']['o_disable_orders'] === 0) {
				     			?>
				     			<form action="" method="post" class="scSelectorBuyNowForm" style="display: inline; vertical-align: top">
				     				<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>" />
									<input type="hidden" name="is_digital" value="<?php echo (int) $product['is_digital']; ?>" />
									<?php
									if ($stock_arr)
									{
										foreach ($stock_arr as $attr)
										{
											list($attr_id, $attr_parent_id, $stock_id, $qty) = explode("_", $attr);
											?>
											<input type="hidden" name="attr[<?php echo $attr_parent_id; ?>]" value="<?php echo $attr_id; ?>" />
											<?php
										}
									}
									if (!empty($product['m_extras']))
									{
										foreach ($product['m_extras'] as $ek => $ev)
										{
											?><input type="hidden" name="extra[<?php echo $ek; ?>]" value="<?php echo $ev; ?>" /><?php
										}
									}
									?>
							<?php
							$show_qty_listing = isset($tpl['option_arr']['o_show_qty_listing']) && $tpl['option_arr']['o_show_qty_listing'] === 'Yes';
							$max_qty_listing = isset($product['stockQty']) && (int) $product['stockQty'] > 0 ? (int) $product['stockQty'] : 999;
							?>
							<?php if ($show_qty_listing) { ?>
								<div class="input-group pjScProductQtyListing" style="max-width:110px;margin:8px auto;">
									<span class="input-group-btn scSelectorSpin" data-direction="down"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-minus"></span></button></span>
									<input type="text" name="qty" class="form-control text-center scSelectorSpinValue" value="1" data-step="1" data-min="1" data-max="<?php echo $max_qty_listing; ?>" maxlength="<?php echo strlen($max_qty_listing); ?>" readonly="readonly" />
									<span class="input-group-btn scSelectorSpin" data-direction="up"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span></button></span>
								</div>
							<?php } else { ?>
								<input type="hidden" name="qty" value="1" />
							<?php } ?>
							<input type="hidden" name="stock_id" value="<?php echo $stock_id; ?>" class="btn btn-primary scSelectorButton"/>
									<button class="btn btn-default scButton scButtonDark scSelectorButton pjScBtnPrimary" type="submit"><?php __('front_buy_now', false, true); ?></button>
				        			<a href="<?php echo $href; ?>" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>"  class="btn btn-default scSelectorButton scSelectorProduct pjScBtnSecondary" type="button"><?php __('front_view_details'); ?></a>
				     			</form>
				     			<?php
				     		} else {
				     			?><a href="<?php echo $href; ?>" data-id="<?php echo $product['id']; ?>" data-slug="<?php echo pjSanitize::html($slug); ?>" class="btn btn-default scSelectorButton scSelectorProduct pjScBtnSecondary" type="button"><?php __('front_view_details'); ?></a><?php
				     		}
				     		?>
				      	</div>
			     		<?php	
			     	}else{
		     			if ((int) $tpl['option_arr']['o_disable_orders'] === 0)
		     			{
		     				?>
		     				<button class="btn btn-primary pjScBtnPrimary" disabled="disabled" type="button"><?php __('front_out_of_stock'); ?></button>
							<?php		     				
		     			}
		     		} 
			     	?>
			      	<br><br>
				</li>
				<?php
			} 
			?>
		</ul>
	</div>
	<?php
	if (isset($tpl['paginator']))
	{
	 	if($tpl['paginator']['count'] > $tpl['paginator']['row_count'])
	 	{
			if ($pagination_mode === 'autoload')
			{
				$is_last_page = $tpl['paginator']['page'] >= $tpl['paginator']['pages'];
			?>
			<div class="sc-load-more-container text-center">
				<?php if (!$is_last_page): ?>
					<button type="button" class="btn btn-default pjScBtnPrimary sc-load-more-btn">Load more products</button>
					<div class="sc-load-more-spinner" style="display:none;margin-top:10px;">Loading...</div>
				<?php endif; ?>
				<div class="sc-load-more-done"<?php echo $is_last_page ? '' : ' style="display:none"'; ?>>All products loaded</div>
			</div>
			<?php
			} else {
			?>
			<div align="center">
				<nav>
					<ul class="pagination">
						<?php
						if ($tpl['paginator']['pages'] > 1 && $tpl['paginator']['page'] > 1)
						{ 
							$i = $tpl['paginator']['page'] - 1;
							?><li><a href="<?php echo pjUtil::getReferer(); ?>#!/Products/q:<?php echo urlencode($controller->_get->toString('q')); ?>/category:<?php echo $controller->_get->toInt('category_id'); ?>/page:<?php echo $i; ?>" class="scSelectorPage pjScPaginationPrev" data-page="<?php echo $i; ?>" title="<?php __('front_prev', false, true); ?>"><span aria-hidden="true">&laquo;</span><span class="sr-only"><?php __('front_prev');?></span></a></li><?php
						}
						for ($i = 1; $i <= $tpl['paginator']['pages']; $i++)
						{
							?><li><a href="<?php echo pjUtil::getReferer(); ?>#!/Products/q:<?php echo urlencode($controller->_get->toString('q')); ?>/category:<?php echo $controller->_get->toInt('category_id'); ?>/page:<?php echo $i; ?>" class="scSelectorPage<?php echo $tpl['paginator']['page'] != $i ? NULL : ' scPaginatorFocus'; ?>" data-page="<?php echo $i; ?>"><?php echo $i; ?></a></li><?php
						}
						if ($tpl['paginator']['pages'] > $tpl['paginator']['page'])
						{
							$i = $tpl['paginator']['page'] + 1;
							?><li><a href="<?php echo pjUtil::getReferer(); ?>#!/Products/q:<?php echo urlencode($controller->_get->toString('q')); ?>/category:<?php echo $controller->_get->toInt('category_id'); ?>/page:<?php echo $i; ?>" class="scSelectorPage pjScPaginationNext" data-page="<?php echo $i; ?>" title="<?php __('front_next', false, true); ?>"><span aria-hidden="true">&raquo;</span><span class="sr-only"><?php __('front_next');?></span></a></li><?php
						}
						?>
					</ul>
				</nav>
			</div>
			<?php
			}
	 	}
	}
	if (isset($tpl['paginator']))
	{
		echo '<script type="application/json" class="sc-pagination-meta">' . json_encode(array(
			'page' => (int) $tpl['paginator']['page'],
			'pages' => (int) $tpl['paginator']['pages'],
			'count' => (int) $tpl['paginator']['count']
		)) . '</script>';
	}
} else {
	?><div class="container-fluid"><p class="pjScEmptyCategoryMessage"><?php __('front_products_not_found'); ?></p></div><?php
}
?>
<div class="modal fade" id="scTermModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  	<div class="modal-dialog">
    	<div class="modal-content">
      		<div class="modal-header">
        		<button type="button" class="close" data-pj-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        		<h4 class="modal-title" id="myModalLabel"><?php __('front_select_attribute', false, true); ?></h4>
      		</div>
	    
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-default pjScBtnSecondary" data-pj-dismiss="modal">OK</button>
	      	</div>
   	 	</div>
  	</div>
</div>
