import URI from 'urijs';

export default () => {
    let recordList = $('#record-list');

    if (recordList.length > 0) {
        recordList.on('click', '.btn-delete', function (e) {
            e.preventDefault();

            let btn = $(this);
            if (!btn.hasClass('btn-reload')) {
                btn.addClass('btn-reload');
                btn.attr('data-action-url', btn.attr('href'));
                btn.attr('data-message', LOCALE.CARD_LIST__DELETE_MESSAGE);
                btn.attr('data-confirm', LOCALE.CARD_LIST__DELETE_CONFIRM);
            }
        });

        let searchWrapper = $('#record-search');
        if (searchWrapper.length > 0) {
            let btnSearch = searchWrapper.find('.btn-search');
            let btnClear = searchWrapper.find('.btn-search-clear');
            let inputs = searchWrapper.find('input, select');
            let searchUrl = new URI(window.location.href);

            btnSearch.on('click', (e) => {
                e.preventDefault();

                let query = {};
                inputs.each(function () {
                    let input = $(this);

                    query[input.attr('name')] = input.val();
                });

                window.location = searchUrl.query(query).toString();
            });

            btnClear.on('click', (e) => {
                e.preventDefault();

                window.location = searchUrl.query('').toString();
            });
        }
    }
};
