function showActivatedDisplay() {
    console.log("Plugin is activated");
    const input = document.getElementById("lkInput");
    if (input) {
        input.value = "**************";
        input.setAttribute("readonly", "readonly");
    }
    
    document.getElementById("buttonSection").innerHTML = "";
    document.getElementById("formTitle").innerHTML = "Activated!";
    document.getElementById("formInputLabelLK").innerHTML = "License Key:";
    document.getElementById("buttonSection").innerHTML += '<button type="button" id="deactivateKey" class="button" style="background-color: #080B53; border-color: #080B53; color: #FFFFFF; margin-right: 10px;">Deactivate</button>';

    const deactivateButton = document.getElementById("deactivateKey");
    if (deactivateButton) {
        deactivateButton.addEventListener("click", deactivateLicense);
    }
}

function showDeactivatedDisplay() {
    console.log("Plugin is deactivated");
    const input = document.getElementById("lkInput");
    if (input) {
        input.value = "";
        input.removeAttribute("readonly");
    }
    
    document.getElementById("buttonSection").innerHTML = "";
    document.getElementById("formTitle").innerHTML = "Getting Started";
    document.getElementById("formInputLabelLK").innerHTML = "Enter License Key:";
    document.getElementById("buttonSection").innerHTML += '<button type="submit" id="diyseo_preview_buttonn" class="button" style="background-color: #080B53; border-color: #080B53; color: #FFFFFF; margin-right: 10px;">Activate</button>';
}

// Function to load user account
function loadUserAccount() {
    console.log("This is where I'll run queries and load user data");
}
function setLicenseKey(licenseKey) {
    jQuery.ajax({
        url: diyseoAjax.ajaxurl,
        type: 'POST',
        data: {
            action: 'diyseo_set_license',
            nonce: diyseoAjax.nonce,
            license_key: licenseKey
        },
        success: function(response) {
            if (response.success) {
                localStorage.setItem('diyseoLK', response.data.email);
                localStorage.setItem('diyseoLicK', licenseKey);
                showActivatedDisplay();
            }
        }
    });
}
document.addEventListener('DOMContentLoaded', function() {
    // Check for license key from WordPress settings first
    if (typeof diyseoSettings !== 'undefined' && diyseoSettings.licenseKey) {
        showActivatedDisplay();
    } 
    // Then check localStorage as fallback
    else if (localStorage.getItem("diyseoLK")) {
        showActivatedDisplay();
    } 
    else {
        showDeactivatedDisplay();
    }
});


// Document ready handler

function deactivateLicense() {
    jQuery.ajax({
        url: diyseoAjax.ajaxurl,  // Use the localized AJAX URL
        type: 'POST',
        data: {
            action: 'diyseo_deactivate_license',
            nonce: diyseoAjax.nonce  // Use the localized nonce
        },
        success: function(response) {
            localStorage.removeItem("diyseoLK");
            localStorage.removeItem("diyseoLicK");
            showDeactivatedDisplay();
        },
        error: function(xhr, status, error) {
            console.error("Deactivation failed:", error);
        }
    });
}
// Update deactivate button click handler
document.getElementById("deactivateKey").addEventListener("click", function() {
    deactivateLicense();
});
jQuery(document).ready(function($) {
    // Check for license key
    if (typeof diyseoSettings !== 'undefined' && diyseoSettings.licenseKey) {
        showActivatedDisplay();
    } else if (localStorage.getItem("diyseoLK")) {
        showActivatedDisplay();
    } else {
        showDeactivatedDisplay();
    }

    // Form submit handler
    $('#diyseo_form').on('submit', function(e) {
        e.preventDefault();
        const licenseKey = $('#lkInput').val();
        setLicenseKey(licenseKey);
    });
});
