<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <link rel="stylesheet" href="../css/organization.css">
</head>

<body style="display: none;">
    <div class="container">
        <div class="hiring-badge" style="display: none;" id="isHiringBadge">Now Hiring</div>
        <div class="logo-div">
            <img class="logo" src="../content/blink-logo-small.jpg" alt="Company Logo">
        </div>
        <h1 id="organizationName">Blink</h1>
        <div class="organization-details">
            <p><b>Location: </b><label id="location">Naples, Italy</label></p>
            <p><b>Email: </b><label id="email">contacts@blink-corporation.com</label></p>
            <p><b>Hiring: </b><label id="isHiring">Yes</label></p>
        </div>
        <h2>About Us</h2>
        <p id="description">Blink aims at creating a libre and decentralized LinkedIn alternative</p>
    </div>

    <script src="../js/constants.js"></script>
    <script src="../js/utils.js"></script>

    <script>
        window.addEventListener("load", async function () {
            loadOrganization();
        });

        async function loadOrganization() {
            const idOrganization = new URLSearchParams(window.location.search).get('id');
            if (!idOrganization) {
                alert("Invalid URL.");
                return;
            }
            const response = await fetch(`${API_URL}/organizations/${idOrganization}`, {
                headers: createHeaders(null)
            });
            const data = await response.json();
            if (response.ok) {
                populateFields(data.name, data.location, data.description, await isOrganizationHiring(idOrganization));
                document.body.style.display = "block"; // Show page
            } else {
                alert(data.error);
            }
        }

        function populateFields(name, location, description, isHiring) {
            document.getElementById('organizationName').textContent = name;
            document.title = `${name} - Blink`
            document.getElementById('location').textContent = location;
            document.getElementById('description').textContent = description;
            if (isHiring === true) {
                document.getElementById('isHiring').textContent = 'Yes';
                document.getElementById('isHiringBadge').style.display = 'block';
            } else {
                document.getElementById('isHiring').textContent = 'No';
            }

        }

        async function isOrganizationHiring(organizationId) {
            const response = await fetch(`${API_URL}/organizations/${organizationId}/joboffers`, {
                headers: createHeaders(null)
            });
            const data = await response.json();
            return data.length > 0;
        }
    </script>

</body>

</html>