added params crud lines

This commit is contained in:
2025-12-03 14:07:11 +01:00
parent 9447f3cf00
commit 329b3ffdeb
6 changed files with 607 additions and 17 deletions
+381
View File
@@ -0,0 +1,381 @@
<?php
include('include/headscript.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Read line id from GET
$lineId = isset($_GET['line_id']) ? (int)$_GET['line_id'] : 0;
if ($lineId <= 0) {
die("Invalid line id");
}
// Load line data
$stmtLine = $pdo->prepare("SELECT * FROM production_lines WHERE id = :id");
$stmtLine->execute([':id' => $lineId]);
$line = $stmtLine->fetch(PDO::FETCH_ASSOC);
if (!$line) {
die("Production line not found");
}
// Load parameters for this line
$stmtParams = $pdo->prepare("
SELECT *
FROM production_line_params
WHERE line_id = :line_id
ORDER BY position ASC, id ASC
");
$stmtParams->execute([':line_id' => $lineId]);
$params = $stmtParams->fetchAll(PDO::FETCH_ASSOC);
?>
<!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>Parametri Linea <?= htmlspecialchars($line['name']) ?> - <?= 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>
<style>
body {
font-size: 1.05rem;
background: #f8fafc;
}
.card {
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
th {
text-align: center !important;
}
.back-lines {
background-color: #cfe3ff !important;
color: #1f2d3d !important;
border: 1px solid #bcd4f4 !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-lines:hover {
background-color: #b9d3ff !important;
transform: translateY(-2px);
}
.btn-add {
background-color: #0d6efd;
color: #fff;
border-radius: 8px;
padding: 10px 20px;
font-weight: 500;
transition: all 0.2s ease-in-out;
}
.btn-add:hover {
background-color: #0b5ed7;
transform: scale(1.02);
}
.btn-action {
border: none;
background: transparent;
cursor: pointer;
font-size: 1.2rem;
}
.btn-action.edit {
color: #007bff;
}
.btn-action.delete {
color: #dc3545;
}
.btn-action:hover {
transform: scale(1.15);
}
.table thead {
background-color: #cfe3ff;
color: #1f2d3d;
}
.icon-preview i {
font-size: 1.4rem;
}
.modal-content {
border-radius: 16px;
}
</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 w-100 text-center">
Parametri Linea: <?= htmlspecialchars($line['name']) ?> (Linea <?= (int)$line['line_number']; ?>)
</h5>
<button type="button" class="btn back-lines position-absolute end-0 me-3" onclick="location.href='linee.php'">
↩️ Torna alle Linee
</button>
</div>
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
<div>
<h6 class="fw-semibold mb-0">Elenco Parametri</h6>
<small class="text-muted">
Linea ID: <?= (int)$line['id']; ?> - Colore:
<span style="display:inline-block;width:16px;height:16px;border-radius:4px;border:1px solid #999;background:<?= htmlspecialchars($line['color']); ?>;"></span>
</small>
</div>
<button class="btn btn-add" id="btnAddParam"> Aggiungi Parametro</button>
</div>
<div class="table-responsive">
<table id="tabellaParametri" class="table table-striped align-middle text-center" style="width:100%;">
<thead>
<tr>
<th>ID</th>
<th>Position</th>
<th>Short label</th>
<th>Label</th>
<th>Icon class</th>
<th>Preview</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($params)) : ?>
<tr>
<td colspan="7" class="text-muted">No parameters defined for this line.</td>
</tr>
<?php else : ?>
<?php foreach ($params as $p) : ?>
<tr>
<td><?= (int)$p['id']; ?></td>
<td><?= (int)$p['position']; ?></td>
<td><?= htmlspecialchars($p['short_label']); ?></td>
<td><?= htmlspecialchars($p['label']); ?></td>
<td><?= htmlspecialchars($p['icon']); ?></td>
<td class="icon-preview">
<?php if (!empty($p['icon'])) : ?>
<i class="bi <?= htmlspecialchars($p['icon']); ?>"></i>
<?php else : ?>
<span class="text-muted">-</span>
<?php endif; ?>
</td>
<td>
<button class="btn-action edit"
data-id="<?= (int)$p['id']; ?>">
<i class="fas fa-edit"></i>
</button>
<button class="btn-action delete"
data-id="<?= (int)$p['id']; ?>">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<!-- ADD/EDIT PARAM MODAL -->
<div class="modal fade" id="paramModal" tabindex="-1" aria-labelledby="paramModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background-color:#cfe3ff;">
<h5 class="modal-title" id="paramModalLabel">Add Parameter</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="paramForm">
<input type="hidden" name="param_id" id="param_id" value="">
<input type="hidden" name="line_id" id="line_id" value="<?= (int)$line['id']; ?>">
<div class="mb-3">
<label for="position" class="form-label fw-semibold">Position (slot)</label>
<input type="number" class="form-control" id="position" name="position" required min="1">
</div>
<div class="mb-3">
<label for="short_label" class="form-label fw-semibold">Short label</label>
<input type="text" class="form-control" id="short_label" name="short_label" maxlength="20" required>
</div>
<div class="mb-3">
<label for="label" class="form-label fw-semibold">Label</label>
<input type="text" class="form-control" id="label" name="label" maxlength="100" required>
</div>
<div class="mb-3">
<label for="icon" class="form-label fw-semibold">Bootstrap Icon class (optional)</label>
<input type="text" class="form-control" id="icon" name="icon" placeholder="es. bi-thermometer-half">
<small class="text-muted">Use a Bootstrap Icons class without the <code>bi</code> prefix (prefix is automatically added in preview).</small>
</div>
<div class="text-center">
<button type="submit" class="btn btn-add" id="btnSaveParam">💾 Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include('jsinclude.php'); ?>
<script>
$(document).ready(function() {
const table = $('#tabellaParametri').DataTable({
order: [
[1, 'asc']
],
pageLength: 50,
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
}
});
const paramModal = new bootstrap.Modal(document.getElementById('paramModal'));
function resetForm() {
$('#param_id').val('');
$('#position').val('');
$('#short_label').val('');
$('#label').val('');
$('#icon').val('');
}
// Open modal for new parameter
$('#btnAddParam').on('click', function() {
resetForm();
$('#paramModalLabel').text('Add Parameter');
$('#btnSaveParam').text('💾 Save');
paramModal.show();
});
// Open modal for edit
$(document).on('click', '.edit', function() {
const id = $(this).data('id');
fetch('get_param.php?id=' + id)
.then(r => r.json())
.then(data => {
if (data.success) {
const p = data.param;
$('#param_id').val(p.id);
$('#line_id').val(p.line_id);
$('#position').val(p.position);
$('#short_label').val(p.short_label);
$('#label').val(p.label);
$('#icon').val(p.icon || '');
$('#paramModalLabel').text('Edit Parameter');
$('#btnSaveParam').text('💾 Update');
paramModal.show();
} else {
Swal.fire('Error', data.message || 'Unable to load parameter.', 'error');
}
})
.catch(() => {
Swal.fire('Error', 'Server communication error.', 'error');
});
});
// Save (add or update)
$('#paramForm').on('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
fetch('save_param.php', {
method: 'POST',
body: formData
})
.then(r => r.json())
.then(data => {
if (data.success) {
Swal.fire('Saved', data.message || 'Parameter saved correctly.', 'success')
.then(() => {
location.reload();
});
} else {
Swal.fire('Error', data.message || 'Error while saving parameter.', 'error');
}
})
.catch(() => {
Swal.fire('Error', 'Server communication error.', 'error');
});
});
// Delete parameter
$(document).on('click', '.delete', function() {
const id = $(this).data('id');
Swal.fire({
title: 'Are you sure?',
text: 'This action will permanently delete the parameter.',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete',
cancelButtonText: 'Cancel',
confirmButtonColor: '#d33'
}).then(result => {
if (result.isConfirmed) {
fetch('delete_param.php?id=' + id)
.then(r => r.json())
.then(data => {
if (data.success) {
Swal.fire('Deleted', data.message || 'Parameter deleted.', 'success')
.then(() => location.reload());
} else {
Swal.fire('Error', data.message || 'Error while deleting parameter.', 'error');
}
})
.catch(() => {
Swal.fire('Error', 'Server communication error.', 'error');
});
}
});
});
});
</script>
</body>
</html>