// leads.js

// ==========================================
// 1. MENU TOGGLE 
// ==========================================
const menuToggle = document.getElementById('menuToggle');
const slideMenu = document.getElementById('slideMenu');
const overlay = document.getElementById('overlay');

if (menuToggle && slideMenu && overlay) {
    function toggleMenu() {
        menuToggle.classList.toggle('active');
        slideMenu.classList.toggle('active');
        overlay.classList.toggle('active');
    }

    menuToggle.addEventListener('click', toggleMenu);
    overlay.addEventListener('click', toggleMenu);
}

// ==========================================
// 2. LEADS TABLE LOGIC
// ==========================================

function getLeads() {
    const storedLeads = localStorage.getItem('myLeads');
    if (storedLeads) {
        return JSON.parse(storedLeads);
    } else {
        return []; 
    }
}

function populateLeadsTable() {
    const tableBody = document.getElementById('leadsTableBody');
    const leadsData = getLeads();
    
    if (!tableBody) return;
    
    if (leadsData.length === 0) {
        tableBody.innerHTML = `
            <tr>
                <td colspan="4" class="empty-state">
                    <p>Keine Anfragen vorhanden</p>
                </td>
            </tr>
        `;
        return;
    }
    
    tableBody.innerHTML = leadsData.map(lead => `
        <tr>
            <td>${lead.datum}</td>
            <td>${lead.dienstleistung}</td>
            <td>
                <span class="status-badge ${lead.status}">${lead.statusText}</span>
            </td>
            <td>
                <a href="#" class="action-btn" data-id="${lead.id}">Details ansehen</a>
            </td>
        </tr>
    `).join('');
    
    document.querySelectorAll('.action-btn').forEach(btn => {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            const leadId = this.getAttribute('data-id');
            
            // Redirect to lead details page
            window.location.href = `lead-details.html?id=${leadId}`;
        });
    });
}

document.addEventListener('DOMContentLoaded', populateLeadsTable);