casadoc/public/userportal/assign-owners.php
2025-01-25 21:37:42 +01:00

282 lines
13 KiB
PHP

<?php include('include/headscript.php'); ?>
<?php
// Verifica se la variabile GET 'editpage' è impostata su 'nochange'
$disableEditing = isset($_GET['editpage']) && $_GET['editpage'] === 'nochange';
?>
<?php
// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Errore di connessione: " . $conn->connect_error);
}
// Recupera l'id immobile (idhome) passato tramite GET
$idhome = isset($_GET['idhome']) ? intval($_GET['idhome']) : 0;
// Recupera i dettagli dell'immobile
$queryHome = $conn->prepare("SELECT name, address, zip, city, country FROM home WHERE idhome = ?");
$queryHome->bind_param('i', $idhome);
$queryHome->execute();
$resultHome = $queryHome->get_result();
$homeDetails = $resultHome->fetch_assoc();
if (!$homeDetails) {
die("Immobile non trovato o accesso non autorizzato.");
}
// Recupera i proprietari associati all'immobile
$queryOwners = $conn->prepare("
SELECT
po.owner_id,
po.first_name,
po.last_name,
po.company_name,
po.tax_code,
po.email,
ho.ownership_percentage,
ho.notes
FROM
home_owners AS ho
INNER JOIN
property_owners AS po ON ho.owner_id = po.owner_id
WHERE
ho.home_id = ?
");
$queryOwners->bind_param('i', $idhome);
$queryOwners->execute();
$resultOwners = $queryOwners->get_result();
// Recupera tutti i proprietari disponibili dell'utente per la selezione
$queryAvailableOwners = $conn->prepare("
SELECT
owner_id,
first_name,
last_name,
company_name,
tax_code
FROM
property_owners
WHERE
user_id = ?
AND owner_id NOT IN (
SELECT owner_id FROM home_owners WHERE home_id = ?
)
");
$queryAvailableOwners->bind_param('ii', $iduserlogin, $idhome);
$queryAvailableOwners->execute();
$resultAvailableOwners = $queryAvailableOwners->get_result();
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<title>Assegna Proprietari</title>
<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">
</head>
<body class="fixed-left">
<div id="wrapper">
<?php include('include/navigationbar.php'); ?>
<div class="content-page">
<div class="content">
<?php include('include/topbar.php'); ?>
<br>
<div class="page-content-wrapper">
<div class="container-fluid">
<!-- Dettagli dell'immobile -->
<div class="card mb-4">
<div class="card-header bg-warning text-white">
<h4 class="mb-0"><?php echo htmlspecialchars($homeDetails['name']); ?></h4>
<span class="badge bg-info text-primary">
<?php echo htmlspecialchars($homeDetails['address'] . ', ' . $homeDetails['zip'] . ', ' . $homeDetails['city'] . ', ' . $homeDetails['country']); ?>
</span>
</div>
</div>
<!-- Proprietari associati -->
<div class="card mb-4">
<div class="card-header bg-success text-white">
<h5 class="mb-0">Proprietari</h5>
</div>
<div class="card-body">
<?php if ($resultOwners->num_rows > 0) { ?>
<table class="table table-bordered">
<thead>
<tr>
<th>Nome</th>
<th>Codice Fiscale</th>
<th>%</th>
<th>Note</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php while ($owner = $resultOwners->fetch_assoc()) { ?>
<tr>
<td>
<?php
echo htmlspecialchars($owner['first_name'] . ' ' . $owner['last_name']);
if ($owner['company_name']) {
echo ' (' . htmlspecialchars($owner['company_name']) . ')';
}
?>
</td>
<td><?php echo htmlspecialchars($owner['tax_code']); ?></td>
<td><?php echo htmlspecialchars($owner['ownership_percentage']); ?></td>
<td><?php echo htmlspecialchars($owner['notes']); ?></td>
<td>
<?php if (!$disableEditing) { ?>
<button class="btn btn-danger btn-sm remove-owner-btn" data-id="<?php echo $owner['owner_id']; ?>">
<i class="fas fa-trash-alt"></i> Rimuovi
</button>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } else { ?>
<p class="text-muted">Nessun proprietario associato.</p>
<?php } ?>
</div>
</div>
<!-- Aggiungi proprietari -->
<?php if (!$disableEditing) { ?>
<div class="card mb-4">
<div class="card-header bg-info text-white">
<h5 class="mb-0">Aggiungi Proprietari</h5>
</div>
<div class="card-body">
<form id="addOwnerForm" method="POST" action="add-owner-to-home.php">
<input type="hidden" name="idhome" value="<?php echo $idhome; ?>">
<div class="form-group">
<label for="ownerSelect">Seleziona Proprietario</label>
<select class="form-control" id="ownerSelect" name="owner_id" required>
<option value="">-- Seleziona --</option>
<?php while ($availableOwner = $resultAvailableOwners->fetch_assoc()) { ?>
<option value="<?php echo $availableOwner['owner_id']; ?>">
<?php
echo htmlspecialchars($availableOwner['first_name'] . ' ' . $availableOwner['last_name']);
if ($availableOwner['company_name']) {
echo ' (' . htmlspecialchars($availableOwner['company_name']) . ')';
}
echo ' - ' . htmlspecialchars($availableOwner['tax_code']);
?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="ownershipPercentage">Percentuale di Proprietà</label>
<input type="number" step="0.01" class="form-control" id="ownershipPercentage" name="ownership_percentage" placeholder="Inserisci la percentuale (es. 50.00)">
</div>
<div class="form-group">
<label for="notes">Note</label>
<textarea class="form-control" id="notes" name="notes" rows="3" placeholder="Aggiungi eventuali note"></textarea>
</div>
<button type="submit" class="btn btn-success mt-3">Aggiungi</button>
</form>
</div>
</div>
<?php } ?>
<div class="mb-3">
<button onclick="history.back()" class="btn btn-dark">
<i class="fas fa-arrow-left"></i> Torna indietro
</button>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
</div>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script>
$(document).on('click', '.remove-owner-btn', function() {
const ownerId = $(this).data('id');
const idhome = "<?php echo $idhome; ?>";
if (confirm('Sei sicuro di voler rimuovere questo proprietario?')) {
$.post('remove-owner-from-home.php', {
owner_id: ownerId,
idhome: idhome
}, function(response) {
location.reload();
}).fail(function() {
alert('Errore durante la rimozione del proprietario.');
});
}
});
</script>
<script>
$(document).on('click', '.remove-owner-btn', function() {
const ownerId = $(this).data('id');
const idhome = "<?php echo $idhome; ?>";
Swal.fire({
title: 'Sei sicuro?',
text: 'Questa azione rimuoverà il proprietario selezionato.',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#3085d6',
confirmButtonText: 'Sì, rimuovi',
cancelButtonText: 'Annulla',
}).then((result) => {
if (result.isConfirmed) {
$.post('remove-owner-from-home.php', {
owner_id: ownerId,
idhome: idhome
}, function(response) {
try {
const result = JSON.parse(response);
if (result.success) {
Swal.fire({
icon: 'success',
title: 'Rimosso',
text: result.message,
timer: 1500,
showConfirmButton: false,
}).then(() => {
location.reload(); // Ricarica la pagina
});
} else {
Swal.fire({
icon: 'error',
title: 'Errore',
text: result.message,
});
}
} catch (error) {
console.error('Errore di parsing JSON:', error);
Swal.fire({
icon: 'error',
title: 'Errore',
text: 'Si è verificato un problema.',
});
}
}).fail(function() {
Swal.fire({
icon: 'error',
title: 'Errore',
text: 'Impossibile completare la richiesta.',
});
});
}
});
});
</script>
</body>
</html>