(function ($) {
    function submitLogin() {
        $(".subscription_error_login, #error_message_show_login").css("display", "none");
        var username = document.getElementById("user_name_login").value;
        var password = document.getElementById("password_login").value;

        if (username && password) {
            $("#custom-loader").addClass("show");
            $.ajax({
                url: ajax_object.ajax_url,
                type: "POST",
                data: {
                    action: "unleash_ai_plug_login_portal",
                    nonce: ajax_object.update_nonce,
                    username: username,
                    password: password
                },
                success: function (response) {
                    console.log(response);
                    $("#custom-loader").removeClass("show");
                    if (response.success && response.data.api_token) {
                        $("#message_show_login")
                            .css("display", "block")
                            .html("Login successful! Redirecting...");
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    } else {
                       
                        $("#message_show_login")
                            .css("display", "block")
                            .html("Login failed");
                    }
                },
                error: function () {
                    alert("Error occurred while customer login.");
                }
            });
        } else {
            $("#error_message_show")
                .html("Please fill in all fields.")
                .css({ display: "block", color: "red" });
            if (!username) {
                $("#error_username_login")
                    .html("Please fill User Name.")
                    .css({ display: "block", color: "red" });
            }
            if (!password) {
                $("#error_password_login")
                    .html("Please fill Password.")
                    .css({ display: "block", color: "red" });
            }
        }
    }

    function call_filter() {
        var filterValue = document.getElementById("page_filter").value;
        if (filterValue !== "") {
            addParameterToURL("aifilterpage", filterValue);
        }
    }

    function addParameterToURL(key, value) {
        var url = window.location.href;
        if (url.indexOf("?") !== -1) {
            url += "&" + key + "=" + value;
        } else {
            url += "?" + key + "=" + value;
        }
        window.location.href = url;
    }

    $(document).ready(function () {
        $(".activeExCgtable").slideUp();
        $(".clickToExpand").click(function () {
            $(this).addClass("activeExpand");
            $(".activeExCgtable").slideToggle();
        });
    });

    // Expose your functions if needed
    window.submitLogin = submitLogin;
    window.call_filter = call_filter;
	   $(document).ready(function () {
        $('#copy-btn').on('click', function () {
            const text = $('#text-to-copy').text(); // Get the text from the <pre> element

            // Create a temporary textarea element
            const $tempTextArea = $('<textarea>');
            $('body').append($tempTextArea);
            $tempTextArea.val(text).select();

            // Copy the text to the clipboard
            document.execCommand('copy');

            // Remove the temporary textarea
            $tempTextArea.remove();

            // Show the "Copied!" message
            $('#copy-status').fadeIn().delay(2000).fadeOut();
        });
    });
	
	$(document).ready(function () {
    $('#switch-unleashai-account').on('click', function () {
        if (!confirm('Are you sure you want to switch accounts?')) {
            return;
        }

        $.ajax({
             url: ajax_object.ajax_url, // Use the localized variable for the AJAX URL
            type: 'POST',
            dataType: 'json',
            data: {
                action: 'unleash_ai_logout', // Matches the AJAX action in PHP
                nonce:  ajax_object.update_nonce, // Secure nonce
            },
            success: function (response) {
                if (response.success) {
                    alert(response.data); // Show success message
                    location.reload(); // Reload the page if needed
                } else {
                    alert('Error: ' + response.data);
                }
            },
            error: function (xhr, status, error) {
                console.error('AJAX Error:', status, error);
                alert('An unexpected error occurred.');
            },
        });
    });
});

})(jQuery);
 