let path = location.origin + '/assets/icons/svg/';
$('#myTable, [data-table="dataTables"]').DataTable({
    dom: 'Bfrtip',
    buttons: [{
            extend: 'copy',
            text: '<i class="copy-ico"></i> Copy',
            className: 'dataBtn'
        },
        {
            extend: 'excel',
            text: 'Excel',
            className: 'dataBtn'
        },
        {
            extend: 'pdf',
            text: 'PDF',
            className: 'dataBtn borders'
        },
        {
            extend: 'colvis',
            text: '<i class="eye-ico"></i>',
            className: 'dataBtn ms-1 right_r row-selection-button'
        },
    ],
    pageLength: 20,
    responsive: true,
    rowReorder: {
        selector: 'td:nth-child(2)'
    },
    oLanguage: { "sSearch": "" },
});

$('[type=search]').each(function() {
    $(this).attr("placeholder", "Search");
});

let searchIco = path + '/search.svg',
    addNewButtonHidden = $('.add-new-button-hidden'),
    addNewButton = addNewButtonHidden.html();
addNewButtonHidden.remove();
$('.dataTables_filter').append(addNewButton);
$('.dataTables_filter input[type=search]').css({ "background-image": "url(" + searchIco + ")" });
let tableHeader = $('tr .sorting');
tableHeader.each(function(index, element) {
    if ($(element).text().toLowerCase() !== 'action') {
        let icon = '<i class="sort-ascending-ico"></i><i class="sort-descending-ico"></i><i class="sort-ico"></i>';
        let content = $(element).html();
        $(element).html('<div class="data-tables-header-sorting">' + content + icon + '</div>');
    }

});

function deletePost(id) {
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: 'No, cancel!',
        confirmButtonClass: 'btn btn-danger ms-2',
        cancelButtonClass: 'btn btn-success',
        buttonsStyling: false,
        reverseButtons: true
    }).then((result) => {
        if (result.value) {
            event.preventDefault();
            document.getElementById('delete-form-' + id).submit();
        }
    })
}

$('.permission-list-badge').each(function() {
    let text = $(this).text().toLowerCase();
    if (text.includes('delete')) {
        $(this).addClass('for-delete');
    } else if (text.includes('edit')) {
        $(this).addClass('for-edit');
    } else if (text.includes('view')) {
        $(this).addClass('for-view');
    } else if (text.includes('list')) {
        $(this).addClass('for-list');
    } else if (text.includes('create') || text.includes('show')) {
        $(this).addClass('for-create');
    } else {
        $(this).addClass('for-list');
    }
});

$(".custom-date-format").datepicker({ autoHide: true, format: 'dd-mm-YYYY' });

function printData(divName) {
    let content = $("." + divName);
    content.print();
    content.close();
}


$(document).on('submit', 'form', function() {
    let submitButton = $(this).find('button[type="submit"], input[type="submit"]');
    $(submitButton).prop('disabled', true).prepend('<div class="spinner-border text-white submit-button-spinner" role="status">\n' +
        '<span class="visually-hidden">Loading...</span>\n' +
        '</div>');
});

function googleTranslateElementInit() {
    new google.translate.TranslateElement({ pageLanguage: 'en', includedLanguages: 'en,bn,ms' },
        'google_translate_element'
    );
}

$(document).on('focus', 'input[type="number"]', function() {
    $(this).attr({ 'min': 0 });
})

$('.pre-loader').css('display', 'none');
let interval = setInterval(function() {
    let body = $('body[data-sidebar="dark"]');
    if ($(body).css('top') === '40px') {
        $(body).css('margin-top', '-' + $(body).css('top'))
        clearInterval(interval);
    }
    $('#google_translate_element').empty();
    $('.goog-te-combo').addClass('form-select');
}, 500)