<?php

/**
 * Currency Switcher Header 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'];
    $extra_class = isset($args['class']) ? ' ' . $args['class'] : '';
?>
    <div class="rt-currency-switcher rt-dropdown js-dropdown<?php echo esc_attr($extra_class); ?>" data-trigger="hover">
        <span class="rt-dropdown__toggle flex items-center gap-20" role="button" aria-label="<?php echo esc_attr__('open currency switcher', 'kubelio'); ?>">

            <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"><?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>
        </span>
        <div class="rt-dropdown__content is-style-offcanvas has-body-color has-link-current-color has-x-small-font-size">
            <ul class="rt-dropdown__menu flex flex-col gap-30">
                <?php foreach ($currency_data['list'] as $code => $currency) : ?>
                    <li>
                        <a href="?<?php echo esc_attr($url_param); ?>=<?php echo esc_attr($code); ?>" class="rt-currency-item flex items-center gap-20">
                            <?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>
<?php endif; ?>