/* 
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */
var notes = function () {
    var the;
    return {
        init: function () {
            the = this;
            the.handleNote();
        },
        handleNote: function () {
            $('.message:not(.chat)').each(function (index, value) {
                the.openNote($(value), 500 * (index + 1));
                the.closeNote($(value), 10000 + (500 * (index + 1)));
            });
            $('.close-message').click(function () {
                the.closeNote($(this).parent());
            });
        },
        createNote: function (msg, type = 'success') {
            var text = $('<p>').html(msg);
            var closeBtn = $('<button>', {'class': 'close-message'}).append($('<i>', {'class': 'fa fa-times'}));
            var msgObj = $('<div>', {'class': 'message message-' + type}).append(text, closeBtn);
            closeBtn.click(function () {
                the.closeNote(msgObj);
            });
            $('.message-wrapper').append(msgObj);
            the.openNote(msgObj);
            the.closeNote(msgObj, 10000);
        },
        openNote: function (obj, cooldown = 500) {
            setTimeout(function () {
                obj.addClass('open');
            }, cooldown);
        },
        closeNote: function (obj, cooldown = 0) {
            setTimeout(function () {
                obj.css('opacity', 0)
                        .css('height', 0);
                setTimeout(function () {
                    obj.remove();
                }, cooldown + 1000);
            }, cooldown);
        },
        handleRemoveNote: function (obj, timer = 1000) {
            setTimeout(function () {
                $(obj).remove();
            }, timer);
        }
    };
}();
var script = function () {
    var the;
    return {
        init: function () {
            the = this;
            $('.ajaxlink').click(function () {
                the.ajaxlink($(this).data('url'), $(this).data('title'));
            });
            $('#metronic-modal').on('hidden.bs.modal', function () {
                $('#modal-title').empty();
                $('#modal-body').empty().append(the.loading());
            });
        },
        loading: function () {
            var div = $('<div>');
            var div2 = $('<div>').append(div);
            var divInner = $('<div>', {'class': 'ldio-n3ie7x8tqo'}).append(div, div2);
            var divWrap = $('<div>', {'class': 'loadingio-spinner-dual-ring-6tlihebqrnr'}).append(divInner);
            var loadingio = $('<div>', {'class': 'loadingio'}).append(divWrap);
            return loadingio;
        },
        prompt: function (text) {
            return confirm(text);
        },
        deletePrompt: function () {
            return confirm("Are you sure delete this item?");
        },
        orderTable: function (key) {
            if (key.length === 0) {
                return;
            }
            var table = $('.table.sort');
            var isDesc = key.search('-');
            var sortClass = 'asc';
            if (isDesc === 0) {
                key = key.substr(1, key.length);
                sortClass = 'desc';
            }
            table.find('th[data-order="' + key + '"]').removeAttr('class').addClass(sortClass);
        },
        change_per_page: function (obj) {
            var searchForm = $('#search-form');
            console.log(searchForm.find('.btn-search'));
            searchForm.find('.btn-search').click();
        },
        handleOrder: function () {
            var searchForm = $('#search-form');
            $('.table.sort').find('th[data-order]').each(function () {
                $(this).click(function () {
                    var valOrder = '';
                    if ($(this).hasClass('both')) {
                        valOrder = $(this).data('order');
                    } else if ($(this).hasClass('asc')) {
                        valOrder = '-' + $(this).data('order');
                    } else if ($(this).hasClass('desc')) {
                        valOrder = $(this).data('order');
                    }
                    searchForm.find('input[name="order"]').val(valOrder);
                    searchForm.find('.btn-search').click();
                });

            });
        },
        toggle_checkbox: function (obj) {
            var target = $('.' + $(obj).data('target'));
            if (target.length <= 0) {
                return;
            }
            target.each(function () {
                this.checked = $(obj).is(':checked');
            });
            // toggle the confirm button
            $('.check-all-confirm').toggle($(obj).is(':checked'));
        },
        toggle_checkbox_all: function (obj) {
            var target = $('#' + $(obj).data('target'));
            var siblings = $('.' + $(obj).attr('class'));
            var counter = 0;
            siblings.each(function () {
                if ($(this).is(':checked')) {
                    counter++;
                }
            });
            target.prop('checked', counter === siblings.length);
            // toggle the confirm button
            $('.check-all-confirm').toggle(counter > 0);
        },
        handleFakeNominal: function () {
            var fakenominal = $('.fake-nominal');
            if (fakenominal.length === 0) {
                return;
            }
            fakenominal.each(function () {
                $(this).on('keyup', function (e) {
                    // skip for arrow keys
                    if (e.which >= 37 && e.which <= 40)
                        return;

                    $(this).val(function (idx, val) {
                        return val.replace(/[^0-9-]/g, "")
                                .replace(/\B(?=(\d{3})+(?!\d))/g, ".");
                    });

                    $('#' + $(this).data('hidden')).val($(this).val().replace(/[^0-9-]/g, ''));
                });
            });
        },
        handleDatePicker: function (format = 'dd-mm-yy') {
            if ($('.datepicker').length !== 0) {
                $('.datepicker').each(function () {
//                    $(this).attr('readonly', 'readonly');
                    $(this).datepicker({
                        changeYear: true,
                        changeMonth: true,
                        dateFormat: format,
                        maxDate: $(this).data('maxdate'),
                        minDate: $(this).data('mindate'),
                        defaultDate: $(this).val(),
                        yearRange: $(this).data('yearrange'),
                    });
                });
        }
        },

        ajaxlink: function (url, title) {
            $('#metronic-modal').modal('show');
            $.ajax({
                url: url, type: 'GET', dataType: 'HTML',
                success: function (res) {
                    $('#modal-title').empty().html(title);
                    $('#modal-body').empty().html(res);
                }
            });
        },
        ajaxlinkbtn: function (obj, title, closemodal = true) {
            $.ajax({
                url: $(obj).data('url'), type: 'GET', dataType: 'HTML',
                success: function (res) {
                    if (title !== null) {
                        $('#modal-title').empty().html(title);
                    }
                    $('#modal-body').empty().html(res);
                }
            });
        },
        ajaxconfbtn: function (obj, title, conftxt) {
            var conf = confirm(conftxt);
            if (conf) {
                $.ajax({
                    url: $(obj).data('url'), type: 'GET', dataType: 'HTML',
                    success: function (res) {
                        if (title !== null) {
                            $('#modal-title').empty().html(title);
                        }
                        $('#modal-body').empty().html(res);
                    }
                });
            }
        },
        ajaxlinkform: function (e) {
            e.preventDefault();
            e.stopPropagation();
            var obj = $(e.target);
            var params = obj.serialize();
            var url_compiled = obj.attr('action');
            if (typeof params !== 'undefined' && params) {
                if (wshtml.strpos(url_compiled, '?') === false) {
                    url_compiled += '?' + params;
                } else {
                    url_compiled += '&' + params;
                }
            }
            var submit_val = $(e.submitter).val();
            if (submit_val) {
                if (wshtml.strpos(url_compiled, '?') === false) {
                    url_compiled += '?submit=' + submit_val;
                } else {
                    url_compiled += '&submit=' + submit_val;
                }
            }
            $.ajax({
                url: url_compiled, type: "GET", dataType: 'HTML',
                success: function (res) {
                    $('#modal-body').empty().html(res);
                }
            });
        }
    };
}();
$(document).ready(function () {
    notes.init();
    script.init();
});