let AJAX_RETURN_SUCCESS = "Success";
let AJAX_RETURN_FAIL = "Fail";
let MSG_DATA_CAN_NOT_EMPTY = "Data Can’t be Empty";

function isAjaxStatusSuccess(status) {
    return status === AJAX_RETURN_SUCCESS
}

function showModal(elm) {
    $(elm).modal('show');
}

function hideModal() {
    $('.modal').modal('hide');
}

function reloadPage() {
    setTimeout(() => {
        location.reload();
    }, 2000)
}

function alertLoading() {
    Swal.fire({
        title: 'Loading...',
        allowOutsideClick: false,
        allowEscapeKey: false,
        timerProgressBar: true,
        didOpen: () => {
          Swal.showLoading()
        },
      })
}



function hideLoading() {
    setTimeout(function() {
        Swal.close();
    }, 2000);
}



function alertSuccessWithUrl(title, message, url) {
    Swal.fire({
        title: title,
        text: message,
        confirmButtonText: "OK",
        closeOnConfirm: false,
        confirmButtonColor: '#009ef7',
        cancelButtonColor: '#f1416c',
        allowOutsideClick: false
      }).then((result) => {
        /* Read more about isConfirmed, isDenied below */
        if (result.isConfirmed) {
            window.location.href = url;
        }
      })
}

function alertSuccess(title, message) {
    Swal.fire({
        title: title,
        text: message,
        icon: "success",
        type: "success",
        showCancelButton: false,
        confirmButtonClass: "btn-success",
        confirmButtonText: "OK",
        closeOnConfirm: false,
        confirmButtonColor: '#009ef7',
        cancelButtonColor: '#f1416c',
        allowOutsideClick: false,
    });
}

function alertFail(title, message) {
    Swal.fire({
        title: title,
        text: message,
        type: "warning",
        icon: "error",
        showCancelButton: false,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "OK",
        closeOnConfirm: false,
        confirmButtonColor: '#009ef7',
        cancelButtonColor: '#f1416c',
        allowOutsideClick: false,
    });
}


function toastySuccess(title, message) {
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "newestOnTop": false,
        "progressBar": false,
        "positionClass": "toastr-top-right",
        "preventDuplicates": false,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };
    toastr.success('was created!');
}

function toastyFail(title, message) {
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "newestOnTop": false,
        "progressBar": false,
        "positionClass": "toastr-top-right",
        "preventDuplicates": false,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };
    toastr.error('was created!');
}

function isEmpty(str) {
    return (!str || 0 === str.length);
}

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

// for modal
function showLoadingModal() {
    $('#Loading_Modal').modal({ backdrop: 'static', keyboard: false })
}

function hideLoadingModal() {
    setTimeout(() => {
        $("#Loading_Modal").modal('hide');
    }, 1000);
}

function initAjaxSetupToken() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
}

function ajaxKeepDataCkEditor() {
    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].updateElement();
    }
}

function logAjaxError(xhr, status, error) {
    console.log(xhr.statusText);
    console.log(status);
    console.log(error);
}

function resetFormInput(formName) {
    $(formName).trigger("reset");
}

function inputNumberOnly() {
    $(".number_only").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
            // Allow: Ctrl+A, Command+A
            (e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||
            // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
            // let it happen, don't do anything
            return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
}

function goToTopPage() {
    window.scrollTo({ top: 0, behavior: 'smooth' });
}

function validateEmail(email) {
    const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function setInputFilter(textbox, inputFilter) {
    ["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(function (event) {
        textbox.addEventListener(event, function () {
            if (inputFilter(this.value)) {
                this.oldValue = this.value;
                this.oldSelectionStart = this.selectionStart;
                this.oldSelectionEnd = this.selectionEnd;
            } else if (this.hasOwnProperty("oldValue")) {
                this.value = this.oldValue;
                this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
            } else {
                this.value = "";
            }
        });
    });
}
