<?php include('include/headscript.php'); ?>
<?php
$message = "";
$alertClass = "";

if (isset($_GET['success'])) {
    $message = "Operazione completata con successo.";
    $alertClass = "alert-success";
} elseif (isset($_GET['error'])) {
    $message = "Si è verificato un errore. Riprova.";
    $alertClass = "alert-danger";
}
?>

<?php
// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);

// Verifica connessione
if ($conn->connect_error) {
    die("Errore di connessione: " . $conn->connect_error);
}

// Recupera l'elenco delle pagine
$queryPages = $conn->query("SELECT * FROM pages ORDER BY idpages DESC");
$pages = [];
while ($row = $queryPages->fetch_assoc()) {
    $pages[] = $row;
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title>Gestione Pagine</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">
    <link rel="stylesheet" href="https://cdn.materialdesignicons.com/5.4.55/css/materialdesignicons.min.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">
                        <?php if (!empty($message)) { ?>
                            <div class="alert <?php echo $alertClass; ?> alert-dismissible fade show" role="alert">
                                <?php echo $message; ?>
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                        <?php } ?>

                        <div class="row text-center mt-4">
                            <div class="col-lg-6 col-md-6 mb-4">
                                <a href="page-settings.php" class="btn btn-warning d-flex flex-column justify-content-center align-items-center py-5 shadow rounded-lg">
                                    <i class="mdi mdi-book-outline" style="font-size: 3rem;"></i>
                                    <span class="mt-2 fw-bold" style="font-size: 1.25rem;">Gestione Pagine</span>
                                </a>
                            </div>
                            <div class="col-lg-6 col-md-6 mb-4">
                                <a href="documents-settings.php" class="btn btn-danger d-flex flex-column justify-content-center align-items-center py-5 shadow rounded-lg">
                                    <i class="mdi mdi-file-cog-outline" style="font-size: 3rem;"></i>
                                    <span class="mt-2 fw-bold" style="font-size: 1.25rem;">Gestione Documenti</span>
                                </a>
                            </div>
                        </div>
                        <!-- Titolo e Pulsanti -->
                        <div class="row mb-4">
                            <div class="col-lg-6">
                                <a href="index.php" class="btn btn-outline-primary btn-block shadow-sm">
                                    <i class="mdi mdi-arrow-left"></i> Torna al Dashboard
                                </a>
                            </div>
                            <div class="col-lg-6">
                                <button class="btn btn-outline-success btn-block shadow-sm" data-toggle="modal" data-target="#addPageModal">
                                    <i class="mdi mdi-plus"></i> Aggiungi Pagina
                                </button>
                            </div>
                        </div>

                        <!-- Tabella Pagine -->
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card">
                                    <div class="card-body">
                                        <h4 class="card-title">Elenco Pagine</h4>
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Nome</th>
                                                    <th>Slug</th>
                                                    <th>Descrizione</th>
                                                    <th>Azioni</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($pages as $page) { ?>
                                                    <tr>
                                                        <td><?php echo $page['idpages']; ?></td>
                                                        <td><?php echo htmlspecialchars($page['namepages']); ?></td>
                                                        <td><?php echo htmlspecialchars($page['slug']); ?></td>
                                                        <td><?php echo htmlspecialchars($page['descriptionpages']); ?></td>
                                                        <td>
                                                            <button class="btn btn-sm btn-warning" data-toggle="modal" data-target="#editPageModal"
                                                                data-id="<?php echo $page['idpages']; ?>"
                                                                data-name="<?php echo htmlspecialchars($page['namepages']); ?>"
                                                                data-slug="<?php echo htmlspecialchars($page['slug']); ?>"
                                                                data-description="<?php echo htmlspecialchars($page['descriptionpages']); ?>">
                                                                <i class="mdi mdi-pencil"></i>
                                                            </button>
                                                            <button class="btn btn-sm btn-danger" onclick="deletePage(<?php echo $page['idpages']; ?>)">
                                                                <i class="mdi mdi-delete"></i>
                                                            </button>
                                                        </td>

                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                    </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 -->

    <!-- Modale: Aggiungi Pagina -->
    <div class="modal fade" id="addPageModal" tabindex="-1" role="dialog" aria-labelledby="addPageModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="add-page.php" method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title" id="addPageModalLabel">Aggiungi Pagina</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="pageName">Nome Pagina</label>
                            <input type="text" class="form-control" id="pageName" name="namepages" required>
                        </div>
                        <div class="form-group">
                            <label for="pageSlug">Slug</label>
                            <input type="text" class="form-control" id="pageSlug" name="slug" required>
                        </div>
                        <div class="form-group">
                            <label for="pageDescription">Descrizione</label>
                            <textarea class="form-control" id="pageDescription" name="descriptionpages"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Chiudi</button>
                        <button type="submit" class="btn btn-success">Salva</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Modale: Modifica Pagina -->
    <div class="modal fade" id="editPageModal" tabindex="-1" role="dialog" aria-labelledby="editPageModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="edit-page.php" method="POST">
                    <!-- Campo nascosto per l'ID -->
                    <input type="hidden" id="editPageId" name="idpages">
                    <div class="modal-header">
                        <h5 class="modal-title" id="editPageModalLabel">Modifica Pagina</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="editPageName">Nome Pagina</label>
                            <input type="text" class="form-control" id="editPageName" name="namepages" required>
                        </div>
                        <div class="form-group">
                            <label for="editPageSlug">Slug</label>
                            <input type="text" class="form-control" id="editPageSlug" name="slug" required>
                        </div>
                        <div class="form-group">
                            <label for="editPageDescription">Descrizione</label>
                            <textarea class="form-control" id="editPageDescription" name="descriptionpages"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Chiudi</button>
                        <button type="submit" class="btn btn-warning">Salva Modifiche</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <!-- Funzione Delete -->
    <script>
        function deletePage(id) {
            if (confirm("Sei sicuro di voler eliminare questa pagina?")) {
                window.location.href = "delete-page.php?idpages=" + id;
            }
        }

        // Popola i campi del modale di modifica
        $('#editPageModal').on('show.bs.modal', function(event) {
            var button = $(event.relatedTarget); // Bottone che ha aperto il modale
            console.log("Bottone cliccato:", button); // DEBUG

            var id = button.data('id'); // Recupera l'attributo data-id
            var name = button.data('name'); // Recupera l'attributo data-name
            var slug = button.data('slug'); // Recupera l'attributo data-slug
            var description = button.data('description'); // Recupera l'attributo data-description

            console.log("Dati recuperati:", {
                id,
                name,
                slug,
                description
            }); // DEBUG

            // Popola i campi del modale
            var modal = $(this);
            modal.find('#editPageId').val(id);
            modal.find('#editPageName').val(name);
            modal.find('#editPageSlug').val(slug);
            modal.find('#editPageDescription').val(description);
        });
    </script>

    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
</body>

</html>