<!-- BEGIN: Page JS-->
<script>
    'use strict';
    const formAuthentication = document.querySelector('#formAuthentication');
    const login = "<%= i18n.__('auth.login') %>",
        emailValid = "<%= i18n.__('auth.emailValid') %>",
        passwordValid = "<%= i18n.__('auth.passwordValid') %>",
        filedRequired = "<%= i18n.__('auth.filedRequired') %>";
    document.addEventListener('DOMContentLoaded', function (e) {
        (function () {
            // Form validation for Add new record
            if (formAuthentication) {
                const fv = FormValidation.formValidation(formAuthentication, {
                    fields: {
                        email: {
                            validators: {
                                notEmpty: {
                                    message: filedRequired
                                },
                                emailAddress: {
                                    message: emailValid
                                }
                            }
                        },
                        password: {
                            validators: {
                                notEmpty: {
                                    message: filedRequired
                                },
                                stringLength: {
                                    min: 6,
                                    message: passwordValid
                                }
                            }
                        },
                    },
                    plugins: {
                        trigger: new FormValidation.plugins.Trigger(),
                        bootstrap5: new FormValidation.plugins.Bootstrap5({
                            eleValidClass: '',
                            rowSelector: '.zt-login__field'
                        }),
                        submitButton: new FormValidation.plugins.SubmitButton(),
                        //  defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
                        autoFocus: new FormValidation.plugins.AutoFocus()
                    },
                    init: instance => {
                        instance.on('plugins.message.placed', function (e) {
                            if (e.element.parentElement.classList.contains('input-group')) {
                                e.element.parentElement.insertAdjacentElement('afterend', e.messageElement);
                            }
                        });
                    }
                }).on('core.form.valid', function () {
                    submitForm()
                });;
            }


            //  Two Steps Verification
            const numeralMask = document.querySelectorAll('.numeral-mask');
            console.log("numeralMask", numeralMask)
            // Verification masking
            if (numeralMask.length) {
                numeralMask.forEach(e => {
                    new Cleave(e, {
                        numeral: true
                    });
                });

            }
        })();
    });

    function submitForm() {
        let form = $("#formAuthentication");
        let url = form.attr('action')
        let method = form.attr('method')
        let data = form.serializeArray();
        $.ajax({
            url,
            method: method,
            data: data,
            beforeSend: function () {
                $("#btnForm").html('<i class="fas fa-spinner fa-spin"></i>').attr('disabled', true).css('opacity', '0.7')
            },

            success: function (response) {
                $("#btnForm").html(login).attr('disabled', false).css('opacity', '1');

                toastr.success(response.message);

                // Swal.fire({
                //     position: 'top-start',
                //     icon: 'error',
                //     title: response.message,
                //     showConfirmButton: false,
                //     timer: 1500,
                //     confirmButtonClass: 'btn btn-primary',
                //     buttonsStyling: false,
                // })
                // Swal.fire({
                //     position: 'top-center',
                //     icon:'success',
                //     text: response.message,
                //     showConfirmButton: false,
                //     timer: 1500,
                //     confirmButtonClass: 'btn btn-primary',
                //     buttonsStyling: false,
                // })
                window.location.href = response.data.url;
            },

            error: function (err) {
                $("#btnForm").html(login).attr('disabled', false).css('opacity', '1');
                toastr.error(err.responseJSON.message);
                // Swal.fire({
                //     icon: 'error',
                //     title: `${err.responseJSON.message}`,
                //     showConfirmButton: false,
                //     timer: 1500
                // })

            },
        });

    }

    // Legacy getTheme() removed — zt-theme.js handles all theme/style switching now.

</script>

<!-- Page JS -->