export function createItinerary() {
    if (!document.getElementById('case-filter').value) {
        return;
    }

    let date = document.getElementById('date');
    date.value = new Date().toLocaleDateString();

    let btn = document.getElementById('create-itinerary');
    btn.setAttribute('data-bs-toggle', 'modal');
    btn.setAttribute('data-bs-target', '#exampleModalMessage');
    btn.click();
}

export function addLocationToItinerary() {
    let date = document.getElementById('date').value;
    let origin = document.getElementById('origin').value;
    let destination = document.getElementById('destination').value;
    let departure = document.getElementById('departure').value;
    let arrival = document.getElementById('arrival').value;
    let caseMileage = document.getElementById('case-mileage').checked;
    let caseId = document.getElementById('case-filter').value;

    fetch('/api/add-location-to-itinerary', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            caseId: caseId,
            date: date,
            origin: origin,
            destination: destination,
            departure: departure,
            arrival: arrival,
            caseMileage: caseMileage
        })
    })
        .then(response => response.json())
        .then(data => {
            if (data.success === true) {
                $('#close-modal').click();
            }
        });
}

export function openMap() {
    document.getElementById('caseId').value = document.getElementById('case-filter').value;
    document.getElementById('startDate').value = document.getElementById('start-date-filter').value;
    document.getElementById('endDate').value = document.getElementById('end-date-filter').value;

    document.getElementById('map-form').submit();
}
