'use strict'

const GerowTheme = GerowTheme || {}
window.GerowTheme = GerowTheme

toastr.options = {
    positionClass: 'toast-bottom-right',
}

GerowTheme.showError = (message) => {
    toastr.error(message)
}

GerowTheme.showSuccess = (message) => {
    toastr.success(message)
}

GerowTheme.isRtl = () => {
    return document.body.dir === 'rtl'
}

GerowTheme.handleError = (data) => {
    if (typeof data.errors !== 'undefined' && data.errors.length) {
        GerowTheme.handleValidationError(data.errors)
    } else if (typeof data.responseJSON !== 'undefined') {
        if (typeof data.responseJSON.errors !== 'undefined') {
            if (data.status === 422) {
                GerowTheme.handleValidationError(data.responseJSON.errors)
            }
        } else if (typeof data.responseJSON.message !== 'undefined') {
            GerowTheme.showError(data.responseJSON.message)
        } else {
            $.each(data.responseJSON, (index, el) => {
                $.each(el, (key, item) => {
                    GerowTheme.showError(item)
                })
            })
        }
    } else {
        GerowTheme.showError(data.statusText)
    }
}

GerowTheme.handleValidationError = (errors) => {
    let message = ''
    $.each(errors, (index, item) => {
        if (message !== '') {
            message += '<br />'
        }
        message += item
    })
    GerowTheme.showError(message)
}
;(function ($) {
    'use strict'
    $(document).on('submit', '.newsletter-form', function (event) {
        event.preventDefault()
        event.stopPropagation()

        let _self = $(event.target)
        let _btn = _self.find('button[type="submit"]')
        $.ajax({
            type: 'POST',
            cache: false,
            url: _self.closest('form').prop('action'),
            data: new FormData(_self.closest('form')[0]),
            contentType: false,
            processData: false,
            beforeSend: () => {
                _btn.addClass('button-loading')
                _btn.attr('disable')
            },
            success: (res) => {
                if (!res.error) {
                    _self.closest('form').find('input[type=email]').val('')
                    GerowTheme.showSuccess(res.message)
                } else {
                    GerowTheme.handleError(res.message)
                }
            },
            error: (res) => {
                GerowTheme.handleError(res)
            },
            complete: () => {
                if (typeof refreshRecaptcha !== 'undefined') {
                    refreshRecaptcha()
                }
                _btn.removeClass('button-loading')
                _btn.removeAttr('disable')
            },
        })
    })

    $(document).on('submit', '.cons-contact-form', function (event) {
        event.preventDefault()
        event.stopPropagation()

        const $button = $(this).find('button[type="submit"]')

        $.ajax({
            type: 'POST',
            cache: false,
            url: $(this).closest('form').prop('action'),
            data: new FormData($(this).closest('form')[0]),
            contentType: false,
            processData: false,
            beforeSend: () => {
                $button.addClass('button-loading')
                $button.prop('disabled', true)
            },
            success: ({ error, message }) => {
                if (!error) {
                    $(this).closest('form').find('input[type=text]').val('')
                    $(this).closest('form').find('input[type=email]').val('')
                    $(this).closest('form').find('textarea').val('')
                    GerowTheme.showSuccess(message)
                } else {
                    GerowTheme.showError(message)
                }
            },
            error: (error) => {
                GerowTheme.handleError(error)
            },
            complete: () => {
                if (typeof refreshRecaptcha !== 'undefined') {
                    refreshRecaptcha()
                }

                $button.removeClass('button-loading')
                $button.prop('disabled', false)
            },
        })
    })
})(jQuery)

$(document).on('submit', '#request-quote-form', function (event) {
    event.preventDefault()

    const $form = $(this)
    const $button = $(this).find('button[type="submit"]')

    $.ajax({
        url: $form.prop('action'),
        method: 'POST',
        data: $form.serialize(),
        beforeSend: () => {
            $button.addClass('button-loading')
        },
        success: ({ error, message }) => {
            if (error) {
                GerowTheme.showError(message)

                return
            }

            GerowTheme.showSuccess(message)

            $form.find('input[type="text"], input[type="number"], input[type="email"], select, textarea').val('')
        },
        error: (error) => {
            GerowTheme.handleError(error)
        },
        complete: () => {
            $button.removeClass('button-loading')
        },
    })
})
