let _qCtx = null;
function openQuotaModal(type, name, path, validUsers) {
_qCtx = { type, name, path, users: validUsers ? validUsers.split(/[\s,]+/).filter(Boolean) : [] };
document.getElementById('quotaModalTitle').textContent =
'Quota — ' + (type === 'smb' ? esc(name) : esc(path));
let body = '';
if (type === 'smb' && _qCtx.users.length > 0) {
body = _qCtx.users.map(u => `
`).join('');
} else {
const note = type === 'nfs'
? `Path: ${esc(path)}
`
: `No valid users on this share — enter a username manually.
`;
body = note + `
`;
}
document.getElementById('quotaModalBody').innerHTML = body;
new bootstrap.Modal(document.getElementById('quotaModal')).show();
}
async function submitQuota() {
if (!_qCtx) return;
const calls = [];
if (_qCtx.type === 'smb' && _qCtx.users.length > 0) {
for (const inp of document.querySelectorAll('.quota-inp')) {
const gb = parseInt(inp.value);
if (!gb || gb <= 0) continue;
calls.push({ user: inp.dataset.user, quota: gb, path: _qCtx.path });
}
} else {
const user = document.getElementById('qManualUser')?.value.trim();
const gb = parseInt(document.getElementById('qManualGB')?.value);
if (!user) return showToast('Username is required.', 'danger');
if (!gb || gb <= 0) return showToast('Quota must be a positive number.', 'danger');
calls.push({ user, path: _qCtx.path || '/srv', quota: gb });
}
if (!calls.length) return showToast('No quota values entered.', 'warning');
bootstrap.Modal.getInstance(document.getElementById('quotaModal')).hide();
const responses = await Promise.all(calls.map(body =>
apiFetch('/api/quota', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(body) })
));
const errors = [];
for (const r of responses) {
if (r && !r.ok) {
const d = await r.json().catch(() => ({}));
errors.push(d.error || `HTTP ${r.status}`);
}
}
if (errors.length) showToast('Quota error: ' + errors.join('; '), 'danger');
else showToast(`Quota${calls.length > 1 ? 's' : ''} applied.`);
loadDashboard();
}