addition API BV

This commit is contained in:
2025-06-05 10:11:57 +02:00
parent d925726ecd
commit e8b15d8096
22 changed files with 1335 additions and 470 deletions
+166 -34
View File
@@ -39,6 +39,7 @@ if (json_last_error() !== JSON_ERROR_NONE) {
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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'); ?>
<style>
.client-field-row .row {
@@ -123,7 +124,15 @@ if (json_last_error() !== JSON_ERROR_NONE) {
<label class="form-label"><?= htmlspecialchars($desttable, ENT_QUOTES, 'UTF-8'); ?>*</label>
<input type="text" name="target_table" class="form-control" value="<?php echo htmlspecialchars($template['target_table']); ?>" readonly required>
</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>
<!-- Le opzioni verranno popolate tramite JavaScript -->
</select>
<span id="clientLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Recupero clienti in corso...</span>
</div>
<!-- Sezione per i campi specifici del cliente -->
<div class="mb-3">
<label class="form-label">Client-Specific Fields</label>
@@ -227,23 +236,116 @@ if (json_last_error() !== JSON_ERROR_NONE) {
<!--end switcher-->
<!-- Temporaneamente disabilitato jsinclude.php per test -->
<!-- <?php include('jsinclude.php'); ?> -->
<!-- Includi jQuery e Select2 -->
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
// Dati del cliente associato al template
const templateClientId = <?php echo json_encode($template['idclient'] ?? 0); ?>;
</script>
<script>
document.addEventListener("DOMContentLoaded", function() {
const form = document.getElementById("editTemplateForm");
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);
// Pre-seleziona il cliente associato al template
if (parseInt(id) === parseInt(templateClientId)) {
option.selected = true;
}
select.add(option);
});
// Forza l'aggiornamento di Select2
$(select).trigger('change');
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();
// Debug iniziale del DOM
const debugDom = () => {
const initialRows = container.getElementsByClassName("client-field-row");
@@ -300,38 +402,38 @@ if (json_last_error() !== JSON_ERROR_NONE) {
const newField = document.createElement("div");
newField.className = "client-field-row mb-2";
newField.innerHTML = `
<div class="row align-items-center">
<div class="col-md-3">
<input type="text" name="specific_fields[${fieldCount}][name]" class="form-control" placeholder="Field Name (e.g., SKU)">
</div>
<div class="col-md-2">
<select name="specific_fields[${fieldCount}][type]" class="form-control" onchange="toggleDropdownValues(this)">
<option value="text">Text</option>
<option value="dropdown">Dropdown</option>
<option value="date">Date</option>
<option value="boolean">Yes/No</option>
</select>
</div>
<div class="col-md-2 dropdown-values" style="visibility: hidden;">
<input type="text" name="specific_fields[${fieldCount}][possible_values]" class="form-control" placeholder="Values (e.g., Red, Blue, Green)">
</div>
<div class="col-md-1">
<select name="specific_fields[${fieldCount}][required]" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
<div class="col-md-2">
<input type="text" name="specific_fields[${fieldCount}][export_column_name]" class="form-control" placeholder="Export Column Name (e.g., MONCLER_SKU)">
</div>
<div class="col-md-1">
<input type="text" name="specific_fields[${fieldCount}][default_value]" class="form-control" placeholder="Default Value (optional)">
</div>
<div class="col-md-1">
<button type="button" class="btn btn-danger remove-field">-</button>
</div>
<div class="row align-items-center">
<div class="col-md-3">
<input type="text" name="specific_fields[${fieldCount}][name]" class="form-control" placeholder="Field Name (e.g., SKU)">
</div>
`;
<div class="col-md-2">
<select name="specific_fields[${fieldCount}][type]" class="form-control" onchange="toggleDropdownValues(this)">
<option value="text">Text</option>
<option value="dropdown">Dropdown</option>
<option value="date">Date</option>
<option value="boolean">Yes/No</option>
</select>
</div>
<div class="col-md-2 dropdown-values" style="visibility: hidden;">
<input type="text" name="specific_fields[${fieldCount}][possible_values]" class="form-control" placeholder="Values (e.g., Red, Blue, Green)">
</div>
<div class="col-md-1">
<select name="specific_fields[${fieldCount}][required]" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
<div class="col-md-2">
<input type="text" name="specific_fields[${fieldCount}][export_column_name]" class="form-control" placeholder="Export Column Name (e.g., MONCLER_SKU)">
</div>
<div class="col-md-1">
<input type="text" name="specific_fields[${fieldCount}][default_value]" class="form-control" placeholder="Default Value (optional)">
</div>
<div class="col-md-1">
<button type="button" class="btn btn-danger remove-field">-</button>
</div>
</div>
`;
container.appendChild(newField);
newField.querySelector(".remove-field").addEventListener("click", function() {
@@ -383,6 +485,36 @@ if (json_last_error() !== JSON_ERROR_NONE) {
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 = {};