Repository navigation
Expand file tree
/
Copy pathadmin-panel.html
More file actions
653 lines (589 loc) · 35.5 KB
/
Copy pathadmin-panel.html
File metadata and controls
653 lines (589 loc) · 35.5 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>👑 پنل مدیریت مدرسه | ققنوس ادب</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
body { background: linear-gradient(135deg, #0a0a1a, #0f0f23); min-height: 100vh; padding: 20px; color: white; }
.container { max-width: 1600px; margin: 0 auto; }
.header { background: rgba(255,255,255,0.05); backdrop-filter: blur(10px); border-radius: 20px; padding: 25px 30px; margin-bottom: 25px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 20px; border: 1px solid rgba(255,255,255,0.1); }
.header h1 { background: linear-gradient(45deg, #2ecc71, #3498db); -webkit-background-clip: text; background-clip: text; color: transparent; }
.badge { display: inline-flex; align-items: center; gap: 8px; padding: 8px 16px; background: rgba(46,204,113,0.2); border-radius: 30px; border: 1px solid rgba(46,204,113,0.3); }
.badge-dot { width: 10px; height: 10px; background: #2ecc71; border-radius: 50%; animation: pulse 1.5s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.5; transform: scale(1.2); } }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 20px; margin-bottom: 25px; }
.stat-card { background: rgba(255,255,255,0.05); backdrop-filter: blur(10px); border-radius: 15px; padding: 20px; text-align: center; border: 1px solid rgba(255,255,255,0.1); transition: all 0.3s; cursor: pointer; }
.stat-card:hover { transform: translateY(-5px); border-color: #2ecc71; }
.stat-number { font-size: 2rem; font-weight: bold; color: #2ecc71; }
.tabs { display: flex; flex-wrap: wrap; gap: 5px; background: rgba(0,0,0,0.3); border-radius: 15px; padding: 10px; margin-bottom: 25px; }
.tab { padding: 12px 24px; border-radius: 12px; cursor: pointer; transition: all 0.3s; background: transparent; color: #95a5a6; }
.tab:hover { background: rgba(255,255,255,0.1); color: white; }
.tab.active { background: linear-gradient(45deg, #2ecc71, #27ae60); color: white; }
.tab-content { display: none; background: rgba(255,255,255,0.03); backdrop-filter: blur(10px); border-radius: 20px; padding: 25px; border: 1px solid rgba(255,255,255,0.1); }
.tab-content.active { display: block; animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 12px; text-align: center; border-bottom: 1px solid rgba(255,255,255,0.1); }
.data-table th { background: rgba(0,0,0,0.3); color: #2ecc71; }
.data-table tr:hover { background: rgba(255,255,255,0.05); }
.btn { padding: 10px 20px; border: none; border-radius: 10px; cursor: pointer; font-weight: bold; transition: all 0.3s; }
.btn-primary { background: linear-gradient(45deg, #2ecc71, #27ae60); color: white; }
.btn-danger { background: linear-gradient(45deg, #e74c3c, #c0392b); color: white; }
.btn-warning { background: linear-gradient(45deg, #f39c12, #e67e22); color: white; }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn:hover { transform: translateY(-2px); filter: brightness(1.05); }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; margin-bottom: 8px; color: #95a5a6; }
.form-group input, .form-group select, .form-group textarea { width: 100%; padding: 12px; background: rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.1); border-radius: 8px; color: white; }
.search-box { background: rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.1); border-radius: 25px; padding: 10px 20px; color: white; width: 250px; }
.modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.9); backdrop-filter: blur(10px); z-index: 1000; }
.modal-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #0a0a1a; border-radius: 20px; padding: 30px; width: 90%; max-width: 600px; max-height: 80vh; overflow-y: auto; border: 1px solid rgba(255,255,255,0.2); }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; padding-bottom: 15px; border-bottom: 1px solid rgba(255,255,255,0.1); }
.close-modal { background: none; border: none; color: white; font-size: 28px; cursor: pointer; }
.loading { text-align: center; padding: 40px; color: #95a5a6; }
.alert { position: fixed; top: 20px; right: 20px; padding: 15px 20px; border-radius: 10px; z-index: 1100; animation: slideIn 0.3s ease; }
.alert-success { background: #2ecc71; color: white; }
.alert-error { background: #e74c3c; color: white; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.footer { text-align: center; margin-top: 30px; padding: 20px; color: rgba(255,255,255,0.7); border-top: 1px solid rgba(255,255,255,0.1); }
.school-badge { display: inline-block; padding: 4px 12px; border-radius: 15px; font-size: 12px; background: #3498db; color: white; }
.role-badge-student { background: #27ae60; }
.role-badge-teacher { background: #e67e22; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<div>
<h1>👑 پنل مدیریت مدرسه</h1>
<p id="schoolNameDisplay">در حال بارگذاری...</p>
</div>
<div class="badge">
<div class="badge-dot"></div>
<span id="systemStatus">سیستم فعال</span>
</div>
</div>
<div class="stats-grid">
<div class="stat-card"><div class="stat-number" id="totalStudents">0</div><div>🎓 دانشآموزان</div></div>
<div class="stat-card"><div class="stat-number" id="totalTeachers">0</div><div>👨🏫 معلمان</div></div>
<div class="stat-card"><div class="stat-number" id="totalExams">0</div><div>📝 آزمونها</div></div>
<div class="stat-card"><div class="stat-number" id="avgScore">0%</div><div>📊 میانگین کلاس</div></div>
</div>
<div class="tabs">
<div class="tab active" onclick="switchTab('dashboard')">📊 داشبورد</div>
<div class="tab" onclick="switchTab('students')">🎓 دانشآموزان</div>
<div class="tab" onclick="switchTab('teachers')">👨🏫 معلمان</div>
<div class="tab" onclick="switchTab('exams')">📝 آزمونها</div>
<div class="tab" onclick="switchTab('results')">📈 نتایج</div>
<div class="tab" onclick="switchTab('settings')">⚙️ تنظیمات</div>
</div>
<div id="dashboard-tab" class="tab-content active">
<h2>📊 داشبورد مدیریت</h2>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px,1fr)); gap: 20px; margin-top: 20px;">
<div class="stat-card"><h3>🏆 دانشآموزان برتر</h3><div id="topStudents"></div></div>
<div class="stat-card"><h3>⚠️ نیاز به توجه</h3><div id="weakStudents"></div></div>
<div class="stat-card"><h3>📊 آمار آزمونها</h3><div id="examStats"></div></div>
</div>
</div>
<div id="students-tab" class="tab-content">
<h2>🎓 مدیریت دانشآموزان</h2>
<div style="display: flex; gap: 15px; margin-bottom: 20px;">
<input type="text" id="studentSearch" class="search-box" placeholder="🔍 جستجو..." onkeyup="searchStudents()">
<button class="btn btn-primary" onclick="showAddStudentModal()">➕ افزودن دانشآموز</button>
<button class="btn btn-warning" onclick="loadStudents()">🔄 بروزرسانی</button>
</div>
<div style="overflow-x: auto;">
<table class="data-table">
<thead><tr><th>نام</th><th>کد ملی</th><th>پایه</th><th>میانگین</th><th>عملیات</th></tr></thead>
<tbody id="studentsTable"></tbody>
</table>
</div>
</div>
<div id="teachers-tab" class="tab-content">
<h2>👨🏫 مدیریت معلمان</h2>
<button class="btn btn-primary" onclick="showAddTeacherModal()" style="margin-bottom: 15px;">➕ افزودن معلم</button>
<div style="overflow-x: auto;">
<table class="data-table">
<thead><tr><th>نام</th><th>کد ملی</th><th>کد اختصاصی</th><th>درس</th><th>عملیات</th></tr></thead>
<tbody id="teachersTable"></tbody>
</table>
</div>
</div>
<div id="exams-tab" class="tab-content">
<h2>📝 آزمونهای مدرسه</h2>
<button class="btn btn-primary" onclick="showAddExamModal()" style="margin-bottom: 15px;">➕ ایجاد آزمون جدید</button>
<div id="examsList"></div>
</div>
<div id="results-tab" class="tab-content">
<h2>📈 نتایج آزمونها</h2>
<div id="resultsList"></div>
</div>
<div id="settings-tab" class="tab-content">
<h2>⚙️ تنظیمات مدرسه</h2>
<div class="form-group"><label>نام مدرسه:</label><input type="text" id="schoolName" placeholder="نام مدرسه"></div>
<div class="form-group"><label>شهر:</label><input type="text" id="schoolCity" placeholder="شهر"></div>
<div class="form-group"><label>تلفن:</label><input type="text" id="schoolPhone" placeholder="تلفن"></div>
<div class="form-group"><label>رمز عبور جدید:</label><input type="password" id="adminPassword" placeholder="رمز جدید"></div>
<button class="btn btn-primary" onclick="updateSchoolSettings()">💾 ذخیره تغییرات</button>
<hr style="margin: 30px 0;">
<button class="btn btn-danger" onclick="logout()">🚪 خروج از پنل مدیریت</button>
</div>
<div class="footer"><p>ققنوس ادب | سیستم مدیریت مدارس</p></div>
</div>
<div id="modal" class="modal"><div class="modal-content"><div class="modal-header"><h2 id="modalTitle">عنوان</h2><button class="close-modal" onclick="closeModal()">×</button></div><div id="modalBody"></div></div></div>
<script>
const API_URL = 'https://ltrp.ir';
let currentAdmin = null;
let schoolData = null;
let students = [];
let teachers = [];
let exams = [];
let examResults = [];
// ==================== INIT ====================
document.addEventListener('DOMContentLoaded', async () => {
await loadAdminData();
if (!currentAdmin) {
window.location.href = 'school-admin-login.html';
return;
}
await loadSchoolData();
await loadAllData();
renderAll();
});
async function loadAdminData() {
const saved = localStorage.getItem('currentAdmin');
if (saved) {
currentAdmin = JSON.parse(saved);
}
}
async function loadSchoolData() {
const res = await fetch(`${API_URL}/api/get_schools.php`);
const schools = await res.json();
schoolData = schools.find(s => s.school_code === currentAdmin.school_code);
document.getElementById('schoolNameDisplay').innerHTML = `🏫 ${schoolData?.school_name || currentAdmin.school_code}`;
document.getElementById('schoolName').value = schoolData?.school_name || '';
document.getElementById('schoolCity').value = schoolData?.city || '';
document.getElementById('schoolPhone').value = schoolData?.phone || '';
}
async function loadAllData() {
await loadStudents();
await loadTeachers();
await loadExams();
await loadResults();
}
async function loadStudents() {
const res = await fetch(`${API_URL}/api/get_users.php?school_code=${currentAdmin.school_code}&role=student`);
students = await res.json();
renderStudents();
}
async function loadTeachers() {
const res = await fetch(`${API_URL}/api/get_users.php?school_code=${currentAdmin.school_code}&role=teacher`);
teachers = await res.json();
renderTeachers();
}
async function loadExams() {
const res = await fetch(`${API_URL}/api/get_exams.php?school_code=${currentAdmin.school_code}`);
exams = await res.json();
renderExams();
}
async function loadResults() {
const res = await fetch(`${API_URL}/api/get_results.php`);
const allResults = await res.json();
examResults = allResults.filter(r => {
const student = students.find(s => s.national_code === r.student_national_code);
return student;
});
renderResults();
}
// ==================== RENDER ====================
function renderAll() {
renderStats();
renderStudents();
renderTeachers();
renderExams();
renderResults();
renderDashboard();
}
function renderStats() {
document.getElementById('totalStudents').innerText = students.length;
document.getElementById('totalTeachers').innerText = teachers.length;
document.getElementById('totalExams').innerText = exams.length;
let totalAvg = 0;
students.forEach(s => {
const results = examResults.filter(r => r.student_national_code === s.national_code);
if (results.length) {
totalAvg += results.reduce((a,b) => a + b.percentage, 0) / results.length;
}
});
const classAvg = students.length ? (totalAvg / students.length).toFixed(1) : 0;
document.getElementById('avgScore').innerText = classAvg + '%';
}
function renderStudents() {
const tbody = document.getElementById('studentsTable');
if (!students.length) {
tbody.innerHTML = '<tr><td colspan="5" class="loading">هیچ دانشآموزی ثبت نشده</td</tr>';
return;
}
tbody.innerHTML = '';
students.forEach(s => {
const results = examResults.filter(r => r.student_national_code === s.national_code);
const avg = results.length ? (results.reduce((a,b) => a + b.percentage, 0) / results.length).toFixed(1) : 0;
tbody.innerHTML += `
<tr>
<td>${s.full_name}</td>
<td>${s.national_code}</td>
<td><span class="school-badge">پایه ${s.class_level}</span></td>
<td style="color: ${avg >= 70 ? '#2ecc71' : avg >= 50 ? '#f39c12' : '#e74c3c'}">${avg}%</td>
<td><button class="btn btn-sm btn-warning" onclick="viewStudentDetail('${s.national_code}')">📊 جزئیات</button></td>
</tr>
`;
});
}
function renderTeachers() {
const tbody = document.getElementById('teachersTable');
if (!teachers.length) {
tbody.innerHTML = '<td><td colspan="5" class="loading">هیچ معلمی ثبت نشده</td</tr>';
return;
}
tbody.innerHTML = '';
teachers.forEach(t => {
tbody.innerHTML += `
<tr>
<td>${t.full_name}</td>
<td>${t.national_code}</td>
<td><span style="background:#2ecc71; color:#1a1a2e; padding:4px 8px; border-radius:8px; font-family:monospace;">${t.teacher_code || '-'}</span></td>
<td>${t.subject || '-'}</td>
<td><button class="btn btn-sm btn-danger" onclick="deleteTeacher('${t.national_code}')">🗑️</button></td>
</tr>
`;
});
}
function renderExams() {
const container = document.getElementById('examsList');
if (!exams.length) {
container.innerHTML = '<div class="loading">📭 هیچ آزمونی ایجاد نشده</div>';
return;
}
container.innerHTML = '';
exams.slice().reverse().forEach(e => {
const isActive = new Date(e.end_time) > new Date();
container.innerHTML += `
<div class="stat-card" style="margin-bottom:15px; text-align:right;">
<div style="display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap;">
<h3>📝 ${e.title}</h3>
<span style="background:${isActive ? '#2ecc71' : '#95a5a6'}; padding:4px 12px; border-radius:20px;">${isActive ? 'فعال' : 'پایان یافته'}</span>
</div>
<div>📖 ${e.subject} | 🎯 پایه ${e.class_level} | ⏰ ${e.duration} دقیقه | 📋 ${e.questions?.length || 0} سوال</div>
<div style="margin-top:10px;">
<button class="btn btn-sm btn-warning" onclick="previewExam('${e.id}')">پیشنمایش</button>
<button class="btn btn-sm btn-primary" onclick="viewExamResults('${e.id}')">نتایج</button>
</div>
</div>
`;
});
}
function renderResults() {
const container = document.getElementById('resultsList');
if (!examResults.length) {
container.innerHTML = '<div class="loading">📭 نتیجهای ثبت نشده</div>';
return;
}
container.innerHTML = '';
examResults.slice().reverse().forEach(r => {
const student = students.find(s => s.national_code === r.student_national_code);
container.innerHTML += `
<div class="stat-card" style="margin-bottom:10px;">
<div><strong>👤 ${student?.full_name || r.student_name || r.student_national_code}</strong></div>
<div>📝 ${r.exam_title}</div>
<div>📊 ${r.percentage}% (${r.score}/${r.total_score})</div>
<div>📅 ${new Date(r.submitted_at).toLocaleDateString('fa-IR')}</div>
</div>
`;
});
}
function renderDashboard() {
const studentStats = students.map(s => {
const r = examResults.filter(rr => rr.student_national_code === s.national_code);
const avg = r.length ? r.reduce((a,b) => a + b.percentage, 0) / r.length : 0;
return { ...s, avg };
});
const top = studentStats.filter(s => s.avg >= 70).slice(0,5);
const weak = studentStats.filter(s => s.avg < 50 && s.avg > 0).slice(0,5);
document.getElementById('topStudents').innerHTML = top.length ? top.map(s => `<div>🏆 ${s.full_name} - ${s.avg.toFixed(1)}%</div>`).join('') : '<div>هنوز دانشآموز برتری ثبت نشده</div>';
document.getElementById('weakStudents').innerHTML = weak.length ? weak.map(s => `<div>⚠️ ${s.full_name} - ${s.avg.toFixed(1)}%</div>`).join('') : '<div>هیچ دانشآموز ضعیفی وجود ندارد 🎉</div>';
const totalExams = exams.length;
const totalStudents = students.length;
const totalParticipants = examResults.length;
document.getElementById('examStats').innerHTML = `
<div>📋 تعداد آزمونها: ${totalExams}</div>
<div>🎓 شرکتکنندگان: ${totalParticipants}</div>
<div>📊 میانگین مشارکت: ${totalStudents ? ((totalParticipants / totalExams / totalStudents) * 100).toFixed(1) : 0}%</div>
`;
}
// ==================== عملیات ====================
function showAddStudentModal() {
showModalWithContent('➕ افزودن دانشآموز', `
<div class="form-group"><label>نام کامل:</label><input type="text" id="studentName" required></div>
<div class="form-group"><label>کد ملی:</label><input type="text" id="studentNationalCode" maxlength="10" required></div>
<div class="form-group"><label>پایه:</label><select id="studentClass"><option value="7">هفتم</option><option value="8">هشتم</option><option value="9">نهم</option></select></div>
<div class="form-group"><label>رمز عبور:</label><input type="password" id="studentPassword" value="123456"></div>
<button class="btn btn-primary" onclick="addStudent()">✅ ثبت دانشآموز</button>
`);
}
async function addStudent() {
const name = document.getElementById('studentName').value;
const nationalCode = document.getElementById('studentNationalCode').value;
const classLevel = document.getElementById('studentClass').value;
const password = document.getElementById('studentPassword').value;
if (!name || !nationalCode) {
alert('لطفاً همه فیلدها را پر کنید');
return;
}
const userData = {
national_code: nationalCode,
full_name: name,
role: 'student',
password: password,
class_level: classLevel,
school_code: currentAdmin.school_code
};
const res = await fetch(`${API_URL}/api/create_user.php`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(userData)
});
if (res.ok) {
loadStudents();
closeModal();
showAlert('✅ دانشآموز اضافه شد', 'success');
} else {
alert('خطا در ایجاد دانشآموز');
}
}
function showAddTeacherModal() {
const randomCode = generateTeacherCode();
showModalWithContent('➕ افزودن معلم جدید', `
<div class="form-group"><label>نام کامل:</label><input type="text" id="teacherName" required></div>
<div class="form-group"><label>کد ملی:</label><input type="text" id="teacherNationalCode" maxlength="10" required></div>
<div class="form-group"><label>درس تدریس:</label><input type="text" id="teacherSubject" required></div>
<div class="form-group"><label>کد اختصاصی:</label><input type="text" id="teacherCode" value="${randomCode}" readonly style="background:#333; color:#2ecc71;"></div>
<div class="form-group"><label>رمز عبور:</label><input type="password" id="teacherPassword" value="123456"></div>
<button class="btn btn-primary" onclick="addTeacher()">✅ ثبت معلم</button>
`);
}
function generateTeacherCode() {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let code = '';
for (let i = 0; i < 8; i++) {
code += chars[Math.floor(Math.random() * chars.length)];
}
return code;
}
async function addTeacher() {
const name = document.getElementById('teacherName').value;
const nationalCode = document.getElementById('teacherNationalCode').value;
const subject = document.getElementById('teacherSubject').value;
const teacherCode = document.getElementById('teacherCode').value;
const password = document.getElementById('teacherPassword').value;
if (!name || !nationalCode || !subject) {
alert('لطفاً همه فیلدها را پر کنید');
return;
}
const userData = {
national_code: nationalCode,
full_name: name,
role: 'teacher',
password: password,
subject: subject,
teacher_code: teacherCode,
school_code: currentAdmin.school_code
};
const res = await fetch(`${API_URL}/api/create_user.php`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(userData)
});
if (res.ok) {
loadTeachers();
closeModal();
showAlert(`✅ معلم با کد ${teacherCode} اضافه شد`, 'success');
} else {
alert('خطا در ایجاد معلم');
}
}
async function deleteTeacher(nationalCode) {
if (!confirm('حذف معلم؟')) return;
const res = await fetch(`${API_URL}/api/delete_user.php`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ national_code: nationalCode })
});
if (res.ok) {
loadTeachers();
showAlert('✅ معلم حذف شد', 'success');
}
}
function viewStudentDetail(nationalCode) {
const student = students.find(s => s.national_code === nationalCode);
if (!student) return;
const results = examResults.filter(r => r.student_national_code === nationalCode);
const avg = results.length ? (results.reduce((a,b) => a + b.percentage, 0) / results.length).toFixed(1) : 0;
let html = `<div><h3>📊 ${student.full_name}</h3>
<p>کد ملی: ${student.national_code}</p>
<p>پایه: ${student.class_level}</p>
<p>میانگین: ${avg}%</p>
<div style="background:#2c3e50; height:8px; border-radius:10px; margin:15px 0;">
<div style="width:${avg}%; height:8px; background:#2ecc71; border-radius:10px;"></div>
</div>
<h4>📝 تاریخچه آزمونها:</h4>`;
results.forEach(r => {
html += `<div style="margin:10px 0; padding:10px; background:rgba(255,255,255,0.05); border-radius:10px;">
<div>📌 ${r.exam_title}</div>
<div>📊 ${r.percentage}% (${r.score}/${r.total_score})</div>
<div>📅 ${new Date(r.submitted_at).toLocaleDateString('fa-IR')}</div>
</div>`;
});
html += `</div>`;
showModalWithContent(`🔍 جزئیات ${student.full_name}`, html);
}
function previewExam(examId) {
const exam = exams.find(e => e.id === examId);
if (!exam) return;
let html = `<h3>${exam.title}</h3><p>${exam.subject} | ${exam.duration} دقیقه</p><hr>`;
exam.questions?.forEach((q,i) => {
html += `<div style="margin:15px 0; padding:10px; background:rgba(255,255,255,0.05); border-radius:8px;">
<strong>${i+1}. ${q.text}</strong><br>
${q.options.map((o,oi) => `${String.fromCharCode(1570+oi)}) ${o}<br>`).join('')}
</div>`;
});
showModalWithContent('پیشنمایش آزمون', html);
}
function viewExamResults(examId) {
const exam = exams.find(e => e.id === examId);
const results = examResults.filter(r => r.exam_id === examId);
if (!results.length) {
alert('نتیجهای ثبت نشده');
return;
}
let html = `<h3>نتایج ${exam.title}</h3><hr>`;
results.sort((a,b) => b.percentage - a.percentage);
results.forEach(r => {
const student = students.find(s => s.national_code === r.student_national_code);
html += `<div style="margin:10px 0;">${student?.full_name || r.student_name}: ${r.percentage}% (${r.score}/${r.total_score})</div>`;
});
showModalWithContent('نتایج آزمون', html);
}
function showAddExamModal() {
showModalWithContent('📝 ایجاد آزمون جدید', `
<div class="form-group"><label>عنوان:</label><input type="text" id="examTitle" placeholder="عنوان آزمون"></div>
<div class="form-group"><label>درس:</label><select id="examSubject"><option value="ریاضی">ریاضی</option><option value="فارسی">فارسی</option><option value="علوم">علوم</option></select></div>
<div class="form-group"><label>پایه:</label><select id="examClass"><option value="7">هفتم</option><option value="8">هشتم</option><option value="9">نهم</option></select></div>
<div class="form-group"><label>مدت (دقیقه):</label><input type="number" id="examDuration" value="45"></div>
<div class="form-group"><label>مهلت انجام:</label><input type="datetime-local" id="examEndTime"></div>
<button class="btn btn-primary" onclick="createExam()">✅ ایجاد آزمون</button>
`);
const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 7);
document.getElementById('examEndTime').value = tomorrow.toISOString().slice(0,16);
}
async function createExam() {
const title = document.getElementById('examTitle').value;
if (!title) { alert('عنوان را وارد کنید'); return; }
const examData = {
id: Date.now().toString(),
title: title,
subject: document.getElementById('examSubject').value,
class_level: document.getElementById('examClass').value,
duration: parseInt(document.getElementById('examDuration').value),
end_time: document.getElementById('examEndTime').value,
questions: [],
teacher_id: 'admin_created',
school_code: currentAdmin.school_code,
is_active: 1
};
const res = await fetch(`${API_URL}/api/create_exam.php`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(examData)
});
if (res.ok) {
loadExams();
closeModal();
showAlert('✅ آزمون ایجاد شد', 'success');
} else {
alert('خطا در ایجاد آزمون');
}
}
async function updateSchoolSettings() {
const newName = document.getElementById('schoolName').value;
const newCity = document.getElementById('schoolCity').value;
const newPhone = document.getElementById('schoolPhone').value;
const res = await fetch(`${API_URL}/api/update_school.php`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
school_code: currentAdmin.school_code,
school_name: newName,
city: newCity,
phone: newPhone
})
});
if (res.ok) {
showAlert('✅ تنظیمات ذخیره شد', 'success');
await loadSchoolData();
}
}
function searchStudents() {
const term = document.getElementById('studentSearch').value.toLowerCase();
const filtered = students.filter(s => s.full_name.toLowerCase().includes(term) || s.national_code.includes(term));
const tbody = document.getElementById('studentsTable');
tbody.innerHTML = '';
filtered.forEach(s => {
const results = examResults.filter(r => r.student_national_code === s.national_code);
const avg = results.length ? (results.reduce((a,b) => a + b.percentage, 0) / results.length).toFixed(1) : 0;
tbody.innerHTML += `<tr>
<td>${s.full_name}</td>
<td>${s.national_code}</td>
<td><span class="school-badge">پایه ${s.class_level}</span></td>
<td style="color: ${avg >= 70 ? '#2ecc71' : avg >= 50 ? '#f39c12' : '#e74c3c'}">${avg}%</td>
<td><button class="btn btn-sm btn-warning" onclick="viewStudentDetail('${s.national_code}')">📊 جزئیات</button></td>
</tr>`;
});
}
function logout() {
if (confirm('آیا از خروج مطمئن هستید؟')) {
localStorage.removeItem('currentAdmin');
window.location.href = 'school-admin-login.html';
}
}
// ==================== UTILITIES ====================
function switchTab(tabName) {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
if (event && event.target) event.target.classList.add('active');
document.getElementById(`${tabName}-tab`).classList.add('active');
if (tabName === 'dashboard') renderDashboard();
if (tabName === 'students') loadStudents();
if (tabName === 'teachers') loadTeachers();
if (tabName === 'exams') loadExams();
if (tabName === 'results') loadResults();
}
function showModalWithContent(title, content) {
document.getElementById('modalTitle').innerHTML = title;
document.getElementById('modalBody').innerHTML = content;
document.getElementById('modal').style.display = 'block';
}
function closeModal() { document.getElementById('modal').style.display = 'none'; }
function showAlert(msg, type) {
const alert = document.createElement('div');
alert.className = `alert alert-${type}`;
alert.innerText = msg;
document.body.appendChild(alert);
setTimeout(() => alert.remove(), 3000);
}
</script>
</body>
</html>