<?php
/**
 * @package DJ-Catalog2-Yootheme
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */
defined('_JEXEC') or die;
use DJExtensions\DJCatalog2\YOOtheme\Builder\Source\ElementHelper;
$panel = $this->el('div', [

	'class' => [
		'el-content uk-panel',
	],
	'uk-grid' => true,
]);

$producer = $quickview =  $quickview =  $producer_image = $category_image = $panel;

// Title
$title = $this->el($element['title_element'], [

	'class' => [
		'djc_title',
		'el-title',
		'uk-{title_style}',
		'uk-card-title {@panel_style} {@!title_style}',
		'uk-heading-{title_decoration}',
		'uk-font-{title_font_family}',
		'uk-text-{title_color} {@!title_color: background}',
		'uk-margin[-{title_margin}]-top {@!title_margin: remove}',
		'uk-margin-remove-top {@title_margin: remove}',
		'uk-margin-remove-bottom',
		'uk-inline {@position_inline_title}' => ['position_inline_title' => $element['position_inline_title']],
	],
]);

//Title - Style
$title->attr('style', [
	'position:{position_title};' => ['position_title' => $element['position_title']],
	'left: {position_left_title}px;' => ['position_left_title' => $element['position_left_title']],
	'right: {position_right_title}px;' => ['position_right_title' => $element['position_right_title']],
	'top: {position_top_title}px;' => ['position_top_title' => $element['position_top_title']],
	'bottom: {position_bottom_title}px;' => ['position_bottom_title' => $element['position_bottom_title']],
	'z-index:{position_z_index_title};' => ['position_z_index_title' => $element['position_z_index_title']],
	'width:{title_width};' => ['title_width' => $element['title_width']],
	'height:{title_height};' => ['title_height' => $element['title_height']],
], $element);

// Title Grid
$grid = $this->el('div', [

	'class' => [
		'djc_title_grid',
		'uk-child-width-expand',
	],
	'uk-grid' => true,

]);

$cell_title = $this->el('div', [

	'class' => [
		'djc_cell_title',
		'uk-margin-remove-first-child',
	],

]);

$cell_content = $this->el('div', [

	'class' => [
		'djc_cell_content',
		'uk-margin-remove-first-child',
	],
]);

// Button readmore
$buttonReadmore = $this->el('a', [

	'class' => $this->expr([
		'djc_readmore',
		'el-content',
		'uk-width-1-1 {@readmore_fullwidth}',
		'uk-{readmore_button_style: readmore_link-\w+}' => ['readmore_button_style' => $element['readmore_button_style']],
		'uk-button uk-button-{!readmore_button_style: |readmore_link-\w+} [uk-button-{readmore_button_size}]' => ['readmore_button_style' => $element['readmore_button_style']],
		'uk-margin[-{readmore_margin}]-top {@!readmore_margin: remove}',
		'uk-margin-remove-top {@readmore_margin: remove}',
		'uk-margin-remove-bottom',
		'uk-inline {@position_inline_readmore}' => ['position_inline_readmore' => $element['position_inline_readmore']],
	], $element),

	'title' => ['{readmore_link_title}'],
]);

$buttonReadmore->attr($props['readmore_link_target'] == 'modal' ? [
	'href' => ['{readmore_link}/preview'],
	'class' => $this->expr([
		'djc_readmore',
		'uk-{readmore_button_style: readmore_link-\w+}' => ['readmore_button_style' => $props['readmore_button_style']],
		'uk-button readmore djc_item_preview uk-button-{!readmore_button_style: |readmore_link-\w+} [uk-button-{readmore_button_size}]' => ['readmore_button_style' => $props['readmore_button_style']],
		'uk-margin[-{readmore_margin}]-top {@!readmore_margin: remove}',
		'uk-margin-remove-top {@readmore_margin: remove}',
		'uk-margin-remove-bottom',
	], $element),
] : [
	'href' => ['{readmore_link}'],
	'target' => ['_blank {@readmore_link_target}'],
	'uk-scroll' => strpos($props['readmore_link'], '#') === 0,
]);
//ButtonReadmore Style
$buttonReadmore->attr('style', [
	'position:{position_readmore};' => ['position_readmore' => $element['position_readmore']],
	'left: {position_left_readmore}px;' => ['position_left_readmore' => $element['position_left_readmore']],
	'right: {position_right_readmore}px;' => ['position_right_readmore' => $element['position_right_readmore']],
	'top: {position_top_readmore}px;' => ['position_top_readmore' => $element['position_top_readmore']],
	'bottom: {position_bottom_readmore}px;' => ['position_bottom_readmore' => $element['position_bottom_readmore']],
	'z-index:{position_z_index_readmore};' => ['position_z_index_readmore' => $element['position_z_index_readmore']],
	'width:{readmore_width};' => ['readmore_width' => $element['readmore_width']],
	'height:{readmore_height};' => ['readmore_height' => $element['readmore_height']],
], $element);

