Stellflächenvergleich mit anderen gängigen Stühlen

// JavaScript Logik für den R.U.M. CO2-Rechner
// Diese Datei ist als "Asset" geschützt und wird von der Übersetzungs-App nicht verändert.

document.addEventListener('DOMContentLoaded', () => {
// 1. Definition der Stühle und ihrer CO2-Fußabdrücke (in KG CO₂)
// Alle kundenspezifischen Texte sind hier in Deutsch enthalten
const chairs = [
{ name: "3107 Stuhl von Fritz Hansen", footprint: 5.75, circular: true },
{ name: "A-Stuhl", footprint: 6.20, circular: false },
{ name: "B-Stuhl", footprint: 5.80, circular: false },
{ name: "C-Stuhl", footprint: 6.50, circular: false },
{ name: "D-Stuhl", footprint: 5.90, circular: true },
{ name: "E-Stuhl", footprint: 6.10, circular: false },
];

// 2. DOM-Elemente abrufen
const chairSelect = document.getElementById('chair-select');
const quantityInput = document.getElementById('quantity-input');
const resultDiv = document.getElementById('result');
const calculateButton = document.getElementById('calculate-button');

// 3. Dropdown mit Stuhl-Optionen füllen
chairs.forEach((chair, index) => {
const option = document.createElement('option');
// Sicherstellen, dass die Wertübergabe (value) der Index ist
option.value = index;
option.textContent = chair.name + (chair.circular ? ' (VOLL Zirkulär)' : '');
chairSelect.appendChild(option);
});

// 4. Hauptberechnungsfunktion
function updateResult() {
const selectedIndex = parseInt(chairSelect.value);
const quantity = parseInt(quantityInput.value);

// Überprüfung auf gültige Auswahl und Menge
if (selectedIndex === -1 || isNaN(quantity) || quantity <= 0) {
resultDiv.innerHTML = '<p class="text-red-600">Bitte wählen Sie einen Stuhl aus und geben Sie eine gültige Menge ein.</p>';
return;
}

const selectedChair = chairs[selectedIndex];

// Annahme: R.U.M. Stühle haben einen CO2-Fußabdruck von 0.00 KG CO2 (oder sehr niedrig)
const rumFootprint = 0.00;

// Berechnung der CO2-Einsparung
const potentialFootprint = selectedChair.footprint * quantity;
const actualFootprint = rumFootprint * quantity;
const totalSavings = potentialFootprint - actualFootprint;

// Anzeige des Ergebnisses (Text in Deutsch!)
resultDiv.innerHTML = `
<div class="mt-4 p-6 rounded-xl bg-green-50 shadow-lg border border-green-200 text-gray-800">
<p class="font-bold text-xl mb-3 text-green-700">Geschätzte Gesamteinsparungen:</p>
<div class="flex justify-between items-center border-b pb-2 mb-2">
<span class="text-lg">CO₂-Fußabdruck (Stuhl "${selectedChair.name}"):</span>
<span class="font-mono text-xl text-red-600">${potentialFootprint.toFixed(2)} KG CO₂</span>
</div>
<div class="flex justify-between items-center border-b pb-2 mb-2">
<span class="text-lg">CO₂-Fußabdruck (R.U.M. Kreislaufwirtschaft):</span>
<span class="font-mono text-xl text-green-600">0.00 KG CO₂</span>
</div>
<div class="flex justify-between items-center pt-2">
<span class="text-xl font-extrabold text-gray-900">Gesamteinsparung:</span>
<span class="font-extrabold text-2xl text-green-700">${totalSavings.toFixed(2)} KG CO₂</span>
</div>
<p class="text-sm mt-4 text-center italic text-gray-500">
Diese Berechnung zeigt die Einsparung pro Projekt im Vergleich zur Neuanschaffung des ausgewählten Stuhls.
</p>
</div>
`;
}

// 5. Event-Listener hinzufügen
// Die Berechnung soll bei Klick oder bei Änderung der Werte ausgeführt werden
calculateButton.addEventListener('click', updateResult);
chairSelect.addEventListener('change', updateResult);
quantityInput.addEventListener('input', updateResult);

// Initiales Ergebnis beim Laden anzeigen
updateResult();
});