<?php
use eftec\bladeone\BladeOne;

$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
if(!defined("BLADEONE_MODE")) {
    define("BLADEONE_MODE", 1); // (optional) 1=forced (test),2=run fast (production), 0=automatic, default value.
}
$blade = new BladeOne($views, $cache);

$ribbon_theme = "swiper";
$ribbon_elements = array();
$ribbon_id = null;
$ribbon_id = isset($module_params['ribbon_id']) ? $module_params['ribbon_id'] : null;
if( isset($module_params['ribbon_theme']) && $module_params['ribbon_theme'] != "" ) {
    $ribbon_theme = $module_params['ribbon_theme'];
}
$ribbon_settings = [
    "slides_per_view" => 5,
    "effect" => null
];
if( isset($module_params['ribbon_settings']) && is_array($module_params['ribbon_settings']) ) {
    $ribbon_settings = array_merge($ribbon_settings, $module_params['ribbon_settings']);
}

require_once(__DIR__ . "/themes/{$ribbon_theme}/theme_includes.php");
$optres['css'][] = $module_css_path . "amodules/ribbon/themes/{$ribbon_theme}/css/{$ribbon_theme}_ribbon.css";
//$optres['css'][] = $path . "amodules/ribbon/themes/{$ribbon_theme}/css/{$ribbon_theme}_ribbon.css";
$optres['js'][] = $path . "amodules/ribbon/themes/{$ribbon_theme}/js/{$ribbon_theme}_ribbon.js";


if($ribbon_id > 0 ) {

    $q = "SELECT * FROM ribbon_elements WHERE ribbon_id='$ribbon_id' AND status='1' ORDER BY order_seq ASC;";
    //echo $q;
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        while ($row = mysql_fetch_assoc($r)) {
            $ribbon_elements[$row['id']] = array_map('stripslashes', $row);
        }
    }
}
//echo '<pre class="text-left">', var_export($ribbon_elements, true), '</pre>';
if (is_array($ribbon_elements) && count($ribbon_elements) > 0) {

    $num_elements = count($ribbon_elements) + 0;

    $ribbon_cnt = 1;
    $data_merge = 1;
    if($num_elements < 6) {
        $data_merge = (5 % $num_elements == 0) ? 5 / $num_elements : 5 / $num_elements;
    }

    $promo_pages = [];
    $ribbon_cards = [];
    $images_folder = __DIR__ . "/../../images/ribbons/$domain_id/$ribbon_id";

    foreach ($ribbon_elements as $element_id => $element_data) {

        $no_image = true;
        $el_image_url = null;
        $el_image_url_th = null;
        if(!empty($element_data['image']) ) {
            $el_image_file = $images_folder . "/$element_id/{$element_data['image']}";
            $el_image_url = $path . "images/ribbons/{$element_data['domain_id']}/{$element_data['ribbon_id']}/$element_id/{$element_data['image']}";
            $el_image_url_th = $path . "images/ribbons/{$element_data['domain_id']}/{$element_data['ribbon_id']}/$element_id/thumbs/{$element_data['image']}";

            if(file_exists($el_image_file)) {
                $no_image = false;
            }
        }

        $rel = [];
        if($element_data['follow'] != 1) {
            $rel [] = 'nofollow';
        }

        if($element_data['url_target'] == "_blank") {
            $rel [] = "noopener noreferrer";
        }

        $rel_attr = count($rel) > 0 ? 'rel="'.(implode(" ", $rel)).'"' : '';
        $target_attr = $element_data['url_target'];

        $card = [
            "domain_id" => $element_data['domain_id'],
            "ribbon_id" => $element_data['ribbon_id'],
            "image" => $element_data['image'],
            "image_url" => $el_image_url,
            "image_url_th" => $el_image_url_th,
            "url_type" => $element_data['url_type'],
            "url" => $element_data['url'],
            "url_target" => $element_data['url_target'],
            "alt_text" => $element_data['alt_text'],
            "title" => $element_data['title'],
            "description" => $element_data['description'],
            "follow" => $element_data['follow'],
            "rel_attr" => $rel_attr,
            "target_attr" => $target_attr
        ];

        if ($card["url_type"] == "promo_cat") {
            $promo_pages[$element_id] = $card["url"];
        }

        $ribbon_cards [$element_id] = $card;
    }

    if(count($promo_pages) > 0) {
        $promo_pages_catdom = [];
        $promo_catdoms = array_values($promo_pages);
        $promo_catdoms = implode(",", array_unique($promo_catdoms));
        // Make all null
        foreach($promo_pages as $pi=>$pv) {
            if(!isset($promo_pages_catdom[$pv])) $promo_pages_catdom[$pv] = [];
            $promo_pages_catdom[$pv][] = $pi;
            $promo_pages[$pi] = null;
        }
        $q = "SELECT * FROM domaincategories WHERE catdom_id IN ($promo_catdoms) AND active='yes';";
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) > 0) {
            while($row = mysql_fetch_assoc($r)) {
                $p_catdom_id = intval($row['catdom_id']);
                $p_friendly_url = stripslashes($row['friendly_url']);
                foreach($promo_pages_catdom[$p_catdom_id] as $elm_id) {
                    $promo_pages[$elm_id] = "$base_url/" . $p_friendly_url . ".html";
                }
            }
        }

        unset($promo_pages_catdom);
        // All those that not null should update ribbon data
        // the null ones should be removed from ribbon data
        //echo '<pre class="text-left">$promo_pages = ', var_export($promo_pages, true), '</pre>';
        foreach($promo_pages as $pi=>$p_url) {
            if($p_url == null) {
                unset($ribbon_cards[$pi]);
            }
            else {
                $ribbon_cards[$pi]['url'] = $p_url;
            }
        }
    }

    //echo '<pre class="text-left">', var_export($ribbon_cards, true), '</pre>';

    $ribbon_view = $blade->run("$ribbon_theme/ribbon_default",
        [
            "ribbon_cards" => $ribbon_cards,
            "num_elements" => $num_elements,
            "data_merge"    => $data_merge,
            "settings"      => $ribbon_settings,
            "id"            => "ribbon-" . $ribbon_id . '-' . number_format(microtime(true), 0, '', '') . rand(1000, 9999)
        ]);

    echo $ribbon_view;
}