<?php
session_start(); // Required if using login session

// Database connection
$conn = new mysqli("localhost", "job_portal", "job_portal", "job_portal");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Handle delete action
if (isset($_GET['delete_id'])) {
    $id = intval($_GET['delete_id']);
    $conn->query("DELETE FROM contact_queries WHERE id=$id");
    header("Location: condispaly.php");
    exit;
}

// Fetch all records
$result = $conn->query("SELECT * FROM contact_queries ORDER BY submitted_at DESC");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Submitted Queries</title>
    <style>
        body {
            font-family: Arial;
            margin: 20px;
        }
        .top-bar {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        .btn {
            padding: 8px 16px;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-weight: bold;
        }
        .btn:hover {
            background: #0056b3;
        }
        .delete-btn {
            padding: 4px 8px;
            color: white;
            background-color: #dc3545;
            text-decoration: none;
            border-radius: 3px;
        }
        .delete-btn:hover {
            background-color: #b52a36;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            font-size: 14px;
        }
        th, td {
            border: 1px solid #aaa;
            padding: 8px;
            text-align: left;
        }
        th {
            background: #003f88;
            color: white;
        }

        /* Print Styling */
        @media print {
            .top-bar, .delete-btn {
                display: none !important;
            }
            table {
                page-break-inside: avoid;
                width: 100%;
                font-size: 12pt;
            }
        }
    </style>
</head>
<body>

<div class="top-bar">
    <a class="btn" href="#" onclick="window.print()"> Print</a>
    <a class="btn" href="logout.php"> Logout</a>
</div>

<h2>Contact Form Submissions</h2>

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Message</th>
        <th>Submitted At</th>
        <th>Action</th>
    </tr>
    <?php while($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= htmlspecialchars($row['name']) ?></td>
        <td><?= htmlspecialchars($row['email']) ?></td>
        <td><?= htmlspecialchars($row['phone']) ?></td>
        <td><?= nl2br(htmlspecialchars($row['message'])) ?></td>
        <td><?= $row['submitted_at'] ?></td>
        <td><a class="delete-btn" href="display.php?delete_id=<?= $row['id'] ?>" onclick="return confirm('Are you sure?')">Delete</a></td>
    </tr>
    <?php } ?>
</table>

</body>
</html>
