<?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;
// Title
$title = $this->el($element['title_element'], [

	'class' => [
		'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',
	],

]);

// Meta
$meta = $this->el($element['meta_element'], [

	'class' => [
		'el-meta',
		'uk-[text-{@meta_style: meta}]{meta_style}',
		'uk-text-{meta_color}',
		'uk-margin[-{meta_margin}]-top {@!meta_margin: remove}',
		'uk-margin-remove-bottom [uk-margin-{meta_margin: remove}-top]' => !in_array($element['meta_style'], ['', 'meta']) || $element['meta_element'] != 'div',
	],

]);

// Price
$price = $this->el($element['price_element'], [

	'class' => [
		'el-price',
		'uk-[text-{@price_style: price}]{price_style}',
		'uk-text-{price_color}',
		'uk-margin[-{price_margin}]-top {@!price margin: remove}',
		'uk-margin-remove-bottom [uk-margin-{price_margin: remove}-top]' => !in_array($element['price_style'], ['', 'price']) || $element['price_element'] != 'div',
	],

]);

// Price
$cart = $this->el($element['cart_element'], [

	'class' => [
		'el-cart',
		'uk-margin[-{cart_margin}]-top {@!cart margin: remove}',
		'uk-margin-remove-bottom [uk-margin-{price_margin: remove}-top]' => !in_array($element['cart_style'], ['', 'cart']) || $element['cart_element'] != 'div',
	],

]);


// Content
$content = $this->el('div', [

	'class' => [
		'el-content uk-panel',
		'uk-text-{content_style}',
		'[uk-text-left{@content_align}]',
		'uk-dropcap {@content_dropcap}',
		'uk-column-{content_column}[@{content_column_breakpoint}]',
		'uk-column-divider {@content_column} {@content_column_divider}',
		'uk-margin[-{content_margin}]-top {@!content_margin: remove}',
	],

]);

// Link
$link_container = $this->el('div', [

	'class' => [
		'uk-margin[-{link_margin}]-top {@!link_margin: remove}',
	],

]);

// 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);


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

	'class' => [
		'uk-child-width-expand',
		$element['title_grid_column_gap'] == $element['title_grid_row_gap'] ? 'uk-grid-{title_grid_column_gap}' : '[uk-grid-column-{title_grid_column_gap}] [uk-grid-row-{title_grid_row_gap}]',
		'uk-margin[-{title_margin}]-top {@!title_margin: remove} {@image_align: top}' => !$props['meta'] || $element['meta_align'] != 'above-title',
		'uk-margin[-{meta_margin}]-top {@!meta_margin: remove} {@image_align: top} {@meta_align: above-title}' => $props['meta'],
	],

	'uk-grid' => true,
]);

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

	'class' => [
		'uk-width-{title_grid_width}[@{title_grid_breakpoint}]',
		'uk-margin-remove-first-child',
	],

]);

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

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

]);

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

?>
<?php
foreach($element as $elem => $key){
	if(preg_match($re, $elem)){
		$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 ($props['title'] && $element['title_align'] == 'left' && $orders['title'] == $i - 1 ) : ?>
		<?= $grid($element) ?>
		<?= $cell_title($element) ?>
	<?php endif ?>

	<?php if ($props['meta'] && $element['meta_align'] == 'above-title' && $orders['meta']  == $i - 1 ): ?>
		<?= $meta($element, $props['meta']) ?>
	<?php endif ?>

	<?php if ($props['price'] && $element['price_align'] == 'above-title' && $orders['price'] == $i - 1 ) : ?>
		<?= $price($element, $props['price']) ?>
	<?php endif ?>

	<?php if ($props['cart'] && $element['cart_align'] == 'above-title' && $orders['cart'] == $i - 1 ) : ?>
		<?= $cart($element, $props['cart']) ?>
	<?php endif ?>

	<?php if ($props['title'] && $orders['title'] == $i - 1) : ?>
		<?= $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['meta'] && $element['meta_align'] == 'below-title' && $orders['meta'] == $i - 1) : ?>
		<?= $meta($element, $props['meta']) ?>
	<?php endif ?>

	<?php if ($props['price'] && $element['price_align'] == 'below-title' && $orders['price'] == $i - 1) : ?>
		<?= $price($element, $props['price']) ?>
	<?php endif ?>

	<?php if ($props['cart'] && $element['cart_align'] == 'below-title' && $orders['cart'] == $i - 1 ) : ?>
		<?= $cart($element, $props['cart']) ?>
	<?php endif ?>

	<?php if ($props['title'] && $element['title_align'] == 'left' && $orders['title'] == $i - 1 ) : ?>
		<?= $cell_title->end() ?>
		<?= $cell_content($element) ?>
	<?php endif ?>

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

	<?php if ($props['meta'] && $element['meta_align'] == 'above-content' && $orders['meta'] == $i - 1 ) : ?>
		<?= $meta($element, $props['meta']) ?>
	<?php endif ?>

	<?php if ($props['price'] && $element['price_align'] == 'above-content' && $orders['price'] == $i - 1 ) : ?>
		<?= $price($element, $props['price']) ?>
	<?php endif ?>

	<?php if ($props['cart'] && $element['cart_align'] == 'above-content' && $orders['cart'] == $i - 1 ) : ?>
		<?= $cart($element, $props['cart']) ?>
	<?php endif ?>

	<?php if ($props['content'] && $orders['content'] == $i + 1) : ?>
		<?= $content($element, $props['content']) ?>
	<?php endif ?>

	<?php if ($props['meta'] && $element['meta_align'] == 'below-content' && $orders['meta'] == $i - 1) : ?>
		<?= $meta($element, $props['meta']) ?>
	<?php endif ?>

	<?php if ($props['price'] && $element['price_align'] == 'below-content' && $orders['price'] == $i - 1) : ?>
		<?= $price($element, $props['price']) ?>
	<?php endif ?>



	<?php if ($props['link'] && $element['link_text'] && $orders['link'] == $i - 1) : ?>
		<?= $link_container($element, $link($element, $element['link_text'])) ?>
	<?php endif ?>

	<?php if ($props['cart'] && $element['cart_align'] == 'below-content' && $orders['cart'] == $i - 1) : ?>
		<?= $cart($element, $props['cart']) ?>
	<?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['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 ($props['title'] && $element['title_align'] == 'left' && $orders['title'] == $i - 1 ) : ?>
		<?= $cell_content->end() ?>
		<?= $grid->end() ?>
	<?php endif ?>
<?php } ?>
