391 lines
19 KiB
PHP

<?php include('../include/headscript.php'); ?>
<?php
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<?php include("../class/company.php"); ?>
<?php include('../include/navigationbar.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<?php include('../include/seo.php'); ?>
<link rel="shortcut icon" href="../assets/images/favicon.ico">
<link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="../assets/css/icons.css" rel="stylesheet" type="text/css">
<link href="../assets/css/style.css" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.css">
<script src="../assets/js/jquery.min.js"></script>
<link rel="stylesheet" href="../assets/plugins/select2/select2.min.css">
<script src="../assets/plugins/select2/select2.min.js"></script>
</head>
<style>
/* Assicura che gli input e il select abbiano margini e padding corretti */
.swal2-input,
.swal2-select {
width: 100% !important;
margin-bottom: 15px;
/* Aggiungi uno spazio tra gli elementi */
padding: 10px;
box-sizing: border-box;
/* Assicura che padding e bordi siano inclusi nella larghezza */
}
/* Corregge la visualizzazione del select all'interno di SweetAlert */
.swal2-select select {
display: block;
width: 100%;
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 1rem;
}
</style>
<body class="fixed-left">
<!-- Loader -->
<!-- Begin page -->
<div id="wrapper">
<!-- Start right Content here -->
<div class="content-page">
<!-- Start content -->
<div class="content">
<?php include('../include/topbar.php'); ?>
<div class="page-content-wrapper ">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
<ol class="breadcrumb hide-phone p-0 m-0">
<li class="breadcrumb-item"><a href="#">Reportify</a></li>
<li class="breadcrumb-item active">Laboratories</li>
</ol>
</div>
<h4 class="page-title">Laboratories Management</h4>
</div>
</div>
</div>
<!-- end page title end breadcrumb -->
<div class="row">
<div class="col-xl-12">
<div class="card">
<div class="card-body">
<h5 class="header-title pb-3 mt-0">Laboratories List</h5>
<div class="table-responsive">
<table class="table table-striped table-custom">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Reflab</th>
<th>Country</th>
<th>Status</th>
<th>API Key</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM laboratories";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['idlab'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['reflab'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . substr($row['api_key'], 0, 5) . "*****" . "</td>";
echo "<td>
<button class='btn btn-primary btn-sm' onclick='editLab(" . $row['idlab'] . ")'>Edit</button>
<button class='btn btn-danger btn-sm' onclick='deleteLab(" . $row['idlab'] . ")'>Delete</button>
<button class='btn btn-warning btn-sm' onclick='regenerateKeys(" . $row['idlab'] . ")'>Regenerate Keys</button>
</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='7'>No laboratories found</td></tr>";
}
?>
</tbody>
</table>
</div><!-- end table-responsive -->
<button class="btn btn-success mt-3" onclick="addLab()">Add New Laboratory</button>
</div>
</div>
</div>
</div>
<!-- end row -->
</div><!-- container -->
</div> <!-- Page content Wrapper -->
</div> <!-- content -->
<?php include('../include/footer.php'); ?>
</div>
<!-- End Right content here -->
</div>
<!-- END wrapper -->
<!-- plugin JS -->
<script src="../assets/js/popper.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/modernizr.min.js"></script>
<script src="../assets/js/detect.js"></script>
<script src="../assets/js/fastclick.js"></script>
<script src="../assets/js/jquery.slimscroll.js"></script>
<script src="../assets/js/jquery.blockUI.js"></script>
<script src="../assets/js/waves.js"></script>
<script src="../assets/js/jquery.nicescroll.js"></script>
<script src="../assets/js/jquery.scrollTo.min.js"></script>
<script src="../assets/js/common_helper.js"></script>
<script src="../assets/plugins/chart.js/chart.min.js"></script>
<script src="../assets/pages/dashboard.js"></script>
<!-- App js -->
<script src="../assets/js/app.js"></script>
<script src="../assets/plugins/alertify/js/alertify.js"></script>
<script>
function addLab() {
Swal.fire({
title: 'Add New Laboratory',
html: `
<input id="lab_name" class="swal2-input" placeholder="Lab Name">
<input id="country" class="swal2-input" placeholder="Country">
`,
focusConfirm: false,
preConfirm: () => {
const name = document.getElementById('lab_name').value;
const country = document.getElementById('country').value;
if (!name || !country) {
Swal.showValidationMessage('Please enter all fields');
} else {
$.ajax({
url: 'add_lab.php',
method: 'POST',
data: {
name: name,
country: country
},
success: function(response) {
response = JSON.parse(response);
if (response.status === 'success') {
Swal.fire({
title: 'Laboratory Added',
html: `<p>Reflab: ${response.reflab}</p>
<p>API Key: <strong>${response.api_key}</strong></p>
<p>Secret Key: <strong>${response.api_secret}</strong></p>
<p>Please copy these keys securely. This is your only chance to view the secret key.</p>`,
icon: 'success',
showCancelButton: true,
cancelButtonText: 'Close'
}).then(() => {
// Aggiorna la tabella dinamicamente senza ricaricare l'intera pagina
loadLaboratories();
});
} else {
alertify.error('Failed to add laboratory');
}
},
error: function() {
alertify.error('Failed to add laboratory');
}
});
}
}
});
}
function loadLaboratories() {
$.ajax({
url: 'load_laboratories.php', // Questo file restituisce il codice HTML della tabella
method: 'GET',
success: function(data) {
$('tbody').html(data); // Aggiorna solo il contenuto della tabella
},
error: function() {
alertify.error('Failed to load laboratories');
}
});
}
function editLab(idlab) {
// Recupera i dati del laboratorio tramite AJAX
$.ajax({
url: 'get_lab.php',
method: 'POST',
data: {
idlab: idlab
},
success: function(response) {
response = JSON.parse(response);
if (response.name && response.country) {
// Mostra SweetAlert con i dati precompilati
Swal.fire({
title: 'Edit Laboratory',
html: `
<div style="margin-bottom: 10px;">
<input id="lab_name" class="swal2-input" placeholder="Lab Name" value="${response.name}">
</div>
<div style="margin-bottom: 10px;">
<input id="country" class="swal2-input" placeholder="Country" value="${response.country}">
</div>
<div style="margin-bottom: 10px;">
<select id="status" name="status" class="swal2-select">
<option value="active" ${response.status === 'active' ? 'selected' : ''}>Active</option>
<option value="inactive" ${response.status === 'inactive' ? 'selected' : ''}>Inactive</option>
<option value="suspended" ${response.status === 'suspended' ? 'selected' : ''}>Suspended</option>
</select>
</div>
`,
focusConfirm: false,
preConfirm: () => {
const name = document.getElementById('lab_name').value.trim();
const country = document.getElementById('country').value.trim();
const status = document.getElementById('status').value;
if (name === '' || country === '' || status === '') {
Swal.showValidationMessage('Please enter all fields');
return false;
} else {
// Esegui l'aggiornamento tramite AJAX
$.ajax({
url: 'update_lab.php',
method: 'POST',
data: {
idlab: idlab,
name: name,
country: country,
status: status // Assicurati che lo status venga inviato
},
success: function(response) {
console.log(response); // Debug per verificare cosa restituisce il server
response = JSON.parse(response);
if (response.status === 'success') {
Swal.fire('Updated!', response.message, 'success').then(() => {
loadLaboratories(); // Aggiorna la tabella dinamicamente
});
} else {
alertify.error('Failed to update laboratory');
}
},
error: function() {
alertify.error('Failed to update laboratory');
}
});
}
}
});
} else {
alertify.error('Failed to retrieve laboratory data');
}
},
error: function() {
alertify.error('Failed to retrieve laboratory data');
}
});
}
function deleteLab(idlab) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: 'delete_lab.php',
method: 'POST',
data: {
idlab: idlab
},
success: function(response) {
response = JSON.parse(response);
if (response.status === 'success') {
Swal.fire('Deleted!', response.message, 'success').then(() => {
// Aggiorna la tabella dinamicamente senza ricaricare l'intera pagina
loadLaboratories();
});
} else {
alertify.error('Failed to delete laboratory');
}
},
error: function() {
alertify.error('Failed to delete laboratory');
}
});
}
});
}
function regenerateKeys(idlab) {
Swal.fire({
title: 'Regenerate API Keys?',
text: "This will invalidate the current keys!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, regenerate!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: 'regenerate_keys.php',
method: 'POST',
data: {
idlab: idlab
},
success: function(response) {
response = JSON.parse(response);
if (response.status === 'success') {
// Mostra le nuove chiavi rigenerate in un SweetAlert
Swal.fire({
title: 'Keys Regenerated',
html: `<p>API Key: <strong>${response.api_key}</strong></p>
<p>Secret Key: <strong>${response.api_secret}</strong></p>
<p>Please copy these keys securely. This is your only chance to view the secret key.</p>`,
icon: 'success',
showCancelButton: true,
cancelButtonText: 'Close'
});
} else {
alertify.error('Failed to regenerate keys');
}
},
error: function() {
alertify.error('Failed to regenerate keys');
}
});
}
});
}
</script>
</body>
</html>