(function ($) {
    $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        if (clickedIndex === null) {
            const values = $(this).val();

            // deselect all options
            let href = $(this).data('deselect-all-uri');

            // select all options
            if (values.length > 0) {
                href = $(this).data('select-all-uri');
            }

            if (href) {
                window.location = href;
            }
        } else {
            // select an option
            $(this).find('option').each(function(index, element) {
                if (index === clickedIndex) {
                    const href = element.getAttribute('value');
                    if (href) {
                        window.location = href;
                    }
                }
            });
        }
    });

    const lang = document.getElementsByTagName("html")[0].getAttribute("lang");
    $('input.datepicker').each(function () {
        //$(this).datepicker($.datepicker.regional[ lang ? lang : 'en' ]);
        $(this).datepicker({
            onSelect : function (dateText, inst) {
                $("input[name='start']").trigger('selectDate');
            }
        });

        const dateFormat = $(this).data('dateFormat');
        const options = $(this).data();
        if (options) {
            for (const option in options) {
                let value = options[option];
                if (value) {
                    // parse date options
                    if (option === 'value' || option === 'minDate' || option === 'maxDate') {
                        value = $.datepicker.parseDate(dateFormat ? dateFormat : 'dd-mm-yy', value);
                    }

                    if (option === 'value') {
                        $(this).datepicker('setDate', value);
                    } else if (option !== 'datepicker' && option !== 'uri') {
                        $(this).datepicker('option', option, value);
                    }
                }
            }
        }
    });

    $("input[name='q']").on('keyup keypress', function(e) {
        if (e.target.value) {
            $('#reset-search').removeClass('hide');
            $('#submit-search').removeClass('disabled');
        } else {
            $('#reset-search').addClass('hide');
            $('#submit-search').addClass('disabled');
        }
    });

    $("input[name='start']").on('keypress selectDate', function(e) {
        if (e.keyCode === 13 || e.type === 'selectDate') {
            let uri = e.target.getAttribute('data-uri') + '';
            if (uri) {
                if(e.target.value) {
                    uri += uri.indexOf('?') >= 0 ? '&' : '?';
                    uri += 'start=' + e.target.value;
                }
                window.location = uri;
            }
        }
    });

    $('#event-form').on('submit', function (e) {
        e.preventDefault();
        const value = $('#directory_search').val();
        if (value !== '') {
            $('#submit-search').addClass('disabled');

            let uri = e.target.getAttribute('action') + '';
            uri += uri.indexOf('?') >= 0 ? '&' : '?';
            uri += 'q=' + value;
            window.location = uri;
        }
    });

})(jQuery);
