<?php
$themeHelper = $this->helper('Wyomind\ElasticsearchCore\Helper\Theme');
$dataHelper = $this->helper('Wyomind\ElasticsearchCore\Helper\Data');
$taxHelper = $this->helper('Wyomind\ElasticsearchCore\Helper\Tax');
$currencyHelper = $this->helper('Wyomind\ElasticsearchCore\Helper\Currency');
$configHelper = $this->helper('Wyomind\ElasticsearchAutocomplete\Helper\Config');
$helper = $this->helper('Magento\Search\Helper\Data');

$eaConfig = $block->escapeHtml(json_encode($configHelper->getConfig()));

$storeCode = $dataHelper->getCurrentStoreCode();
$storeId = $dataHelper->getCurrentStoreId();

$autocompleteUrl = $dataHelper->getElasticsearchUrl($storeCode);
$jsComponent = "wyoea";

$searchId = "search_".rand(0,999999);
$searchMiniFormId = "search_mini_form".rand(0,999999);

/* @noEscape */ echo $block->getChildBlock('elasticsearchautocomplete.autocomplete.mage_version')->toHtml();

?>


<?php if (stripos($themeHelper->getThemeModel(), "codazon") !== false) : ?>
    <?php
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // CODAZON
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ?>
    <?php
    /** @var $block \Magento\Framework\View\Element\Template */
    /** @var $helper \Magento\Search\Helper\Data */
    $helper = $this->helper('Magento\Search\Helper\Data');
    $themeHelper = $this->helper('Codazon\ThemeLayoutPro\Helper\Data');
    $displayAdvancedSearch = (bool)$themeHelper->getConfig('themelayoutpro/header/display_advanced_search');
    $enableFullBox = (bool)$themeHelper->getConfig('themelayoutpro/header/enable_full_search_box');
    $widget = ['themewidgets' => [
        'codazon.searchtoggle' => ['onlyMobi' => true],
        'codazon.fullsearchbox' => ['enable' => $enableFullBox]
    ]];
    ?>

    <div class="header-search<?= $displayAdvancedSearch ? '' : ' hide-advanced-search'; ?><?= $enableFullBox ? '' : ' no-full-box'; ?>"
         data-role="search_container" data-mage-init='<?php echo json_encode($widget); ?>'>
        <div class="header-search-inner">
            <a href="javascript:void(0)"
               class="search-toggle<?= $enableFullBox ? '' : ' full-box-trigger'; ?> visible-xs"
               data-role="search_toggle"><?= __('Search') ?></a>
            <div class="hidden-xs search-form" data-role="search_form">
                <form name="searchForm" class="form minisearch" id="<?= $searchMiniFormId ?>"
                      action="<?php /* @escapeNotVerified */
                      echo $helper->getResultUrl() ?>" method="get">
                    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label relative_pos md-input-container">
                        <!--<label class="mdl-textfield__label" for="search"
                               data-role="minisearch-label"><?php echo __('Search entire store here...'); ?></label>-->
                        <input id="<?= /* @noEscape */ $searchId; ?>"
                               data-mage-init='{"<?= /* @noEscape */ $jsComponent; ?>": {
                               "formSelector": "#<?= /* @noEscape */ $searchMiniFormId; ?>",
                               "url":"<?php /* @escapeNotVerified */ echo $autocompleteUrl ?>",
                               "searchUrl":"<?= /* @noEscape */ $helper->getResultUrl(); ?>",
                               "storeCode":"<?= /* @noEscape */ $storeCode; ?>",
                               "destinationSelector": "#search_autocomplete",
                               "config": <?= /* @noEscape */ $eaConfig; ?>,
                               "urlUpdateSearchTerm": "<?= /* @noEscape */ $block->getUrl('elasticsearchcore/searchTerm/add'); ?>"
                               }}'
                               type="text"
                               name="<?php /* @escapeNotVerified */ echo $helper->getQueryParamName() ?>"
                               value="<?php /* @escapeNotVerified */echo $helper->getEscapedQueryText() ?>"
                               placeholder="<?php /* @escapeNotVerified */ echo __('Search entire store here...'); ?>"
                               class="input-text"
                               maxlength="<?php /* @escapeNotVerified */ echo $helper->getMaxQueryLength(); ?>"
                               role="combobox"
                               aria-haspopup="false"
                               aria-autocomplete="both"
                               autocomplete="off"/>
                        <?= /* @noEscape */ $block->getChildHtml() ?>
                    </div>
                    <div class="actions">
                        <button type="submit"
                                title="<?php echo $block->escapeHtml(__('Search')) ?>"
                                class="action search mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect">
                            <?php /* @escapeNotVerified */
                            echo __('Search'); ?>
                        </button>
                    </div>
                </form>
            </div>
            <div class="visible-xs search-toggle-wrap">
                <button class="search-toggle mdl-js-button mdl-button--fab mdl-js-ripple-effect"
                        data-role="search_toggle"><span class="icon"><?php echo __('Search') ?></span></button>
            </div>
        </div>
    </div>


