document.addEventListener("DOMContentLoaded", function () {
    const applyForm = document.getElementById("Apply-form");

    applyForm.addEventListener("submit", function (event) {
        event.preventDefault();

        const name = document.getElementById("firstname").value;
        const email = document.getElementById("@").value;
        const selectedTechnologies = getSelectedTechnologies();
        const timezone = document.getElementById("timezone").value;
        const extra = document.getElementById("extra").value;

        if (!name || !email || selectedTechnologies.length === 0 || !timezone) {
            alert("Please fill in all required fields.");
            return;
        }

        const message = `
        ### New Team Application\n\nName: ${name}\nEmail: ${email}\nKnown Languages: ${selectedTechnologies.join(", ")}\nTimezone: ${timezone}\nMessage: ${extra}
        `;

        // Replace 'YOUR_DISCORD_WEBHOOK_URL' with your actual Discord webhook URL
        const webhookUrl = 'https://discord.com/api/webhooks/1184066949467082762/GI51ltjJE3bXOqxOEaga0nymZfPZXmNdyeiRwHWJVzkq3WsvAZCkdM2MUIkbBC6Iawod';

        fetch(webhookUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ content: message }),
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('Error sending application. Please try again later.');
            }
            alert('Application submitted successfully!');
        })
        .catch(error => {
            console.error(error);
            alert('Error sending application. Please try again later.');
        });
    });

    function getSelectedTechnologies() {
        const checkboxes = document.querySelectorAll('input[type="checkbox"]:checked');
        const selectedTechnologies = Array.from(checkboxes).map((checkbox) => checkbox.value);
        return selectedTechnologies;
    }
});
