export function filterAddressesByCase() {
    if (!document.getElementById('case-filter').value) {
        return;
    }
    fetch('/api/filter-address-by-case/' + document.getElementById('case-filter').value, {
        method: 'POST',
        header: {
            'Content-Type': 'application/json'
        }
    })
        .then(response => response.json())
        .then(result => {
            const addressList = document.getElementById('addressList');
            const origin = document.getElementById('origin');
            const destination = document.getElementById('destination');

            origin.innerHTML = '';
            destination.innerHTML = '';
            addressList.innerHTML = '';

            origin.innerHTML += '<option value="">-- Origin --</option>';
            destination.innerHTML += '<option value="">-- Destination --</option>';

            result.forEach(a => {
                origin.innerHTML += `<option value='${a.id}'>${a.name}</option>`;
                destination.innerHTML += `<option value='${a.id}'>${a.name}</option>`;
                addressList.innerHTML += `
                <tr>
                    <td>
                        <div class='d-flex px-2 py-1'>
                            <div class='d-flex flex-column justify-content-center'>
                                <h6 class='mb-0 text-small'>
                                    ${a.name}
                                </h6>
                            </div>
                        </div>
                    </td>
                    <td>${a.formattedAddress}</td>
                    <td class='align-middle text-center text-xs'>${a.lat}/${a.lon}</td>
                    <td class='align-middle'>
                        <a href='/addresses/edit/${a.id}' class='text-secondary' title='Edit Address'>
                            <i class='material-symbols-rounded opacity-5'>edit</i>
                        </a>
                    </td>
                </tr>`;
            })
        });
}

export function filterItineraryByCase() {
    let caseId = null;
    let startDate = null;
    let endDate = null;

    if (document.getElementById('case-filter').value) {
        caseId = document.getElementById('case-filter').value;
    }

    if (document.getElementById('start-date-filter').value) {
        startDate = document.getElementById('start-date-filter').value;
    }

    if (document.getElementById('end-date-filter').value) {
        endDate = document.getElementById('end-date-filter').value;
    }

    fetch('/api/filter-itinerary-by-case', {
        method: 'POST',
        header: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            caseId: caseId,
            startDate: startDate,
            endDate: endDate,
        })
    })
        .then(response => response.json())
        .then(result => {
            const itineraryList = document.getElementById('itineraryList');
            itineraryList.innerHTML = '';

            result.forEach(i => {
                itineraryList.innerHTML += `
                <tr>
                    <td>${i.date}</td>
                    <td>${i.case}</td>
                    <td>${i.origin.name}</td>
                    <td>${i.destination.name}</td>
                    <td>${i.duration}</td>
                    <td>${i.distance}</td>
                    <td></td>
                </tr>`;
            })
        });
}

