$(document).ready(function() {
    $('.ui.form').form('add prompt', 'pin', 'Enter NUIT');
    // $('.ui.dropdown')
    //     .dropdown();
    $('.ui.form').form({
        on: 'blur',
        inline: true,
        fields: {
            userName: {
                identifier: 'userName',
                rules: [{
                        type: 'empty',
                        prompt: 'Please enter Email'
                    }, {
                        type: 'email',
                        prompt: 'Please enter a valid company e-mail'
                    },
                    {
                        type: 'regExp[/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/]',
                        prompt: 'Should be a Valid Email Address'
                    }
                ]
            },
            password: {
                identifier: 'password',
                rules: [{
                        type: 'empty',
                        prompt: 'Password cannot be empty'
                    },
                    {
                        type: 'minLength[6]',
                        prompt: 'Password must be at least 8 characters long'
                    },
                    {
                        type: 'maxLength[50]',
                        prompt: 'Password cannot be longer than 50 characters'
                    },
                    // {
                    //     type: 'passwordStrength',
                    //     prompt: 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character'
                    // }
                ]

            }


        },
        onSuccess: function(event, fields) {
            event.preventDefault(); // Prevent form submission

            $('#loginbtn').html("Logging in... Please Wait!");
            $("#loginbtn").attr("disabled", true);


            // window.location.href = "index.html";

            var formJson = $("#login").serializeObject();
            var formData = JSON.stringify(formJson);
            var url = serverURL + "auth/signin";
            var async_status = true;

            controller.loginrequest(url, formData, async_status, function(data, status) {
                console.log("datadatadata 1", data)
                console.log("datadatadata 2", status)

                if(status == true){
                    var jsdata = data;
                    var jsonObj = JSON.stringify(jsdata); // stringify user data 
                    localStorage.setItem("userObj", jsonObj);
                    setTimeout(function() {

                        $('#loginbtn').html("Sign In");
                        $("#loginbtn").attr("disabled", false);

                        window.location.href = "./#app/devicetracking";
                       
                    }, 100);
                }else{
                controller.showToastMsg("Sorry! Username or Password is Wrong", "#ff6666");
                $('#loginbtn').html("Log In");
                        $("#loginbtn").attr("disabled", false);
                    setTimeout(function() {
                        $('#loginbtn').html("Sign In");
                        $("#loginbtn").attr("disabled", false);
                    }, 1000);
                }


                 setTimeout(function() {
                        $('#loginbtn').html("Sign In");
                        $("#loginbtn").attr("disabled", false);
                       
                    }, 2000);

               
            });

        }
    }, {
        on: 'blur',
        inline: 'true'
    });


}(jQuery));

function loginpage() {
    setTimeout(function() {
        window.location.href = "otp.html";
    }, 1000);
}

$.fn.form.settings.rules.passwordStrength = function(value) {
    return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}|:"<>?~\-=[\]\\;',./]).{8,}$/.test(value);
};