toastr.options = {
  "closeButton": true,
  "progressBar": true,
  "positionClass": "toast-top-right",
  "timeOut": "3000"
};

$(document).ready(function(){

    function validateField(field){
        var value = field.val().trim();
        var name = field.attr('name');
        var error = "";
        var isValid = true;

        if(name === "name" && value === ""){
            error = "Full Name is required";
            isValid = false;
        }

        if(name === "email"){
            var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if(value === ""){
                error = "Email is required";
                isValid = false;
            } else if(!emailRegex.test(value)){
                error = "Enter a valid email";
                isValid = false;
            }
        }

        if(name === "phone"){
            var phoneRegex = /^[0-9]{10,15}$/;
            if(value === ""){
                error = "Phone is required";
                isValid = false;
            } else if(!phoneRegex.test(value)){
                error = "Enter a valid phone number";
                isValid = false;
            }
        }

        if(name === "password"){
            if(value === ""){
                error = "Password is required";
                isValid = false;
            } else if(value.length < 6){
                error = "Password must be at least 6 characters";
                isValid = false;
            }
        }

        // Show / hide error message and set border
        var msg = field.siblings('.error-msg');
        if(isValid){
            field.removeClass('invalid').addClass('valid');
            msg.removeClass('show').text('');
        } else {
            field.removeClass('valid').addClass('invalid');
            msg.addClass('show').text(error);
        }

        return isValid;
    }

    // Validate on blur
    $("#userForm input").on('blur', function(){
        validateField($(this));
    });

    // Clear validation on focus
    $("#userForm input").on('focus', function(){
        $(this).removeClass('valid invalid');
        $(this).siblings('.error-msg').removeClass('show').text('');
    });

    // Form submit
    $("#userForm").submit(function(e){
        e.preventDefault();
        var form = $(this);
        var formData = new FormData(this);
        var allValid = true;

        form.find('input').each(function(){
            if(!validateField($(this))){
                allValid = false;
            }
        });

        if(!allValid) return;

        // AJAX submit
        $.ajax({
            url: 'signup_process.php',
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(response){
                if(response == 1){
                    form.trigger('reset');
                    form.find('input').removeClass('valid invalid');
                    form.find('.error-msg').removeClass('show').text('');
                    toastr.success("User added successfully!");
                    // Optional redirect
                    // window.location.href = 'login.php';
                } else if(response == "Email already exists") {
                    toastr.error(response);
                } else {
                    toastr.error("Error: " + response);
                }
            },
            error: function(){
                toastr.error("Something went wrong!");
            }
        });

    });

});

$(document).ready(function(){

    function validateField(field){
        var value = field.val().trim();
        var name = field.attr('name');
        var error = "";
        var isValid = true;

        if(name === "email"){
            var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if(value === ""){
                error = "Email is required";
                isValid = false;
            } else if(!emailRegex.test(value)){
                error = "Enter a valid email";
                isValid = false;
            }
        }

        if(name === "password"){
            if(value === ""){
                error = "Password is required";
                isValid = false;
            } else if(value.length < 6){
                error = "Password must be at least 6 characters";
                isValid = false;
            }
        }

        var msg = field.siblings('.error-msg');
        if(isValid){
            field.removeClass('invalid').addClass('valid');
            msg.removeClass('show').text('');
        } else {
            field.removeClass('valid').addClass('invalid');
            msg.addClass('show').text(error);
        }

        return isValid;
    }

    // Validate on blur
    $("#loginForm input").on('blur', function(){
        validateField($(this));
    });

    // Clear validation on focus
    $("#loginForm input").on('focus', function(){
        $(this).removeClass('valid invalid');
        $(this).siblings('.error-msg').removeClass('show').text('');
    });

    // Form submit
    $("#loginForm").submit(function(e){
    e.preventDefault();
    var form = $(this);
    var formData = new FormData(this);
    var allValid = true;

    // Validate all fields
    form.find('input').each(function(){
        if(!validateField($(this))){
            allValid = false;
        }
    });

    if(!allValid) return;

    // AJAX submit
    $.ajax({
        url: 'login_process.php',
        type: 'POST',
        data: formData,
        contentType: false,
        processData: false,
        dataType: 'json', // Important for JSON response
        success: function(response){
            if(response.status == 1){
                toastr.success("Login successful!");
                // Redirect based on role
                setTimeout(function(){
                    if(response.role == 'admin'){
                        window.location.href = 'admin/show_users.php';
                    } else {
                        window.location.href = 'dashboard.php';
                    }
                }, 1000);
            } else {
                toastr.error(response.msg); // show backend error
            }
        },
        error: function(){
            toastr.error("Something went wrong!");
        }
    });

});


});