// Button category
$buttonCategory = $this->el('a', [

	'class' => $this->expr([
		'djc_category_btn',
		'el-content',
		'uk-width-1-1 {@category_fullwidth}',
		'uk-{category_button_style: category_link-\w+}' => ['category_button_style' => $element['category_button_style']],
		'uk-button uk-button-{!category_button_style: |category_link-\w+} [uk-button-{category_button_size}]' => ['category_button_style' => $element['category_button_style']],
		'uk-margin[-{category_button_margin}]-top {@!category_button_margin: remove}',
		'uk-margin-remove-top {@category_button_margin: remove}',
		'uk-margin-remove-bottom',
		'uk-inline {@position_inline_category_button}' => ['position_inline_category_button' => $element['position_inline_category_button']],
	], $element),

	'title' => ['{category_link_title}'],

]);

$buttonCategory->attr($props['category_link_target'] == 'modal' ? [
	'href' => ['{category_link}/preview'],
	'class' => $this->expr([
		'djc_category_btn',
		'uk-{category_button_style: category_link-\w+}' => ['category_button_style' => $element['category_button_style']],
		'uk-button readmore djc_item_preview uk-button-{!category_button_style: |category_link-\w+} [uk-button-{category_button_size}]' => ['category_button_style' => $element['category_button_style']],
		'uk-margin[-{category_button_margin}]-top {@!category_button_margin: remove}',
		'uk-margin-remove-top {@category_button_margin: remove}',
		'uk-margin-remove-bottom',
	], $element),
] : [
	'href' => ['{category_link}'],
	'target' => ['_blank {@category_link_target}'],
	'uk-scroll' => strpos($props['category_link'], '#') === 0,
]);

//ButtonCategory Style
$buttonCategory->attr('style', [
	'position:{position_category_button};' => ['position_category_button' => $element['position_category_button']],
	'left: {position_left_category_button}px;' => ['position_left_category_button' => $element['position_left_category_button']],
	'right: {position_right_category_button}px;' => ['position_right_category_button' => $element['position_right_category_button']],
	'top: {position_top_category_button}px;' => ['position_top_category_button' => $element['position_top_category_button']],
	'bottom: {position_bottom_category_button}px;' => ['position_bottom_category_button' => $element['position_bottom_category_button']],
	'z-index:{position_z_index_category_button};' => ['position_z_index_category_button' => $element['position_z_index_category_button']],
	'width:{category_button_width};' => ['category_button_width' => $element['category_button_width']],
	'height:{category_button_height};' => ['category_button_height' => $element['category_button_height']],
], $element);

// ButtonProducer
$buttonProducer = $this->el('a', [

	'class' => $this->expr([
		'el-content',
		'uk-width-1-1 {@producer_fullwidth}',
		'uk-{producer_button_style: producer_link-\w+}' => ['producer_button_style' => $element['producer_button_style']],
		'uk-button uk-button-{!producer_button_style: |producer_link-\w+} [uk-button-{producer_button_size}]' => ['producer_button_style' => $element['producer_button_style']],
		'uk-margin[-{producer_button_margin}]-top {@!producer_button_margin: remove}',
		'uk-margin-remove-top {@producer_button_margin: remove}',
		'uk-margin-remove-bottom',
		'uk-inline {@position_inline_producer_button}' => ['position_inline_producer_button' => $element['position_inline_producer_button']],
	], $element),

	'title' => ['{producer_link_title}'],

]);

$buttonProducer->attr($props['producer_link_target'] == 'modal' ? [
	'href' => ['{producer_link}/preview'],
	'class' => $this->expr([
		'uk-{producer_button_style: producer_link-\w+}' => ['producer_button_style' => $element['producer_button_style']],
		'uk-button readmore djc_item_preview uk-button-{!producer_button_style: |producer_link-\w+} [uk-button-{producer_button_size}]' => ['producer_button_style' => $element['producer_button_style']],
		'uk-margin[-{producer_button_margin}]-top {@!producer_button_margin: remove}',
		'uk-margin-remove-top {@producer_button_margin: remove}',
		'uk-margin-remove-bottom',
	], $element),
] : [
	'href' => ['{producer_link}'],
	'target' => ['_blank {@producer_link_target}'],
	'uk-scroll' => strpos($props['producer_link'], '#') === 0,
]);

