<?php

/**
 * Currency Switcher Offcanvas Template
 * 
 * @package Kubelio
 */

$currency_data = kubelio_get_currency_list();

if (!empty($currency_data)) :
    $current_currency = $currency_data['current'];
    $current_flag     = $currency_data['flag'];
    $current_label    = $currency_data['label'];
    $url_param        = $currency_data['url_param'];
    $show_flag        = $currency_data['show_flag'];
?>
    <div class="rt-currency-switcher has-body-color">
        <div class="rt-currency-switcher__wrapper rt-dropdown is-dropdown-dropup js-dropdown" data-trigger="click">
            <div class="rt-dropdown__toggle flex items-center justify-between gap-20" role="button">
                <span class="rt-currency-item flex items-center gap-20">
                    <?php if ($show_flag && $current_flag) : ?>
                        <img src="<?php echo esc_url($current_flag); ?>" class="rt-currency-item__flag" alt="<?php echo esc_attr($current_currency); ?>">
                    <?php endif; ?>
                    <span class="rt-currency-item__label font-medium"><?php echo esc_html($current_label); ?></span>
                </span>

                <span class="rt-dropdown__arrow">
                    <i class="rt-icon">
                        <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" class="bi bi-chevron-down" viewBox="0 0 16 16">
                            <path fill-rule="evenodd" d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z"></path>
                        </svg>
                    </i>
                </span>
            </div>

            <div class="rt-dropdown__content is-style-base has-x-small-font-size has-body-color has-link-static-color mb-30">
                <ul class="rt-dropdown__menu flex flex-col gap-30">
                    <?php foreach ($currency_data['list'] as $code => $currency) :
                        $active_class = ($code === $current_currency) ? 'is-active' : '';
                    ?>
                        <li>
                            <a href="?<?php echo esc_attr($url_param); ?>=<?php echo esc_attr($code); ?>" class="rt-currency-item flex items-center gap-20 <?php echo esc_attr($active_class); ?>">
                                <?php if ($show_flag && !empty($currency['flag'])) : ?>
                                    <img src="<?php echo esc_url($currency['flag']); ?>" class="rt-currency-item__flag" alt="<?php echo esc_attr($code); ?>">
                                <?php endif; ?>
                                <span class="rt-currency-item__label"><?php echo esc_html($currency['label']); ?></span>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>
<?php endif; ?>