added schema details
This commit is contained in:
@@ -107,6 +107,15 @@
|
||||
</select>
|
||||
<span id="clientLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Recupero clienti in corso...</span>
|
||||
</div>
|
||||
<!-- Aggiungi il campo per selezionare lo schema -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Select Schema *</label>
|
||||
<select name="schema_id" id="schemaSelect" class="form-control" required>
|
||||
<option value="">Select a schema...</option>
|
||||
</select>
|
||||
<span id="schemaLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Loading schemas...</span>
|
||||
</div>
|
||||
|
||||
<!-- new section for specific client field -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Client-Specific Fields</label>
|
||||
@@ -207,13 +216,15 @@
|
||||
const addFieldButton = document.getElementById("addField");
|
||||
const container = document.getElementById("clientSpecificFields");
|
||||
const clientLoadingStatus = document.getElementById("clientLoadingStatus");
|
||||
const schemaLoadingStatus = document.getElementById("schemaLoadingStatus");
|
||||
|
||||
if (!form || !addFieldButton || !container || !clientLoadingStatus) {
|
||||
if (!form || !addFieldButton || !container || !clientLoadingStatus || !schemaLoadingStatus) {
|
||||
console.error("One or more DOM elements not found:", {
|
||||
form,
|
||||
addFieldButton,
|
||||
container,
|
||||
clientLoadingStatus
|
||||
clientLoadingStatus,
|
||||
schemaLoadingStatus
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -231,16 +242,24 @@
|
||||
placeholder: "Search for a client...",
|
||||
allowClear: true
|
||||
}).on('select2:open', function() {
|
||||
console.log("Select2 initialized successfully");
|
||||
console.log("Select2 initialized successfully for clientSelect");
|
||||
}).on('select2:select', function(e) {
|
||||
console.log("Client selected:", e.params.data.id, e.params.data.text);
|
||||
});
|
||||
|
||||
// Carica i clienti al caricamento della pagina
|
||||
// Inizializza Select2 sulla tendina degli schemi
|
||||
$('#schemaSelect').select2({
|
||||
placeholder: "Search for a schema...",
|
||||
allowClear: true
|
||||
}).on('select2:open', function() {
|
||||
console.log("Select2 initialized successfully for schemaSelect");
|
||||
});
|
||||
|
||||
// Funzione per caricare i clienti
|
||||
async function loadClients() {
|
||||
try {
|
||||
// Mostra il messaggio di caricamento
|
||||
clientLoadingStatus.style.display = 'inline';
|
||||
clientLoadingStatus.textContent = 'Recupero clienti in corso...';
|
||||
|
||||
const response = await fetch("get_clienti.php", {
|
||||
method: "GET",
|
||||
@@ -259,7 +278,7 @@
|
||||
|
||||
if (data.value && Array.isArray(data.value)) {
|
||||
const select = document.getElementById("clientSelect");
|
||||
select.innerHTML = '<option value="">Select a client...</option>'; // Resetta la tendina
|
||||
select.innerHTML = '<option value="">Select a client...</option>';
|
||||
data.value.forEach(client => {
|
||||
const nome = client.Nominativo || "Nome non disponibile";
|
||||
const id = client.IdCliente || "ID non disponibile";
|
||||
@@ -272,8 +291,8 @@
|
||||
console.error("Nessun cliente trovato o formato dati non valido.", data);
|
||||
clientLoadingStatus.textContent = "Nessun cliente trovato.";
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "No clients found or invalid data format.",
|
||||
title: "Errore!",
|
||||
text: "Nessun cliente trovato o formato dati non valido.",
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
@@ -282,21 +301,90 @@
|
||||
console.error("Errore nel caricamento dei clienti:", error);
|
||||
clientLoadingStatus.textContent = "Errore nel caricamento.";
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "Failed to load clients: " + error.message,
|
||||
title: "Errore!",
|
||||
text: "Impossibile caricare i clienti: " + error.message,
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
} finally {
|
||||
// Nasconde il messaggio dopo un breve ritardo per leggibilità
|
||||
setTimeout(() => {
|
||||
clientLoadingStatus.style.display = 'none';
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
// Chiama la funzione per caricare i clienti
|
||||
loadClients();
|
||||
// Funzione per caricare gli schemi con ritentativi
|
||||
async function loadSchemas() {
|
||||
const maxRetries = 3;
|
||||
let attempt = 0;
|
||||
|
||||
while (attempt < maxRetries) {
|
||||
try {
|
||||
schemaLoadingStatus.style.display = 'inline';
|
||||
schemaLoadingStatus.textContent = 'Caricamento schemi in corso...';
|
||||
|
||||
const response = await fetch("get_schemi.php", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
console.log("Risposta raw (schemi):", text);
|
||||
const data = JSON.parse(text);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `Errore HTTP: ${response.status}, Dettagli: ${JSON.stringify(data)}`);
|
||||
}
|
||||
|
||||
const select = document.getElementById("schemaSelect");
|
||||
select.innerHTML = '<option value="">Select a schema...</option>';
|
||||
data.value.forEach(schema => { // Nota: usa data.value per coerenza con il JSON restituito
|
||||
const option = new Option(`${schema.Nome} (ID: ${schema.IdSchemaCustomFields})`, schema.IdSchemaCustomFields);
|
||||
select.add(option);
|
||||
});
|
||||
|
||||
schemaLoadingStatus.textContent = "Schemi caricati.";
|
||||
break; // Esci dal ciclo se la chiamata ha successo
|
||||
} catch (error) {
|
||||
attempt++;
|
||||
console.error(`Tentativo ${attempt} fallito per schemi:`, error);
|
||||
if (attempt === maxRetries) {
|
||||
console.error("Errore finale nel caricamento degli schemi:", error);
|
||||
schemaLoadingStatus.textContent = "Errore nel caricamento.";
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Impossibile caricare gli schemi: " + error.message,
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
} else {
|
||||
// Ritardo prima di riprovare
|
||||
await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
|
||||
}
|
||||
} finally {
|
||||
if (attempt === maxRetries || schemaLoadingStatus.textContent === "Schemi caricati.") {
|
||||
setTimeout(() => {
|
||||
schemaLoadingStatus.style.display = 'none';
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione combinata per caricare i dati in sequenza
|
||||
async function loadData() {
|
||||
try {
|
||||
await loadClients(); // Carica prima i clienti
|
||||
await loadSchemas(); // Poi carica gli schemi
|
||||
} catch (error) {
|
||||
console.error("Errore nel caricamento dei dati:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Avvia il caricamento dei dati
|
||||
loadData();
|
||||
|
||||
// Gestione dinamica dei campi specifici
|
||||
addFieldButton.addEventListener("click", function() {
|
||||
@@ -387,36 +475,44 @@
|
||||
// Aggiungi il nome del cliente selezionato a FormData
|
||||
const clientSelect = document.getElementById("clientSelect");
|
||||
const clientId = clientSelect.value;
|
||||
const selectedOption = clientSelect.options[clientSelect.selectedIndex];
|
||||
const selectedClientOption = clientSelect.options[clientSelect.selectedIndex];
|
||||
let clientName = "";
|
||||
if (selectedClientOption) {
|
||||
const optionText = selectedClientOption.text.trim();
|
||||
const nameMatch = optionText.match(/^(.+?)(?:\s*\(ID:\s*\d+\))?$/);
|
||||
clientName = nameMatch ? nameMatch[1].trim() : optionText;
|
||||
}
|
||||
formData.append("client_name", clientName);
|
||||
|
||||
// Validazione: assicurati che un cliente sia selezionato
|
||||
if (!clientId) {
|
||||
// Aggiungi l'ID e il nome dello schema selezionato a FormData
|
||||
const schemaSelect = document.getElementById("schemaSelect");
|
||||
const schemaId = schemaSelect.value;
|
||||
const selectedSchemaOption = schemaSelect.options[schemaSelect.selectedIndex];
|
||||
let schemaName = "";
|
||||
if (selectedSchemaOption && schemaId) {
|
||||
const optionText = selectedSchemaOption.text.trim();
|
||||
const nameMatch = optionText.match(/^(.+?)(?:\s*\(ID:\s*\d+\))?$/);
|
||||
schemaName = nameMatch ? nameMatch[1].trim() : optionText;
|
||||
formData.append("idschema", schemaId);
|
||||
formData.append("schemamaname", schemaName);
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "Please select a client.",
|
||||
title: "Errore!",
|
||||
text: "Per favore seleziona uno schema.",
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Estrai il nome del cliente in modo più robusto
|
||||
let clientName = "";
|
||||
if (selectedOption) {
|
||||
const optionText = selectedOption.text.trim();
|
||||
const nameMatch = optionText.match(/^(.+?)(?:\s*\(ID:\s*\d+\))?$/);
|
||||
clientName = nameMatch ? nameMatch[1].trim() : optionText;
|
||||
}
|
||||
|
||||
formData.append("client_name", clientName);
|
||||
|
||||
// Log per debug
|
||||
console.log("Client ID:", clientId);
|
||||
console.log("Client Name:", clientName);
|
||||
console.log("Schema ID:", schemaId);
|
||||
console.log("Schema Name:", schemaName);
|
||||
|
||||
// Genera il JSON per client_specific_fields
|
||||
let finalSpecificFields = {};
|
||||
|
||||
const fieldRows = container.getElementsByClassName("client-field-row");
|
||||
for (let i = 0; i < fieldRows.length; i++) {
|
||||
const row = fieldRows[i];
|
||||
@@ -444,8 +540,6 @@
|
||||
}
|
||||
|
||||
console.log("Generated JSON for client_specific_fields:", JSON.stringify(finalSpecificFields));
|
||||
|
||||
// Aggiungi il JSON al FormData
|
||||
formData.append("client_specific_fields", JSON.stringify(finalSpecificFields));
|
||||
|
||||
// Debug del FormData
|
||||
@@ -463,8 +557,8 @@
|
||||
console.log("Fetch response:", data);
|
||||
if (data.success) {
|
||||
Swal.fire({
|
||||
title: "Success!",
|
||||
text: "Template created successfully!",
|
||||
title: "Successo!",
|
||||
text: "Template creato con successo!",
|
||||
icon: "success",
|
||||
confirmButtonText: "OK"
|
||||
}).then(() => {
|
||||
@@ -472,7 +566,7 @@
|
||||
});
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
title: "Errore!",
|
||||
text: data.message,
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
@@ -480,10 +574,10 @@
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Fetch error:", error);
|
||||
console.error("Errore Fetch:", error);
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "An unexpected error occurred.",
|
||||
title: "Errore!",
|
||||
text: "Si è verificato un errore imprevisto.",
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user