var monecore = {
    configs: {
        pageSize: 12,
        pageIndex: 1
    },
    sweetalert: function (title,message,type) {
        // Advanced initialization
        swal({
            title: title,
            text: message,
            type: type,
            timer: 3000
        });
    },
    notynotifications: function (message, type) {
        var n = noty({
            layout: 'topRight',
            theme: 'defaultTheme',
            type: type, // alert, success, error, warning, information, confirm
            text: message,
            dismissQueue: true, // If you want to use queue feature set this true
            template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
            animation: {
                open: { height: 'toggle' }, // or Animate.css class names like: 'animated bounceInLeft'
                close: { height: 'toggle' }, // or Animate.css class names like: 'animated bounceOutLeft'
                easing: 'swing',
                speed: 500 // opening & closing animation speed
            },
            timeout: 6000, // delay for closing event. Set false for sticky notifications
            force: false, // adds notification to the beginning of queue when set to true
            modal: false,
            maxVisible: 1, // you can set max visible notification for dismissQueue true option,
            killer: false, // for close all notifications before show
            closeWith: ['click'], // ['click', 'button', 'hover', 'backdrop'] // backdrop click will close all notifications
            callback: {
                onShow: function () { },
                afterShow: function () { },
                onClose: function () { },
                afterClose: function () { },
                onCloseClick: function () { },
            }
        });
    },
    dateFormat: function(datetime) {
        var currentTime = new Date(datetime);
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        var year = currentTime.getFullYear();
        var date = day + "/" + month + "/" + year;
        return date;
    },
    notify: function (message, type) {
        $.notify(message, {
            // whether to hide the notification on click
            clickToHide: true,
            // whether to auto-hide the notification
            autoHide: true,
            // if autoHide, hide after milliseconds
            autoHideDelay: 5000,
            // show the arrow pointing at the element
            arrowShow: true,
            // arrow size in pixels
            arrowSize: 5,
            // position defines the notification position though uses the defaults below
            position: '...',
            // default positions
            elementPosition: 'top right',
            globalPosition: 'top right',
            // default style
            style: 'bootstrap',
            // default class (string or [string])
            className: type,
            // show animation
            showAnimation: 'slideDown',
            // show animation duration
            showDuration: 400,
            // hide animation
            hideAnimation: 'slideUp',
            // hide animation duration
            hideDuration: 200,
            // padding between element and notification
            gap: 2
        });
    },
    confirm: function (message, okCallback) {
        bootbox.confirm({
            message: message,
            buttons: {
                confirm: {
                    label: 'Đồng ý',
                    className: 'btn-success'
                },
                cancel: {
                    label: 'Hủy',
                    className: 'btn-danger'
                }
            },
            callback: function (result) {
                if (result === true) {
                    okCallback();
                }
            }
        });
    },
    dateFormatJson: function (datetime) {
        if (datetime === null || datetime === '')
            return '';
        var myDate = new Date(datetime);
        var output = myDate.getFullYear() + "-" + myDate.getDate() + "-" + (myDate.getMonth() + 1);
        return output;
    },
    dateTimeFormatJson: function (datetime) {
        if (datetime === null || datetime === '')
            return '';
        var myDate = new Date(datetime);
        //var output = myDate.getFullYear() + "-" + myDate.getDate() + "-" + (myDate.getMonth() + 1) + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
        var output2 = myDate.getDate() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getFullYear() +   " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
        return output2;
    },
    startLoading: function () {
        if ($('.dv-loading').length > 0)
            $('.dv-loading').removeClass('hide');
    },
    stopLoading: function () {
        if ($('.dv-loading').length > 0)
            $('.dv-loading')
                .addClass('hide');
    },
    getStatus: function (status) {
        if (status === 1)
            return '<span class="label label-success">Kích hoạt</span>';
        else
            return '<span class="label label-danger">Chưa kích hoạt</span>';
    },
    formatNumber: function (number, precision) {
        if (!isFinite(number)) {
            return number.toString();
        }

        var a = number.toFixed(precision).split('.');
        a[0] = a[0].replace(/\d(?=(\d{3})+$)/g, '$&,');
        return a.join('.');
    },
    unflattern: function (arr) {
        var map = {};
        var roots = [];
        for (var i = 0; i < arr.length; i += 1) {
            var node = arr[i];
            node.children = [];
            map[node.id] = i; // use map to look-up the parents
            if (node.parentId !== null) {
                arr[map[node.parentId]].children.push(node);
            } else {
                roots.push(node);
            }
        }
        return roots;
    }
}

$(document).ajaxSend(function (e, xhr, options) {
    if (options.type.toUpperCase() === "POST" || options.type.toUpperCase() === "PUT") {
        var token = $('form').find("input[name='__RequestVerificationToken']").val();
        xhr.setRequestHeader("RequestVerificationToken", token);
    }
});