${T('ที่มาข้อมูล · ยังไม่ผ่านการตรวจสอบ', 'Data source · unverified')} ${recipeLabel} · ${costLabel} ${T('ตัวเลขพลังงานและคุณค่าทางโภชนาการยังไม่ผ่านการตรวจสอบจากแหล่งภายนอก สูตรตั้งต้นไม่ได้มาจาก Thai School Lunch ตรวจสูตรและปริมาณก่อนนำไปใช้จริง', 'Energy and nutrition values have not been externally verified. Seed recipes were not imported from Thai School Lunch. Review the recipe and quantities before real use.')}
`;
}
function syncUrl() {
const query = new URLSearchParams({ week: state.week });
if (chosenGrades().length !== data.settings.grades.length) query.set('grades', chosenGrades().join(','));
history.replaceState(null, '', `/planner.html?${query}`);
}
async function load(week) {
const version = ++loadVersion;
busy = true;
if (data) render();
try {
const result = await api(`/api/planner?week=${encodeURIComponent(week)}`);
if (version !== loadVersion) return;
data = result; state.week = result.week; shopping = null; state.draft.clear(); state.expanded.clear();
if (!state.grades.length) state.grades = (params.get('grades') || params.get('grade') || '').split(',').filter((g) => data.settings.grades.includes(g));
state.grades = state.grades.filter((g) => data.settings.grades.includes(g));
if (!state.grades.length) state.grades = [...data.settings.grades];
state.view = isAdmin() ? 'plan' : 'shopping'; syncUrl();
} catch (e) {
if (data) toast(errorText(e.message), true);
else document.getElementById('main').innerHTML = `
${T('โหลดแผนไม่สำเร็จ', 'Could not load the plan')}
${esc(errorText(e.message))}
`;
} finally { if (version === loadVersion) { busy = false; if (data) render(); else document.getElementById('retry')?.addEventListener('click', () => load(week)); } }
// Only the coordinator plans; the kitchen gets the saved week's purchase order and recipes straight away.
if (version === loadVersion && data && !isAdmin()) await openShopping();
}
function renderHeader() {
document.title = `${isAdmin() ? T('วางเมนูรายสัปดาห์', 'Weekly meal planner') : T('ใบสั่งซื้อประจำสัปดาห์', 'Weekly purchase order')} · EatBack`;
const header = document.getElementById('header'); header.className = 'app-header planner-header';
header.innerHTML = `
${T('จากแผนมื้อกลางวัน สู่ครัวโรงเรียน', 'FROM WEEKLY MENUS TO THE SCHOOL KITCHEN')}
${isAdmin() ? `
${T('วางเมนูทั้งสัปดาห์', 'Plan lunches for the week')}
${T('เลือกเมนูวันจันทร์–ศุกร์ ปรับจำนวนเด็กแยกชั้น แล้วรวมวัตถุดิบเป็นใบสั่งซื้อและสูตรสำหรับครัวในครั้งเดียว', 'Plan Monday to Friday, adjust head counts by grade, then bring the shopping list and scaled recipes together for your kitchen.')}
`
: `
${T('ใบสั่งซื้อและสูตรประจำสัปดาห์', "This week's purchase order and recipes")}
${T('จากแผนที่ผู้ดูแลบันทึกไว้ รวมวัตถุดิบตามจำนวนเด็กของแต่ละชั้น เลือกสัปดาห์หรือชั้นแล้วพิมพ์ได้เลย แก้แผนได้ที่ผู้ดูแล', 'From the plan the coordinator saved, with ingredients for each grade’s head count. Pick a week or grades and print; the coordinator edits the plan.')}
`}
5${T('วันเรียน วางแผนได้ในหน้าเดียว', 'school days, one plan')}
↳${T('เลือกเมนูในแต่ละวันเพื่อใช้กับชั้นที่เลือกทั้งหมด กด “ปรับแยกรายชั้น” เพื่อเปลี่ยนเมนูหรือจำนวนเด็กเฉพาะชั้น มื้อทดลองและมื้อที่เริ่มแล้วจะคงไว้', 'Choose a daily menu for every selected grade. Open “Adjust by grade” to change an individual menu or head count. Reserved trials and started meals are kept.')}
${T('เริ่มจากเมนูมื้อแรกของสัปดาห์', 'Start with the first lunch of the week')}
${T('ยังไม่มีเมนูของชั้นที่เลือกในสัปดาห์นี้ วางเมนูและบันทึกแล้วระบบจะรวมวัตถุดิบให้', 'There are no meals for the selected grades this week. Save a plan to build your ingredient list.')}
${isAdmin() ? `` : `
${T('ผู้ดูแลยังไม่ได้วางแผนของสัปดาห์นี้ ลองเลือกสัปดาห์อื่น', 'The coordinator has not planned this week yet; try another week.')}
${T('สัปดาห์นี้มีมื้อที่เริ่มหรือจบแล้ว ใบนี้รวมทุกมื้อที่บันทึกในสัปดาห์ของชั้นที่เลือก โปรดตรวจรายการที่ซื้อแล้วก่อนสั่งเพิ่ม', 'This week includes started or completed meals. This list includes every recorded meal for the selected grades; check what has already been purchased before ordering.')}
` : ''}
${report.warnings.length ? `
${T('มีข้อมูลต้องเติมก่อนสั่งซื้อ', 'Details to complete before ordering')}
${T('แผนเมนูที่ใช้คำนวณ', 'Meals included in this order')}
${T('วัน', 'Day')}
${T('เมนู / ชั้น', 'Menu / grades')}
${T('คน', 'Students')}
${scheduleRows(meals)}
${T('คิดปริมาณจากจำนวนเด็กและสัดส่วนตักตามวัยที่ตั้งไว้ รวมวัตถุดิบชื่อเดียวกันเมื่อแปลงหน่วยได้ ของนับเป็นชิ้นปัดขึ้นหลังรวมทั้งสัปดาห์ ปริมาณนี้ยังไม่หักสต็อกคงเหลือ', 'Amounts use each grade’s head count and configured portion size. Matching ingredients are combined when their units are compatible. Counted items round up after the weekly total. Existing stock has not been deducted.')}
${T('สูตรนี้เป็นยอดรวมทั้งสัปดาห์ ให้แบ่งเตรียมตามวันและจำนวนเด็กแต่ละมื้อ ของนับเป็นชิ้นปัดขึ้นต่อเมนู จึงอาจต่างจากยอดรวมในใบสั่งซื้อเล็กน้อย', 'This is the weekly total for this recipe. Prepare each lunch according to its day and head count. Counted items round up per menu, so totals may differ slightly from the combined order.')}
` : `
${T('เมนูนี้ยังไม่มีสูตร จึงยังไม่รวมวัตถุดิบในใบสั่งซื้อ', 'This menu has no recipe, so its ingredients are not included in the order.')}