-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (137 loc) · 5.51 KB
/
Copy pathscript.js
File metadata and controls
162 lines (137 loc) · 5.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
// Data storage
let students = [];
let rooms = Array(50).fill(null);
let users = JSON.parse(localStorage.getItem('users')) || {
students: [],
admins: []
};
let currentUser = null;
// Show/hide sections
function showSection(sectionId) {
const sections = currentUser ?
document.querySelectorAll('#main-container section') :
document.querySelectorAll('#auth-container section');
sections.forEach(section => section.classList.remove('active'));
document.getElementById(sectionId).classList.add('active');
if (currentUser) {
updateDashboard();
updateRoomList();
document.getElementById('registerBtn').classList.toggle('hidden', currentUser.type !== 'admin');
}
}
// Login
document.getElementById('loginForm').addEventListener('submit', function(e) {
e.preventDefault();
const userType = document.getElementById('userType').value;
const id = document.getElementById('loginId').value;
const password = document.getElementById('loginPassword').value;
const messageDiv = document.getElementById('loginMessage');
const userList = users[userType + 's'];
const user = userList.find(u => u.id === id && u.password === password);
if (user) {
currentUser = { ...user, type: userType };
document.getElementById('auth-container').style.display = 'none';
document.getElementById('main-container').style.display = 'block';
showSection('dashboard');
} else {
messageDiv.textContent = 'Invalid credentials!';
}
});
// Signup
document.getElementById('signupForm').addEventListener('submit', function(e) {
e.preventDefault();
const userType = document.getElementById('signupUserType').value;
const id = document.getElementById('signupId').value;
const name = document.getElementById('signupName').value;
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
const messageDiv = document.getElementById('signupMessage');
if (users[userType + 's'].some(u => u.id === id)) {
messageDiv.textContent = 'ID already exists!';
return;
}
const newUser = { id, name, email, password };
users[userType + 's'].push(newUser);
localStorage.setItem('users', JSON.stringify(users));
messageDiv.textContent = 'Sign up successful! Please login.';
this.reset();
});
// Logout
function logout() {
currentUser = null;
document.getElementById('main-container').style.display = 'none';
document.getElementById('auth-container').style.display = 'block';
showSection('login');
}
// Handle student registration (admin only)
document.getElementById('studentForm').addEventListener('submit', function(e) {
e.preventDefault();
if (currentUser.type !== 'admin') return;
const student = {
name: document.getElementById('studentName').value,
id: document.getElementById('studentId').value,
email: document.getElementById('studentEmail').value,
year: document.getElementById('studentYear').value,
room: null
};
if (students.some(s => s.id === student.id)) {
document.getElementById('registrationMessage').textContent = 'Student ID already exists!';
return;
}
students.push(student);
this.reset();
document.getElementById('registrationMessage').textContent = 'Student registered successfully!';
updateDashboard();
});
// Room allocation
function allocateRoom() {
if (!currentUser) return;
const studentId = document.getElementById('allocateStudentId').value;
const roomNum = parseInt(document.getElementById('roomNumber').value) - 1;
const messageDiv = document.getElementById('allocationMessage');
if (!studentId || isNaN(roomNum) || roomNum < 0 || roomNum >= 50) {
messageDiv.textContent = 'Please enter valid student ID and room number!';
return;
}
const student = students.find(s => s.id === studentId);
if (!student) {
messageDiv.textContent = 'Student not found!';
return;
}
if (rooms[roomNum]) {
messageDiv.textContent = 'Room already occupied!';
return;
}
if (student.room !== null) {
rooms[student.room] = null;
}
rooms[roomNum] = studentId;
student.room = roomNum;
messageDiv.textContent = `Room ${roomNum + 1} allocated to ${student.name}!`;
updateDashboard();
updateRoomList();
}
// Update dashboard stats
function updateDashboard() {
const totalStudents = students.length;
const occupiedRooms = rooms.filter(room => room !== null).length;
const availableRooms = 50 - occupiedRooms;
document.getElementById('totalStudents').textContent = totalStudents;
document.getElementById('occupiedRooms').textContent = occupiedRooms;
document.getElementById('availableRooms').textContent = availableRooms;
}
// Update room list display
function updateRoomList() {
const roomList = document.getElementById('roomList');
roomList.innerHTML = '<h3>Room Status</h3>';
rooms.forEach((studentId, index) => {
const status = studentId ?
`Room ${index + 1}: Occupied by ${students.find(s => s.id === studentId).name}` :
`Room ${index + 1}: Available`;
const p = document.createElement('p');
p.textContent = status;
roomList.appendChild(p);
});
}
// Initialize
showSection('login');