const message = document.querySelector('#message');
async function api(path, options = {}) {
const response = await fetch(path, {headers: {'Content-Type': 'application/json'}, ...options});
if (!response.ok) throw new Error((await response.json().catch(() => ({}))).error || `HTTP ${response.status}`);
return response.json();
}
async function refresh() {
try {
const items = await api('/api/requests');
document.querySelector('#rows').innerHTML = items.map(item => `
| ${item.pdbName} | ${item.storageGb} | ${item.requestedBy} | ${item.status} | ${item.status === 'PENDING' ? `` : ''} |
`).join('');
} catch (error) { message.textContent = error.message; }
}
async function approve(id) { try { await api(`/api/requests/${id}/approve`, {method: 'POST'}); message.textContent = 'Provisionamento submetido à OCI.'; refresh(); } catch (error) { message.textContent = error.message; } }
document.querySelector('#request').addEventListener('submit', async event => {
event.preventDefault(); const data = Object.fromEntries(new FormData(event.target)); data.storageGb = Number(data.storageGb);
try { await api('/api/requests', {method: 'POST', body: JSON.stringify(data)}); event.target.reset(); message.textContent = 'Solicitação enviada para aprovação.'; refresh(); } catch (error) { message.textContent = error.message; }
});
document.querySelector('#refresh').addEventListener('click', refresh); refresh();