/* 
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */
var script = function () {
    var the;
    return {
        init: function () {
            the = this;
            the.handleNote();
        },
        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);
        },
        handleNote: function () {

            $('.message').each(function (index, value) {
                setTimeout(function () {
                    $(value).addClass('open');
                }, 500 * (index + 1));
                setTimeout(function () {
                    $(value)
                            .css('opacity', 0)
                            .css('height', 0);
                    the.handleRemoveNote(value);
                }, 10000 + (500 * (index + 1)));
            });
            $('.close-message').click(function () {
                $(this).parent()
                        .css('opacity', 0)
                        .css('height', 0);
                the.handleRemoveNote($(this).parent());
            });
        },
        handleRemoveNote: function (obj, timer = 1000) {
            setTimeout(function () {
                $(obj).remove();
            }, timer);
        },
        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'),
                    });
                });
        }
        },

    };
}();
$(document).ready(function () {
    script.init();
});