<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

$settings = wp_parse_args(get_option(SAPSFWC_OPT_SETTINGS, []), [
        'logo_url' => '',
        'load_more_enabled' => '',
        'close_button_transform' => '',
]);

$is_pro = function_exists('sapsfwc_fs') && sapsfwc_fs()->can_use_premium_code__premium_only();
$shopUrlAllResults = add_query_arg('s', '', wc_get_page_permalink('shop'));
?>

<div id="sapsfwc-root"
     data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>"
     data-nonce="<?php echo esc_attr(wp_create_nonce(SAPSFWC_NONCE_ACTION)); ?>"
     data-shop-url="<?php echo esc_url(wc_get_page_permalink('shop')); ?>"
     data-load-more-enabled="<?php echo esc_attr($is_pro && !empty($settings['load_more_enabled']) ? '1' : '0'); ?>"
     data-close-button-transform="<?php echo esc_attr(!empty($settings['close_button_transform']) ? '1' : '0'); ?>">

    <div id="sapsfwc-overlay" class="sapsfwc-wrap">
        <div class="sapsfwc-container">
            <div class="sapsfwc">

                <div class="sapsfwc-header">
                    <div class="sapsfwc-header-logo">
                        <?php
                        if ($is_pro && !empty($settings['logo_url']) && filter_var($settings['logo_url'], FILTER_VALIDATE_URL)) :
                            $html = sprintf(
                                    '<img src="%s" alt="%s">',
                                    esc_url($settings['logo_url']),
                                    esc_attr__('Search Logo', 'simple-ajax-product-search-for-woocommerce')
                            );
                            echo wp_kses($html, ['img' => ['src' => [], 'alt' => []]]);
                        endif;
                        ?>
                    </div>
                    <button type="button" class="sapsfwc-close"
                            aria-label="<?php esc_attr_e('Close search', 'simple-ajax-product-search-for-woocommerce'); ?>">
                        ×
                    </button>
                </div>

                <div class="sapsfwc-form">
                    <form class="sapsfwc-form" role="search" method="get"
                          action="<?php echo esc_url(wc_get_page_permalink('shop')); ?>">
                        <input type="text" name="s"
                               placeholder="<?php esc_attr_e('Search products...', 'simple-ajax-product-search-for-woocommerce'); ?>">
                        <button class="sapsfwc-form-submit" type="button"
                                aria-label="<?php esc_attr_e('Submit search', 'simple-ajax-product-search-for-woocommerce'); ?>">
                            <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor"
                                 class="bi bi-search" viewBox="0 0 20 20" aria-hidden="true" focusable="false">
                                <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001q.044.06.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1 1 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0"/>
                            </svg>
                        </button>
                    </form>
                </div>

                <div class="sapsfwc-view-all-results is-hidden">
                    <a href="<?php echo esc_url($shopUrlAllResults); ?>">
                        <?php esc_html_e('View All Results', 'simple-ajax-product-search-for-woocommerce'); ?>
                    </a>
                </div>

                <div class="sapsfwc-archive" data-current-paged="1"></div>

                <?php if ($is_pro && !empty($settings['load_more_enabled'])) : ?>
                    <div class="sapsfwc-load-more-wrap is-hidden">
                        <button type="button" class="sapsfwc-load-more button wp-element-button">
                            <?php esc_html_e('Load More', 'simple-ajax-product-search-for-woocommerce'); ?>
                        </button>
                    </div>
                <?php endif; ?>

            </div>
        </div>
    </div>
</div>