import $ from 'jquery';

export default function initOverview() {
    const $root = $('[data-page="vineyard-overview"]');
    if (!$root.length) {
        return;
    }

    const $filterForm = $root.find('[data-role="filters"]');
    const $resetButton = $root.find('[data-control="reset-filters"]');

    $filterForm.find('select, input').on('change', () => {
        $filterForm.trigger('submit');
    });

    $resetButton.on('click', (event) => {
        event.preventDefault();
        $filterForm.find('input, select').each((_, element) => {
            if (element.tagName === 'SELECT') {
                element.selectedIndex = 0;
            } else {
                element.value = '';
            }
        });
        $filterForm.trigger('submit');
    });
}