<?php elseif (stripos($themeHelper->getThemeModel(),"infortis") !== false) : ?>
    <?php
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // INFORTIS/ULTIMO
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ?>
    <?php
    $theme = $this->helper('Infortis\Base\Helper\Data');

    $searchClasses = '';
    $mode = $theme->getCfgDesign('search/mode');
    if ($mode === 'e') {
        $searchClasses .= ' expanding';
    }

    $searchSize = $theme->getCfgDesign('search/size');
    if (!empty($searchSize)) {
        $searchClasses .= ' size-' . $searchSize;
    }
    ?>
    <div id="header-search" class="ultimo skip-content skip-content--style">
        <div id="block-search"
             class="block block-search search-wrapper<?= /* @noEscape */ $searchClasses; ?>"> <?php /* @deprecated class "search-wrapper" */ ?>
            <div class="block block-title"><strong><?php /* @escapeNotVerified */
                    echo __('Search'); ?></strong></div>
            <div class="block block-content">
                <form class="form minisearch" id="<?= /* @noEscape */ $searchMiniFormId; ?>" action="<?php echo /* @escapeNotVerified */
                $helper->getResultUrl() ?>" method="get">
                    <div class="field search">
                        <label class="label" for="<?= /* @noEscape */ $searchId; ?>" data-role="minisearch-label">
                        </label>
                        <div class="control">
                            <input id="<?= /* @noEscape */ $searchId; ?>"
                                   data-mage-init='{"<?= /* @noEscape */ $jsComponent; ?>": {
                               "formSelector": "#<?= /* @noEscape */ $searchMiniFormId; ?>",
                               "url":"<?php /* @escapeNotVerified */ echo $autocompleteUrl ?>",
                               "searchUrl":"<?= /* @noEscape */ $helper->getResultUrl(); ?>",
                               "storeCode":"<?= /* @noEscape */ $storeCode; ?>",
                               "destinationSelector": "#search_autocomplete",
                               "config": <?= /* @noEscape */ $eaConfig; ?>,
                               "urlUpdateSearchTerm": "<?= /* @noEscape */ $block->getUrl('elasticsearchcore/searchTerm/add'); ?>"
                               }}'
                                   type="text"
                                   name="<?php /* @escapeNotVerified */ echo $helper->getQueryParamName() ?>"
                                   value="<?php /* @escapeNotVerified */echo $helper->getEscapedQueryText() ?>"
                                   placeholder="<?php /* @escapeNotVerified */ echo __('Search entire store here...'); ?>"
                                   class="input-text"
                                   maxlength="<?php /* @escapeNotVerified */ echo $helper->getMaxQueryLength(); ?>"
                                   role="combobox"
                                   aria-haspopup="false"
                                   aria-autocomplete="both"
                                   autocomplete="off"/>
                            <?= /* @noEscape */ $block->getChildHtml() ?>
                        </div>
                    </div>
                    <div class="actions">
                        <button id="action-search" type="submit"
                                title="<?= /* @noEscape */ $block->escapeHtml(__('Search')) ?>"
                                class="action search">
                            <span class="icon ic ic-search ib ib-square ib-hover"></span>
                        </button>
                    </div>
                </form>
            </div>
        </div> <!-- end: block-search -->
        <?php if ($mode === 'e'): ?>
            <script type="text/javascript">
                //<![CDATA[
                requirejs(['jquery', 'expandingsearch'], function (jQuery, expandingsearch) {
                    jQuery(function ($) {
                        $('#block-search').expandingsearch();
                    });
                }); //end: requirejs
                //]]>
            </script>
        <?php endif; ?>
    </div>


