<?php
include "db.php";
session_start();
if (!isset($_SESSION['email'])) {
  header("Location:login.php");
}
?>

<!DOCTYPE html>
<html lang="en" dir="ltr" data-bs-theme="light" data-color-theme="Blue_Theme" data-layout="vertical">

<head>
    <!-- Required meta tags -->
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- Favicon icon-->
    <link rel="shortcut icon" type="image/png" href="../sglogo.png" />

    <!-- Core Css -->
    <link rel="stylesheet" href="assets/css/styles.css" />

    <title>Admin || Contact Data</title>
    <link rel="stylesheet" href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.2/css/buttons.dataTables.min.css">

    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

</head>

<style>
table.table-bordered.dataTable th,
table.table-bordered.dataTable td {
    border-bottom-width: 1px;
    color: #000000d4;
}
</style>

<body class="link-sidebar">
    <!-- Preloader -->

    <div id="main-wrapper">
        <!-- Sidebar Start --> 

    <?php
    include "header.php";
    ?>

        <div class="body-wrapper">
            <div class="container-fluid">
                <div class="card card-body py-3">
                    <div class="row align-items-center">
                        <div class="col-12">
                            <div class="d-sm-flex align-items-center justify-space-between">
                                <h4 class="mb-4 mb-sm-0 card-title">Contact Data</h4>
                                <nav aria-label="breadcrumb" class="ms-auto">
                                    <ol class="breadcrumb">
                                        <li class="breadcrumb-item d-flex align-items-center">
                                            <a class="text-muted text-decoration-none d-flex" href="index.php">
                                                <iconify-icon icon="solar:home-2-line-duotone" class="fs-6">
                                                </iconify-icon>
                                            </a>
                                        </li>
                                    </ol>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="datatables">
                    <!-- start File export -->
                    <div class="card">
                        <div class="card-body">

                            <div class="table-responsive">

                                <table id="file_export"
                                    class="table w-100 table-striped table-bordered display">
                                    <thead>
                                        <tr>
                                            <th>Sr.No</th>
                                            <th>Name</th>
                                            <th>Email</th>
                                            <th>Phone</th>
                                            <th>Subject</th>
                                            <th>Message</th>
                                            <th>Contact Date</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $sql = "SELECT * FROM contact ORDER BY id DESC";
                                        $result = mysqli_query($conn, $sql);
                                        $sr = 1;

                                        if (mysqli_num_rows($result) > 0) {
                                            while ($row = mysqli_fetch_assoc($result)) {
                                        ?>
                                        <tr id="row-<?= $row['id'] ?>">
                                            <td><?= $sr++ ?></td>
                                            <td><?= htmlspecialchars($row['name']) ?></td>
                                            <td><?= htmlspecialchars($row['email']) ?></td>
                                            <td><?= htmlspecialchars($row['phone']) ?></td>
                                            <td><?= htmlspecialchars($row['subject']) ?></td>
                                            <td style="max-width: 300px; white-space: normal; word-wrap: break-word;">
                                                <?= nl2br(htmlspecialchars($row['message'])) ?>
                                            </td>
                                            <td><?= date("d-m-Y H:i A", strtotime($row['created_at'])) ?></td>
                                            <td>
                                                <button class="btn btn-danger btn-sm deleteBtn"
                                                    data-id="<?= $row['id'] ?>">Delete</button>
                                            </td>
                                        </tr>
                                        <?php
                                            }
                                        } ?>
                                    </tbody>
                                </table>

                            </div>
                        </div>
                    </div>
                    <!-- end File export -->

                </div>
            </div>
        </div>
    </div>


    <script>
    function handleColorTheme(e) {
        document.documentElement.setAttribute("data-color-theme", e);
    }
    </script>
    </div>

    <script>
    document.addEventListener("DOMContentLoaded", function() {
        const deleteButtons = document.querySelectorAll(".deleteBtn");

        deleteButtons.forEach(button => {
            button.addEventListener("click", function() {
                const contactId = this.dataset.id;

                Swal.fire({
                    title: "Are you sure?",
                    text: "This contact will be deleted!",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#d33",
                    cancelButtonColor: "#3085d6",
                    confirmButtonText: "Yes, delete it!"
                }).then((result) => {
                    if (result.isConfirmed) {
                        fetch("delete_contact.php", {
                                method: "POST",
                                headers: {
                                    "Content-Type": "application/x-www-form-urlencoded"
                                },
                                body: "delete_id=" + contactId
                            })
                            .then(response => response.text())
                            .then(data => {
                                if (data === "success") {
                                    document.getElementById("row-" + contactId)
                                        .remove();
                                    Swal.fire("Deleted!",
                                        "The contact has been deleted.",
                                        "success");
                                } else {
                                    Swal.fire("Error!", "Failed to delete contact.",
                                        "error");
                                }
                            });
                    }
                });
            });
        });
    });
    </script>

    <div class="dark-transparent sidebartoggler"></div>
    <script src="assets/js/vendor.min.js"></script>
    <!-- Import Js Files -->
    <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/libs/simplebar/dist/simplebar.min.js"></script>
    <script src="assets/js/theme/app.init.js"></script>
    <script src="assets/js/theme/theme.js"></script>
    <script src="assets/js/theme/app.min.js"></script>
    <script src="assets/js/theme/sidebarmenu-default.js"></script>

    <!-- solar icons -->
    <script src="https://cdn.jsdelivr.net/npm/iconify-icon@1.0.8/dist/iconify-icon.min.js"></script>

    <!-- highlight.js (code view) -->
    <script src="assets/js/highlights/highlight.min.js"></script>
    <script>
    hljs.initHighlightingOnLoad();


    document.querySelectorAll("pre.code-view > code").forEach((codeBlock) => {
        codeBlock.textContent = codeBlock.innerHTML;
    });
    </script>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.print.min.js"></script>

    <script src="assets/js/datatable/datatable-advanced.init.js"></script>
</body>


</html>