diff --git a/public/js/views/ticketList.js b/public/js/views/ticketList.js index 73d2145..838065d 100644 --- a/public/js/views/ticketList.js +++ b/public/js/views/ticketList.js @@ -118,6 +118,7 @@ const TicketListView = { ` : ''} + @@ -310,6 +311,12 @@ const TicketListView = { batchApply.addEventListener('click', () => this.applyBatch()); } + // Batch add to group + const batchAddGroup = document.getElementById('batch-add-group'); + if (batchAddGroup) { + batchAddGroup.addEventListener('click', () => this.addToGroup()); + } + // Batch merge (Issue #7) const batchMerge = document.getElementById('batch-merge'); if (batchMerge) { @@ -597,6 +604,12 @@ const TicketListView = { count.textContent = `${this.selectedIds.size} selezionat${this.selectedIds.size === 1 ? 'o' : 'i'}`; } + // Enable/disable add to group button + const addGroupBtn = document.getElementById('batch-add-group'); + if (addGroupBtn) { + addGroupBtn.disabled = this.selectedIds.size === 0; + } + // Enable/disable merge button const mergeBtn = document.getElementById('batch-merge'); if (mergeBtn) { @@ -714,10 +727,86 @@ const TicketListView = { Toast.success(res.message || 'Ticket uniti con successo'); this.selectedIds.clear(); this.selectedOrder = []; - this.render(); } catch (err) { Toast.error('Errore durante l\'unione: ' + err.message); this.updateBatchBar(); } }, + + async addToGroup() { + if (this.selectedIds.size === 0) return; + + try { + const groups = await App.api('/api/groups'); + if (!groups || groups.length === 0) { + Toast.warning('non sono presenti gruppi'); + return; + } + + // Show group selection prompt/dialog + const groupOptions = groups.map(g => ``).join(''); + const dialogHtml = ` +
+
+

Aggiungi a Gruppo

+

Seleziona il gruppo a cui aggiungere i ${this.selectedIds.size} ticket selezionati:

+ +
+ + +
+
+
+ `; + + // Append modal to body + const modalContainer = document.createElement('div'); + modalContainer.innerHTML = dialogHtml; + document.body.appendChild(modalContainer); + + const closeModal = () => modalContainer.remove(); + + document.getElementById('batch-group-cancel').addEventListener('click', closeModal); + document.getElementById('batch-group-confirm').addEventListener('click', async () => { + const groupId = document.getElementById('batch-group-select').value; + if (!groupId) return; + + const confirmBtn = document.getElementById('batch-group-confirm'); + confirmBtn.disabled = true; + confirmBtn.textContent = 'Aggiunta...'; + + let addedCount = 0; + let errorsCount = 0; + + for (const ticketId of this.selectedIds) { + try { + await App.api(`/api/groups/${groupId}/tickets`, { + method: 'POST', + body: JSON.stringify({ ticket_identifier: ticketId }) + }); + addedCount++; + } catch (err) { + // Conflict (already in group) or other errors + errorsCount++; + } + } + + closeModal(); + + if (addedCount > 0) { + Toast.success(`${addedCount} ticket aggiunti al gruppo!`); + this.selectedIds.clear(); + this.selectedOrder = []; + this.render(); + } else if (errorsCount > 0) { + Toast.warning('I ticket selezionati appartengono giĆ  a questo gruppo.'); + } + }); + + } catch (err) { + Toast.error('Errore durante il recupero dei gruppi: ' + err.message); + } + }, };