Primo commit
This commit is contained in:
167
frontend/js/admin-offices.js
Normal file
167
frontend/js/admin-offices.js
Normal file
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* Admin Offices Page
|
||||
* Manage offices, quotas, and prefixes
|
||||
*/
|
||||
|
||||
let currentUser = null;
|
||||
let offices = [];
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
currentUser = await api.requireAuth();
|
||||
if (!currentUser) return;
|
||||
|
||||
if (currentUser.role !== 'admin') {
|
||||
window.location.href = '/presence';
|
||||
return;
|
||||
}
|
||||
|
||||
await loadOffices();
|
||||
setupEventListeners();
|
||||
});
|
||||
|
||||
async function loadOffices() {
|
||||
const response = await api.get('/api/offices');
|
||||
if (response && response.ok) {
|
||||
offices = await response.json();
|
||||
renderOffices();
|
||||
}
|
||||
}
|
||||
|
||||
function renderOffices() {
|
||||
const tbody = document.getElementById('officesBody');
|
||||
|
||||
if (offices.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="5" class="text-center">Nessun ufficio trovato</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
tbody.innerHTML = offices.map(office => {
|
||||
return `
|
||||
<tr>
|
||||
<td>${office.name}</td>
|
||||
<td><span class="badge badge-info">${office.parking_quota} posti</span></td>
|
||||
<td><strong>${office.spot_prefix || '-'}</strong></td>
|
||||
<td>${office.user_count || 0} utenti</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-secondary" onclick="editOffice('${office.id}')">Modifica</button>
|
||||
<button class="btn btn-sm btn-danger" onclick="deleteOffice('${office.id}')" ${office.user_count > 0 ? 'title="Impossibile eliminare uffici con utenti" disabled' : ''}>Elimina</button>
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function openModal(title) {
|
||||
document.getElementById('officeModalTitle').textContent = title;
|
||||
document.getElementById('officeModal').style.display = 'flex';
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
document.getElementById('officeModal').style.display = 'none';
|
||||
document.getElementById('officeForm').reset();
|
||||
document.getElementById('officeId').value = '';
|
||||
}
|
||||
|
||||
async function editOffice(officeId) {
|
||||
const office = offices.find(o => o.id === officeId);
|
||||
if (!office) return;
|
||||
|
||||
document.getElementById('officeId').value = office.id;
|
||||
document.getElementById('officeName').value = office.name;
|
||||
document.getElementById('officeQuota').value = office.parking_quota;
|
||||
|
||||
openModal('Modifica Ufficio');
|
||||
}
|
||||
|
||||
async function deleteOffice(officeId) {
|
||||
const office = offices.find(o => o.id === officeId);
|
||||
if (!office) return;
|
||||
|
||||
if (!confirm(`Eliminare l'ufficio "${office.name}"?`)) return;
|
||||
|
||||
const response = await api.delete(`/api/offices/${officeId}`);
|
||||
if (response && response.ok) {
|
||||
utils.showMessage('Ufficio eliminato', 'success');
|
||||
await loadOffices();
|
||||
} else {
|
||||
const error = await response.json();
|
||||
utils.showMessage(error.detail || 'Impossibile eliminare l\'ufficio', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function setupEventListeners() {
|
||||
// Add button
|
||||
document.getElementById('addOfficeBtn').addEventListener('click', () => {
|
||||
openModal('Nuovo Ufficio');
|
||||
});
|
||||
|
||||
// Modal close
|
||||
document.getElementById('closeOfficeModal').addEventListener('click', closeModal);
|
||||
document.getElementById('cancelOffice').addEventListener('click', closeModal);
|
||||
utils.setupModalClose('officeModal');
|
||||
|
||||
// Debug tracking for save button
|
||||
const saveBtn = document.getElementById('saveOfficeBtn');
|
||||
if (saveBtn) {
|
||||
saveBtn.addEventListener('click', () => console.log('Save button clicked'));
|
||||
}
|
||||
|
||||
// Form submit
|
||||
const form = document.getElementById('officeForm');
|
||||
form.addEventListener('submit', handleOfficeSubmit);
|
||||
}
|
||||
|
||||
async function handleOfficeSubmit(e) {
|
||||
e.preventDefault();
|
||||
console.log('Form submitting...');
|
||||
|
||||
const saveBtn = document.getElementById('saveOfficeBtn');
|
||||
const originalText = saveBtn.innerHTML;
|
||||
saveBtn.disabled = true;
|
||||
saveBtn.innerHTML = 'Salvataggio...';
|
||||
|
||||
const officeId = document.getElementById('officeId').value;
|
||||
const data = {
|
||||
name: document.getElementById('officeName').value,
|
||||
parking_quota: parseInt(document.getElementById('officeQuota').value) || 0
|
||||
};
|
||||
|
||||
console.log('Payload:', data);
|
||||
|
||||
try {
|
||||
let response;
|
||||
if (officeId) {
|
||||
response = await api.put(`/api/offices/${officeId}`, data);
|
||||
} else {
|
||||
response = await api.post('/api/offices', data);
|
||||
}
|
||||
|
||||
console.log('Response status:', response ? response.status : 'null');
|
||||
|
||||
if (response && response.ok) {
|
||||
closeModal();
|
||||
utils.showMessage(officeId ? 'Ufficio aggiornato' : 'Ufficio creato', 'success');
|
||||
await loadOffices();
|
||||
} else {
|
||||
let errorMessage = 'Errore operazione';
|
||||
try {
|
||||
const error = await response.json();
|
||||
errorMessage = error.detail || errorMessage;
|
||||
} catch (e) {
|
||||
console.error('Error parsing JSON error:', e);
|
||||
errorMessage = 'Errore server imprevisto (' + (response ? response.status : 'network') + ')';
|
||||
}
|
||||
utils.showMessage(errorMessage, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Form submit exception:', error);
|
||||
utils.showMessage('Errore di connessione: ' + error.message, 'error');
|
||||
} finally {
|
||||
saveBtn.disabled = false;
|
||||
saveBtn.innerHTML = originalText;
|
||||
}
|
||||
}
|
||||
|
||||
// Global functions
|
||||
window.editOffice = editOffice;
|
||||
window.deleteOffice = deleteOffice;
|
||||
Reference in New Issue
Block a user