<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Templates use local variables supplied by the render context.
if (!defined('ABSPATH')) {
    exit;
}
$wrapper_id = sanitize_html_class((string) ($wrapper_id ?? wp_unique_id('nexdirectory-widget-search-')));
$template = sanitize_html_class((string) ($template ?? '1'));
$title = sanitize_text_field((string) ($title ?? ''));
$subtitle = sanitize_text_field((string) ($subtitle ?? ''));
$style_vars = trim((string) ($style_vars ?? ''));
$custom_class = trim((string) ($custom_class ?? ''));
$results_markup = (string) ($results_markup ?? '');
$result_count = absint((int) ($result_count ?? 0));
$search_placeholder = sanitize_text_field((string) ($search_placeholder ?? __('Search listings by keyword...', 'nexdirectory')));
$default_filters = isset($default_filters) && is_array($default_filters) ? $default_filters : [];
$event_type_options = isset($event_type_options) && is_array($event_type_options) ? $event_type_options : [];
$event_mode_options = isset($event_mode_options) && is_array($event_mode_options) ? $event_mode_options : [];
$event_tag_options = isset($event_tag_options) && is_array($event_tag_options) ? $event_tag_options : [];
$event_price_options = isset($event_price_options) && is_array($event_price_options) ? $event_price_options : [];
$search_config = isset($search_config) && is_array($search_config) ? $search_config : [];
$search_config['defaults'] = [
    'search' => sanitize_text_field((string) ($default_filters['search'] ?? '')),
    'event_type' => sanitize_text_field((string) ($default_filters['event_type'] ?? '')),
    'event_mode' => sanitize_text_field((string) ($default_filters['event_mode'] ?? '')),
    'event_tag' => sanitize_text_field((string) ($default_filters['event_tag'] ?? '')),
    'event_price' => sanitize_text_field((string) ($default_filters['event_price'] ?? '')),
    'city' => sanitize_text_field((string) ($default_filters['city'] ?? '')),
    'date_from' => sanitize_text_field((string) ($default_filters['date_from'] ?? '')),
    'official' => sanitize_text_field((string) ($default_filters['official'] ?? '')),
];
$config_json = wp_json_encode($search_config);
$custom_css = trim((string) ($custom_css ?? ''));
$custom_css_scoped = '';
if ($custom_css !== '') {
    $custom_css_scoped = str_replace('{{WRAPPER}}', '#' . $wrapper_id, $custom_css);
    if (strpos($custom_css_scoped, '{') === false) {
        $custom_css_scoped = '#' . $wrapper_id . '{' . $custom_css_scoped . '}';
    }
}
$wrapper_classes = trim('nexdirectory-widget-block nexdirectory-widget-search-block nexdirectory-widget-template-' . $template . ' ' . $custom_class);
?>
<section
    id="<?php echo esc_attr($wrapper_id); ?>"
    class="<?php echo esc_attr($wrapper_classes); ?>"
    data-nexdirectory-widget-search
    data-nexdirectory-widget-config="<?php echo esc_attr((string) $config_json); ?>"
    <?php echo $style_vars !== '' ? ' style="' . esc_attr($style_vars) . '"' : ''; ?>
