/** * Main Application * Manages UI interactions and coordinates between components */ class DienstplanApp { constructor() { this.storage = new DataStorage(); this.holidayProvider = new HolidayProvider(); this.calculator = new BonusCalculator(this.holidayProvider); this.currentMonth = new Date().getMonth() + 1; this.currentYear = new Date().getFullYear(); this.init(); } init() { this.setupEventListeners(); this.populateYearSelects(); this.setCurrentMonthYear(); this.loadEmployeeSelects(); this.loadEmployeeList(); this.switchTab('duties'); } /** * Setup all event listeners */ setupEventListeners() { // Tab switching document.querySelectorAll('.tab-btn').forEach(btn => { btn.addEventListener('click', (e) => { this.switchTab(e.target.dataset.tab); }); }); // Employee management document.getElementById('add-employee-btn').addEventListener('click', () => this.addEmployee()); document.getElementById('new-employee-name').addEventListener('keypress', (e) => { if (e.key === 'Enter') this.addEmployee(); }); // Duty management document.getElementById('add-duty-btn').addEventListener('click', () => this.addDuty()); document.getElementById('employee-select-duty').addEventListener('change', () => this.loadDutiesForSelectedEmployee()); // Date stepper buttons (Feature C) document.getElementById('duty-date-prev').addEventListener('click', () => this.stepDutyDate(-1)); document.getElementById('duty-date-next').addEventListener('click', () => this.stepDutyDate(+1)); document.getElementById('duty-date').addEventListener('change', () => this.updateDateStepperState()); document.getElementById('month-select').addEventListener('change', () => this.onDutyMonthChange()); document.getElementById('year-select').addEventListener('change', () => this.onDutyMonthChange()); // Bild-Import (Feature A) const imageImportBtn = document.getElementById('open-image-import-btn'); if (imageImportBtn) { imageImportBtn.addEventListener('click', () => { if (window.imageImporter) { window.imageImporter.openImportDialog(); } else { this.showToast('Bild-Import nicht verfuegbar.', 'error'); } }); } // Calculation document.getElementById('calculate-btn').addEventListener('click', () => this.calculateBonuses()); // Settings document.getElementById('export-csv-btn').addEventListener('click', () => this.exportCSV()); document.getElementById('export-report-btn').addEventListener('click', () => this.exportBonusReport()); // NEW: Email Report Generator const emailBtn = document.getElementById('email-report-btn'); if (emailBtn) { emailBtn.addEventListener('click', () => this.generateEmailReport()); } document.getElementById('export-btn').addEventListener('click', () => this.exportData()); document.getElementById('import-btn').addEventListener('click', () => this.importData()); document.getElementById('clear-all-btn').addEventListener('click', () => this.clearAllData()); // Bild-Import (KI) settings const setKeyBtn = document.getElementById('set-api-key-btn'); if (setKeyBtn) setKeyBtn.addEventListener('click', () => this.setApiKeyFromPrompt()); const clearKeyBtn = document.getElementById('clear-api-key-btn'); if (clearKeyBtn) clearKeyBtn.addEventListener('click', () => this.clearApiKey()); const modelSelect = document.getElementById('api-model-select'); if (modelSelect) { modelSelect.value = this.storage.getApiModel(); modelSelect.addEventListener('change', () => { this.storage.setApiModel(modelSelect.value); this.showToast(`Modell geaendert: ${modelSelect.options[modelSelect.selectedIndex].text}`, 'success'); }); } this.refreshApiKeyStatus(); } /** * Populate year select dropdowns */ populateYearSelects() { const currentYear = new Date().getFullYear(); const years = []; for (let year = currentYear - 1; year <= currentYear + 5; year++) { years.push(year); } const yearSelects = ['year-select', 'calc-year-select']; yearSelects.forEach(selectId => { const select = document.getElementById(selectId); select.innerHTML = ''; years.forEach(year => { const option = document.createElement('option'); option.value = year; option.textContent = year; if (year === currentYear) option.selected = true; select.appendChild(option); }); }); } /** * Set current month and year in selects */ setCurrentMonthYear() { const currentMonth = new Date().getMonth() + 1; const currentYear = new Date().getFullYear(); document.getElementById('month-select').value = currentMonth; document.getElementById('year-select').value = currentYear; document.getElementById('calc-month-select').value = currentMonth; document.getElementById('calc-year-select').value = currentYear; // Set date input to today const today = new Date().toISOString().split('T')[0]; document.getElementById('duty-date').value = today; this.updateDateStepperState(); } /** * Switch between tabs */ switchTab(tabName) { // Update tab buttons document.querySelectorAll('.tab-btn').forEach(btn => { btn.classList.remove('active'); if (btn.dataset.tab === tabName) { btn.classList.add('active'); } }); // Update tab content document.querySelectorAll('.tab-content').forEach(content => { content.classList.remove('active'); }); document.getElementById(`tab-${tabName}`).classList.add('active'); // Refresh data when switching to certain tabs if (tabName === 'employees') { this.loadEmployeeList(); } else if (tabName === 'duties') { this.loadDutiesForSelectedEmployee(); } else if (tabName === 'settings') { this.refreshApiKeyStatus(); } } /** * Load employee select dropdowns */ loadEmployeeSelects() { const employees = this.storage.getEmployees(); const selects = ['employee-select-duty']; selects.forEach(selectId => { const select = document.getElementById(selectId); const currentValue = select.value; select.innerHTML = ''; employees.forEach(employee => { const option = document.createElement('option'); option.value = employee; option.textContent = employee; select.appendChild(option); }); // Restore previous selection if still valid if (employees.includes(currentValue)) { select.value = currentValue; } }); } /** * Add a new employee */ addEmployee() { const input = document.getElementById('new-employee-name'); const name = input.value.trim(); if (!name) { this.showToast('Bitte geben Sie einen Namen ein.', 'error'); return; } const success = this.storage.addEmployee(name); if (success) { this.showToast(`Mitarbeiter "${name}" wurde hinzugefügt.`, 'success'); input.value = ''; this.loadEmployeeList(); this.loadEmployeeSelects(); } else { this.showToast(`Mitarbeiter "${name}" existiert bereits.`, 'error'); } } /** * Remove an employee */ removeEmployee(employeeName) { if (!confirm(`Möchten Sie "${employeeName}" wirklich löschen? Alle Dienste werden ebenfalls gelöscht.`)) { return; } this.storage.removeEmployee(employeeName); this.showToast(`Mitarbeiter "${employeeName}" wurde gelöscht.`, 'success'); this.loadEmployeeList(); this.loadEmployeeSelects(); this.loadDutiesForSelectedEmployee(); } /** * Load and display employee list */ loadEmployeeList() { const employees = this.storage.getEmployees(); const container = document.getElementById('employee-list-display'); if (employees.length === 0) { container.innerHTML = '
Keine Mitarbeiter vorhanden.
'; return; } container.innerHTML = ''; employees.forEach(employee => { const item = document.createElement('div'); item.className = 'employee-item'; item.innerHTML = ` ${employee} `; container.appendChild(item); }); } /** * Add a duty */ addDuty() { const employeeSelect = document.getElementById('employee-select-duty'); const dateInput = document.getElementById('duty-date'); const shareSelect = document.getElementById('duty-share'); const employeeName = employeeSelect.value; const dateStr = dateInput.value; const share = parseFloat(shareSelect.value); if (!employeeName) { this.showToast('Bitte wählen Sie einen Mitarbeiter aus.', 'error'); return; } if (!dateStr) { this.showToast('Bitte wählen Sie ein Datum aus.', 'error'); return; } const date = new Date(dateStr + 'T12:00:00'); // Add time to avoid timezone issues const year = date.getFullYear(); const month = date.getMonth() + 1; this.storage.addDuty(employeeName, year, month, date, share); this.showToast('Dienst wurde hinzugefügt.', 'success'); this.loadDutiesForSelectedEmployee(); // Update month/year selects to match the added duty document.getElementById('month-select').value = month; document.getElementById('year-select').value = year; } /** * Remove a duty */ removeDuty(employeeName, year, month, date) { this.storage.removeDuty(employeeName, year, month, date); this.showToast('Dienst wurde gelöscht.', 'success'); this.loadDutiesForSelectedEmployee(); } /** * Step the duty-date input by +/-1 day, clamped to the currently selected month. */ stepDutyDate(delta) { const dateInput = document.getElementById('duty-date'); const monthSelect = document.getElementById('month-select'); const yearSelect = document.getElementById('year-select'); const month = parseInt(monthSelect.value); const year = parseInt(yearSelect.value); const lastDay = new Date(year, month, 0).getDate(); if (!dateInput.value) { // Initialize to 1st of the selected month dateInput.value = `${year}-${String(month).padStart(2, '0')}-01`; this.updateDateStepperState(); return; } const cur = new Date(dateInput.value + 'T12:00:00'); // If outside selected month, snap to 1st const inMonth = (cur.getFullYear() === year) && ((cur.getMonth() + 1) === month); if (!inMonth) { dateInput.value = `${year}-${String(month).padStart(2, '0')}-01`; this.updateDateStepperState(); return; } const curDay = cur.getDate(); const newDay = curDay + delta; if (newDay < 1 || newDay > lastDay) return; // clamp const newDate = new Date(year, month - 1, newDay, 12, 0, 0); const yyyy = newDate.getFullYear(); const mm = String(newDate.getMonth() + 1).padStart(2, '0'); const dd = String(newDate.getDate()).padStart(2, '0'); dateInput.value = `${yyyy}-${mm}-${dd}`; this.updateDateStepperState(); } /** * Update the disabled state of the stepper buttons based on current date / month. */ updateDateStepperState() { const dateInput = document.getElementById('duty-date'); const monthSelect = document.getElementById('month-select'); const yearSelect = document.getElementById('year-select'); const prevBtn = document.getElementById('duty-date-prev'); const nextBtn = document.getElementById('duty-date-next'); if (!dateInput || !prevBtn || !nextBtn) return; const month = parseInt(monthSelect.value); const year = parseInt(yearSelect.value); const lastDay = new Date(year, month, 0).getDate(); if (!dateInput.value) { prevBtn.disabled = false; nextBtn.disabled = false; return; } const cur = new Date(dateInput.value + 'T12:00:00'); const inSelectedMonth = (cur.getFullYear() === year) && ((cur.getMonth() + 1) === month); if (!inSelectedMonth) { prevBtn.disabled = false; nextBtn.disabled = false; return; } prevBtn.disabled = cur.getDate() <= 1; nextBtn.disabled = cur.getDate() >= lastDay; } /** * Handle month/year change in the duty tab: set date to 1st of new month, refresh list, refresh stepper. */ onDutyMonthChange() { const monthSelect = document.getElementById('month-select'); const yearSelect = document.getElementById('year-select'); const month = parseInt(monthSelect.value); const year = parseInt(yearSelect.value); document.getElementById('duty-date').value = `${year}-${String(month).padStart(2, '0')}-01`; this.updateDateStepperState(); this.loadDutiesForSelectedEmployee(); } /** * Load duties for the selected employee and month */ loadDutiesForSelectedEmployee() { const employeeSelect = document.getElementById('employee-select-duty'); const monthSelect = document.getElementById('month-select'); const yearSelect = document.getElementById('year-select'); const container = document.getElementById('duties-display'); const employeeName = employeeSelect.value; const month = parseInt(monthSelect.value); const year = parseInt(yearSelect.value); if (!employeeName) { container.innerHTML = 'Wählen Sie einen Mitarbeiter aus, um Dienste anzuzeigen.
'; return; } const duties = this.storage.getDutiesForMonth(employeeName, year, month); if (duties.length === 0) { const monthNames = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']; container.innerHTML = `Keine Dienste für ${monthNames[month - 1]} ${year}.
`; return; } container.innerHTML = ''; duties.forEach(duty => { const isQualifying = this.calculator.isQualifyingDay(duty.date); const dayType = this.calculator.getDayTypeLabel(duty.date); const dateStr = duty.date.toLocaleDateString('de-DE', { weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric' }); const item = document.createElement('div'); item.className = `duty-item ${isQualifying ? 'qualifying' : ''}`; item.innerHTML = `Keine Daten verfügbar.
'; return; } // Stash current calc context for vacation-toggle handler this._currentCalcContext = { year, month, yearMonth }; employees.forEach(employeeName => { const result = results[employeeName]; const resultCard = this.createResultCard(employeeName, result); resultsContainer.appendChild(resultCard); }); this.showToast('Berechnung abgeschlossen.', 'success'); } /** * Create a result card for an employee (new variants shape). */ createResultCard(employeeName, result) { const card = document.createElement('div'); card.className = 'result-card'; const ctx = this._currentCalcContext || {}; const yearMonth = ctx.yearMonth || ''; const vacChecked = result.isVacation ? 'checked' : ''; const safeName = String(employeeName).replace(/"/g, '"'); const safeYm = String(yearMonth).replace(/"/g, '"'); // Header + vacation toggle let content = `Mit den eingetragenen Diensten erreicht keine der drei Varianten einen positiven Bonus.
Keine Bonuszahlung
Übersicht:
`; reportHtml += `| Mitarbeiter | WE-Dienste | Abzug | Bemerkung |
|---|---|---|---|
| ${name} | ${this.formatNumber(totalWe)} | ${this.formatNumber(deducted)} | ${statusText} |
| Keine Daten für diesen Monat | |||
${text}
`; }); } else { reportHtml += `Keine relevanten Dienste.
`; } reportHtml += `Kopieren Sie diesen Inhalt direkt in Ihre E-Mail an die Verwaltung.
Für die im ${monthNames[month - 1]} ${year} geleisteten Bereitschaftsdienste ergeben sich folgende Bonuszahlungen:
| Mitarbeiter | Mo | Di | Mi | Do | Fr | Sa | So | Bonus (€) |
|---|---|---|---|---|---|---|---|---|
| ${safeName} | `; const dayOrder = [1, 2, 3, 4, 5, 6, 0]; for (const dayIdx of dayOrder) { const dayDuties = data.byWeekday[dayIdx]; if (dayDuties.length === 0) { html += ``; } else { let cellContent = ''; dayDuties.forEach(duty => { const shareStr = duty.share === 0.5 ? '½' : ''; const tag = duty.isQual ? 'we-tag' : 'wt-tag'; cellContent += `${shareStr}X | ${cellContent} | `; } } html += `${bonus > 0 ? this.calculator.formatCurrency(bonus) : '-'} |
Gesamtsumme: ${this.calculator.formatCurrency(totalBonus)}
Berechnungsregeln (NRW Psychiatrie 2011):
Erstellt am: ${new Date().toLocaleDateString('de-DE')} | Dienstplan-Pro - NRW Psychiatrie 2011
`; // Open in new window const reportWindow = window.open('', '_blank'); if (reportWindow) { reportWindow.document.write(html); reportWindow.document.close(); this.showToast('Bonus-Bericht wurde in einem neuen Fenster geöffnet.', 'success'); } else { this.showToast('Popup wurde blockiert. Bitte erlauben Sie Popups für diese Seite.', 'error'); } } /** * Import data from JSON file */ importData() { const fileInput = document.getElementById('import-file'); const file = fileInput.files[0]; if (!file) { this.showToast('Bitte wählen Sie eine Datei aus.', 'error'); return; } const reader = new FileReader(); reader.onload = (e) => { const success = this.storage.importData(e.target.result); if (success) { this.showToast('Daten wurden erfolgreich importiert.', 'success'); this.loadEmployeeList(); this.loadEmployeeSelects(); this.loadDutiesForSelectedEmployee(); } else { this.showToast('Import fehlgeschlagen. Bitte überprüfen Sie die Datei.', 'error'); } }; reader.readAsText(file); fileInput.value = ''; // Reset file input } /** * Clear all data */ clearAllData() { if (!confirm('Möchten Sie wirklich ALLE Daten löschen? Diese Aktion kann nicht rückgängig gemacht werden!')) { return; } this.storage.clearAll(); this.showToast('Alle Daten wurden gelöscht.', 'info'); this.loadEmployeeList(); this.loadEmployeeSelects(); this.loadDutiesForSelectedEmployee(); } /** * Update the API-key status line in Settings. */ refreshApiKeyStatus() { const el = document.getElementById('api-key-status'); if (!el) return; if (this.storage.getApiKey()) { el.textContent = 'API-Key gespeichert'; el.className = 'api-key-status-ok'; } else { el.textContent = 'Kein Key hinterlegt'; el.className = 'api-key-status-none'; } } setApiKeyFromPrompt() { const input = window.prompt('OpenRouter API-Key eingeben:', ''); if (input === null) return; const trimmed = input.trim(); if (!trimmed) return; this.storage.setApiKey(trimmed); this.refreshApiKeyStatus(); this.showToast('API-Key gespeichert.', 'success'); } clearApiKey() { if (!window.confirm('API-Key wirklich loeschen?')) return; this.storage.clearApiKey(); this.refreshApiKeyStatus(); this.showToast('API-Key geloescht.', 'info'); } /** * Show toast notification */ showToast(message, type = 'info') { const toast = document.getElementById('toast'); toast.textContent = message; toast.className = `toast ${type}`; setTimeout(() => { toast.classList.add('show'); }, 100); setTimeout(() => { toast.classList.remove('show'); }, 3000); } formatNumber(num) { return num.toLocaleString('de-DE', { minimumFractionDigits: 0, maximumFractionDigits: 1 }); } getMonthName(monthIndex) { const names = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]; return names[monthIndex - 1]; } } // Initialize app when DOM is ready let app; document.addEventListener('DOMContentLoaded', () => { app = new DienstplanApp(); window.app = app; });