<?php
global $options, $labels, $post, $user_id;

$navigation = $options->get_option('bottom-navigation');
$navigation = is_array($navigation) ? $navigation : [];
$actual_link = irk_actual_link();
if (irk_is_woo_active()) {
    $cart_url = wc_get_cart_url();
    $fav_url = wc_get_account_endpoint_url('favorites');
    $notif_url = wc_get_account_endpoint_url('notifications');
    $account_url = intval($user_id) ? $options->get_option('account-url') : $options->get_option('login-url');
    $cart_count = WC()->cart->cart_contents_count;
    $fav_count = IRK_Favorites_Manager::get_all($user_id);
    $notif_count = (new IRK_Notifications_Views())->unviewed_count($user_id);
}
?>
<?php wp_footer(); ?>
<div class="modal-loader-wrapper">
    <div class="modal-loader">
        <?php echo irk_get_logo(); ?>
        <div class="loader-bullets">
            <i class="loader-bullet"></i>
            <i class="loader-bullet"></i>
            <i class="loader-bullet"></i>
            <i class="loader-bullet"></i>
        </div>
    </div>
</div>
<?php if (irk_is_woo_active() && is_product() && $options->get_option('product-share')): ?>
    <?php
    global $product;
    $product_id = $product->get_id();
    $product_url = get_permalink($product_id);
    ?>
    <div class="modal" id="share-modal">
        <div class="modal-title"><?php echo esc_html($labels->get_label('share')) ?></div>
        <p class="share-text"><?php echo esc_html($labels->get_label('share-text')) ?></p>
        <ul class="share-buttons">
            <li>
                <a href="https://twitter.com/intent/tweet?url=<?php echo $product_url; ?>" class="twitter" rel="nofollow" target="_blank"></a>
            </li>
            <li>
                <a href="https://www.facebook.com/sharer/sharer.php?m2w&s=100&p[url]=<?php echo $product_url; ?>" class="facebook" rel="nofollow" target="_blank"></a>
            </li>
            <li>
                <a href="https://wa.me/?text=<?php echo $product_url; ?>" class="whatsapp" rel="nofollow" target="_blank"></a>
            </li>
            <li>
                <a href="https://telegram.me/share/url?url=<?php echo $product_url; ?>" class="telegram" rel="nofollow" target="_blank"></a>
            </li>
            <li>
                <a href="#" class="copy-url" data-url="<?php echo wp_get_shortlink($product_id);; ?>" title="<?php echo esc_html($labels->get_label('copy-url')) ?>" rel="nofollow"></a>
            </li>
        </ul>
    </div>
<?php endif; ?>
<?php
if (irk_is_woo_active() && $options->get_option('price-chart') && is_product()) {
    $changes_data = irk_get_price_changes_data($post->ID);
    if ($changes_data) {
        ?>
        <div class="modal" id="price-chart-modal">
            <div class="modal-title"><?php echo esc_html($labels->get_label('price-chart')) ?></div>
            <canvas id="price-chart"></canvas>
        </div>
        <?php
        $_labels = [];
        foreach ($changes_data as $item) {
            foreach ($item as $k => $v) {
                $_labels[] = $k;
            }
        }
        $_labels = array_unique($_labels);
        sort($_labels);
        ?>
        <script type="text/javascript">
            var colors = [
                '#3498db',
                '#ff4757',
                '#34495e',
                '#2ecc71',
                '#e67e22',
                '#9b59b6',
                '#e74c3c',
                '#f1c40f',
                '#e84393',
                '#B53471',
                '#12CBC4',
                '#009432',
                '#A3CB38',
                '#a4b0be',
                '#eccc68',
                '#1e3799',
                '#18dcff',
                '#4b4b4b',
                '#1abc9c',
                '#8c7ae6',
            ];

            function addCommas(nStr) {
                nStr += '';
                x = nStr.split('.');
                x1 = x[0];
                x2 = x.length > 1 ? '.' + x[1] : '';
                var rgx = /(\d+)(\d{3})/;
                while (rgx.test(x1)) {
                    x1 = x1.replace(rgx, '$1' + ',' + '$2');
                }
                return x1 + x2;
            }

            var ctx = document.getElementById("price-chart");
            var myChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: <?php echo json_encode($_labels); ?>,
                    datasets: [
                        <?php
                        $i = 0;

                        $data = array();
                        foreach ( $changes_data as $key => $value ):
                        $product = wc_get_product($key);
                        $product_name = $product->get_name();

                        $data = array();
                        foreach ($_labels as $label) {
                            if (in_array($label, array_keys($value))) {
                                $data[] = $value[$label];
                            } else {
                                $data[] = null;
                            }
                        }
                        ?>
                        {
                            label: '<?php echo $product_name; ?>',
                            data: <?php echo json_encode($data); ?>,
                            lineTension: 0,
                            fill: false,
                            borderColor: colors[<?php echo $i; ?>],
                            backgroundColor: colors[<?php echo $i; ?>],
                            borderWidth: 1
                        },
                        <?php
                        $i++;
                        endforeach;
                        ?>
                    ]
                },
                options: {
                    responsive: true,
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: false,
                                callback: function (value, index, values) {
                                    return addCommas(value);
                                }
                            }
                        }]
                    },
                    legend: {
                        display: true,
                        labels: {
                            padding: 20
                        }
                    },
                    tooltips: {
                        backgroundColor: 'rgba(85,85,85,0.73)',
                    }
                }
            });
            Chart.defaults.global.defaultFontFamily = '<?php echo $options->get_option('font-family'); ?>';
        </script>
        <?php
    }
}

