|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + const loginForm = document.getElementById('login-form'); |
| 3 | + const registerForm = document.getElementById('register-form'); |
| 4 | + const payFeeButton = document.getElementById('pay-fee'); |
| 5 | + const semFeeButton = document.getElementById('sem-pay-fee'); |
| 6 | + const paymentHistoryList = document.getElementById('payment-history'); |
| 7 | + const paymentStatus = document.getElementById('payment-status'); |
| 8 | + const studentList = document.getElementById('student-list'); |
| 9 | + const loggedInStudent = JSON.parse(localStorage.getItem('loggedInStudent')); |
| 10 | + |
| 11 | + //bill |
| 12 | + if (window.location.pathname.includes('bill.html')) { |
| 13 | + const urlParams = new URLSearchParams(window.location.search); |
| 14 | + const name = urlParams.get('name') || 'N/A'; |
| 15 | + const regNo = urlParams.get('regNo') || 'N/A'; |
| 16 | + const paymentDate = urlParams.get('date') || 'N/A'; |
| 17 | + const feeType = urlParams.get('feeType') || ''; |
| 18 | + const amountPaid = urlParams.get('amount') || '0.00'; |
| 19 | + |
| 20 | + document.getElementById('student-name').textContent = name; |
| 21 | + document.getElementById('reg-no').textContent = regNo; |
| 22 | + document.getElementById('payment-date').textContent = paymentDate; |
| 23 | + |
| 24 | + // set specific fee |
| 25 | + if (feeType === 'Semester fee Paid') { |
| 26 | + document.getElementById('semester-fee').textContent = amountPaid; |
| 27 | + } else if (feeType === 'Exam fee Paid') { |
| 28 | + document.getElementById('exam-fee').textContent = amountPaid; |
| 29 | + } |
| 30 | + |
| 31 | + document.querySelector('.total-amount').textContent = `Amount Paid: ₹${amountPaid}`; |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + //open bill |
| 36 | + function openBillPage(studentName, regNo, paymentDate, feeType, amountPaid) { |
| 37 | + const billUrl = `bill.html?name=${encodeURIComponent(studentName)}®No=${encodeURIComponent(regNo)}&date=${encodeURIComponent(paymentDate)}&feeType=${encodeURIComponent(feeType)}&amount=${encodeURIComponent(amountPaid)}`; |
| 38 | + window.open(billUrl, '_blank'); |
| 39 | + } |
| 40 | + |
| 41 | + // Registration Logic |
| 42 | + if (registerForm) { |
| 43 | + registerForm.addEventListener('submit', (e) => { |
| 44 | + e.preventDefault(); |
| 45 | + const name = document.getElementById('name').value; |
| 46 | + const regNo = document.getElementById('reg-no').value; |
| 47 | + const password = document.getElementById('password').value; |
| 48 | + |
| 49 | + if (localStorage.getItem(regNo)) { |
| 50 | + alert('Registration number already exists.'); |
| 51 | + } else { |
| 52 | + localStorage.setItem(regNo, JSON.stringify({ name, password, status: '', paymentHistory: [] })); |
| 53 | + alert(`Registered successfully as ${name}`); |
| 54 | + window.location.href = 'index.html'; |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + // Login Logic |
| 60 | + if (loginForm) { |
| 61 | + loginForm.addEventListener('submit', (e) => { |
| 62 | + e.preventDefault(); |
| 63 | + const regNo = document.getElementById('reg-no').value; |
| 64 | + const password = document.getElementById('password').value; |
| 65 | + const userData = JSON.parse(localStorage.getItem(regNo)); |
| 66 | + |
| 67 | + if (userData && userData.password === password) { |
| 68 | + alert(`Logged in successfully as ${userData.name}`); |
| 69 | + localStorage.setItem('loggedInStudent', JSON.stringify({ studentName: userData.name, regNo })); |
| 70 | + window.location.href = 'dashboard.html'; |
| 71 | + } else { |
| 72 | + alert('Invalid registration number or password.'); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + //dashboard logic |
| 78 | + if (window.location.pathname.includes('dashboard.html')){ |
| 79 | + |
| 80 | + if (!loggedInStudent) { |
| 81 | + alert('No student information found. Please log in.'); |
| 82 | + window.location.href = 'index.html'; |
| 83 | + return; |
| 84 | + } |
| 85 | + document.getElementById('student-name').textContent = loggedInStudent.studentName; |
| 86 | + |
| 87 | + const regNo = loggedInStudent.regNo; |
| 88 | + const userData = JSON.parse(localStorage.getItem(regNo)); |
| 89 | + |
| 90 | + if (userData) { |
| 91 | + updatePaymentButtons(userData); |
| 92 | + } |
| 93 | + // change button to 'paid' if paid |
| 94 | + function updatePaymentButtons(userData) { |
| 95 | + if (userData.status.includes('Exam fee Paid')) { |
| 96 | + payFeeButton.textContent = 'Paid'; |
| 97 | + payFeeButton.disabled = true; |
| 98 | + } |
| 99 | + if (userData.status.includes('Semester fee Paid')) { |
| 100 | + semFeeButton.textContent = 'Paid'; |
| 101 | + semFeeButton.disabled = true; |
| 102 | + } |
| 103 | + paymentStatus.textContent = userData.status || 'Your payment status will be displayed here.'; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Handle payments |
| 108 | + function handlePayment(button, feeType, amountPaid) { |
| 109 | + const loggedInStudent = JSON.parse(localStorage.getItem('loggedInStudent')); |
| 110 | + if (!loggedInStudent) { |
| 111 | + alert('No student information found. Please log in again.'); |
| 112 | + window.location.href = 'index.html'; |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const regNo = loggedInStudent.regNo; |
| 117 | + const userData = JSON.parse(localStorage.getItem(regNo)); |
| 118 | + |
| 119 | + if (userData) { |
| 120 | + userData.status += (userData.status ? ', ' : '') + feeType; |
| 121 | + const paymentDate = new Date().toLocaleDateString(); |
| 122 | + const paymentRecord = `${feeType} of ₹${amountPaid} paid on ${paymentDate}`; |
| 123 | + userData.paymentHistory.push(paymentRecord); |
| 124 | + |
| 125 | + localStorage.setItem(regNo, JSON.stringify(userData)); |
| 126 | + alert(`${feeType} successfully!`); |
| 127 | + paymentStatus.textContent = userData.status; |
| 128 | + |
| 129 | + const li = document.createElement('li'); //payment date time show |
| 130 | + li.textContent = paymentRecord; |
| 131 | + paymentHistoryList.appendChild(li); |
| 132 | + |
| 133 | + button.textContent = 'Paid'; |
| 134 | + button.disabled = true; |
| 135 | + openBillPage(loggedInStudent.studentName, regNo, paymentDate, feeType, amountPaid); |
| 136 | + |
| 137 | + updateAdminStatus(userData); |
| 138 | + } else { |
| 139 | + alert('User not found!'); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Exam Fee Payment |
| 144 | + if (payFeeButton) { |
| 145 | + payFeeButton.addEventListener('click', () => { |
| 146 | + handlePayment(payFeeButton, 'Exam fee Paid', 1200); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + // Semester Fee Payment |
| 151 | + if (semFeeButton) { |
| 152 | + semFeeButton.addEventListener('click', () => { |
| 153 | + handlePayment(semFeeButton, 'Semester fee Paid', 85000); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + // Update admin dashboard |
| 158 | + function updateAdminStatus(userData) { |
| 159 | + const studentRows = document.querySelectorAll('#student-list tr'); |
| 160 | + studentRows.forEach(row => { |
| 161 | + const regNo = row.children[1].textContent; |
| 162 | + if (regNo === userData.regNo) { |
| 163 | + row.children[2].textContent = userData.status; |
| 164 | + } |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + // Admin - Display all students |
| 169 | + if (studentList) { |
| 170 | + const students = Object.keys(localStorage).map((key) => { |
| 171 | + if (key !== 'loggedInStudent') { |
| 172 | + const student = JSON.parse(localStorage.getItem(key)); |
| 173 | + return { name: student.name, regNo: key, status: student.status || 'Not Paid' }; |
| 174 | + } |
| 175 | + }).filter(Boolean); |
| 176 | + |
| 177 | + students.forEach(student => { |
| 178 | + const row = document.createElement('tr'); |
| 179 | + row.innerHTML = `<td>${student.name}</td><td>${student.regNo}</td><td>${student.status}</td>`; |
| 180 | + studentList.appendChild(row); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + // Admin password |
| 185 | + window.checkAdminPassword = function() { |
| 186 | + const adminPassword = "123"; |
| 187 | + const enteredPassword = prompt("Please enter the admin password:"); |
| 188 | + |
| 189 | + if (enteredPassword === adminPassword) { |
| 190 | + window.location.href = 'admin.html'; |
| 191 | + } else { |
| 192 | + alert("Incorrect password. Access denied."); |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + // Logout |
| 197 | + window.logout = function() { |
| 198 | + localStorage.removeItem('loggedInStudent'); |
| 199 | + window.location.href = 'index.html'; |
| 200 | + }; |
| 201 | +}); |
0 commit comments