<?php
defined('ABSPATH') || exit;
?>
<section class="kb-search">
    <h2><?php echo esc_html($title ?? __('Dokumentation durchsuchen', 'kb-markdown-importer')); ?></h2>
    <form method="get" class="kb-search-form">
        <input type="search" name="kbq" value="<?php echo esc_attr($query ?? ''); ?>" placeholder="<?php esc_attr_e('Dokumentation durchsuchen', 'kb-markdown-importer'); ?>">
        <button type="submit"><?php esc_html_e('Suchen', 'kb-markdown-importer'); ?></button>
    </form>
    <?php if (! empty($results)) : ?>
        <ul class="kb-search-results">
            <?php foreach ((array) $results as $result) : ?>
                <?php
                $product = get_post_meta($result->ID, '_kb_product_slug', true);
                $version = get_post_meta($result->ID, '_kb_version_slug', true);
                $page = get_post_meta($result->ID, '_kb_page_slug', true);
                $url = \KbMarkdownImporter\Frontend\UrlBuilder::page((string) $product, (string) $version, (string) $page);
                ?>
                <li>
                    <a href="<?php echo esc_url($url); ?>"><?php echo esc_html(get_the_title($result)); ?></a>
                    <p><?php echo esc_html(wp_trim_words(wp_strip_all_tags($result->post_content), 24)); ?></p>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</section>
