function updateTimeOptions() {
console.log("Update time startttttttt");const selectedDate = dateField.value;
console.log(selectedDate, "SELECTED DATE");timeField.innerHTML = "";// Chưa chọn ngày thì hiện toàn bộ
if (!selectedDate) {
originalOptions.forEach((opt) => {
timeField.add(new Option(opt.text, opt.value));
});
return;
}// Parse ngày được chọn (dd-mm-yyyy)
const [day, month, year] = selectedDate.split("-").map(Number);const selected = new Date(year, month - 1, day);
selected.setHours(0, 0, 0, 0);// Ngày hôm nay (00:00)
const today = new Date();
const todayOnly = new Date(today);
todayOnly.setHours(0, 0, 0, 0);// Ngày trong quá khứ -> không còn slot
if (selected < todayOnly) {
console.log("Past date => no time slots");
return;
}// Số phút hiện tại
const currentMinutes = today.getHours() * 60 + today.getMinutes();originalOptions.forEach((opt) => {
if (!/^\d{1,2}:\d{2}$/.test(opt.value)) {
timeField.add(new Option(opt.text, opt.value));
return;
}// Nếu là ngày tương lai -> hiện hết
if (selected > todayOnly) {
timeField.add(new Option(opt.text, opt.value));
return;
}// Chỉ lọc khi là hôm nay
const [h, m] = opt.value.split(":").map(Number);
const optionMinutes = h * 60 + m;if (optionMinutes > currentMinutes) {
timeField.add(new Option(opt.text, opt.value));
}
});// Nếu không còn slot nào thì thêm option thông báo
if (timeField.options.length === 0) {
timeField.add(new Option("No available time", ""));
timeField.disabled = true;
} else {
timeField.disabled = false;
}
}