Interaktywne Porównanie Ofert Finansowania

<div class="container mx-auto p-4 sm:p-6 lg:p-8">
    <header class="text-center mb-8">
        <h1 class="text-3xl md:text-4xl font-bold text-slate-900">Interaktywne Porównanie Ofert Finansowania</h1>
        <p class="mt-2 text-lg text-slate-600">Analiza dla kwoty 100 000 PLN</p>
    </header>

    <div class="bg-white rounded-lg shadow-sm p-6 mb-8">
        <h2 class="text-xl font-semibold text-slate-800 mb-4 text-center">Filtruj oferty według swoich potrzeb</h2>
        <div class="flex flex-wrap justify-center gap-4">
            <button id="filter-all" class="filter-btn active py-2 px-5 bg-white border border-slate-300 rounded-full text-slate-700 font-medium hover:bg-slate-100">Pokaż wszystkie</button>
            <button id="filter-no-bik" class="filter-btn py-2 px-5 bg-white border border-slate-300 rounded-full text-slate-700 font-medium hover:bg-slate-100">Bez weryfikacji w BIK</button>
            <button id="filter-krd" class="filter-btn py-2 px-5 bg-white border border-slate-300 rounded-full text-slate-700 font-medium hover:bg-slate-100">Akceptujące wpisy w KRD</button>
            <button id="filter-zus-us" class="filter-btn py-2 px-5 bg-white border border-slate-300 rounded-full text-slate-700 font-medium hover:bg-slate-100">Zaległości w ZUS/US</button>
            <button id="filter-residency" class="filter-btn py-2 px-5 bg-white border border-slate-300 rounded-full text-slate-700 font-medium hover:bg-slate-100">Dla posiadaczy paszportu/karty pobytu</button>
        </div>
         <p class="text-center text-sm text-slate-500 mt-4">Wybierz jedną z opcji, aby zobaczyć tylko te oferty, które najlepiej pasują do Twojej sytuacji.</p>
    </div>

    <div class="bg-white rounded-lg shadow-sm p-4 md:p-6 mb-8">
        <h2 class="text-xl font-semibold text-slate-800 mb-4 text-center">Kluczowe Porównanie Ofert</h2>
        <div id="table-container" class="overflow-x-auto rounded-lg border border-slate-200">
            <!-- Table will be generated by JS -->
        </div>
    </div>

    <h2 class="text-2xl font-semibold text-slate-800 mb-6 text-center">Szczegóły Ofert</h2>
    <div id="offers-grid" class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-6">
    </div>

    <section class="mt-12 pt-8 border-t border-slate-200">
        <h2 class="text-2xl font-semibold text-slate-800 mb-6 text-center">Chcesz wiedzieć więcej?</h2>
        <div class="grid grid-cols-1 md:grid-cols-3 gap-8">

            <!-- Opcja 1: Generuj do email -->
            <div class="bg-white rounded-lg shadow-sm p-6 text-center flex flex-col items-center">
                <h3 class="text-lg font-semibold text-slate-800 mb-4">Wyślij podsumowanie na e-mail</h3>
                <p class="text-sm text-slate-600 mb-6 flex-grow">Otrzymaj to porównanie w wiadomości e-mail, aby mieć je zawsze pod ręką.</p>
                <button id="email-btn" class="w-full bg-blue-600 text-white font-bold py-2 px-4 rounded-lg hover:bg-blue-700 transition-colors">Generuj do e-mail</button>
            </div>

            <!-- Opcja 2: Zadzwoń -->
            <div class="bg-white rounded-lg shadow-sm p-6 text-center flex flex-col items-center">
                <h3 class="text-lg font-semibold text-slate-800 mb-4">Zadzwoń i dowiedz się więcej</h3>
                <p class="text-sm text-slate-600 mb-6 flex-grow">Nasi eksperci są dostępni, aby odpowiedzieć na wszystkie Twoje pytania.</p>
                <a href="tel:+48510027118" class="w-full bg-green-600 text-white font-bold py-2 px-4 rounded-lg hover:bg-green-700 transition-colors block">Zadzwoń: +48 510 027 118</a>
            </div>

            <!-- Opcja 3: Zamów kontakt -->
            <div class="bg-white rounded-lg shadow-sm p-6">
                <h3 class="text-lg font-semibold text-slate-800 mb-4 text-center">Zamów bezpłatną konsultację</h3>
                <form id="contact-form" class="space-y-4">
                    <div>
                        <label for="phone" class="block text-sm font-medium text-slate-700">Numer telefonu</label>
                        <input type="tel" id="phone" name="phone" required class="mt-1 block w-full px-3 py-2 bg-white border border-slate-300 rounded-md shadow-sm placeholder-slate-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
                    </div>
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label for="date" class="block text-sm font-medium text-slate-700">Dzień</label>
                            <input type="date" id="date" name="date" class="mt-1 block w-full px-3 py-2 bg-white border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
                        </div>
                        <div>
                            <label for="time" class="block text-sm font-medium text-slate-700">Godzina</label>
                            <input type="time" id="time" name="time" class="mt-1 block w-full px-3 py-2 bg-white border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
                        </div>
                    </div>
                    <button type="submit" class="w-full bg-blue-600 text-white font-bold py-2 px-4 rounded-lg hover:bg-blue-700 transition-colors">Zamów kontakt</button>
                    <a href="tel:+48510027118" class="w-full mt-4 block text-center bg-slate-600 text-white font-bold py-2 px-4 rounded-lg hover:bg-slate-700 transition-colors">Zadzwoń teraz!</a>
                </form>
            </div>
        </div>
    </section>
