"use strict";
let $loading = $('#spinnerBtn').hide();

$(document).ready(function () {
    $(document).on('keyup change', '#formLogin input', function () {
        var input = $(this);
        if (input.parent().find('.validation-error,.custom-error').length) {
            input.parent().find('.validation-error,.custom-error').html('');
        } else {
            var inputName = input.attr('name');
            $("#" + inputName + "_error").html('');
        }
        input.removeClass('invalid-input').addClass('border-clr');
    });
});

function resetPassword() {
    let $nextBtn = $('#reset-btn');
    let formData = new FormData($('#formLogin')[0]);

    $.ajax({
        url: baseURL + 'password/email',
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        beforeSend: function () {
            $nextBtn.prop('disabled', true);
            $loading.show();
        },
        success: function (resp) {
            $nextBtn.prop('disabled', true);
            $loading.show();
            $('.custom-error').remove();
            $("input").removeClass('is-invalid');
            $(".error-msg").removeClass('alert-danger');
            $(".error-msg").addClass('alert-success');
            $(".error-msg").css('margin-bottom', '20px');
            $(".error-msg").html(resp.msg).show();
            $("#formLogin").hide();
            $loading.hide();
        },
        error: function (xhr) {
         $nextBtn.prop('disabled', false);
         $loading.hide();
         if (globalErrorHandler(xhr.responseJSON))
             return false
         showValidationErrors(xhr.responseJSON.errors);
        },
        complete: function () {
            $nextBtn.prop('disabled', false);
            $loading.hide();
        }
    });
}
