addition API BV
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<!--favicon-->
|
||||
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
|
||||
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
||||
<?php include('cssinclude.php'); ?>
|
||||
<title>Insert XLS Template <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?></title>
|
||||
</head>
|
||||
@@ -98,7 +99,14 @@
|
||||
<label class="form-label">Button Label</label>
|
||||
<input type="text" name="button_label" class="form-control" value="Click Me">
|
||||
</div>
|
||||
|
||||
<!-- Aggiungi il campo per selezionare il cliente -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Select Client *</label>
|
||||
<select name="client_id" id="clientSelect" class="form-control" required>
|
||||
<option value="">Select a client...</option>
|
||||
</select>
|
||||
<span id="clientLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Recupero clienti in corso...</span>
|
||||
</div>
|
||||
<!-- new section for specific client field -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Client-Specific Fields</label>
|
||||
@@ -183,7 +191,11 @@
|
||||
<?php //include('include/themeswitcher.php');
|
||||
?>
|
||||
<!--end switcher-->
|
||||
|
||||
<!-- Includi Select2 JS -->
|
||||
|
||||
<?php include('jsinclude.php'); ?>
|
||||
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
||||
<script>
|
||||
// Debug iniziale
|
||||
console.log("JavaScript is loaded and running!");
|
||||
@@ -194,18 +206,98 @@
|
||||
const form = document.getElementById("insertTemplateForm");
|
||||
const addFieldButton = document.getElementById("addField");
|
||||
const container = document.getElementById("clientSpecificFields");
|
||||
const clientLoadingStatus = document.getElementById("clientLoadingStatus");
|
||||
|
||||
if (!form || !addFieldButton || !container) {
|
||||
if (!form || !addFieldButton || !container || !clientLoadingStatus) {
|
||||
console.error("One or more DOM elements not found:", {
|
||||
form,
|
||||
addFieldButton,
|
||||
container
|
||||
container,
|
||||
clientLoadingStatus
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("All DOM elements found");
|
||||
|
||||
// Controllo che jQuery sia caricato
|
||||
if (typeof jQuery === 'undefined') {
|
||||
console.error("jQuery non è caricato!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Inizializza Select2 sulla tendina dei clienti
|
||||
$('#clientSelect').select2({
|
||||
placeholder: "Search for a client...",
|
||||
allowClear: true
|
||||
}).on('select2:open', function() {
|
||||
console.log("Select2 initialized successfully");
|
||||
}).on('select2:select', function(e) {
|
||||
console.log("Client selected:", e.params.data.id, e.params.data.text);
|
||||
});
|
||||
|
||||
// Carica i clienti al caricamento della pagina
|
||||
async function loadClients() {
|
||||
try {
|
||||
// Mostra il messaggio di caricamento
|
||||
clientLoadingStatus.style.display = 'inline';
|
||||
|
||||
const response = await fetch("get_clienti.php", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
console.log("Risposta raw (clienti):", text);
|
||||
const data = JSON.parse(text);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `Errore HTTP: ${response.status}, Dettagli: ${JSON.stringify(data)}`);
|
||||
}
|
||||
|
||||
if (data.value && Array.isArray(data.value)) {
|
||||
const select = document.getElementById("clientSelect");
|
||||
select.innerHTML = '<option value="">Select a client...</option>'; // Resetta la tendina
|
||||
data.value.forEach(client => {
|
||||
const nome = client.Nominativo || "Nome non disponibile";
|
||||
const id = client.IdCliente || "ID non disponibile";
|
||||
const option = new Option(`${nome.trim()} (ID: ${id})`, id);
|
||||
select.add(option);
|
||||
});
|
||||
console.log("Clienti caricati con successo.");
|
||||
clientLoadingStatus.textContent = "Clienti caricati.";
|
||||
} else {
|
||||
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.",
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Errore nel caricamento dei clienti:", error);
|
||||
clientLoadingStatus.textContent = "Errore nel caricamento.";
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "Failed to load clients: " + 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();
|
||||
|
||||
// Gestione dinamica dei campi specifici
|
||||
addFieldButton.addEventListener("click", function() {
|
||||
console.log("Add Field button clicked");
|
||||
@@ -292,10 +384,39 @@
|
||||
|
||||
let formData = new FormData(this);
|
||||
|
||||
// Aggiungi il nome del cliente selezionato a FormData
|
||||
const clientSelect = document.getElementById("clientSelect");
|
||||
const clientId = clientSelect.value;
|
||||
const selectedOption = clientSelect.options[clientSelect.selectedIndex];
|
||||
|
||||
// Validazione: assicurati che un cliente sia selezionato
|
||||
if (!clientId) {
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "Please select a client.",
|
||||
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);
|
||||
|
||||
// Genera il JSON per client_specific_fields
|
||||
let finalSpecificFields = {};
|
||||
|
||||
// Raccolta dei dati direttamente dal DOM
|
||||
const fieldRows = container.getElementsByClassName("client-field-row");
|
||||
for (let i = 0; i < fieldRows.length; i++) {
|
||||
const row = fieldRows[i];
|
||||
|
||||
Reference in New Issue
Block a user