{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template theme_adaptable/core/search_input_navbar

    Navbar search input template.

    Example context (json):
    {
        "action": "https://moodle.local/admin/search.php",
        "inputname": "search",
        "searchstring": "Search",
        "hiddenfields": [
            {
                "name": "cmid",
                "value": "11"
            }
        ]
    }
}}
<div id="searchinput-navbar-{{uniqid}}" class="simplesearchform adsimplesearchform">
    <div class="collapse" id="searchform-navbar-{{uniqid}}">
        <form autocomplete="off" action="{{{ action }}}" method="get" accept-charset="utf-8" class="mform d-flex flex-wrap align-items-center searchform-navbar">
            {{#hiddenfields}}
                <input type="hidden" name="{{ name }}" value="{{ value }}">
            {{/hiddenfields}}
            <div class="input-group">
                <input type="text"
                    id="searchinput-{{uniqid}}"
                    class="form-control withclear"
                    placeholder="{{{ searchstring }}}"
                    aria-label="{{{ searchstring }}}"
                    name="{{{ inputname }}}"
                    data-region="input"
                    autocomplete="off"
                >
                <label for="searchinput-{{uniqid}}">
                    <span class="visually-hidden">{{{ searchstring }}}</span>
                </label>
                <button class="btn btn-close adsimplesearchclose"
                    data-action="closesearch"
                    data-bs-toggle="collapse"
                    data-bs-target="#searchform-navbar-{{uniqid}}"
                    type="button"
                >
                    {{#pix}} e/cancel, core {{/pix}}
                    <span class="visually-hidden">{{#str}} closebuttontitle {{/str}}</span>
                </button>
                <button type="submit" class="btn btn-submit" data-action="submit">
                    {{#pix}} a/search, core {{/pix}}
                    <span class="visually-hidden">{{{ searchstring }}}</span>
                </button>
            </div>
        </form>
    </div>
    <button
        class="rounded-0 nav-link icon-no-margin"
        data-bs-toggle="collapse"
        data-bs-target="#searchform-navbar-{{uniqid}}"
        data-action="opensearch"
        type="button"
        aria-expanded="false"
        aria-controls="searchform-navbar-{{uniqid}}"
        title="{{#str}} togglesearch {{/str}}"
    >
        {{#pix}} a/search, core {{/pix}}
        <span class="visually-hidden">{{#str}} togglesearch {{/str}}</span>
    </button>
</div>

{{#js}}
require(
[],
function() {
    const uniqid = "{{uniqid}}";
    const container = document.getElementById('searchinput-navbar-' + uniqid);
    const collapse = container.querySelector('#searchform-navbar-' + uniqid);
    const opensearch = container.querySelector('[data-action="opensearch"]');
    const input = collapse.querySelector('[data-region="input"]');
    const submit = collapse.querySelector('[data-action="submit"]');

    submit.addEventListener('click', (e) => {
        if (input.valUE === '') {
            e.preventDefault();
        }
    });

    collapse.addEventListener('hidden.bs.collapse', () => {
        opensearch.classList.remove('d-none');
        input.value = '';
    });

    collapse.addEventListener('show.bs.collapse', () => {
        opensearch.classList.add('d-none');
    });

    collapse.addEventListener('shown.bs.collapse', () => {
        input.focus();
    });
});
{{/js}}