<?php else: ?>


    <?php
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// DEFAULT (LUMA)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ?>
    <div class="block block-search">
        <div class="block block-title"><strong><?php /* @escapeNotVerified */
                echo __('Search'); ?></strong></div>
        <div class="block block-content">
            <form class="form minisearch" id="<?= /* @noEscape */ $searchMiniFormId; ?>" action="<?php /* @escapeNotVerified */
            echo $helper->getResultUrl() ?>" method="get">
                <div class="field search">
                    <label class="label" for="<?= /* @noEscape */ $searchId; ?>" data-role="minisearch-label">
                    </label>
                    <div class="control">
                        <input id="<?= /* @noEscape */ $searchId; ?>"
                               data-mage-init='{"<?= /* @noEscape */ $jsComponent; ?>": {
                               "formSelector": "#<?= /* @noEscape */ $searchMiniFormId; ?>",
                               "url":"<?php /* @escapeNotVerified */ echo $autocompleteUrl ?>",
                               "searchUrl":"<?= /* @noEscape */ $helper->getResultUrl(); ?>",
                               "storeCode":"<?= /* @noEscape */ $storeCode; ?>",
                               "destinationSelector": "#search_autocomplete",
                               "config": <?= /* @noEscape */ $eaConfig; ?>,
                               "urlUpdateSearchTerm": "<?= /* @noEscape */ $block->getUrl('elasticsearchcore/searchTerm/add'); ?>"
                               }}'
                               type="text"
                               name="<?php /* @escapeNotVerified */ echo $helper->getQueryParamName() ?>"
                               value="<?php /* @escapeNotVerified */ echo $helper->getEscapedQueryText() ?>"
                               placeholder="<?php /* @escapeNotVerified */ echo __('Search entire store here...'); ?>"
                               class="input-text"
                               maxlength="<?php /* @escapeNotVerified */ echo $helper->getMaxQueryLength(); ?>"
                               role="combobox"
                               aria-haspopup="false"
                               aria-autocomplete="both"
                               autocomplete="off"/>
                        <?= /* @noEscape */ $block->getChildHtml() ?>
                    </div>
                </div>
                <div class="actions">
                    <button type="submit"
                            title="<?= /* @noEscape */ $block->escapeHtml(__('Search')) ?>"
                            class="action search">
                    <span><?php /* @escapeNotVerified */
                        echo __('Search'); ?></span>
                    </button>
                </div>
            </form>
        </div>
    </div>
<?php endif; ?>

<?php
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// COMMON
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?>
<script>
    require(['underscore', 'Magento_Catalog/js/price-utils'], function (_, utils) {
        _.mixin({
            getBaseUrl: function () {
                return '<?= /* @noEscape */ $block->getBaseUrl() ?>';
            },
            getSearchUrl: function () {
                return '<?= /* @noEscape */ $helper->getResultUrl(); ?>';
            },
            getFormattedPrice: function (price, productTaxClassId) {
                return _.formatPrice(_.calculateTax(_.convertPrice(price), productTaxClassId));
            },
            formatPrice: function (price) {
                var priceFormat = <?php /* @escapeNotVerified */ echo $this->helper('Magento\Tax\Helper\Data')->getPriceFormat($block->getStore()); ?>;

                return utils.formatPrice(price, priceFormat);
            },
            calculateTax: function (price, productTaxClassId) {
                var needPriceConversion = <?= /* @noEscape */ $taxHelper->needPriceConversion() ? 'true' : 'false' ?>;
                var taxRates = <?php /* @escapeNotVerified */ echo json_encode($taxHelper->getRates()) ?>;

                if (!needPriceConversion || !taxRates[productTaxClassId]) {
                    return price;
                }

                var rate = taxRates[productTaxClassId] / 100;
                var priceIncludesTax = <?= /* @noEscape */ $taxHelper->priceIncludesTax() ? 'true' : 'false' ?>;

                if (priceIncludesTax) {
                    return price / (1 + rate);
                }

                return price + price * rate;
            },
            convertPrice: function (price) {
                var rate = <?= /* @noEscape */ $currencyHelper->getCurrentCurrencyRate() ?>;

                return price * rate;
            }
        });
    });
</script>

