$(document).ready(function() {
    if (enabled) {
        $('#enable2FA').hide();
        $('#disable2FA').show();
    } else {
        $('#enable2FA').show();
        $('#disable2FA').hide();
    }
    let theme = localStorage.getItem("_x_darkMode_on") === "true" ? "dark" : "light";
    $('#enable2FA').click(function() {
        $.ajax({
            url: "/profile/enable-2fa",
            type: "POST",
            data: {
                _token: $('meta[name="csrf-token"]').attr('content')
            },
            success: function(response) {
                if (response.status === 'success') {
                    $('#qrContainer').html('<img src="' + response.qrcode + '">');
                    $('#secretKey').text(response.secret);
                    $('#result').show();
                    $('#enable2FA').hide();
                }
            },
            error: function(xhr) {
                let swalOptions = {
                    icon: "error",
                    title: "Failed!",
                    html: `<p>${xhr.responseText}</p>`,
                    toast: true,
                    position: "bottom-end",
                    showConfirmButton: false,
                    customClass: {
                        popup: "toast-popup",
                        title: "toast-title",
                        content: "toast-content"
                    },
                    showCloseButton: true,
                    closeButtonHtml: "<i class='fas fa-times'></i>"
                };
                        
                if (theme === "dark") {
                    swalOptions.background = "#1E293B";
                    swalOptions.color = "#F8FAFC";
                    swalOptions.confirmButtonColor = "#4F46E5";
                }
                        
                Swal.fire(swalOptions);
            }
        });
    });

    $('#verifyOTPForm').on('submit', function(e) {
        e.preventDefault();

        $.ajax({
            url: "/profile/verify-2fa",
            type: "POST",
            data: $(this).serialize(),
            success: function(response) {
                let swalOptions = {
                    icon: "success",
                    title: "Success!",
                    html: `<p>${response.message}</p>`,
                    toast: true,
                    position: "bottom-end",
                    showConfirmButton: false,
                    customClass: {
                        popup: "toast-popup",
                        title: "toast-title",
                        content: "toast-content"
                    },
                    showCloseButton: true,
                    closeButtonHtml: "<i class='fas fa-times'></i>"
                };
                        
                if (theme === "dark") {
                    swalOptions.background = "#1E293B";
                    swalOptions.color = "#F8FAFC";
                    swalOptions.confirmButtonColor = "#4F46E5";
                }
                        
                Swal.fire(swalOptions);
                $('#disable2FA').show();
                $('#result').hide();
            },
            error: function(xhr) {
                let swalOptions = {
                    icon: "error",
                    title: "Failed!",
                    html: `<p>${xhr.responseText}</p>`,
                    toast: true,
                    position: "bottom-end",
                    showConfirmButton: false,
                    customClass: {
                        popup: "toast-popup",
                        title: "toast-title",
                        content: "toast-content"
                    },
                    showCloseButton: true,
                    closeButtonHtml: "<i class='fas fa-times'></i>"
                };
                        
                if (theme === "dark") {
                    swalOptions.background = "#1E293B";
                    swalOptions.color = "#F8FAFC";
                    swalOptions.confirmButtonColor = "#4F46E5";
                }
                        
                Swal.fire(swalOptions);
            }
        });
    });
    
    $('#disable2FA').click(function() {
        $('#disable2FA').hide();
        $('#disableForm').show();
    });
    $('#DisableForm').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: "/profile/disable-2fa",
            type: "POST",
            data: $(this).serialize(),
            success: function(response) {
                let swalOptions = {
                    icon: "success",
                    title: "Success!",
                    html: `<p>${response.message}</p>`,
                    toast: true,
                    position: "bottom-end",
                    showConfirmButton: false,
                    customClass: {
                        popup: "toast-popup",
                        title: "toast-title",
                        content: "toast-content"
                    },
                    showCloseButton: true,
                    closeButtonHtml: "<i class='fas fa-times'></i>"
                };
                        
                if (theme === "dark") {
                    swalOptions.background = "#1E293B";
                    swalOptions.color = "#F8FAFC";
                    swalOptions.confirmButtonColor = "#4F46E5";
                }
                        
                Swal.fire(swalOptions);
                $('#disableForm').hide();
                $('#enable2FA').show();
            },
            error: function(xhr) {
                let swalOptions = {
                    icon: "error",
                    title: "Failed!",
                    html: `<p>${xhr.responseText}</p>`,
                    toast: true,
                    position: "bottom-end",
                    showConfirmButton: false,
                    customClass: {
                        popup: "toast-popup",
                        title: "toast-title",
                        content: "toast-content"
                    },
                    showCloseButton: true,
                    closeButtonHtml: "<i class='fas fa-times'></i>"
                };
                        
                if (theme === "dark") {
                    swalOptions.background = "#1E293B";
                    swalOptions.color = "#F8FAFC";
                    swalOptions.confirmButtonColor = "#4F46E5";
                }
                        
                Swal.fire(swalOptions);
            }
        });
    });

});