if (irk_is_woo_active() && $options->get_option('product-video') && is_product()) {
    $aparat_code = get_post_meta(get_the_ID(), '_irk_aparat_video_code', true);
    if ($aparat_code) {
        echo '<div class="modal" id="video-modal">' . $aparat_code . '</div>';
    }
}

if (irk_is_woo_active() && ($options->get_option('products-slider-quick-view') || $options->get_option('catalog-quick-view'))) {
    echo '<div class="modal" id="quick-view-modal"><div class="inner"></div></div>';
}
?>
<?php if (irk_is_active_bottom_nav()): ?>
    <div class="bottom-navigation">
        <?php if ($options->get_option('bottom-navigation-home')): ?>
            <div class="navigation-item navigation-item-home <?php echo $actual_link == home_url('/') ? 'active' : ''; ?>">
                <a href="<?php echo esc_url(home_url('/')); ?>">
                    <span class="icon"><span data-icon="Icon-Action-Home-Outline"></span></span>
                    <span class="text"><?php esc_html_e($labels->get_label('home')); ?></span>
                </a>
            </div>
        <?php endif; ?>
        <?php if (irk_is_woo_active()): ?>
            <?php if ($options->get_option('bottom-navigation-favorites')): ?>
                <div class="navigation-item navigation-item-favorites <?php echo $actual_link == $fav_url ? 'active' : ''; ?> <?php echo count($fav_count) ? 'has-badge' : ''; ?>">
                    <a href="<?php echo esc_url($fav_url); ?>">
                        <span class="icon"><span data-icon="Icon-Action-Favorite-Off"></span></span>
                        <span class="text"><?php esc_html_e($labels->get_label('favorites')); ?></span>
                    </a>
                </div>
            <?php endif; ?>
            <?php if ($options->get_option('bottom-navigation-notifications')): ?>
                <div class="navigation-item navigation-item-notifications <?php echo $actual_link == $notif_url ? 'active' : ''; ?> <?php echo $notif_count ? 'has-badge' : ''; ?>">
                    <a href="<?php echo esc_url($notif_url); ?>">
                        <span class="icon"><span data-icon="dk-message"></span></span>
                        <span class="text"><?php esc_html_e($labels->get_label('notifications')); ?></span>
                    </a>
                </div>
            <?php endif; ?>
            <?php if ($options->get_option('bottom-navigation-cart')): ?>
                <div class="navigation-item navigation-item-cart <?php echo $actual_link == $cart_url ? 'active' : ''; ?> <?php echo $cart_count ? 'has-badge' : ''; ?>">
                    <a href="<?php echo esc_url($cart_url); ?>">
                        <span class="icon"><span data-icon="Icon-Action-Cart-Off"></span></span>
                        <span class="text"><?php esc_html_e($labels->get_label('cart')); ?></span>
                    </a>
                </div>
            <?php endif; ?>
            <?php if ($options->get_option('bottom-navigation-account')): ?>
                <div class="navigation-item navigation-item-account <?php echo is_account_page() && $fav_url != $actual_link && $notif_url != $actual_link ? 'active' : ''; ?> <?php echo intval($user_id) ? 'has-badge' : ''; ?>">
                    <a href="<?php echo esc_url($account_url); ?>">
                        <span class="icon"><span data-icon="user-small"></span></span>
                        <span class="text"><?php esc_html_e($labels->get_label('account')); ?></span>
                    </a>
                </div>
            <?php endif; ?>
        <?php endif; ?>
        <?php if (count($navigation)): ?>
            <?php foreach ($navigation as $nav): ?>
                <?php
                if (empty($nav)) {
                    continue;
                }
                ?>
                <div class="navigation-item <?php echo $actual_link == $nav['url'] ? 'active' : ''; ?>">
                    <a href="<?php echo esc_url($nav['url']); ?>">
                        <span class="icon"><?php echo irk_get_icon($nav['icon']) ?></span>
                        <span class="text"><?php esc_html_e($nav['text']); ?></span>
                    </a>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
<?php endif; ?>
</div>
</body>
</html>