document.addEventListener("DOMContentLoaded", function () { const searchPointInput = document.getElementById("search_point"); const searchResultsPoints = document.getElementById("search_results_points"); const selectedPointDisplay = document.getElementById("selected_point"); const searchPointSection = document.querySelector(".search-point-section"); // Sekcja wyszukiwania punktów const searchInput = document.getElementById("search_query"); const resultsContainer = document.getElementById("search_results"); const tagsContainer = document.getElementById("tags"); const totalField = document.getElementById("total_price"); const analyzeButton = document.getElementById("analyze_button"); const analysisResults = document.getElementById("analysis_results"); const clearAllButton = document.getElementById("clear_all_button"); const infoMessage = document.createElement("div"); infoMessage.id = "info_message"; infoMessage.style.position = "fixed"; infoMessage.style.bottom = "20px"; infoMessage.style.left = "50%"; infoMessage.style.transform = "translateX(-50%)"; infoMessage.style.backgroundColor = "#28a745"; infoMessage.style.color = "white"; infoMessage.style.padding = "10px 20px"; infoMessage.style.borderRadius = "5px"; infoMessage.style.boxShadow = "0 2px 5px rgba(0,0,0,0.2)"; infoMessage.style.display = "none"; document.body.appendChild(infoMessage); let ID_Intytucji = null; let selectedTests = []; let totalPrice = 0; // Obsługa przycisku "Wyczyść wszystko" clearAllButton.addEventListener("click", function () { location.reload(); // Przeładuj stronę }); // Obsługa kliknięcia w pole wyszukiwania searchInput.addEventListener("click", function () { if (searchInput.value !== "") { searchInput.value = ""; // Resetuj wartość pola resultsContainer.innerHTML = ""; // Wyczyść poprzednie wyniki } }); // Wyszukiwanie punktów searchPointInput.addEventListener("input", function () { const query = searchPointInput.value; if (query.length > 0) { fetch(`/search_points?query=${encodeURIComponent(query)}`) .then(response => response.json()) .then(data => { searchResultsPoints.innerHTML = ""; // Wyczyść poprzednie wyniki data.forEach(point => { const resultItem = document.createElement("div"); resultItem.className = "col-md-4 result-item"; resultItem.innerHTML = ` ${point.name}
${point.address.fullAddress}
`; searchResultsPoints.appendChild(resultItem); }); }) .catch(err => console.error("Błąd podczas wyszukiwania punktów:", err)); } else { searchResultsPoints.innerHTML = ""; // Wyczyść wyniki, jeśli pole jest puste } }); // Wybór punktu window.selectPoint = function (id, name) { ID_Intytucji = id; // Ukryj sekcję wyszukiwania punktów if (searchPointSection) { searchPointSection.style.display = "none"; } // Zaktualizuj wybrany punkt i dodaj przycisk zmiany punktu selectedPointDisplay.innerHTML = ` Wybrany punkt: ${name} `; searchResultsPoints.innerHTML = ""; searchPointInput.value = ""; }; // Funkcja do zmiany punktu (przeładowanie strony) window.changePoint = function () { location.reload(); }; // Wyszukiwanie badań searchInput.addEventListener("input", function () { if (!ID_Intytucji) { alert("Wybierz najpierw punkt pobrań!"); searchInput.value = ""; return; } const query = searchInput.value; if (query.length > 0) { fetch(`/search?query=${encodeURIComponent(query)}&institution_id=${ID_Intytucji}`) .then(response => response.json()) .then(data => { resultsContainer.innerHTML = ""; // Wyczyść poprzednie wyniki data.forEach(test => { const resultItem = document.createElement("div"); resultItem.className = "col-md-4 result-item"; let resultHTML = `${test.title}
`; if (test.displayStatus) { resultHTML += `${test.displayStatus}
`; } else { resultHTML += `Cena: ${test.price} PLN
`; } resultItem.innerHTML = resultHTML; resultsContainer.appendChild(resultItem); }); }) .catch(err => console.error("Błąd podczas wyszukiwania:", err)); } else { resultsContainer.innerHTML = ""; // Wyczyść wyniki, jeśli pole jest puste } }); // Dodawanie badania window.addTest = function (code, title, price) { if (!selectedTests.find(test => test.code === code)) { selectedTests.push({ code, title, price }); totalPrice += price; const tag = document.createElement("div"); tag.className = "tag"; tag.innerHTML = `${title} `; tag.dataset.code = code; tagsContainer.appendChild(tag); updateTotalPrice(); // Wyczyszczenie wyników analizy analysisResults.innerHTML = ""; showInfoMessage("Dodano! Wybierz kolejne badanie lub przejdź do analizy."); } }; function showInfoMessage(message) { infoMessage.textContent = message; infoMessage.style.display = "block"; setTimeout(() => { infoMessage.style.display = "none"; }, 3000); }; // Funkcja do przewijania ekranu do sumy cen function scrollToTotalPrice() { const totalPriceElement = document.getElementById("total_price"); if (totalPriceElement) { totalPriceElement.scrollIntoView({ behavior: "smooth", block: "start" }); } } // Usuwanie badania window.removeTest = function (code) { const index = selectedTests.findIndex(test => test.code === code); if (index !== -1) { totalPrice -= selectedTests[index].price; selectedTests.splice(index, 1); const tag = tagsContainer.querySelector(`[data-code='${code}']`); if (tag) { tagsContainer.removeChild(tag); } updateTotalPrice(); // Wyczyszczenie wyników analizy analysisResults.innerHTML = ""; } }; function updateTotalPrice() { totalField.textContent = `Suma cen bez pakietu: ${totalPrice} PLN`; } // Analiza wybranych badań analyzeButton.addEventListener("click", function () { searchInput.value = ""; // Wyczyść pole wyszukiwania resultsContainer.innerHTML = ""; // Wyczyść wyniki wyszukiwania if (!ID_Intytucji) { alert("Wybierz punkt pobrań przed analizą!"); return; } const selectedCodes = selectedTests.map(test => test.code); fetch(`/analyze`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ selected_codes: selectedCodes, institution_id: ID_Intytucji }), }) .then(response => response.json()) .then(data => { analysisResults.innerHTML = ""; // Wyczyść poprzednie wyniki data.forEach(result => { const resultDiv = document.createElement("div"); resultDiv.innerHTML = ` Pakiet: ${result.package_name}
Suma całkowita: ${result.total_price}
Cena pakietu: ${result.price}
Cena brakujących badań: ${result.missing_tests_price}
Liczba znalezionych badań: ${result.matching_tests_count}
Brakujące badania: ${result.not_found_tests.map(test => `${test.name} (${test.price.toFixed(2).replace('.', ',')} PLN)`).join(", ")}
Dodatkowe badania: ${result.additional_tests.map(test => test.name).join(", ")}

`; analysisResults.appendChild(resultDiv); // Subtelna animacja pojawiania się (fade-in) setTimeout(() => { resultDiv.style.transition = "opacity 0.6s ease-in-out"; resultDiv.style.opacity = "1"; }, 100); }); // Przewiń ekran do sumy cen scrollToTotalPrice(); }) .catch(err => console.error("Błąd podczas analizy:", err)); }); });