-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstages-manage.js
More file actions
213 lines (197 loc) · 8.51 KB
/
Copy pathstages-manage.js
File metadata and controls
213 lines (197 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// ══════════════════════════════════════════════════
// STAGE MANAGEMENT (create / delete stages & terms)
// ══════════════════════════════════════════════════
function renderAddStagePage() {
const opts = Object.keys(STAGE_TYPES).map(k =>
`<option value="${k}">${STAGE_TYPES[k].label}</option>`
).join('');
document.getElementById('add-stage-form').innerHTML = `
<div class="form-row single">
<div class="form-field">
<label class="form-label">Stage Type</label>
<select class="form-input" id="f-stage-type" onchange="onStageTypeChange()">${opts}</select>
</div>
</div>
<div class="form-row single">
<div class="form-field">
<label class="form-label">Display Name</label>
<input class="form-input" id="f-stage-label" value="${STAGE_TYPES.sslc.label}">
</div>
</div>
<div class="form-row single" id="f-stage-mode-row">
<div class="form-field">
<label class="form-label">Marking Pattern</label>
<select class="form-input" id="f-stage-mode">
<option value="annual">Annual (single yearly result)</option>
<option value="semester">Semester-wise</option>
</select>
</div>
</div>
<div class="form-row single" id="f-stage-cet-note" style="display:none;">
<p style="font-size:0.8rem;color:var(--text-dim);margin:0;">CET stages don't track subjects or marks — just your previous-stage conversion score and your current rank. You'll fill these in after creating the stage.</p>
</div>
<div class="modal-footer" style="border-top:none;padding-top:0.5rem;">
<button class="btn btn-ghost" onclick="showSection('dashboard')">Cancel</button>
<button class="btn btn-solid" onclick="saveStage()"><i class="bi bi-plus-lg"></i> Create Stage</button>
</div>
`;
onStageTypeChange();
}
function onStageTypeChange() {
const type = document.getElementById('f-stage-type').value;
const def = STAGE_TYPES[type];
document.getElementById('f-stage-label').value = def.label;
const isCet = type === 'cet';
document.getElementById('f-stage-mode-row').style.display = isCet ? 'none' : '';
document.getElementById('f-stage-cet-note').style.display = isCet ? '' : 'none';
if (!isCet) document.getElementById('f-stage-mode').value = def.defaultMode;
}
async function saveStage() {
const type = document.getElementById('f-stage-type').value;
const label = document.getElementById('f-stage-label').value.trim() || STAGE_TYPES[type].label;
let stage;
if (type === 'cet') {
stage = {
id: uid(),
type, label, mode: 'cet',
order: state.stages.length,
terms: [],
cet: { conversion: 0, rank: '', categoryRank: '' },
};
} else {
const mode = document.getElementById('f-stage-mode').value;
stage = {
id: uid(),
type, label, mode,
order: state.stages.length,
terms: mode === 'annual'
? [{ id: 'annual', label: 'Annual' }]
: [{ id: uid(), label: 'Semester 1' }],
};
}
state.stages.push(stage);
await dbPut('stages', stage);
renderNavTabs();
showSection('stage:' + stage.id);
toast('✓ Stage added');
if (type === 'cet') openEditCetModal(stage.id);
}
// ══════════════════════════════════════════════════
// CET STAGE — conversion score + rank editor
// ══════════════════════════════════════════════════
function openEditCetModal(stageId) {
const stage = getStage(stageId);
const c = stage.cet || (stage.cet = { conversion: 0, rank: '', categoryRank: '' });
openModal('CET Details', `
<div class="form-row single">
<div class="form-field">
<label class="form-label">Previous Stage Conversion Score (out of 100)</label>
<input class="form-input" type="number" step="0.01" min="0" max="100" id="f-cet-conversion" value="${c.conversion || ''}" placeholder="e.g. PUC marks converted to /100">
</div>
</div>
<div class="form-row single">
<div class="form-field">
<label class="form-label">Current CET Rank</label>
<input class="form-input" type="text" id="f-cet-rank" value="${escHtml(c.rank || '')}" placeholder="e.g. 4521">
</div>
</div>
<div class="form-row single">
<div class="form-field">
<label class="form-label">Category Rank <span style="color:var(--text-dim);font-weight:400;">(optional)</span></label>
<input class="form-input" type="text" id="f-cet-category-rank" value="${escHtml(c.categoryRank || '')}" placeholder="e.g. 812">
</div>
</div>
`);
setModalFooter([
{ label: 'Cancel', cls: 'btn-ghost', fn: 'closeModal()' },
{ label: 'Save', cls: 'btn-solid', fn: `saveCetDetails('${stageId}')` },
]);
}
async function saveCetDetails(stageId) {
const stage = getStage(stageId);
stage.cet = {
conversion: +document.getElementById('f-cet-conversion').value || 0,
rank: document.getElementById('f-cet-rank').value.trim(),
categoryRank: document.getElementById('f-cet-category-rank').value.trim(),
};
await dbPut('stages', stage);
closeModal();
renderStageView(stageId);
if (currentSection === 'dashboard') renderDashboard();
toast('✓ CET details saved');
}
function confirmDeleteStage(stageId) {
const stage = getStage(stageId);
const count = stageSubjects(stageId).length;
openModal('Delete Stage', `
<div class="confirm-box">
<div class="modal-title" style="text-align:center;border:none;margin-bottom:0.5rem;">Are you sure?</div>
<p class="confirm-msg">"${escHtml(stageLabel(stage))}" with ${count} subject${count !== 1 ? 's' : ''} across all terms will be permanently removed.</p>
</div>
`);
setModalFooter([
{ label: 'Cancel', cls: 'btn-ghost', fn: 'closeModal()' },
{ label: 'Delete Stage', cls: 'btn-danger', fn: `deleteStage('${stageId}')` },
]);
}
async function deleteStage(stageId) {
const subs = stageSubjects(stageId);
for (const s of subs) await dbDelete('subjects', s.id);
state.subjects = state.subjects.filter(s => s.stageId !== stageId);
state.stages = state.stages.filter(s => s.id !== stageId);
await dbDelete('stages', stageId);
closeModal();
toast('Stage deleted');
renderNavTabs();
showSection('dashboard');
}
async function moveStage(stageId, dir) {
sortStages();
const idx = state.stages.findIndex(s => s.id === stageId);
const swapIdx = idx + dir;
if (idx < 0 || swapIdx < 0 || swapIdx >= state.stages.length) return;
[state.stages[idx], state.stages[swapIdx]] = [state.stages[swapIdx], state.stages[idx]];
for (let i = 0; i < state.stages.length; i++) {
state.stages[i].order = i;
await dbPut('stages', state.stages[i]);
}
renderNavTabs();
if (currentSection === 'stats') renderStatsPage();
if (currentSection === 'dashboard') renderDashboard();
if (currentSection === 'stages') renderStagesPage();
toast('Stage order updated');
}
async function addTerm(stageId) {
const stage = getStage(stageId);
const num = stage.terms.length + 1;
stage.terms.push({ id: uid(), label: `Semester ${num}` });
await dbPut('stages', stage);
renderStageView(stageId);
toast(`Semester ${num} added`);
}
function confirmDeleteTerm(stageId, termId) {
const stage = getStage(stageId);
const term = stage.terms.find(t => t.id === termId);
const count = termSubjects(stageId, termId).length;
openModal('Delete Semester', `
<div class="confirm-box">
<div class="modal-title" style="text-align:center;border:none;margin-bottom:0.5rem;">Are you sure?</div>
<p class="confirm-msg">"${escHtml(term?.label)}" with ${count} subject${count !== 1 ? 's' : ''} will be permanently removed.</p>
</div>
`);
setModalFooter([
{ label: 'Cancel', cls: 'btn-ghost', fn: 'closeModal()' },
{ label: 'Delete Semester', cls: 'btn-danger', fn: `deleteTerm('${stageId}','${termId}')` },
]);
}
async function deleteTerm(stageId, termId) {
const toDelete = termSubjects(stageId, termId);
for (const s of toDelete) await dbDelete('subjects', s.id);
state.subjects = state.subjects.filter(s => !(s.stageId === stageId && s.termId === termId));
const stage = getStage(stageId);
stage.terms = stage.terms.filter(t => t.id !== termId);
await dbPut('stages', stage);
closeModal();
renderStageView(stageId);
toast('Semester deleted');
}