document.addEventListener("DOMContentLoaded", function () {
let groupSelectElement = document.getElementById("election-group"),
areaSelectElement = document.getElementById("election-area"),
formContainer = document.querySelector(".election-form--form"),
electionDetail = document.querySelector(".election-detail");
// STEP 1: LOAD SEARCH ITEMS
// Load all groups and areas
if(formContainer) {
fetch("https://ihk-wahl.info/selects?ihk_id=M6uOiDGzTD7SlhF1")
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Fill in groups
// Remove old entries
while (groupSelectElement.firstChild) {
groupSelectElement.removeChild(groupSelectElement.firstChild);
}
// Sort groups alphabetically by name
let sortedGroups = data.groups.slice().sort(function (a, b) {
return a.number.localeCompare(b.number);
});
// Populate with sorted options
let groupsOptionElement = document.createElement("option");
groupsOptionElement.value = "-";
groupsOptionElement.textContent = "Wahlgruppe (Alle)";
groupsOptionElement.setAttribute("data-number", "");
groupsOptionElement.setAttribute("data-title", "Alle");
groupSelectElement.appendChild(groupsOptionElement);
sortedGroups.forEach(function (group) {
let groupsOptionElement = document.createElement("option");
groupsOptionElement.value = group.id;
groupsOptionElement.textContent = group.number + " " + group.title;
groupsOptionElement.setAttribute("data-number", group.number);
groupsOptionElement.setAttribute("data-title", group.title);
groupSelectElement.appendChild(groupsOptionElement);
});
// Fill in areas
// Remove old entries
while (areaSelectElement.firstChild) {
areaSelectElement.removeChild(areaSelectElement.firstChild);
}
// Sort groups alphabetically by name
let sortedAreas = data.districts.slice().sort(function (a, b) {
return a.number.localeCompare(b.number);
});
// Populate with sorted options
let areasOptionElement = document.createElement("option");
areasOptionElement.value = "-";
areasOptionElement.textContent = "Wahlbezirk (Alle)";
areasOptionElement.setAttribute("data-number", "");
areasOptionElement.setAttribute("data-title", "Alle");
areaSelectElement.appendChild(areasOptionElement);
sortedAreas.forEach(function (district) {
let areasOptionElement = document.createElement("option");
areasOptionElement.value = district.id;
areasOptionElement.textContent = district.number + " " + district.title;
areasOptionElement.setAttribute("data-number", district.number);
areasOptionElement.setAttribute("data-title", district.title);
areaSelectElement.appendChild(areasOptionElement);
});
})
.catch(error => {
// Handle errors here
console.error('Fetch error:', error);
});
// STEP 2: CLICK SEARCH
// Click on search
const hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden';
hiddenInput.name = 'ihk_id';
hiddenInput.value = 'M6uOiDGzTD7SlhF1';
formContainer.appendChild(hiddenInput);
formContainer.onsubmit = function (e) {
e.preventDefault();
submitForm(e);
};
let selectElements = formContainer.querySelectorAll("select");
selectElements.forEach(select => {
select.addEventListener("change", function (e) {
submitForm();
});
});
document.addEventListener("click", function (event) {
if (event.target && (event.target.getAttribute("data-search") === "group" || event.target.getAttribute("data-search") === "area")) {
let id = event.target.getAttribute("data-id");
document.getElementById("election-searchstring").value = "";
if (event.target.getAttribute("data-search") === "group") {
document.getElementById("election-group").value = id;
document.getElementById("election-area").value = "-";
} else if (event.target.getAttribute("data-search") === "area") {
document.getElementById("election-group").value = "-";
document.getElementById("election-area").value = id;
}
submitForm();
}
});
}
// STEP 3: LOAD DETAIL PAGE
// Get the URL query string parameters
const queryParams = new URLSearchParams(window.location.search);
if (electionDetail && queryParams.has('ihk_id') && queryParams.has('c')) {
// Get the values of the 'c' and 'a' parameters
const ihkIdValue = queryParams.get('ihk_id');
const cValue = queryParams.get('c');
console.log('Value of "c":', ihkIdValue);
console.log('Value of "a":', cValue);
let electionDetailText = electionDetail.querySelector('.election-detail--text'),
electionDetailVideo = electionDetail.querySelector('.election-detail--video'),
electionDetailInfo = electionDetail.querySelector('.election-detail--info'),
htmlText = '',
htmlVideo = '',
htmlInfo = '';
fetch("https://ihk-wahl.info/candidate?ihk_id=" + ihkIdValue + "&candidate=" + cValue)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
// Build the detail page for the candidate
htmlText += data.img_path ? `
` : '';
htmlText += '
${data.position}
` : ''; htmlText += data.election_statement ? `${data.election_statement}
` : ''; if (data.twitter_link || data.facebook_link || data.xing_link || data.whatsapp_link || data.linkedin_link || data.threema_link || data.telegram_link) { htmlText += ` `; } htmlText += 'Wahlgruppe 02
Wahlbezirk 05
Unternehmen
${data.company}
` : ''; htmlInfo += data.company_address ? `${data.company_address}
` : ''; htmlInfo += data.website ? ` ` : ''; htmlInfo += data.email ? ` ` : ''; if (data.phone) { htmlInfo += ` `; } htmlInfo += data.company ? `Wahlgruppe ${groupNumber}: ${groupTitle}
Wahlbezirk ${areaNumber}: ${areaTitle}
Suchbegriff: ${searchContent.value}
${resultsIndex}Kanditat:innen