<?php
if (!defined('WPINC')) {
    die();
}

use EstatebudConnect\Utilities\Helper;
use EstatebudConnect\Configs\Icons;
use EstatebudConnect\Models\Field;

$fields = $fields ?? [];
$advanced_fields = $advanced_fields ?? [];
$field_options = $field_options ?? [];
$shortcode_id = $shortcode_id ?? '';
$form_id = $form_id ?? '';
$icon_close = Icons::get_svg_icon('xmark-large', Icons::SHAPE_SHARP, Icons::STYLE_SOLID, 24, 24, '#B8B8B8');
?>
<div class="estatebud-more-filters-form estatebud-hidden" id="search-more-filters-<?php echo esc_attr($shortcode_id); ?>">
    <div class="estatebud-more-filters-form-header">
        <h2 class="estatebud-more-filters-form-title"><?php echo esc_html(Helper::term('listings.button.filters')); ?></h2>
        <button type="button" class="estatebud-more-filters-form-close" id="estatebud-mobile-filter-close-<?php echo esc_attr($shortcode_id); ?>" aria-label="Close"><?php echo wp_kses($icon_close, Icons::get_kses_ruleset()); ?></button>
    </div>
    <div class="estatebud-more-filters-form-scroll">
    <?php $advanced_field_ids = array_filter(
        array_map(static function ($advanced_field) {
            return $advanced_field['id'] ?? null;
        }, $advanced_fields)
    ); ?>
    <?php foreach ($fields as $field): ?>
        <?php
        $field_obj = new Field($field);
        $field_id = $field_obj->get_id();
        $is_server_advanced = $field_id && in_array($field_id, $advanced_field_ids, true);
        $is_conditional = $field_obj->is_conditional();

        $wrapper_styles = [];
        if ($is_conditional) {
            $wrapper_styles[] = 'display: none;';
        }
        if (!$is_server_advanced) {
            $wrapper_styles[] = 'display: none;';
        }
        $style_attr = !empty($wrapper_styles) ? implode(' ', $wrapper_styles) : '';

        $field_type_for_attr = $field_obj->get_type();
        if ($field_obj->get_name() && !empty($field_options)) {
            $matching_api_field = array_values(
                array_filter($field_options, function ($option) use ($field_obj) {
                    return isset($option['name']) && $option['name'] === $field_obj->get_name();
                })
            );
            if (!empty($matching_api_field)) {
                $field_type_for_attr = $field_obj->get_name();
            }
        }
        ?>
        <div class="estatebud-advanced-field-wrapper<?php echo $is_conditional ? ' conditional-field' : ''; ?>"
            id="advanced-filter-<?php echo esc_attr($field_id . '-' . $shortcode_id); ?>"
            data-server-advanced="<?php echo $is_server_advanced ? 'true' : 'false'; ?>"
            data-field-id="<?php echo esc_attr($field_id); ?>"
            data-field-type="<?php echo esc_attr($field_type_for_attr); ?>"
            data-custom-advanced="false"
            <?php if ($is_conditional): ?>
            data-conditional-field="<?php echo esc_attr($field_obj->get_conditional_field()); ?>"
            data-conditional-value="<?php echo esc_attr($field_obj->get_conditional_value()); ?>"
            <?php endif; ?>
            <?php echo $style_attr ? ' style="' . esc_attr(trim($style_attr)) . '"' : ''; ?>>
        <?php Helper::render_markup_from_views('Public/Form/fields/search-fields-group.php', [
            'form_id' => $form_id,
            'field' => $field,
            'field_options' => $field_options,
        ]); ?>
        </div>
    <?php endforeach; ?>
    </div>
    <?php if ($form_data && $form_data['is_button_available']): ?>
    <div class="estatebud-more-filters-form-actions">
        <?php $button_icon = isset($form_data['button_icon_type']) && $form_data['button_icon_type']
        ? Icons::get_svg_icon($form_data['button_icon_type'], Icons::SHAPE_SHARP, Icons::STYLE_SOLID, 18, 18, '#ffffff')
        : ''; ?>
        <button type="button" class="estatebud-button estatebud-more-filters-search" id="estatebud-mobile-filter-search-<?php echo esc_attr($shortcode_id); ?>" aria-label="Search">
            <?php if ($button_icon && $form_data['button_icon_position'] === 'left'):
                echo wp_kses($button_icon, Icons::get_kses_ruleset());
            endif; ?>
            <span class="estatebud-button-title"><?php echo esc_html(Helper::term(sprintf('form.%s.button.label', $form_id))); ?></span>
            <?php if ($button_icon && $form_data['button_icon_position'] === 'right'):
                echo wp_kses($button_icon, Icons::get_kses_ruleset());
            endif; ?>
        </button>
    </div>
    <?php endif; ?>
</div>