$(document).ready(function () {

    $('#btn_login').on('click', function (e) {

        e.preventDefault(); // Prevent default form submission

        
        
        var email = $('#email').val().trim();
        var password = $('#password').val();

        // Validate email and password (as before)
        if (email === '' || password === '') {
            $('.err-msg').html('Both fields are required.');
            return;
        }

        if (!isValidEmail(email)) {
            $('.err-msg').html('Please enter a valid email address');
            return;
        }

        // Perform AJAX POST request
        $.ajax({
            type: 'POST', // Use POST method
            url: './classes/StudentLoginController.php', // Correct URL to your PHP script
            data: {
                USER_LOGIN: 1,
                email: email,
                password: password
            },
            success: function (response) {
                console.log('response', response);
                var res = $.parseJSON(response);

                if (res.status == '1') {
                    if (res.data.user_role_id == '5') {
                        // Student role
                        location.replace ('./dashboard.php'); // Redirect to dashboard
                    }
                } else if (res.status == '0') {
                    $('.err-msg').html(res.message);
                }
    
            }
        });
    });
});

  
  // FUNCTIONS
  
  function isValidEmail (email) {
    // Regular expression for basic email validation
    const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
    return emailRegex.test (email);
  }
  