document.addEventListener('DOMContentLoaded', function () {
var currentUserName = document.getElementById('currentUserName');
var logoutBtn = document.getElementById('logoutBtn');
var navItems = document.querySelectorAll('.nav-item');
var sectionTitle = document.getElementById('sectionTitle');
var sectionDesc = document.getElementById('sectionDesc');
var subTabs = document.getElementById('subTabs');
var profileView = document.getElementById('profileView');
var eventsView = document.getElementById('eventsView');
var adminView = document.getElementById('adminView');
var registrationsView = document.getElementById('registrationsView');
var adminNavBtn = document.getElementById('adminNavBtn');
var registrationsNavBtn = document.getElementById('registrationsNavBtn');
var state = {
currentView: 'profile',
currentTab: 'all',
user: null,
events: [],
myEvents: [],
adminUsers: [],
adminRegistrations: [],
eventPage: 1,
myEventPage: 1,
registrationsPage: 1,
pageSize: 7
};
function escapeHtml(value) {
return String(value == null ? '' : value)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function buildOptions(list, current) {
return list.map(function (item) {
return '' + item + ' ';
}).join('');
}
function buildCollegeOptions(current) {
var colleges = [
'文学与文化传播学院',
'马克思主义学院',
'教育学院',
'外国语学院',
'历史文化学院',
'商学院',
'化学工程与技术学院',
'电子信息与电气工程学院',
'数学与统计学院',
'生物工程与技术学院',
'机电工程学院',
'土木工程学院',
'资源与环境工程学院',
'体育学院',
'美术与设计学院',
'音乐舞蹈学院',
'卫生健康学院',
'继续教育学院(培训中心)'
];
return '请选择学院 ' + colleges.map(function (item) {
return '' + item + ' ';
}).join('');
}
function getPaginatedList(list, page) {
var start = (page - 1) * state.pageSize;
var end = start + state.pageSize;
return list.slice(start, end);
}
function getTotalPages(list) {
return Math.ceil(list.length / state.pageSize);
}
function renderPagination(totalPages, currentPage, type) {
if (totalPages <= 1) {
return '';
}
var html = '
';
return html;
}
function renderTabs() {
var html = '';
if (state.currentView === 'events') {
html = ''
+ '报名 '
+ '报名信息 ';
} else if (state.currentView === 'admin') {
html = '用户信息 ';
}
subTabs.innerHTML = html;
subTabs.classList.toggle('hidden', !html);
Array.prototype.slice.call(subTabs.querySelectorAll('.tab-item')).forEach(function (item) {
item.addEventListener('click', function () {
state.currentTab = item.getAttribute('data-tab');
renderTabs();
renderCurrentView();
});
});
}
function renderProfile() {
if (!state.user) {
return;
}
profileView.innerHTML = ''
+ ''
+ '
基础资料 支持在线更新姓名、电话、性别、学院和类别信息。
'
+ '
'
+ '
身份证号 ' + escapeHtml(state.user.idCard) + '
'
+ '
登录账号 ' + escapeHtml(state.user.username) + '
'
+ '
姓名 ' + escapeHtml(state.user.name) + '
'
+ '
联系电话 ' + escapeHtml(state.user.phone) + '
'
+ '
性别 ' + escapeHtml(state.user.gender) + '
'
+ '
学院 ' + escapeHtml(state.user.college) + '
'
+ '
类别 ' + escapeHtml(state.user.category) + '
'
+ '
'
+ '
'
+ '';
document.getElementById('profileForm').addEventListener('submit', function (event) {
event.preventDefault();
var formData = new FormData(event.target);
appUtils.ajax({
method: 'PUT',
url: '/api/users/me',
data: {
name: String(formData.get('name') || '').trim(),
phone: String(formData.get('phone') || '').trim(),
gender: String(formData.get('gender') || ''),
college: String(formData.get('college') || '').trim(),
category: String(formData.get('category') || '')
},
success: function (response) {
if (!response.success) {
appUtils.showMessage(document.getElementById('profileMessage'), response.message || '保存失败', false);
return;
}
state.user = response.data;
currentUserName.textContent = state.user.name + (state.user.role === 'ADMIN' ? ' 管理员' : ' 老师');
renderProfile();
appUtils.showMessage(document.getElementById('profileMessage'), '个人信息已更新', true);
},
error: function (xhr, response) {
appUtils.showMessage(document.getElementById('profileMessage'), (response && response.message) || '保存失败', false);
}
});
});
}
function renderEventTable(list, isMine) {
if (!list.length) {
return '暂无数据 ' + (isMine ? '你还没有报名任何项目。' : '当前暂无可报名项目。') + '
';
}
var currentPage = isMine ? state.myEventPage : state.eventPage;
var totalPages = getTotalPages(list);
var paginatedList = getPaginatedList(list, currentPage);
var html = ''
+ ''
+ ' 项目名称 项目类别 比赛时间 比赛地点 报名情况 项目说明 操作 '
+ ' '
+ paginatedList.map(function (item) {
var actionHtml = isMine
? '取消报名 '
: '' + (item.registered ? '已报名' : '报名') + ' ';
return ''
+ ''
+ '' + escapeHtml(item.eventName) + ' '
+ '' + escapeHtml(item.eventCategory) + ' '
+ '' + escapeHtml(item.eventTime) + ' '
+ '' + escapeHtml(item.location) + ' '
+ '' + escapeHtml(item.registeredCount + '/' + item.quota) + ' '
+ '' + escapeHtml(item.description) + ' '
+ '' + actionHtml + ' '
+ ' ';
}).join('')
+ ' '
+ '
';
html += renderPagination(totalPages, currentPage, isMine);
html += '第 ' + currentPage + ' 页 / 共 ' + totalPages + ' 页,共 ' + list.length + ' 条记录
';
return html;
}
function renderUserTable() {
if (!state.adminUsers.length) {
return '';
}
return ''
+ ''
+ '姓名 账号 身份证号 电话 性别 学院 类别 角色 操作 '
+ ''
+ state.adminUsers.map(function (item) {
return ''
+ ''
+ '' + escapeHtml(item.name) + ' '
+ '' + escapeHtml(item.username) + ' '
+ '' + escapeHtml(item.idCard) + ' '
+ '' + escapeHtml(item.phone) + ' '
+ '' + escapeHtml(item.gender) + ' '
+ '' + escapeHtml(item.college) + ' '
+ '' + escapeHtml(item.category) + ' '
+ '' + escapeHtml(item.role === 'ADMIN' ? '管理员' : '普通用户') + ' '
+ ''
+ '重置密码 '
+ '删除账号 '
+ ' '
+ ' ';
}).join('')
+ '
';
}
function bindUserActions() {
// 绑定重置密码按钮事件
Array.prototype.slice.call(document.querySelectorAll('.reset-btn')).forEach(function (button) {
button.addEventListener('click', function () {
var userId = button.getAttribute('data-id');
if (confirm('确定要重置该用户的密码吗?重置后密码将变为默认值。')) {
appUtils.ajax({
method: 'POST',
url: '/api/admin/users/' + userId + '/reset-password',
success: function (response) {
if (response.success) {
alert('密码重置成功!');
} else {
alert('密码重置失败:' + (response.message || '未知错误'));
}
},
error: function (xhr, response) {
alert('密码重置失败:' + (response && response.message) || '网络异常');
}
});
}
});
});
// 绑定删除账号按钮事件
Array.prototype.slice.call(document.querySelectorAll('.delete-btn')).forEach(function (button) {
button.addEventListener('click', function () {
var userId = button.getAttribute('data-id');
if (confirm('确定要删除该账号吗?此操作不可恢复。')) {
appUtils.ajax({
method: 'DELETE',
url: '/api/admin/users/' + userId,
success: function (response) {
if (response.success) {
alert('账号删除成功!');
loadAdminData();
} else {
alert('账号删除失败:' + (response.message || '未知错误'));
}
},
error: function (xhr, response) {
alert('账号删除失败:' + (response && response.message) || '网络异常');
}
});
}
});
});
}
function uniqueEventCount() {
var map = {};
state.adminRegistrations.forEach(function (item) {
map[item.eventName] = true;
});
return Object.keys(map).length;
}
function renderRegistrationTable() {
var paginatedRegistrations = getPaginatedList(state.adminRegistrations, state.registrationsPage);
if (!paginatedRegistrations.length) {
return '';
}
return ''
+ ''
+ '姓名 账号 电话 学院 类别 项目名称 项目类别 时间地点 状态 报名时间 '
+ ''
+ paginatedRegistrations.map(function (item) {
return ''
+ ''
+ '' + escapeHtml(item.studentName) + ' '
+ '' + escapeHtml(item.username) + ' '
+ '' + escapeHtml(item.phone) + ' '
+ '' + escapeHtml(item.college) + ' '
+ '' + escapeHtml(item.category) + ' '
+ '' + escapeHtml(item.eventName) + ' '
+ '' + escapeHtml(item.eventCategory) + ' '
+ '' + escapeHtml(item.eventTime + ' / ' + item.location) + ' '
+ '' + escapeHtml(item.status) + ' '
+ '' + escapeHtml(item.createdAt) + ' '
+ ' ';
}).join('')
+ '
';
}
function renderAdminContent() {
adminView.innerHTML = ''
+ ''
+ '
用户总数 ' + state.adminUsers.length + '
'
+ '
管理员数量 ' + state.adminUsers.filter(function (item) { return item.role === "ADMIN"; }).length + '
'
+ '
普通用户数量 ' + state.adminUsers.filter(function (item) { return item.role !== "ADMIN"; }).length + '
'
+ '
'
+ renderUserTable();
bindUserActions();
}
function bindRegisterButtons() {
// 绑定报名按钮事件
Array.prototype.slice.call(document.querySelectorAll('.register-btn')).forEach(function (button) {
button.addEventListener('click', function () {
appUtils.ajax({
method: 'POST',
url: '/api/events/' + button.getAttribute('data-id') + '/register',
success: function (response) {
if (!response.success) {
window.alert(response.message || '报名失败');
return;
}
window.alert('报名成功');
loadEventData();
},
error: function (xhr, response) {
window.alert((response && response.message) || '报名失败');
}
});
});
});
// 绑定取消报名按钮事件
Array.prototype.slice.call(document.querySelectorAll('.cancel-btn')).forEach(function (button) {
button.addEventListener('click', function () {
if (confirm('确定要取消报名吗?')) {
appUtils.ajax({
method: 'DELETE',
url: '/api/events/' + button.getAttribute('data-id') + '/register',
success: function (response) {
if (!response.success) {
window.alert(response.message || '取消报名失败');
return;
}
window.alert('取消报名成功');
loadEventData();
},
error: function (xhr, response) {
window.alert((response && response.message) || '取消报名失败');
}
});
}
});
});
}
function bindPaginationButtons(type) {
var totalPages, currentPage;
if (type === 'registrations') {
totalPages = getTotalPages(state.adminRegistrations);
currentPage = state.registrationsPage;
} else {
var isMine = type === 'mine';
totalPages = getTotalPages(isMine ? state.myEvents : state.events);
currentPage = isMine ? state.myEventPage : state.eventPage;
}
Array.prototype.slice.call(document.querySelectorAll('.pagination .page-btn')).forEach(function (button) {
button.addEventListener('click', function () {
var page = button.getAttribute('data-page');
if (page === 'prev') {
currentPage = Math.max(1, currentPage - 1);
} else if (page === 'next') {
currentPage = Math.min(totalPages, currentPage + 1);
} else {
currentPage = parseInt(page, 10);
}
if (type === 'registrations') {
state.registrationsPage = currentPage;
var totalPages = getTotalPages(state.adminRegistrations);
registrationsView.innerHTML = ''
+ ''
+ '
报名记录总数 ' + state.adminRegistrations.length + '
'
+ '
已报名状态 ' + state.adminRegistrations.filter(function (item) { return item.status === "已报名"; }).length + '
'
+ '
覆盖项目数 ' + uniqueEventCount() + '
'
+ '
'
+ renderRegistrationTable()
+ renderPagination(totalPages, state.registrationsPage, 'registrations');
bindPaginationButtons('registrations');
} else {
var isMine = type === 'mine';
if (isMine) {
state.myEventPage = currentPage;
} else {
state.eventPage = currentPage;
}
eventsView.innerHTML = renderEventTable(isMine ? state.myEvents : state.events, isMine);
bindRegisterButtons();
bindPaginationButtons(type);
}
});
});
}
function renderCurrentView() {
profileView.classList.toggle('hidden', state.currentView !== 'profile');
eventsView.classList.toggle('hidden', state.currentView !== 'events');
adminView.classList.toggle('hidden', state.currentView !== 'admin');
registrationsView.classList.toggle('hidden', state.currentView !== 'registrations');
if (state.currentView === 'profile') {
sectionTitle.textContent = '个人信息';
sectionDesc.textContent = '查看并维护当前登录人员的基础资料。';
renderTabs();
renderProfile();
return;
}
if (state.currentView === 'events') {
sectionTitle.textContent = '运动会报名';
sectionDesc.textContent = '浏览所有项目并完成报名,也可以查看自己的报名信息。';
renderTabs();
eventsView.innerHTML = renderEventTable(state.currentTab === 'mine' ? state.myEvents : state.events, state.currentTab === 'mine');
bindRegisterButtons();
bindPaginationButtons(state.currentTab === 'mine' ? 'mine' : 'all');
return;
}
if (state.currentView === 'admin') {
sectionTitle.textContent = '管理员后台';
sectionDesc.textContent = '查看系统内所有用户资料。';
renderTabs();
renderAdminContent();
return;
}
if (state.currentView === 'registrations') {
sectionTitle.textContent = '报名总览';
sectionDesc.textContent = '查看所有用户的报名记录。';
subTabs.classList.add('hidden');
var totalPages = getTotalPages(state.adminRegistrations);
registrationsView.innerHTML = ''
+ ''
+ '
报名记录总数 ' + state.adminRegistrations.length + '
'
+ '
已报名状态 ' + state.adminRegistrations.filter(function (item) { return item.status === "已报名"; }).length + '
'
+ '
覆盖项目数 ' + uniqueEventCount() + '
'
+ '
'
+ renderRegistrationTable()
+ renderPagination(totalPages, state.registrationsPage, 'registrations');
bindPaginationButtons('registrations');
return;
}
}
function loadEventData() {
appUtils.ajax({
method: 'GET',
url: '/api/events',
success: function (response) {
if (response.success) {
state.events = response.data || [];
renderCurrentView();
}
}
});
appUtils.ajax({
method: 'GET',
url: '/api/events/my',
success: function (response) {
if (response.success) {
state.myEvents = response.data || [];
renderCurrentView();
}
}
});
}
function loadAdminData() {
if (!state.user || state.user.role !== 'ADMIN') {
return;
}
appUtils.ajax({
method: 'GET',
url: '/api/admin/users',
success: function (response) {
if (response.success) {
state.adminUsers = response.data || [];
renderCurrentView();
}
}
});
appUtils.ajax({
method: 'GET',
url: '/api/admin/registrations',
success: function (response) {
if (response.success) {
state.adminRegistrations = response.data || [];
renderCurrentView();
}
}
});
}
function switchView(view) {
state.currentView = view;
if (view === 'events') {
state.currentTab = state.currentTab === 'mine' ? 'mine' : 'all';
}
navItems.forEach(function (item) {
item.classList.toggle('active', item.getAttribute('data-view') === view);
});
renderCurrentView();
}
function loadCurrentUser() {
appUtils.ajax({
method: 'GET',
url: '/api/users/me',
success: function (response) {
if (!response.success || !response.data) {
window.location.href = './login.html';
return;
}
state.user = response.data;
currentUserName.textContent = state.user.name + (state.user.role === 'ADMIN' ? ' 管理员' : ' 同学');
if (state.user.role === 'ADMIN') {
adminNavBtn.classList.remove('hidden');
registrationsNavBtn.classList.remove('hidden');
// 隐藏个人信息和运动会报名菜单
document.querySelector('[data-view="profile"]').classList.add('hidden');
document.querySelector('[data-view="events"]').classList.add('hidden');
// 自动切换到管理员后台视图
switchView('admin');
}
renderProfile();
loadEventData();
loadAdminData();
},
error: function () {
window.location.href = './login.html';
}
});
}
navItems.forEach(function (item) {
item.addEventListener('click', function () {
switchView(item.getAttribute('data-view'));
});
});
logoutBtn.addEventListener('click', function () {
appUtils.ajax({
method: 'POST',
url: '/api/auth/logout',
success: function () {
window.location.href = './login.html';
},
error: function () {
window.location.href = './login.html';
}
});
});
loadCurrentUser();
switchView('profile');
});