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

// Recupera l'id utente loggato
$iduserlogin = $_SESSION['iduserlogin'];

// Query per ottenere le case dell'utente
$sql = "SELECT idhome, name, address, zip, city, country FROM home WHERE iduser = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $iduserlogin);
$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>Elenco delle tue case</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" />

    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" />

    <!-- 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">Elenco Case</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Elenco delle tue case</h4>
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-12">
                                <div class="float-right">
                                    <!-- Pulsante per aggiungere una nuova casa -->
                                    <a href="manage-home.php" class="btn btn-success">
                                        <i class="fas fa-plus"></i> Aggiungi Casa
                                    </a>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xl-12">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table id="homeTable" class="table table-bordered table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Nome</th>
                                                        <th>Indirizzo</th>
                                                        <th>CAP</th>
                                                        <th>Città</th>
                                                        <th>Nazione</th>
                                                        <th>Action</th>
                                                    </tr>
                                                    <tr>
                                                        <!-- Campi di input per i filtri -->
                                                        <th><input type="text" placeholder="Cerca Nome" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Indirizzo" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca CAP" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Città" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Nazione" class="form-control form-control-sm"></th>
                                                        <th></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>
                                                                <!-- Pulsante per modificare i dettagli della casa -->
                                                                <a href="manage-home.php?idhome=<?php echo $row['idhome']; ?>" class="btn btn-info btn-sm">
                                                                    <i class="fas fa-info-circle"></i> Dettagli
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div><!-- end table-responsive -->
                                    </div>
                                </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 con filtri di colonna
            var table = $('#homeTable').DataTable();

            // Aggiungi ricerca personalizzata in ogni colonna
            $('#homeTable thead tr:eq(1) th').each(function(i) {
                $('input', this).on('keyup change', function() {
                    if (table.column(i).search() !== this.value) {
                        table.column(i).search(this.value).draw();
                    }
                });
            });
        });
    </script>
</body>

</html>