import {
    getData
} from './getData.js';
function formatDateToMMDDYYYY(dateStr) {
    const [day, month, year] = dateStr.split('/');
    return `${year}-${month}-${day}`;
}

export async function postData() {
    const dateValue = document.getElementById('ranged').value;
    const date = formatDateToMMDDYYYY(dateValue);
    const time = window.selectedHour ?? '09:00';
    const name = document.getElementById('name').value;
    const email = document.getElementById('email').value;
    const phone = document.getElementById('phone').value;

    const res = await fetch('/api/book', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ name, email, phone, date, time })
    });

    const data = await res.json();

    const responseEl = document.getElementById('response');
    if (responseEl) {
        responseEl.innerText = data.reply;
    } else {
        console.warn('No element with id="response" found');
    }

    console.log(name, email, phone, date, time);

    const form = document.getElementById("form");
    if(form) form.style.display = "none";

    const btn = document.getElementById("btn");
    if(btn) btn.style.display = "none";

    const success = document.getElementById("success");
    if(success) success.style.display = "block";
    getData();
    renderBookings();
}
window.postData = postData;

