<?php include('include/headscript.php'); ?>
<?php
// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);

// Recupera l'id utente loggato e la sua email
$iduserlogin = $_SESSION['iduserlogin'];
$emailuser = $_SESSION['emailuser'];

// Query per ottenere gli immobili condivisi con l'utente loggato
$sql = "
SELECT 
    hs.idhome, 
    h.name, 
    h.address, 
    h.zip, 
    h.city, 
    h.country, 
    hs.sharing_type,
    hs.expiration_date,
    au.first_name,
    au.last_name,
    COUNT(ho.owner_id) AS owner_count
FROM home_sharing hs
LEFT JOIN home h ON hs.idhome = h.idhome
LEFT JOIN auth_users au ON hs.iduser = au.id
LEFT JOIN home_owners ho ON hs.idhome = ho.home_id
WHERE 
    (hs.idshareduser = ? OR hs.shared_email = ?) 
    AND hs.status = 'accepted'
    GROUP BY 
    hs.idhome, 
    h.name, 
    h.address, 
    h.zip, 
    h.city, 
    h.country, 
    hs.sharing_type,
    hs.expiration_date,
    au.first_name,
    au.last_name;
";
$stmt = $conn->prepare($sql);
$stmt->bind_param('is', $iduserlogin, $emailuser);
$stmt->execute();
$result = $stmt->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">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Immobili Condivisi</title>

    <!-- Bootstrap 4 CSS -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/dripicons/2.0.0/webfont.min.css" rel="stylesheet">

    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <!-- Custom 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'); ?>

                <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="#">CasaDoc</a></li>
                                            <li class="breadcrumb-item active">Immobili Condivisi</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Immobili Condivisi</h4>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xl-12">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table id="sharedHomesTable" class="table table-bordered table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Nome</th>
                                                        <th>Indirizzo</th>
                                                        <th>CAP</th>
                                                        <th>Città</th>
                                                        <th>Nazione</th>
                                                        <th>Condiviso da</th>
                                                        <th>Proprietari</th>
                                                        <th>Data Scadenza</th>
                                                        <th>Azioni</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php while ($row = $result->fetch_assoc()) { ?>
                                                        <tr>
                                                            <td><?php echo htmlspecialchars($row['name']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['address']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['zip']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['city']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['country']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?></td>
                                                            <td>
                                                                <?php if ($row['owner_count'] == 0) { ?>
                                                                    <!-- Pulsante rosso per nessun proprietario -->
                                                                    <a href="assign-owners.php?idhome=<?php echo $row['idhome']; ?>&editpage=nochange" class="btn btn-danger btn-sm" title="Nessun Proprietario">
                                                                        <i class="fas fa-user-times"></i> <!-- Icona per nessun proprietario -->
                                                                    </a>
                                                                <?php } else { ?>
                                                                    <!-- Pulsante verde con il numero dei proprietari -->
                                                                    <a href="assign-owners.php?idhome=<?php echo $row['idhome']; ?>&editpage=nochange" class="btn btn-success btn-sm" title="<?php echo $row['owner_count']; ?> Proprietari">
                                                                        <i class="fas fa-user-check"></i> <!-- Icona per proprietari presenti -->

                                                                    </a>
                                                                <?php } ?>
                                                            </td>

                                                            <td>
                                                                <?php echo $row['expiration_date'] ? htmlspecialchars($row['expiration_date']) : 'Nessuna'; ?>
                                                            </td>
                                                            <td>
                                                                <!-- Pulsante per modificare i dettagli della casa -->
                                                                <a href="manage-home.php?idhome=<?php echo $row['idhome']; ?>" class="btn btn-info btn-sm" title="Dettagli">
                                                                    <i class="fas fa-info-circle"></i>
                                                                </a>
                                                                <!-- Pulsante per visualizzare i dettagli -->
                                                                <a href="documents-home-shared.php?idhome=<?php echo $row['idhome']; ?>" class="btn btn-primary btn-sm" title="Documenti">
                                                                    <i class="fas fa-folder-open"></i>
                                                                </a>
                                                                <!-- Pulsante per scaricare zip -->
                                                                <a href="download-zip.php?idhome=<?php echo $row['idhome']; ?>" class="btn btn-warning btn-sm" title="Scarica ZIP">
                                                                    <i class="fas fa-file-archive"></i>
                                                                </a>
                                                                <!-- Pulsante per scaricare report -->
                                                                <a href="download-report.php?idhome=<?php echo $row['idhome']; ?>" class="btn btn-danger btn-sm" title="Scarica Report">
                                                                    <i class="fas fa-file-alt"></i>
                                                                </a>
                                                            </td>

                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div><!-- end table-responsive -->
                                    </div>
                                </div>
                                <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><!-- container -->
                </div><!-- Page content Wrapper -->
            </div><!-- content -->
            <?php include('include/footer.php'); ?>
        </div><!-- End Right content here -->
    </div><!-- END wrapper -->

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.5/js/dataTables.bootstrap4.min.js"></script>

    <script>
        $(document).ready(function() {
            // Inizializza DataTables
            $('#sharedHomesTable').DataTable({
                order: [
                    [0, 'asc']
                ] // Ordina per nome
            });
        });
    </script>

</body>

</html>