
function ajaxRequestForRegister(url,method, formData){
    // Fire off the request to /form.php
    $('.loading').show()

    var request = $.ajax({
        url: url,
        type: method,
        data: formData,
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        console.log("response",response);
        // Log a message to the console
        var selector = $('.register-body .main')

        if(response?.original){
            response = response.original
        }
        if(response.status) {
            if (formData['action'] == "CREATE_ACCOUNT" || formData['action'] == "ADD_PROFILE"){
                if(response?.message){
                    sweetAlertPopup(response.message,"warning",(res)=>{})
                }else {
                    selector.html(response.html)
                }
            }else if(formData['action'] == "CREDIT_REPORT"){
                sweetAlertPopup("Package Taken","success",(res)=>{
                    location.href = response.redirect_url;
                })
            }
        }

//location.href = response.redirect_url;


    });


    const sweetAlertPopup = (message,icon,callback)=>{
            Swal.fire({
                text: message,
                icon: icon,
                showCancelButton: false,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'OK'
            }).then(callback)
    }

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        console.log("Always")
        $('.loading').hide()
    });

}

function submitPage(btn, formname) {
    event.preventDefault();
    var form = $("#" + formname)
    if (form.valid()) {

        var formData = objectifyForm($("#" + formname).serializeArray())
        if(formname == "createAccountForm") {
            formData['action'] = "CREATE_ACCOUNT"
        }else if(formname == "createProfileForm"){
            formData['action'] = "ADD_PROFILE"
        }
        console.log(formData)
        ajaxRequestForRegister(URL,"POST",formData);
        //btn.onclick = function () { return disableButton(); };
    }
    return true;

}

function getPackage(package_id){
    Swal.fire({
        text: "Are you sure want to take the package?",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes',
        cancelButtonText:'No'
    }).then((result) => {
        if(result.value) {
            var formData = {}
            formData['action'] = "CREDIT_REPORT"
            formData['package_id'] = package_id
            ajaxRequestForRegister(URL, "POST", formData);
        }
    })

}
