jQuery(document).ready(function () {
    custom_switch_merchants();
    initAdminLtePlugin();
    initButtonConfirm()
    initNotice();
});

function initNotice() {
    $('#lc-flash-messages .notice').each(function(i, notice){
       generateNotice($(notice).find('.type').html(), $(notice).find('.message').html());
    });
}

function initButtonConfirm() {
    $('.btn-confirm-js').click(function() {
        return confirm('Êtes-vous sûr de vouloir réaliser cette action ?') ;
    }) ;
}

function initLcNoty() {
    $('.lc-noty').each(function () {
        generateNotice($(this).data('type'), $(this).html());
    });
}


function custom_switch_merchants() {
    $('#switch-merchant').on('change', function () {
        $('#switch-merchant').parents('form').submit();
    });
}

function setBootstrapSwitch($checkbox) {

    $checkbox.bootstrapSwitch();
    $checkbox.bootstrapSwitch('state', true);
    $checkbox.on('switchChange.bootstrapSwitch', function (e, state) {
        var event = new Event('change');
        e.target.dispatchEvent(event);
    });
}

function initAdminLtePlugin() {

    $(".checkbox-switch input").each(function () {
        setBootstrapSwitch($(this));
    });

    $('[data-toggle="tooltip"]').tooltip();

    $(document).on('keypress', function (event) {

        /*if(event.keyCode == '13') {
            checkForm();
        }*/
    });
    $('form button[type="submit"]').on('click', function (e) {

        checkForm()
    })

    if ($('.select2, select.form-control').length) {
        $('form .form-widget>select.form-control, .select2').each(function (i, elm) {
            if(!$(this).hasClass('disable-select2')) {
                setSelect2($(elm)) ;
            }
        });

        $('form .form-inline>select.form-control').each(function (i, elm) {
            if(!$(this).hasClass('disable-select2')) {
                setSelect2($(elm)) ;
            }
        });
    }

    $('.autoresize textarea').each(function () {
        this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
    }).on('input', function () {
        log(this.scrollHeight);
        $(this).height('auto');
        log('BLOP' + this.scrollHeight);
        if (this.scrollHeight < 50) {
            $(this).outerHeight(this.scrollHeight - 12);
        } else {
            $(this).outerHeight(this.scrollHeight);
        }
    });


    $('.date-time-range').each(function (i, picker) {
//log(moment('2020-04-05 20:00:00').format( "DD/MM/YYYY HH:mm"))
        options = {
            timePicker: true,
            timePickerIncrement: 30,
            timePicker24Hour: true,
            locale: {
                "format": "DD/MM/YYYY HH:mm",
                "separator": " - ",
                "applyLabel": "Appliquer",
                "cancelLabel": "Annuler",
                "fromLabel": "Du",
                "toLabel": "au",
                "customRangeLabel": "Custom",
                "daysOfWeek": ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
                "monthNames": ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre"],
                "firstDay": 1
            }
        };

        if($(picker).nextAll('.date-time-range-fields').find('.date-start').val()){
            options.startDate = new Date($(picker).nextAll('.date-time-range-fields').find('.date-start').val());
        }
        if($(picker).nextAll('.date-time-range-fields').find('.date-end').val()){
            options.endDate = new Date($(picker).nextAll('.date-time-range-fields').find('.date-end').val());
        }
        $(picker).daterangepicker(options);
        $(picker).on('apply.daterangepicker', function(ev, pickerElm) {
            $(picker).nextAll('.date-time-range-fields').find('.date-start').val(pickerElm.startDate.format('YYYY-MM-DD HH:mm'));
            $(picker).nextAll('.date-time-range-fields').find('.date-end').val(pickerElm.endDate.format('YYYY-MM-DD HH:mm'));
        });
    });


}

function moment() {
    return '2020-04-08';
}


function checkForm(){
    $('form').addClass('form-sent');
    //Panel vues js
    if($('form').find('.panel').length){
        $('form').find('.panel').each(function(i, panel){
            if($(panel).find(':invalid').length){
                $('#nav-params').find('.nav-item:eq('+i+')').addClass('has-invalid');
            }else{
                $('#nav-params').find('.nav-item:eq('+i+')').removeClass('has-invalid');
            }
        })
    }
}

function setSelect2($select ) {
    if (typeof $select.data('select2-id') === 'undefined') {

        $select.data('init', 'set')
        var options = {
            width: "100%",
            dropdownAutoWidth: false,
            allowClear: true,
            minimumResultsForSearch: 8
        };

        if ($select.data('allow-clear') == 'false') {
            options.allowClear = false;
        }
        if ($select.data('width')) {
            options.width = 'auto'
        }
        if ($select.find('option[value=""]')) {
            options.placeholder = $select.find('option[value=""]').html()
        }
        /*if($select.is(':required') == false) {
            options.allowclear = true
        }*/
        myselect = $select.select2(options);

        myselect.on('select2:select', function (e) {
            var event = new Event('change');
            e.target.dispatchEvent(event);
        });
        myselect.on('select2:unselect', function (e) {
            var event = new Event('change');
            e.target.dispatchEvent(event);
        });

        return myselect;
    }
}

function checkForm(){
    $('form').addClass('form-sent');
    //Panel vues js
    if($('form').find('.panel').length){
        $('form').find('.panel').each(function(i, panel){
            if($(panel).find(':invalid').length){
                $('#nav-params').find('.nav-item:eq('+i+')').addClass('has-invalid');
            }else{
                $('#nav-params').find('.nav-item:eq('+i+')').removeClass('has-invalid');
            }
        })
    }
}
