$(function () {
   // $('.selectpicker').selectpicker();
    $('[data-toggle="tooltip"]').tooltip();
});


function update_modal_data_load(e, $ele) {
    var ele = $ele,
        url = ele.data('url'),
        data =  {data:ele.data('data'), target:ele.data('target')} ,
        contentType = 'application/x-www-form-urlencoded; charset=UTF-8';

    console.log(data);
    console.log(url);
    $.ajax({
        url: url,
        data: data,
        type: "POST",
        dataType: "json",
        cache: false,
        contentType: contentType,
        encode: true,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function(data) {
            // do what ever you want here. add content to <div> if it was not 1 .
            $(data.target).find('.modal-content').html(data.response);
           // $('.selectpicker').selectpicker(["refresh"]);
           // $('.input-daterange').datepicker({toggleActive: true,format: 'dd.mm.yyyy'});
            $(data.target).modal('show');
        },
        error: function(xhr, status, errorThrown) {
            console.log(xhr);
            console.log(xhr.responseText);
            console.log(status);
            console.log("Sorry, there was a problem!");
        }
    });
    return false;
}

jQuery(document).ready(function() {
    $(".update_modal_data_load").on('click', function (e) {
        e.preventDefault();
        update_modal_data_load(e, $(this));
    });
    if($('.datepicker-base').length > 0) {
        $('.datepicker-base').datepicker({
            orientation: 'auto right',
            calendarWeeks: true,
            todayBtn: 'linked',
            //daysOfWeekDisabled: '1',
            todayHighlight: true,
            multidate: false,
            daysOfWeekHighlighted: '0,6',
            autoclose: true,
            format: 'dd.mm.yyyy',
            language: 'de',
            clearBtn: true,
        });
    }
    if($('.datepicker-birthday').length > 0) {
        $('.datepicker-birthday').datepicker({
            todayBtn: 'linked',
            // daysOfWeekDisabled: '1',
            multidate: false,
            daysOfWeekHighlighted: '0,6',
            autoclose: true,
            format: 'dd.mm.yyyy',
            language: 'de',
            clearBtn: true,
            startView: 2,

        });
    }
    if($('.b-material-datetime-picker').length > 0) {
        $('.b-material-datetime-picker').bootstrapMaterialDatePicker({
            weekStart: 1,
            format: 'DD.MM.YYYY HH:mm',
            shortTime: false,
            nowButton: true,
            clearButton: true,
            lang: 'de',
            //currentDate: ''
        });
    }

});

function _scrollTo(to, offset) {
    $('html,body').animate({scrollTop: $(to).offset().top - offset}, 800);
}

$(function () {

    $('#modals-load-content').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        if (!button.data('id')) {
            return;
        }
        var data = {};
        $.each(button.data(), function(index, value){
            if(index !== 'bs.tooltip'){
                data[index] = value;
            }
        });
        console.log(data);

        loadModalInner(this, data);

    });

    function initModalInner() {
        $('[data-toggle="reloadModal"]').off().on('click', function(event) {
            event.preventDefault();
            button = $(this);
            var data = {};
            $.each(button.data(), function(index, value){
                if(index !== 'bs.tooltip'){
                    data[index] = value;
                }
            });
            //console.log(data);
            loadModalInner(this, data);
        });

    }
    function loadModalInner(self, data){
        var url = data.route,
            contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
        $.ajax({
            url: url,
            data: data,
            type: "POST",
            dataType: "json",
            cache: false,
            contentType: contentType,
            encode: true,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function(data) {
                console.log(data);
                if(data.response.modal){
                    $(data.response.target).find('.modal-dialog').addClass(data.response.modal);
                }
                $(data.response.target).find('.modal-dialog').html(data.html);
                $(data.response.target).find('.selectpicker').selectpicker('refresh');
                initModalInner();
            },
            error: function(xhr, status, errorThrown) {
                console.log(xhr);
                console.log(xhr.responseText);
                console.log(errorThrown);
                console.log("Sorry, there was a problem!");
            }
        });
        return false;
    }

    function ajax_object_action(event, object, callback) {
        event.preventDefault();
        var data = {};
        $.each(object.data(), function(index, value){
            if(typeof value !== 'object'){
                data[index] = value;
            }
        });
        var url = data['url'];
        console.log(data);
        console.log(url);
        $.ajax({
            url: url,
            data: data,
            type: "POST",
            dataType: "json",
            cache: false,
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            encode: true,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function(data) {
                //data will send data to callback function
                callback(data);
            },
            error: function(xhr, status, errorThrown) {
                console.log(xhr);
                console.log(xhr.responseText);
                console.log(status);
                console.log(errorThrown);
                console.log("Sorry, there was a problem!");
            }
        });
        return false;
    }
});