diff --git a/public/js/app.js b/public/js/app.js index 6615b8d..807418c 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1586,10 +1586,16 @@ class AccountManager {
@@ -1716,6 +1722,11 @@ class AccountManager { if (addBtn) { addBtn.onclick = () => this.openUserModal(); } + + const addRoleBtn = document.getElementById('addRoleBtn'); + if (addRoleBtn) { + addRoleBtn.onclick = () => this.openRoleModal(); + } } getFilteredUsers() { @@ -1799,6 +1810,102 @@ class AccountManager { this.showUserFormModal(null); } + openRoleModal() { + this.showRoleFormModal(); + } + + showRoleFormModal() { + const html = ` + + `; + + const editingContainer = document.getElementById('roleModalContainer'); + if (editingContainer) { + editingContainer.innerHTML = html; + } else { + const container = document.createElement('div'); + container.id = 'roleModalContainer'; + document.body.appendChild(container); + container.innerHTML = html; + } + + const form = document.getElementById('roleForm'); + if (form) { + form.addEventListener('submit', (e) => this.saveRole(e)); + } + + const modal = document.getElementById('roleModal'); + if (modal) { + modal.addEventListener('click', function(e) { + if (e.target === this) { + closeRoleModal(); + } + }); + } + } + + async saveRole(e) { + e.preventDefault(); + + const roleName = document.getElementById('roleName')?.value.trim(); + const description = document.getElementById('roleDescription')?.value.trim(); + + if (!roleName) { + this.notifyFailure('Role name is required'); + return; + } + + const roleExists = this.roles.some(role => + String(role.RoleName || '').trim().toLowerCase() === roleName.toLowerCase() + ); + if (roleExists) { + this.notifyWarning('Role already exists'); + return; + } + + try { + const response = await fetch(`${this.apiBase}/roles`, { + method: 'POST', + headers: this.getAuthHeaders(true), + body: JSON.stringify({ roleName, description: description || null }) + }); + + const data = await response.json(); + if (!response.ok || !data.success) { + this.notifyFailure(data.message || 'Create role failed'); + return; + } + + this.notifySuccess('Role created'); + closeRoleModal(); + await this.fetchRoles(); + if (this.currentPage === 'users') { + this.renderView('users'); + } + } catch (err) { + console.error(err); + this.notifyFailure('Create role failed'); + } + } + showUserFormModal(userId) { const user = userId ? this.users.find(u => u.UserId == userId) : null; @@ -2178,6 +2285,13 @@ function closeUserModal() { } } +function closeRoleModal() { + const roleModalContainer = document.getElementById('roleModalContainer'); + if (roleModalContainer) { + roleModalContainer.innerHTML = ''; + } +} + function closeUserDetailsModal() { const detailsContainer = document.getElementById('userDetailsModalContainer'); if (detailsContainer) {