document.addEventListener("DOMContentLoaded", function() { const popup = document.getElementById("popupMessage"); const allowedTimes = ["10:00","11:00","12:00","13:00","14:00","15:00","16:00"]; const timeSelect = document.getElementById("timeSelect"); // 時間プルダウン作成 function createTimeOptions() { timeSelect.innerHTML = ""; allowedTimes.forEach(t=>{ const option = document.createElement("option"); option.value = t; option.text = t; timeSelect.appendChild(option); }); } createTimeOptions(); // 禁止日判定 function isDisabled(date) { const today = new Date(); today.setHours(0,0,0,0); // 今日の0時基準 // 翌日を算出 const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); const month = date.getMonth() + 1; const day = date.getDate(); // 当日と翌日、さらに過去の日付を禁止 if (date <= tomorrow) return true; // 水曜 if (date.getDay() === 3) return true; // 正月休み(12/30〜1/3) if ((month === 12 && day >= 30) || (month === 1 && day <= 3)) return true; // お盆(8/13〜8/16) if (month === 8 && day >= 13 && day <= 16) return true; return false; } // カレンダー初期化 const fp = flatpickr("#calendar", { locale:"ja", inline:true, onDayCreate: function(dObj, dStr, fpInstance, dayElem) { const date = dayElem.dateObj; if(isDisabled(date)) { dayElem.classList.add("flatpickr-disabled"); dayElem.addEventListener("click", function(e) { e.preventDefault(); showPopup("選択できない日付です"); }); } }, onChange: function(selectedDates) { if(selectedDates.length === 0) return; updateInput(selectedDates[0], timeSelect.value); } }); // 時間変更時 timeSelect.addEventListener("change", function() { if(fp.selectedDates.length === 0) return; updateInput(fp.selectedDates[0], this.value); }); // input に反映 function updateInput(date, time) { const [hh, mm] = time.split(":"); date.setHours(hh); date.setMinutes(mm); const youbi = ["日","月","火","水","木","金","土"]; const y = date.getFullYear(); const m = String(date.getMonth()+1).padStart(2,"0"); const d = String(date.getDate()).padStart(2,"0"); const w = youbi[date.getDay()]; const formatted = `${y}年${m}月${d}日(${w}) ${hh}時${mm}分〜`; document.getElementById("myInput").value = formatted; } // ポップアップ表示 function showPopup(message) { popup.innerText = message; popup.style.display = "block"; const calendar = document.getElementById("calendar"); const rect = calendar.getBoundingClientRect(); const width = popup.offsetWidth; popup.style.top = (rect.bottom + window.scrollY + 10) + "px"; popup.style.left = (rect.left + window.scrollX + (rect.width - width)/2) + "px"; setTimeout(()=>{ popup.style.display = "none"; }, 2000); } // 確認画面ならカレンダーと時間選択を非表示 if (document.querySelector(".mw_wp_form_confirm")) { const calendar = document.querySelector(".flatpickr-calendar"); const timeArea = document.querySelector(".timeArea"); const resTxt = document.querySelector(".resTxt"); if (calendar) calendar.style.display = "none"; if (timeArea) timeArea.style.display = "none"; if (resTxt) resTxt.style.display = "none"; } });