2025-11-17 17:50:42 +01:00

417 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php include('include/headscript.php'); ?>
<!doctype html>
<html lang="it">
<head>
<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" />
<?php include('cssinclude.php'); ?>
<title>Gestione Matrici - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?></title>
<!-- jQuery, Bootstrap, SweetAlert -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<!-- Select2 -->
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.full.min.js"></script>
<style>
body {
font-size: 1.05rem;
background: #f8fafc;
}
.card {
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
.back-dashboard {
background-color: #b9ebc7 !important;
color: #1f2d3d !important;
border: 1px solid #a1d7b4 !important;
border-radius: 10px;
font-weight: 600;
font-size: 1rem;
padding: 10px 18px;
box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1);
transition: all 0.2s ease-in-out;
}
.back-dashboard:hover {
background-color: #a1dfb1 !important;
transform: translateY(-2px);
}
.btn-add {
background-color: #198754;
color: #fff;
border-radius: 8px;
padding: 10px 20px;
font-weight: 500;
transition: all 0.2s ease-in-out;
}
.btn-add:hover {
background-color: #157347;
transform: scale(1.02);
}
.table thead {
background-color: #b9ebc7;
color: #1f2d3d;
text-align: center;
}
.table tbody td {
vertical-align: middle;
}
.modal-content {
border-radius: 16px;
}
.action-btn {
border: none;
background: none;
cursor: pointer;
font-size: 1.2rem;
margin: 0 4px;
}
.action-btn.edit {
color: #0d6efd;
}
.action-btn.delete {
color: #dc3545;
}
.action-btn.linee {
color: #198754;
}
.action-btn.mescole {
color: #ff9800;
}
.dataTables_filter input {
border-radius: 8px;
padding: 6px 10px;
border: 1px solid #ced4da;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card p-3">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Gestione Matrici</h5>
<button type="button" class="btn back-dashboard" onclick="location.href='production_dashboard.php'">
↩️ Torna alla Dashboard
</button>
</div>
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
<h6 class="fw-semibold mb-0">Elenco Completo</h6>
<button class="btn btn-add" data-bs-toggle="modal" data-bs-target="#addMatriceModal"> Aggiungi Matrice</button>
</div>
<!-- TABELLA -->
<div class="table-responsive">
<table id="tabellaMatrici" class="table table-striped align-middle text-center">
<thead>
<tr>
<th>ID</th>
<th>Nome Matrice</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$stmt = $pdo->query("SELECT * FROM matrice ORDER BY id DESC");
if ($stmt->rowCount() === 0) {
echo "<tr><td colspan='3' class='text-muted'>Nessuna matrice presente</td></tr>";
} else {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>{$row['id']}</td>
<td>" . htmlspecialchars($row['nome']) . "</td>
<td>
<button class='action-btn edit' data-id='{$row['id']}' data-nome='" . htmlspecialchars($row['nome'], ENT_QUOTES) . "'><i class='fa-solid fa-pen-to-square'></i></button>
<button class='action-btn delete' data-id='{$row['id']}'><i class='fa-solid fa-trash'></i></button>
<button class='action-btn linee' data-id='{$row['id']}' data-nome='" . htmlspecialchars($row['nome'], ENT_QUOTES) . "'><i class='fa-solid fa-sitemap'></i></button>
<button class='action-btn mescole' data-id='{$row['id']}' data-nome='" . htmlspecialchars($row['nome'], ENT_QUOTES) . "'><i class='fa-solid fa-flask'></i></button>
</td>
</tr>";
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<!-- MODALE AGGIUNTA / MODIFICA -->
<div class="modal fade" id="addMatriceModal" tabindex="-1" aria-labelledby="addMatriceLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background-color:#b9ebc7;">
<h5 class="modal-title" id="addMatriceLabel">Aggiungi / Modifica Matrice</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="addMatriceForm">
<input type="hidden" id="idMatriceEdit">
<div class="mb-3">
<label for="nomeMatrice" class="form-label fw-semibold">Nome Matrice</label>
<input type="text" class="form-control" id="nomeMatrice" name="nomeMatrice" required>
</div>
<div class="text-center">
<button type="submit" class="btn btn-add">💾 Salva</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- MODALE ASSOCIA LINEE -->
<div class="modal fade" id="associaLineeModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background-color:#b9ebc7;">
<h5 class="modal-title">Associa Linee</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="associaLineeForm">
<input type="hidden" id="idMatriceLinee">
<div class="mb-3">
<label class="form-label fw-semibold">Linee di Produzione</label>
<select id="lineeSelect" name="linee[]" class="form-select" multiple required style="width:100%;"></select>
</div>
<div class="text-center">
<button type="submit" class="btn btn-add">💾 Salva Associazioni</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- MODALE ASSOCIA MESCOLE -->
<div class="modal fade" id="associaMescoleModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background-color:#b9ebc7;">
<h5 class="modal-title">Associa Mescole</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="associaMescoleForm">
<input type="hidden" id="idMatriceMescole">
<div class="mb-3">
<label class="form-label fw-semibold">Mescole</label>
<select id="mescoleSelect" name="mescole[]" class="form-select" multiple required style="width:100%;"></select>
</div>
<div class="text-center">
<button type="submit" class="btn btn-add">💾 Salva Associazioni</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include('jsinclude.php'); ?>
<script>
$(document).ready(function() {
const table = $('#tabellaMatrici').DataTable({
order: [
[0, 'desc']
],
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
}
});
// === ADD / EDIT MATRICE ===
$(document).on('click', '.edit', function() {
$('#idMatriceEdit').val($(this).data('id'));
$('#nomeMatrice').val($(this).data('nome'));
$('#addMatriceModal').modal('show');
});
$('#addMatriceForm').on('submit', function(e) {
e.preventDefault();
const id = $('#idMatriceEdit').val();
const nome = $('#nomeMatrice').val().trim();
if (nome === "") return;
fetch('save_matrice.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'id=' + id + '&nome=' + encodeURIComponent(nome)
}).then(r => r.json()).then(data => {
Swal.fire({
icon: data.success ? 'success' : 'error',
title: data.success ? 'Salvata!' : 'Errore',
text: data.message || (data.success ? 'Matrice salvata correttamente.' : 'Errore durante il salvataggio.')
}).then(() => location.reload());
});
});
// === DELETE MATRICE ===
$(document).on('click', '.delete', function() {
const id = $(this).data('id');
Swal.fire({
icon: 'warning',
title: 'Eliminare questa matrice?',
text: 'L\'operazione non è reversibile.',
showCancelButton: true,
confirmButtonText: 'Sì, elimina',
cancelButtonText: 'Annulla',
confirmButtonColor: '#d33'
}).then(res => {
if (res.isConfirmed) {
fetch('delete_matrice.php?id=' + id)
.then(r => r.json())
.then(data => {
Swal.fire({
icon: data.success ? 'success' : 'error',
title: data.success ? 'Eliminata!' : 'Errore',
text: data.message || ''
}).then(() => location.reload());
});
}
});
});
// === ASSOCIA LINEE ===
$(document).on('click', '.linee', function() {
const id = $(this).data('id');
const nome = $(this).data('nome');
$('#idMatriceLinee').val(id);
$('.modal-title').text('Associa Linee a ' + nome);
fetch('get_linee_matrice.php?id=' + id)
.then(r => r.json())
.then(data => {
const sel = $('#lineeSelect');
sel.empty();
data.tutte.forEach(l => {
const selected = data.associate.includes(l.id.toString()) ? 'selected' : '';
sel.append(`<option value="${l.id}" ${selected}>${l.name} (${l.brand || ''})</option>`);
});
sel.select2({
theme: 'bootstrap-5',
width: '100%'
});
$('#associaLineeModal').modal('show');
});
});
$('#associaLineeForm').on('submit', e => {
e.preventDefault();
const id = $('#idMatriceLinee').val();
const linee = $('#lineeSelect').val();
fetch('save_matrice_linee.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id,
linee
})
}).then(r => r.json()).then(d => {
Swal.fire({
icon: d.success ? 'success' : 'error',
title: d.success ? 'Salvato!' : 'Errore',
text: d.message || ''
}).then(() => location.reload());
});
});
// === ASSOCIA MESCOLE ===
$(document).on('click', '.mescole', function() {
const id = $(this).data('id');
const nome = $(this).data('nome');
$('#idMatriceMescole').val(id);
$('#associaMescoleModal .modal-title').text('Associa Mescole a ' + nome);
fetch('get_mescole_matrice.php?id=' + id)
.then(r => r.json())
.then(data => {
const sel = $('#mescoleSelect');
sel.empty();
data.tutte.forEach(m => {
const selected = data.associate.includes(m.id.toString()) ? 'selected' : '';
sel.append(`<option value="${m.id}" ${selected}>${m.nome}</option>`);
});
sel.select2({
theme: 'bootstrap-5',
width: '100%'
});
$('#associaMescoleModal').modal('show');
});
});
$('#associaMescoleForm').on('submit', e => {
e.preventDefault();
const id = $('#idMatriceMescole').val();
const mescole = $('#mescoleSelect').val();
fetch('save_matrice_mescole.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id,
mescole
})
}).then(r => r.json()).then(d => {
Swal.fire({
icon: d.success ? 'success' : 'error',
title: d.success ? 'Salvato!' : 'Errore',
text: d.message || ''
}).then(() => location.reload());
});
});
});
</script>
</body>
</html>