'use strict';
$(document).ready(function () {
    let current = $(".progress-bar").attr("data-current-step");
    let steps = 3;

    setProgressBar(current);

    // Total steps
    $('#total_step').text(steps)
    $('#current_step').text(current)

    function setProgressBar(curStep) {
        let percent = parseFloat(100 / steps) * curStep;
        percent = percent.toFixed();
        $(".progress-bar")
            .css("width", percent + "%")
            .html(percent + "%");
    }
});

let $loading = $('#spinnerBtn').hide();
let $nextBtn = $('#next');
let myForm = $('#msform');
myForm.on('submit', function (e) {
    e.preventDefault()
    let $this = $(this);
    let $action = $(this).attr('action');
    let formData = $this.serializeArray();
    let sponsorModal = $('#sponsorship_confirmation');
    let sponsorModal2 = $('#sponsorship_confirmation_2');

    $.ajax({
        url: $action,
        type: 'POST',
        dataType: 'json',
        data: formData,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (result) {
            $nextBtn.prop('disabled', true);
            $loading.show();

            $('.custom-error').remove();
            $("input").removeClass('is-invalid');

            $('#introduced_by').val() === "" ? sponsorModal.modal('show') : window.location.href = result.redirect_to

            $('#understood_sponsorship_confirmation').on("click", function () {
                sponsorModal.modal('hide');
                sponsorModal2.modal('show');
                $('#understood_sponsorship_confirmation_2').on("click", function () {
                    window.location.href = result.redirect_to
                });
            });
        },
        beforeSend: function () {
            $nextBtn.prop('disabled', true);
            $loading.show();
        },
        error: function (result) {
            if (result.status == 500) {
                swalMsg('Something went wrong.', 'error')
                return;
            }
            $nextBtn.prop('disabled', false);
            $loading.hide();

            $.ajax({
                url: 'store-session-on-fail',
                type: 'POST',
                dataType: 'json',
                data: formData,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
            });
            $('.custom-error').remove();
            $("input").removeClass('is-invalid');
            $.each(result.responseJSON.errors, function (key, value) {
                let input = $("#" + key);
                input.addClass('is-invalid');
                $('#custom-errors-' + key).append('<span class="custom-error custom-error-' + key + '"><strong>' + value + '</strong></span>');
            });
        },
        complete: function () {
            $nextBtn.prop('disabled', false);
            $loading.hide();
        }
    })
});

let $previousBtn = $('#previous');
$previousBtn.on('click', function (e) {
    e.preventDefault();
    let $redirect = $(this).attr('href');
    let formData = myForm.serializeArray();
    formData.push({
        name: 'btn',
        value: 'previous'
    });

    $.ajax({
        url: 'store-session-on-fail',
        type: 'POST',
        dataType: 'json',
        data: formData,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (result) {
            if (result.status == true) {
                window.location.href = $redirect
            }
        },
    });
});
