<?php
/**
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
 * versions in the future.
 *
 * @category  Smile
 * @package   Smile\ElasticsuiteCore
 * @author    Romain Ruaud <romain.ruaud@smile.fr>
 * @copyright 2020 Smile
 * @license   Open Software License ("OSL") v. 3.0
 */

/**
 * Template for quick search mini form.
 * Overriden to manage template injection for the rendering of autocomplete results.
 *
 * @var $block \Smile\ElasticsuiteCore\Block\Search\Form\Autocomplete
 */
?>

<?php
/** @var $helper \Magento\Search\Helper\Data */
$helper = $this->helper('Magento\Search\Helper\Data');

$blockId = preg_replace('/[^a-z0-9]+/i', '-', $block->getNameInLayout());
$formId = 'minisearch-form-' . $blockId;
$inputId = 'minisearch-input-' . $blockId;
$autoCompleteId = 'minisearch-autocomplete-' . $blockId;
?>

<div class="block block-search">
    <div class="block block-title">
        <strong><?= /* @escapeNotVerified */ __('Search') ?></strong>
    </div>

    <div class="block block-content">
        <form
            id="<?= /* @noEscape */ $formId ?>"
            class="form minisearch"
            action="<?= /* @escapeNotVerified */ $helper->getResultUrl() ?>"
            method="get"
        >
            <div class="field search">
                <label class="label" for="<?= /* @noEscape */ $inputId ?>" data-role="minisearch-label">
                    <span><?= /* @escapeNotVerified */ __('Search') ?></span>
                </label>

                <div class="control">
                    <input
                        id="<?= /* @noEscape */ $inputId ?>"
                        type="text"
                        name="<?= /* @escapeNotVerified */ $helper->getQueryParamName() ?>"
                        value="<?= /* @escapeNotVerified */ $helper->getEscapedQueryText() ?>"
                        placeholder="<?= /* @escapeNotVerified */ __('Search entire store here...') ?>"
                        class="input-text"
                        maxlength="<?= /* @escapeNotVerified */ $helper->getMaxQueryLength() ?>"
                        role="combobox"
                        aria-haspopup="false"
                        aria-autocomplete="both"
                        aria-expanded="false"
                        autocomplete="off"
                        data-block="autocomplete-form"
                        data-mage-init="<?= $escaper->escapeHtmlAttr(json_encode([
                            'quickSearch' => [
                                'formSelector' => '#' . $formId,
                                'url' => $block->getUrl('search/ajax/suggest'),
                                'destinationSelector' => '#' . $autoCompleteId,
                                'templates' => json_decode($block->getJsonSuggestRenderers()),
                                'priceFormat' => json_decode($block->getJsonPriceFormat()),
                                'minSearchLength' => $helper->getMinQueryLength(),
                            ],
                        ]))?>"
                    />

                    <div id="<?= /* @noEscape */ $autoCompleteId ?>" class="search-autocomplete"></div>

                    <?= $block->getChildHtml() ?>
                </div>
            </div>

            <div class="actions">
                <button
                    type="submit"
                    title="<?= $block->escapeHtml(__('Search')) ?>"
                    class="action search"
                >
                    <span><?= /* @escapeNotVerified */ __('Search') ?></span>
                </button>
            </div>
        </form>
    </div>
</div>