</div>

<!-- Modal -->
<div id="confirmation-modal" class="hidden fixed inset-0 bg-black bg-opacity-50 z-50 flex justify-center items-center p-4">
    <div class="bg-white rounded-lg shadow-xl p-6 max-w-sm w-full text-center">
        <p id="modal-message" class="text-lg text-slate-800 mb-4"></p>
        <button id="modal-close-btn" class="bg-blue-600 text-white font-bold py-2 px-6 rounded-lg hover:bg-blue-700 transition-colors">OK</button>
    </div>
</div>

<script>
    const offerData = [
        {
            id: 'fundusz-nowosc',
            name: 'Fundusz IV Nowość!',
            totalCost6m: 10600, // Half of 12m cost
            totalCost12m: 21200, // From document
            repaymentAmount6m: null,
            repaymentMethod: 'Rata miesięczna',
            bikCheck: true,
            zusUsArrears: 'Tak, o ile brak zajęć komorniczych na koncie (90 dni)',
            acceptsZusUsArrears: true,
            krdEntries: false,
            creditCapacity: '1000% obrotu (max. 150 tys. PLN)',
            maxPeriod: '96 m-cy',
            mainRequirement: 'Brak konieczności przepinania terminala, Weryfikacja raportu BIK (min. 50%)',
            documents: 'Wyciąg z pełną historią rachunku (7 m-cy), Raport BIK.',
            borderColor: 'border-amber-500',
            acceptsResidencyCard: false,
            residencyCardInfo: 'Nie'
        },
        {
            id: 'fundusz-y',
            name: 'Fundusz I Y',
            totalCost6m: 20000,
            totalCost12m: 40000, // Extrapolated
            repaymentAmount6m: 120000,
            repaymentMethod: '11,9% dziennego obrotu z terminala',
            bikCheck: false,
            zusUsArrears: 'Tak, do 50% kwoty pożyczki',
            acceptsZusUsArrears: true,
            krdEntries: true,
            creditCapacity: '100% obrotu',
            maxPeriod: '12 m-cy',
            mainRequirement: 'Przepięcie obrotów z terminala',
            documents: 'Wyciąg z pełną historią rachunku za okres 7 miesięcy.',
            borderColor: 'border-cyan-500',
            acceptsResidencyCard: true,
            residencyCardInfo: 'Tak'
        },
        {
            id: 'fundusz-f',
            name: 'Fundusz II F',
            totalCost6m: 19900,
            totalCost12m: null, // Max period is 6m
            repaymentAmount6m: 119900,
            repaymentMethod: 'Stała rata dzienna: 667 PLN',
            bikCheck: false,
            zusUsArrears: 'Tak, do 100 000 PLN',
            acceptsZusUsArrears: true,
            krdEntries: true,
            creditCapacity: '400% obrotu',
            maxPeriod: '6 m-cy',
            mainRequirement: 'Zmiana terminala na wskazany przez fundusz',
            documents: 'Wyciąg z widocznymi wpływami z terminala (7 m-cy), Wyciąg z pełną historią rachunku (7 m-cy).',
            borderColor: 'border-blue-500',
            acceptsResidencyCard: true,
            residencyCardInfo: 'Tak'
        },
        {
            id: 'fundusz-w',
            name: 'Fundusz III W',
            totalCost6m: 17950,
            totalCost12m: 35900, // Extrapolated
            repaymentAmount6m: 117950,
            repaymentMethod: '7% dziennego obrotu z terminala',
            bikCheck: true,
            zusUsArrears: 'Tak, do 100% kwoty pożyczki',
            acceptsZusUsArrears: true,
            krdEntries: true,
            creditCapacity: '150% obrotu',
            maxPeriod: '15 m-cy',
            mainRequirement: 'Przepięcie obrotów z terminala',
            documents: 'Wyciąg z pełną historią rachunku (12 m-cy), Zestawienie obrotów od Agenta Rozliczeniowego (7 m-cy).',
            borderColor: 'border-emerald-500',
            acceptsResidencyCard: true,
            residencyCardInfo: 'Tak'
        }
    ];

    function formatCurrency(value) {
        if (value === null || typeof value === 'undefined') return '<span class="text-slate-400">Brak danych</span>';
        return new Intl.NumberFormat('pl-PL', { style: 'currency', currency: 'PLN' }).format(value);
    }

    const grid = document.getElementById('offers-grid');
    const tableContainer = document.getElementById('table-container');

    function renderCards(filterFn = () => true) {
        grid.innerHTML = '';
        offerData.filter(filterFn).forEach(offer => {
            const card = document.createElement('div');
            card.className = `fund-card bg-white rounded-lg shadow-md p-6 flex flex-col border-t-4 ${offer.borderColor}`;
            card.dataset.id = offer.id;

            let costInfo = offer.totalCost6m !== null 
                ? `<p class="text-3xl font-bold text-slate-900">${formatCurrency(offer.totalCost6m)}</p><p class="text-sm text-slate-500">Szacowany koszt dla 6 m-cy</p>`
                : `<p class="text-2xl font-bold text-slate-900">${formatCurrency(offer.totalCost12m)}</p><p class="text-sm text-slate-500">Szacowany koszt dla 12 m-cy</p>`;

            card.innerHTML = `
                <h3 class="text-xl font-semibold text-slate-800 mb-2">${offer.name}</h3>
                <div class="text-center bg-slate-50 rounded-lg py-4 my-4">
                    ${costInfo}
                </div>
                <div class="space-y-3 text-sm flex-grow">
                    <p><strong>Maksymalna zdolność:</strong><br>${offer.creditCapacity}</p>
                    <p><strong>Maksymalny okres:</strong><br>${offer.maxPeriod}</p>
                    <p><strong>Sposób spłaty:</strong><br>${offer.repaymentMethod}</p>
                    <p><strong>Weryfikacja w BIK:</strong> ${offer.bikCheck ? '<span class="font-semibold text-red-600">Tak</span>' : '<span class="font-semibold text-green-600">Brak</span>'}</p>
                    <p><strong>Akceptacja wpisów w KRD:</strong> ${offer.krdEntries ? '<span class="font-semibold text-green-600">Tak</span>' : '<span class="font-semibold text-red-600">Nie</span>'}</p>
                    <p><strong>Zaległości ZUS/US:</strong><br>${offer.zusUsArrears}</p>
                    <p><strong>Dla posiadaczy paszportu/karty pobytu:</strong> ${offer.acceptsResidencyCard ? '<span class="font-semibold text-green-600">Tak</span>' : '<span class="font-semibold text-red-600">Nie</span>'}</p>
                    <p><strong>Główne wymagania:</strong><br>${offer.mainRequirement}</p>
                </div>
                <div class="mt-4 pt-4 border-t border-slate-200">
                    <p class="text-xs text-slate-600"><strong>Wymagane dokumenty:</strong> ${offer.documents}</p>
                </div>
            `;
            grid.appendChild(card);
        });
    }

    function renderTable() {
        tableContainer.innerHTML = '';

        const table = document.createElement('table');
        table.className = 'w-full text-sm text-left text-slate-600';

        const thead = document.createElement('thead');
        thead.className = 'text-xs text-slate-700 uppercase bg-slate-100';
        let headerRowHtml = '<tr><th scope="col" class="px-6 py-3">Kryterium</th>';
        offerData.forEach(offer => {
            let additionalInfo = '';
            if (offer.id === 'fundusz-nowosc') {
                additionalInfo = `<span class="block font-normal text-xs text-slate-500 normal-case">(oferta tylko dla JDG)</span>`;
            }
            headerRowHtml += `<th scope="col" class="px-6 py-3 text-center">${offer.name}${additionalInfo}</th>`;
        });
        headerRowHtml += '</tr>';
        thead.innerHTML = headerRowHtml;
        table.appendChild(thead);

        const tbody = document.createElement('tbody');
        const criteria = [
            { key: 'creditCapacity', label: 'Maksymalna zdolność' },
            { key: 'maxPeriod', label: 'Maksymalny okres' },
            { key: 'totalCost6m', label: 'Szacowany koszt (6 m-cy)', format: 'currency' },
            { key: 'totalCost12m', label: 'Szacowany koszt (12 m-cy)', format: 'currency' },
            { key: 'zusUsArrears', label: 'Zaległości ZUS/US' }
        ];

        let tbodyHtml = '';
        criteria.forEach(criterion => {
            tbodyHtml += `<tr class="bg-white border-b hover:bg-slate-50"><th scope="row" class="px-6 py-4 font-medium text-slate-900 whitespace-nowrap">${criterion.label}</th>`;
            offerData.forEach(offer => {
                const value = offer[criterion.key];
                const formattedValue = criterion.format === 'currency' ? formatCurrency(value) : (value || '<span class="text-slate-400">Brak danych</span>');
                tbodyHtml += `<td class="px-6 py-4 text-center">${formattedValue}</td>`;
            });
            tbodyHtml += '</tr>';
        });
        tbody.innerHTML = tbodyHtml;
        table.appendChild(tbody);
        tableContainer.appendChild(table);
    }

    function showModal(message) {
        const modal = document.getElementById('confirmation-modal');
        const modalMessage = document.getElementById('modal-message');
        modalMessage.textContent = message;
        modal.classList.remove('hidden');
    }

    function hideModal() {
        const modal = document.getElementById('confirmation-modal');
        modal.classList.add('hidden');
    }

    document.addEventListener('DOMContentLoaded', () => {
        renderCards();
        renderTable();

        const filterButtons = document.querySelectorAll('.filter-btn');
        filterButtons.forEach(button => {
            button.addEventListener('click', () => {
                filterButtons.forEach(btn => btn.classList.remove('active'));
                button.classList.add('active');

                let filterFunction;
                switch (button.id) {
                    case 'filter-no-bik':
                        filterFunction = offer => !offer.bikCheck;
                        break;
                    case 'filter-krd':
                        filterFunction = offer => offer.krdEntries;
                        break;
                    case 'filter-zus-us':
                        filterFunction = offer => offer.acceptsZusUsArrears;
                        break;
                    case 'filter-residency':
                        filterFunction = offer => offer.acceptsResidencyCard;
                        break;
                    default:
                        filterFunction = () => true;
                }
                renderCards(filterFunction);
            });
        });

        // Contact form logic
        const contactForm = document.getElementById('contact-form');
        const emailBtn = document.getElementById('email-btn');
        const modalCloseBtn = document.getElementById('modal-close-btn');

        modalCloseBtn.addEventListener('click', hideModal);

        contactForm.addEventListener('submit', (e) => {
            e.preventDefault();
            const phone = document.getElementById('phone').value;
            const date = document.getElementById('date').value;
            const time = document.getElementById('time').value;
            if(phone) {
                let message = `Dziękujemy! Skontaktujemy się z Państwem na numer ${phone}`;
                if(date && time) {
                    message += ` dnia ${date} około godziny ${time}.`;
                } else {
                    message += ` w najszybszym możliwym terminie.`
                }
                showModal(message);
                contactForm.reset();
            } else {
                showModal('Prosimy o podanie numeru telefonu.');
            }
        });

        emailBtn.addEventListener('click', () => {
            let body = "Podsumowanie Ofert Finansowania:\n\n";
            const table = tableContainer.querySelector('table');
            if (table) {
                const rows = table.querySelectorAll('tr');
                rows.forEach(row => {
                    const cells = row.querySelectorAll('th, td');
                    const rowData = Array.from(cells).map(cell => cell.innerText.replace(/\n/g, ' '));
                    body += rowData.join('\t|\t') + '\n';
                });
            }
            body += "\n\nWygenerowano z interaktywnego porównania.";
            const mailtoLink = `mailto:?subject=Porównanie Ofert Finansowania&body=${encodeURIComponent(body)}`;
            window.location.href = mailtoLink;
        });
    });
</script>