# Creating project files and zipping them for download.
from pathlib import Path, PurePosixPath
import json, zipfile, os, textwrap
OUT_DIR = Path('/mnt/data/medicycle_app')
OUT_DIR.mkdir(parents=True, exist_ok=True)
# index.html
index_html = """
${escapeHtml(m.name)}
${escapeHtml(m.dosage)} • ${m.food==='after'? 'After Food':'Before Food'}
Time: ${m.time} • Cycle: ${cycleLabel(m)}
`;
medListAll.appendChild(card);
});
document.querySelectorAll('.edit-med').forEach(b=> b.addEventListener('click', ()=> openEditMedicine(b.dataset.id)));
document.querySelectorAll('.del-med').forEach(b=> b.addEventListener('click', ()=> deleteMed(b.dataset.id)));
}
function renderToday(){
medListToday.innerHTML = '';
if(!currentProfile) return;
const today = new Date();
const todays = currentProfile.medicines.filter(m=> isMedicineForDate(m, today));
if(todays.length===0){ medListToday.innerHTML = '
${escapeHtml(m.name)} ${taken? 'Taken': ''}
${escapeHtml(m.dosage)} • ${m.time} • ${m.food==='after'?'After Food':'Before Food'}
${escapeHtml(h.medName)}${escapeHtml(h.dosage)} • ${new Date(h.timeTakenISO).toLocaleString()}
`;
historyList.appendChild(el);
});
document.querySelectorAll('.btn-copy').forEach(b=> b.addEventListener('click', ()=> navigator.clipboard.writeText(b.dataset.iso)));
}
// ---------- Helpers: cycle label, isMedicineForDate, taken checks ----------
function cycleLabel(m){
if(m.cycle==='daily') return 'Daily';
if(m.cycle==='monthly') return 'Monthly on ' + (m.monthDay||'?');
if(m.cycle==='weekly') return 'Weekly on ' + (m.weekDays? m.weekDays.join(', '):'?');
return '';
}
function isMedicineForDate(m, date){
if(!m) return false;
if(m.cycle==='daily') return true;
if(m.cycle==='monthly'){
const d = Number(m.monthDay);
return d === date.getDate();
}
if(m.cycle==='weekly'){
const names = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
return m.weekDays && m.weekDays.includes(names[date.getDay()]);
}
return false;
}
function wasTakenToday(profileId, medId){
const d = loadData();
const today = (new Date()).toDateString();
return d.history.some(h=> h.profileId===profileId && h.medId===medId && new Date(h.timeTakenISO).toDateString()===today);
}
// ---------- Add/Edit/Delete medicine ----------
function openAddMedicine(){
editingMedId = null; addTitle.textContent = 'Add Medicine'; medForm.reset(); cycleDetails.innerHTML=''; medCycle.value='daily'; renderCycleDetails();
screenProfile.style.display='none'; screenApp.style.display='none'; screenAdd.style.display='block';
}
function openEditMedicine(id){
const med = currentProfile.medicines.find(x=> x.id===id); if(!med) return;
editingMedId = id; addTitle.textContent = 'Edit Medicine';
medName.value = med.name; medDosage.value = med.dosage; medTime.value = med.time; document.querySelectorAll('input[name="food"]').forEach(r=> r.checked = (r.value === med.food));
medCycle.value = med.cycle; renderCycleDetails(med);
screenProfile.style.display='none'; screenApp.style.display='none'; screenAdd.style.display='block';
}
function deleteMed(id){ if(!confirm('Delete this medicine?')) return; const data = loadData(); const profile = data.profiles.find(p=> p.id===data.currentProfileId); profile.medicines = profile.medicines.filter(m=> m.id!==id); saveData(data); initApp(); }
medCycle.addEventListener('change', ()=> renderCycleDetails());
function renderCycleDetails(med=null){
const v = medCycle.value; if(v==='daily'){ cycleDetails.innerHTML = '