>
    <?php if ($title !== '' || $subtitle !== '') : ?>
        <header class="nexdirectory-widget-header">
            <?php if ($title !== '') : ?>
                <h3 class="nexdirectory-widget-title"><?php echo esc_html($title); ?></h3>
            <?php endif; ?>
            <?php if ($subtitle !== '') : ?>
                <p class="nexdirectory-widget-subtitle"><?php echo esc_html($subtitle); ?></p>
            <?php endif; ?>
        </header>
    <?php endif; ?>

    <form class="nexdirectory-widget-search-form" data-nexdirectory-widget-search-form>
        <div class="nexdirectory-widget-search-fields">
            <label class="screen-reader-text" for="<?php echo esc_attr($wrapper_id . '-search'); ?>"><?php esc_html_e('Search listings', 'nexdirectory'); ?></label>
            <input id="<?php echo esc_attr($wrapper_id . '-search'); ?>" type="text" name="search" value="<?php echo esc_attr((string) ($default_filters['search'] ?? '')); ?>" placeholder="<?php echo esc_attr($search_placeholder); ?>">

            <?php if (!empty($show_type)) : ?>
                <select name="event_type">
                    <option value=""><?php esc_html_e('All Types', 'nexdirectory'); ?></option>
                    <?php foreach ($event_type_options as $term) : ?>
                        <?php if (!$term instanceof WP_Term) { continue; } ?>
                        <option value="<?php echo esc_attr((string) $term->slug); ?>" <?php selected((string) ($default_filters['event_type'] ?? ''), (string) $term->slug); ?>><?php echo esc_html((string) $term->name); ?></option>
                    <?php endforeach; ?>
                </select>
            <?php endif; ?>

            <?php if (!empty($show_mode)) : ?>
                <select name="event_mode">
                    <option value=""><?php esc_html_e('All Modes', 'nexdirectory'); ?></option>
                    <?php foreach ($event_mode_options as $term) : ?>
                        <?php if (!$term instanceof WP_Term) { continue; } ?>
                        <option value="<?php echo esc_attr((string) $term->slug); ?>" <?php selected((string) ($default_filters['event_mode'] ?? ''), (string) $term->slug); ?>><?php echo esc_html((string) $term->name); ?></option>
                    <?php endforeach; ?>
                </select>
            <?php endif; ?>

            <?php if (!empty($show_tag)) : ?>
                <select name="event_tag">
                    <option value=""><?php esc_html_e('Any Tag', 'nexdirectory'); ?></option>
                    <?php foreach ($event_tag_options as $term) : ?>
                        <?php if (!$term instanceof WP_Term) { continue; } ?>
                        <option value="<?php echo esc_attr((string) $term->slug); ?>" <?php selected((string) ($default_filters['event_tag'] ?? ''), (string) $term->slug); ?>><?php echo esc_html((string) $term->name); ?></option>
                    <?php endforeach; ?>
                </select>
            <?php endif; ?>

            <?php if (!empty($show_price)) : ?>
                <select name="event_price">
                    <option value=""><?php esc_html_e('Any Price', 'nexdirectory'); ?></option>
                    <?php foreach ($event_price_options as $term) : ?>
                        <?php if (!$term instanceof WP_Term) { continue; } ?>
                        <option value="<?php echo esc_attr((string) $term->slug); ?>" <?php selected((string) ($default_filters['event_price'] ?? ''), (string) $term->slug); ?>><?php echo esc_html((string) $term->name); ?></option>
                    <?php endforeach; ?>
                </select>
            <?php endif; ?>

            <?php if (!empty($show_city)) : ?>
                <input type="text" name="city" value="<?php echo esc_attr((string) ($default_filters['city'] ?? '')); ?>" placeholder="<?php esc_attr_e('City', 'nexdirectory'); ?>">
            <?php endif; ?>

            <?php if (!empty($show_date)) : ?>
                <input type="date" name="date_from" value="<?php echo esc_attr((string) ($default_filters['date_from'] ?? '')); ?>">
            <?php endif; ?>

            <?php if (!empty($show_official)) : ?>
                <select name="official">
                    <option value=""><?php esc_html_e('All Listings', 'nexdirectory'); ?></option>
                    <option value="1" <?php selected((string) ($default_filters['official'] ?? ''), '1'); ?>><?php esc_html_e('Official', 'nexdirectory'); ?></option>
                    <option value="0" <?php selected((string) ($default_filters['official'] ?? ''), '0'); ?>><?php esc_html_e('Unofficial', 'nexdirectory'); ?></option>
                </select>
            <?php endif; ?>
        </div>
        <div class="nexdirectory-widget-search-actions">
            <button type="submit" class="nexdirectory-btn"><?php esc_html_e('Search', 'nexdirectory'); ?></button>
            <button type="button" class="nexdirectory-btn nexdirectory-btn-secondary" data-nexdirectory-widget-search-reset><?php esc_html_e('Reset', 'nexdirectory'); ?></button>
        </div>
    </form>

    <div class="nexdirectory-widget-meta">
        <?php /* translators: 1: Placeholder value 1, 2: Placeholder value 2. */ ?>
        <span class="nexdirectory-widget-count" data-nexdirectory-widget-search-count><?php echo esc_html(sprintf(_n('%d result', '%d results', $result_count, 'nexdirectory'), $result_count)); ?></span>
    </div>

    <div class="nexdirectory-widget-search-results-wrap" data-nexdirectory-widget-search-results>
        <?php echo wp_kses((string) $results_markup, NEXDIRECTORY_Escaping::allowed_html()); ?>
    </div>

    <?php if ($custom_css_scoped !== '') : ?>
        <?php $this->enqueue_inline_style_asset('nexdirectory-widget-events-search-custom', nexdirectory_strip_all_tags($custom_css_scoped)); ?>
    <?php endif; ?>
</section>