//ButtonProducer Style
$buttonProducer->attr('style', [
	'position:{position_producer_button};' => ['position_producer_button' => $element['position_producer_button']],
	'left: {position_left_producer_button}px;' => ['position_left_producer_button' => $element['position_left_producer_button']],
	'right: {position_right_producer_button}px;' => ['position_right_producer_button' => $element['position_right_producer_button']],
	'top: {position_top_producer_button}px;' => ['position_top_producer_button' => $element['position_top_producer_button']],
	'bottom: {position_bottom_producer_button}px;' => ['position_bottom_producer_button' => $element['position_bottom_producer_button']],
	'z-index:{position_z_index_producer_button};' => ['position_z_index_producer_button' => $element['position_z_index_producer_button']],
	'width:{producer_button_width};' => ['producer_button_width' => $element['producer_button_width']],
	'height:{producer_button_height};' => ['producer_button_height' => $element['producer_button_height']],
], $element);


//Regex
$re = '/order_.*/m';
$orders = array();
$ordersCountValues = array();

?>
<div id="djcatalog">
	<?php
	foreach ($element as $elem => $key) {
		if (preg_match($re, $elem)) {
			//Search for order elements
			$orders[str_replace("order_", '', $elem)] = $key;
		}
	}
	for ($i = 0; $i < count($orders); $i++) {
		//Check if order was changed => fix it
		$orders = ElementHelper::shuffleOrder($orders);
		foreach ($element as $elem => $key) {
			if (preg_match($re, $elem)) {
				//Change element order_ values
				$element[str_replace("order_", '', $elem)] = $orders[str_replace("order_", '', $elem)];
			}
		}
		?>
		<?php if($i == 0 ) : ?>
			<?= $grid($element) ?>
			<?= $cell_content($element) ?>
			<?= $cell_title($element) ?>
		<?php endif ?>


		<?php if (($props['autolabels'] || $props['labels']) && ($orders['labels'] == $i - 1)) : ?>
            <div class="djc2-labels-box">
				<?php if(($props['labels'])) :?>
					<?= $this->render("{$__dir}/template-labels")?>
				<?php endif ?>
				<?php if(($props['autolabels'])) :?>
					<?= $this->render("{$__dir}/template-autolabels")?>
				<?php endif ?>
            </div>
		<?php endif ?>



		<?php if ($props['image'] && $element['show_product_image']  && $orders['product_image'] == $i - 1) : ?>
			<?= $props['image']  ?>
		<?php endif ?>

		<?php if ($props['video'] && $element['show_video'] && $orders['video'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-video")?>
		<?php endif ?>

		<?php if ($props['category'] && $props['category_link'] && $orders['category'] == $i - 1) : ?>
			<?= $buttonCategory($props) ?>
			<?php if (isset($element['category_icon'])) : ?>

				<?php if ($element['category_icon_align'] == 'left') : ?>
                    <span uk-icon="<?= $element['category_icon'] ?>"></span>
				<?php endif ?>

                <span class="uk-text-middle"><?= $props['category'] ?></span>

				<?php if ($element['category_icon_align'] == 'right') : ?>
                    <span uk-icon="<?= $element['category_icon'] ?>"></span>
				<?php endif ?>

			<?php else : ?>
				<?= $props['category'] ?>
			<?php endif ?>

            </a>

		<?php endif ?>

		<?php if ($props['producer'] && $props['producer_link'] && $orders['producer'] == $i - 1) : ?>
			<?= $buttonProducer($props) ?>
			<?php if (isset($element['producer_icon'])) : ?>

				<?php if ($element['producer_icon_align'] == 'left') : ?>
                    <span uk-icon="<?= $element['producer_icon'] ?>"></span>
				<?php endif ?>

                <span class="uk-text-middle"><?= $props['producer'] ?></span>

				<?php if ($element['producer_icon_align'] == 'right') : ?>
                    <span uk-icon="<?= $element['producer_icon'] ?>"></span>
				<?php endif ?>

			<?php else : ?>
				<?= $props['producer'] ?>
			<?php endif ?>

            </a>

		<?php endif ?>

		<?php if ($props['title'] && $orders['title'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-link-product") ?>
			<?= $title($element) ?>
			<?php if ($element['title_color'] == 'background') : ?>
                <span class="uk-text-background"><?= $props['title'] ?></span>
			<?php elseif ($element['title_decoration'] == 'line') : ?>
                <span><?= $props['title'] ?></span>
			<?php else : ?>
				<?= $props['title'] ?>
			<?php endif ?>
			<?= $title->end() ?>
		<?php endif ?>

		<?php if ($props['sku'] && $orders['sku'] == $i - 1) : ?>
			<?php if ($element['sku_text_color'] == 'background') : ?>
                <span class="uk-text-background"><?= $this->render("{$__dir}/template-sku") ?></span>
			<?php elseif ($element['sku_text_decoration'] == 'line') : ?>
                <span><?= $this->render("{$__dir}/template-sku") ?></span>
			<?php else : ?>
				<?= $this->render("{$__dir}/template-sku") ?>
			<?php endif ?>
		<?php endif ?>

		<?php if ($props['price'] && $orders['price'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-price") ?>
		<?php endif ?>

		<?php if ($props['compare'] && $orders['compare'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-compare")?>
		<?php endif ?>

		<?php if ($props['wishlist'] && $orders['wishlist'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-wishlist")?>
		<?php endif ?>

		<?php if ($props['availinfo'] && $orders['availinfo'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-availinfo") ?>
		<?php endif ?>

		<?php if ($props['createdby'] && $orders['createdby'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-createdby") ?>
		<?php endif ?>

		<?php if ($props['createdon'] && $orders['createdon'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-createdon") ?>
		<?php endif ?>

		<?php if ($props['hits'] && $orders['hits'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-hits") ?>
		<?php endif ?>

		<?php if ($props['address'] && $orders['address'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-address") ?>
		<?php endif ?>

		<?php if ($props['city'] && $orders['city'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-city") ?>
		<?php endif ?>

		<?php if ($props['postcode'] && $orders['postcode'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-postcode") ?>
		<?php endif ?>

		<?php if ($props['country'] && $orders['country'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-country") ?>
		<?php endif ?>

		<?php if ($props['state'] && $orders['state'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-state") ?>
		<?php endif ?>

		<?php if ($props['phone'] && $orders['phone'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-phone") ?>
		<?php endif ?>

		<?php if ($props['mobile'] && $orders['mobile'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-mobile") ?>
		<?php endif ?>

		<?php if ($props['fax'] && $orders['fax'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-fax") ?>
		<?php endif ?>

		<?php if ($props['website'] && $orders['website'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-website") ?>
		<?php endif ?>

		<?php if ($props['email'] && $orders['email'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-email") ?>
		<?php endif ?>

		<?php if ($props['rating'] && $orders['rating'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-rating") ?>
		<?php endif ?>

		<?php if ($props['vote'] && $orders['vote'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-vote") ?>
		<?php endif ?>

		<?php if ($props['introdescription'] && $orders['introdescription'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-introdescription") ?>
		<?php endif ?>

		<?php if ($props['description'] && $orders['description'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-description") ?>
		<?php endif ?>

		<?php if ($props['attributes'] && $orders['attributes'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-attributes") ?>
		<?php endif ?>

		<?php if ($props['readmore'] && $props['readmore_link'] && $orders['readmore'] == $i - 1) : ?>
			<?= $buttonReadmore($props) ?>
			<?php if (isset($element['readmore_icon'])) : ?>

				<?php if ($props['readmore_icon_align'] == 'left') : ?>
                    <span uk-icon="<?= $props['readmore_icon'] ?>"></span>
				<?php endif ?>

                <span class="uk-text-middle"><?= $props['readmore'] ?></span>

				<?php if ($props['readmore_icon_align'] == 'right') : ?>
                    <span uk-icon="<?= $props['readmore_icon'] ?>"></span>
				<?php endif ?>

			<?php else : ?>
				<?= $props['readmore'] ?>
			<?php endif ?>

            </a>

		<?php endif ?>

		<?php if ($props['addtocart'] && $orders['addtocart'] == $i - 1) : ?>
			<?= $this->render("{$__dir}/template-addtocart") ?>
		<?php endif ?>

		<?php if ($props['producer_image'] && $orders['producer_image'] == $i - 1) : ?>
			<?= $props['producer_image'] ?>
		<?php endif ?>

		<?php if ($props['category_image'] && $orders['category_image'] == $i - 1) : ?>
			<?= $props['category_image'] ?>
		<?php endif ?>

		<?php if ($i+1==count($orders)): ?>
			<?= $cell_title->end() ?>
			<?= $cell_content->end() ?>
			<?= $grid->end() ?>
		<?php endif ?>
	<?php } ?>
</div>







