Current Progress: ${goal.currentProgressValue.toFixed(1)} ${unitDisplay} (${progressPercent.toFixed(1)}%)
${progressPercent.toFixed(1)}%
${goal.description || 'No description.'}
`;
container.appendChild(card);
});
}
function pgpt_populateGoalSelects() {
const selects = [
document.getElementById('pgpt_selectGoalForLog'),
document.getElementById('pgpt_selectGoalForProgressView')
];
selects.forEach((select, index) => {
if (!select) return;
const currentVal = select.value;
select.innerHTML = (index === 1) ? '' : '';
pgpt_growthGoals.sort((a,b)=>a.name.localeCompare(b.name)).forEach(goal => {
const option = document.createElement('option');
option.value = goal.id; option.textContent = goal.name;
select.appendChild(option);
});
if (pgpt_growthGoals.find(g => g.id == currentVal) || (index === 1 && currentVal === "overall")) {
select.value = currentVal;
} else if (index === 1) { // For progress view, default to overall if previous invalid
select.value = "overall";
} else { // For log activity, if prev invalid, it will be blank
select.value = "";
}
});
}
// --- Tab 2: Log Activity ---
function pgpt_updateLogFormUnits() {
const goalId = document.getElementById('pgpt_selectGoalForLog').value;
const unitDisplayEl = document.getElementById('pgpt_logUnitDisplay');
if (!unitDisplayEl) return;
if (goalId) {
const goal = pgpt_growthGoals.find(g => g.id == goalId);
if (goal) {
unitDisplayEl.textContent = goal.targetUnit === 'Custom' ? goal.customUnitName : goal.targetUnit;
pgpt_renderRecentLogsTable(); // Refresh table when goal changes
return;
}
}
unitDisplayEl.textContent = 'Unit'; // Default if no goal selected
pgpt_renderRecentLogsTable(); // Clear/show placeholder if no goal
}
function pgpt_clearLogEntryForm() {
document.getElementById('pgpt_logEntryId').value = '';
// Don't clear goal selection or date, user might be logging multiple entries for same goal/date
// document.getElementById('pgpt_selectGoalForLog').value = '';
// document.getElementById('pgpt_logDate').value = PGPT_CONTEXT_DATE_STR;
document.getElementById('pgpt_activityDescription').value = '';
document.getElementById('pgpt_logTimeSpentHours').value = '';
document.getElementById('pgpt_logTimeSpentMinutes').value = '';
document.getElementById('pgpt_logQuantitativeProgress').value = '';
document.getElementById('pgpt_logNotes').value = '';
document.getElementById('pgpt_logSelfAssessment').value = '';
document.getElementById('pgpt_saveLogEntryButton').textContent = 'Add Log Entry';
const cancelBtn = document.getElementById('pgpt_cancelLogEntryEditButton');
if(cancelBtn) cancelBtn.style.display = 'none';
}
function pgpt_saveLogEntry() {
const id = document.getElementById('pgpt_logEntryId').value;
const goalId = document.getElementById('pgpt_selectGoalForLog').value;
const date = document.getElementById('pgpt_logDate').value;
const activity = document.getElementById('pgpt_activityDescription').value.trim();
const timeHours = parseInt(document.getElementById('pgpt_logTimeSpentHours').value) || 0;
const timeMinutes = parseInt(document.getElementById('pgpt_logTimeSpentMinutes').value) || 0;
const timeSpentMinutes = (timeHours * 60) + timeMinutes;
const quantitativeProgress = parseFloat(document.getElementById('pgpt_logQuantitativeProgress').value) || 0;
const notes = document.getElementById('pgpt_logNotes').value.trim();
const selfAssessment = document.getElementById('pgpt_logSelfAssessment').value ? parseInt(document.getElementById('pgpt_logSelfAssessment').value) : null;
if (!goalId) { alert("Please select a goal to log against."); return; }
if (!date) { alert("Please select the date of activity."); return; }
if (!activity) { alert("Activity description is required."); return; }
// quantitativeProgress can be 0 if it's just time spent or reflection
const logEntry = { goalId: parseInt(goalId), date, activity, timeSpentMinutes, quantitativeProgress, notes, selfAssessment };
if (id) {
const index = pgpt_progressLogs.findIndex(l => l.id == id);
if(index > -1) pgpt_progressLogs[index] = { ...pgpt_progressLogs[index], ...logEntry };
} else {
pgpt_progressLogs.push({ id: Date.now(), ...logEntry });
}
pgpt_updateGoalCurrentProgress(parseInt(goalId));
pgpt_clearLogEntryForm();
pgpt_renderRecentLogsTable();
pgpt_renderGoalsList(); // To update progress bars on goal cards
}
function pgpt_updateGoalCurrentProgress(goalId) {
const goal = pgpt_growthGoals.find(g => g.id == goalId);
if (!goal) return;
const relevantLogs = pgpt_progressLogs.filter(log => log.goalId == goalId);
goal.currentProgressValue = relevantLogs.reduce((sum, log) => sum + (log.quantitativeProgress || 0), 0);
if (relevantLogs.length > 0) { // Update last log date
goal.lastLogDate = relevantLogs.sort((a,b) => new Date(b.date) - new Date(a.date))[0].date;
} else {
goal.lastLogDate = '';
}
}
function pgpt_editLogEntry(logId){
const log = pgpt_progressLogs.find(l => l.id == logId);
if(log){
document.getElementById('pgpt_logEntryId').value = log.id;
document.getElementById('pgpt_selectGoalForLog').value = log.goalId;
pgpt_updateLogFormUnits(); // Update units based on selected goal
document.getElementById('pgpt_logDate').value = log.date;
document.getElementById('pgpt_activityDescription').value = log.activity;
document.getElementById('pgpt_logTimeSpentHours').value = Math.floor(log.timeSpentMinutes / 60);
document.getElementById('pgpt_logTimeSpentMinutes').value = log.timeSpentMinutes % 60;
document.getElementById('pgpt_logQuantitativeProgress').value = log.quantitativeProgress;
document.getElementById('pgpt_logNotes').value = log.notes;
document.getElementById('pgpt_logSelfAssessment').value = log.selfAssessment || '';
document.getElementById('pgpt_saveLogEntryButton').textContent = 'Update Log Entry';
const cancelBtn = document.getElementById('pgpt_cancelLogEntryEditButton');
if(cancelBtn) cancelBtn.style.display = 'inline-block';
}
}
function pgpt_deleteLogEntry(logId){
const logToDelete = pgpt_progressLogs.find(l => l.id == logId);
if (logToDelete && confirm("Delete this log entry?")) {
pgpt_progressLogs = pgpt_progressLogs.filter(l => l.id != logId);
pgpt_updateGoalCurrentProgress(logToDelete.goalId); // Recalculate goal progress
pgpt_renderRecentLogsTable();
pgpt_renderGoalsList();
}
}
function pgpt_renderRecentLogsTable() {
const goalId = document.getElementById('pgpt_selectGoalForLog').value;
const tbody = document.getElementById('pgpt_recentLogsTable')?.getElementsByTagName('tbody')[0];
if (!tbody) return;
tbody.innerHTML = '';
if (!goalId) {
tbody.innerHTML = `
Select a goal to see recent logs.
`; return;
}
const logsForGoal = pgpt_progressLogs.filter(log => log.goalId == goalId)
.sort((a,b) => new Date(b.date) - new Date(a.date) || b.id - a.id) // Sort by date desc, then by id desc
.slice(0, 5); // Show last 5
if (logsForGoal.length === 0) {
tbody.innerHTML = `