`;
renderModalCharts(history, pGrowthLogs);
document.getElementById('aqua-pond-modal').style.display = 'flex';
}
function hidePondDetails() {
document.getElementById('aqua-pond-modal').style.display = 'none';
}
function renderModalCharts(waterHistory, growthHistory) {
const render = (id, type, data, options) => {
if(charts[id]) charts[id].destroy();
charts[id] = new Chart(id, { type, data, options });
};
const chartOptions = { responsive: true, maintainAspectRatio: false, scales: { x: { type: 'time', time: { unit: 'day' }, ticks: { color: '#075985' } }, y: { ticks: { color: '#075985' } } }, plugins: { legend: { labels: { color: '#075985' } } } };
render('modal-water-chart', 'line', {
datasets: [
{ label: 'Temp (°C)', data: waterHistory.map(r => ({x: r.timestamp, y: r.temp})), borderColor: '#f97316' },
{ label: 'pH', data: waterHistory.map(r => ({x: r.timestamp, y: r.ph})), borderColor: '#22c55e', yAxisID: 'y1' },
{ label: 'DO (mg/L)', data: waterHistory.map(r => ({x: r.timestamp, y: r.do})), borderColor: '#3b82f6' },
]
}, { ...chartOptions, scales: {...chartOptions.scales, y1: { type: 'linear', position: 'right', grid: { drawOnChartArea: false } } } });
render('modal-growth-chart', 'line', { datasets: [{ label: 'Avg. Weight (g)', data: growthHistory.map(r => ({x: r.timestamp, y: r.avg_weight_g})), tension: 0.1, fill: true, backgroundColor: 'rgba(14, 165, 233, 0.2)', borderColor: '#0ea5e9' }] }, chartOptions);
}
// --- Data Management ---
window.aquaLogWaterQuality = function() {
const newLog = {
pondId: parseInt(document.getElementById('log-pond-select').value),
timestamp: new Date(),
temp: parseFloat(document.getElementById('log-temp').value),
ph: parseFloat(document.getElementById('log-ph').value),
do: parseFloat(document.getElementById('log-do').value),
};
if(isNaN(newLog.temp) || isNaN(newLog.ph) || isNaN(newLog.do)) { alert('Please enter valid numbers for all fields.'); return; }
waterLogs.push(newLog);
renderFarmLayout();
alert('Data logged successfully!');
}
// --- Global Functions ---
window.aquaShowTab = id => {
document.querySelectorAll('.aqua-main-tab-content').forEach(c => c.classList.remove('active'));
document.querySelectorAll('.aqua-main-tab-button').forEach(b => b.classList.remove('active'));
document.getElementById(`aqua-tab-${id}`)?.classList.add('active');
document.querySelector(`.aqua-main-tab-button[onclick="aquaShowTab('${id}')"]`)?.classList.add('active');
};
window.aquaDownloadPDF = () => {
html2canvas(document.getElementById('aqua-pdf-output'), { scale: 2 }).then(canvas => {
const pdf = new jsPDF({ orientation: 'landscape', unit: 'pt', format: 'a4' });
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 40, 40, pdf.internal.pageSize.getWidth() - 80, 0);
pdf.save(`${document.getElementById('modal-pond-name').textContent.split(' ')[0]}_Report.pdf`);
});
};
initialize();
});
