<?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 i proprietari legati all'utente
$sql = "SELECT owner_id, first_name, last_name, company_name, tax_code, email, phone, city, province, country, owner_type, role FROM property_owners WHERE user_id = ?";
$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 dei proprietari</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" />

    <!-- 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 Proprietari</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Elenco dei proprietari</h4>
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-12">
                                <div class="float-right">
                                    <!-- Pulsante per aggiungere un nuovo proprietario -->
                                    <a href="manage-owner.php" class="btn btn-success">
                                        <i class="fas fa-plus"></i> Aggiungi Proprietario
                                    </a>
                                </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 class="row">
                            <div class="col-xl-12">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table id="ownerTable" class="table table-bordered table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Nome</th>
                                                        <th>Cognome/Società</th>
                                                        <th>Codice Fiscale/Partita IVA</th>
                                                        <th>Email</th>
                                                        <th>Telefono</th>
                                                        <th>Città</th>
                                                        <th>Provincia</th>
                                                        <th>Nazione</th>
                                                        <th>Tipo</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 Cognome/Società" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Codice Fiscale/Partita IVA" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Email" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Telefono" 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 Provincia" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Nazione" class="form-control form-control-sm"></th>
                                                        <th><input type="text" placeholder="Cerca Tipo" class="form-control form-control-sm"></th>

                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php while ($row = $result->fetch_assoc()) { ?>
                                                        <tr>
                                                            <td><?php echo htmlspecialchars($row['first_name']); ?></td>
                                                            <td>
                                                                <?php
                                                                if ($row['owner_type'] == 'individual') {
                                                                    echo htmlspecialchars($row['last_name']);
                                                                } else {
                                                                    echo htmlspecialchars($row['company_name']);
                                                                }
                                                                ?>
                                                            </td>
                                                            <td><?php echo htmlspecialchars($row['tax_code']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['email']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['phone']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['city']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['province']); ?></td>
                                                            <td><?php echo htmlspecialchars($row['country']); ?></td>
                                                            <td><?php echo ucfirst($row['owner_type']); ?></td>
                                                            <td>
                                                                <div class="btn-group" role="group">
                                                                    <!-- Pulsante per modificare i dettagli del proprietario -->
                                                                    <a href="manage-owner.php?owner_id=<?php echo $row['owner_id']; ?>" class="btn btn-info btn-sm">
                                                                        <i class="fas fa-info-circle"></i> Dettagli
                                                                    </a>
                                                                    <form action="documents-person.php" method="POST" style="display:inline;">
                                                                        <input type="hidden" name="owner_id" value="<?php echo $row['owner_id']; ?>">
                                                                        <button type="submit" class="btn btn-warning btn-sm ml-2">
                                                                            <i class="fas fa-file-alt"></i> DOCS
                                                                        </button>
                                                                    </form>

                                                                </div>
                                                            </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 = $('#ownerTable').DataTable();

            // Aggiungi ricerca personalizzata in ogni colonna
            $('#ownerTable 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>