a = false;

$(document).ready(function () {
    $("#btn_savee").click(function () {
        if (a == false) {
            saveperform();
        }
    });
});

function saveperform() {
    var id = $('#id').val();
    $('#mobileerror').text('');

    if (id > 0) {
        a = true;
        var formData = new FormData($("#Form")[0]);
        $.ajax({
            url: base_path + "Customer/updatedata",
            type: "POST",
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            beforeSend: function () {
                $('#btn_savee').prop('disabled', true).html('<i class="fa fa-spinner"></i> Loading');
            },
            success: function (response) {
                $('#btn_savee').html('<i class="fa fa-check-circle"></i> Save').prop('disabled', false);
                a = false;
                if (response.status === 'error') {
                    $('#mobileerror').text(response.message);
                    showProAlert('error', 'Duplicate Entry', response.message);
                    return;
                }
                showProAlert('success', 'Success', 'Data Updated Successfully').then(function () {
                    window.location.href = base_path + "Customer/index";
                });
            },
            error: function () {
                $('#btn_savee').html('<i class="fa fa-check-circle"></i> Save').prop('disabled', false);
                a = false;
                showProAlert('error', 'Error', 'Something went wrong. Please try again.');
            }
        });
    } else {
        var formData = new FormData($("#Form")[0]);
        $.ajax({
            url: base_path + "Customer/insert",
            type: "POST",
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            beforeSend: function () {
                $('#btn_savee').prop('disabled', true).html('<i class="fa fa-spinner"></i> Loading');
            },
            success: function (response) {
                $('#btn_savee').html('<i class="fa fa-check-circle"></i> Save').prop('disabled', false);
                a = false;
                if (response.status === 'error') {
                    $('#mobileerror').text(response.message);
                    showProAlert('error', 'Duplicate Entry', response.message);
                    return;
                }
                $("#Form")[0].reset();
                showProAlert('success', 'Success', 'Data Submitted Successfully').then(function () {
                    window.location.href = base_path + "Customer/index";
                });
            },
            error: function () {
                $('#btn_savee').html('<i class="fa fa-check-circle"></i> Save').prop('disabled', false);
                a = false;
                showProAlert('error', 'Error', 'Something went wrong. Please try again.');
            }
        });
    }
}
