diff --git a/public/userarea/lookup_values.php b/public/userarea/lookup_values.php index 1025711..fd1cd02 100644 --- a/public/userarea/lookup_values.php +++ b/public/userarea/lookup_values.php @@ -496,14 +496,41 @@ function h($v) } }); - // Select2 for category filter + modal selects - $('#categoryFilter, #add_category, #edit_category').select2({ + // Select2 solo per il filtro categoria (fuori dai modali) + $('#categoryFilter').select2({ theme: 'bootstrap4', width: '100%', placeholder: '-- Seleziona --', allowClear: true }); + // Select2 per il modale ADD: inizializza quando il modale è visibile + // e aggancia il dropdown dentro il modale (altrimenti le opzioni non si vedono) + $('#addModal').on('shown.bs.modal', function() { + if (!$('#add_category').data('select2')) { + $('#add_category').select2({ + theme: 'bootstrap4', + width: '100%', + placeholder: '-- Seleziona --', + allowClear: true, + dropdownParent: $('#addModal') + }); + } + }); + + // Select2 per il modale EDIT: stessa logica + $('#editModal').on('shown.bs.modal', function() { + if (!$('#edit_category').data('select2')) { + $('#edit_category').select2({ + theme: 'bootstrap4', + width: '100%', + placeholder: '-- Seleziona --', + allowClear: true, + dropdownParent: $('#editModal') + }); + } + }); + // Filter redirect $('#categoryFilter').on('change', function() { const v = $(this).val() || ''; diff --git a/public/userarea/manage-worksheet.php b/public/userarea/manage-worksheet.php index 3eaa1e7..ea5e554 100644 --- a/public/userarea/manage-worksheet.php +++ b/public/userarea/manage-worksheet.php @@ -365,18 +365,13 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { } } -// Load packaging items from dedicated table -$packagingLookup = [ - 'PACKAGING_TYPE' => [], - 'BOX' => [], - 'PALLET' => [] -]; +// Load ALL packaging items (tutte le categorie, distinct via GROUP nel dropdown) +$packagingByCategory = []; // categoria => [ {value, label}, ... ] $stmt = $pdo->prepare(" - SELECT category, item_name, item_code + SELECT id, category, item_name, item_code FROM packaging_items WHERE is_active = 1 - AND category IN ('PACKAGING_TYPE', 'BOX', 'PALLET') ORDER BY category ASC, item_name ASC "); $stmt->execute(); @@ -387,12 +382,20 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { $label .= ' — ' . $r['item_code']; } - $packagingLookup[$r['category']][] = [ - 'value' => $r['item_code'], + $packagingByCategory[$r['category']][] = [ + 'value' => (int)$r['id'], // salviamo l'ID 'label' => $label ]; } +// Elenco categorie distinte (per il primo dropdown) +$packagingCategories = array_keys($packagingByCategory); +sort($packagingCategories); + +// Questi verranno calcolati DOPO il caricamento del worksheet +$savedPackageCode = ''; +$savedPackageCategory = ''; + // Load mescole dropdown for modal $mescole = $pdo->query(" SELECT id, nome, nomeuscita @@ -432,6 +435,19 @@ if ($worksheet_id > 0) { } } +// Ora che $worksheet è caricato, ricava categoria + codice salvati per l'imballo +$savedPackageCode = $worksheet['requested_package_code'] ?? ''; +if ($savedPackageCode !== '' && $savedPackageCode !== null) { + foreach ($packagingByCategory as $cat => $items) { + foreach ($items as $it) { + if ((string)$it['value'] === (string)$savedPackageCode) { + $savedPackageCategory = $cat; + break 2; + } + } + } +} + // Helpers function h($v) { @@ -1045,14 +1061,28 @@ $isEdit = ($worksheet_id > 0);