<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}

require_once "includes/config.php";

if(!isset($_GET['id']) || empty($_GET['id'])){
    header("location: contacts.php");
    exit;
}

$id = (int)$_GET['id'];

// Prepare statement
$stmt = $conn->prepare("SELECT id, name, email, phone, message, submission_date, status FROM contact_submissions WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();

// Replace get_result() with bind_result + fetch
$stmt->bind_result($id, $name, $email, $phone, $message, $submission_date, $status);

if($stmt->fetch()){
    // Data is now in variables: $id, $name, $email, $phone, $message, $submission_date, $status
} else {
    // No record found
    echo "<div class='alert alert-danger'>No submission found.</div>";
    exit;
}

$stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Contact Submission</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h2>Contact Submission Details</h2>
    <table class="table table-bordered mt-3">
        <tr><th>Name</th><td><?php echo htmlspecialchars($name); ?></td></tr>
        <tr><th>Email</th><td><?php echo htmlspecialchars($email); ?></td></tr>
        <tr><th>Phone</th><td><?php echo htmlspecialchars($phone); ?></td></tr>
        <tr><th>Message</th><td><?php echo nl2br(htmlspecialchars($message)); ?></td></tr>
        <tr><th>Date</th><td><?php echo date('M d, Y h:i A', strtotime($submission_date)); ?></td></tr>
        <tr><th>Status</th><td><?php echo htmlspecialchars($status); ?></td></tr>
    </table>
    <a href="contacts.php" class="btn btn-secondary">Back</a>
</div>
</body>
</html>
