<?php include 'includes/header.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Panel - Contact Submissions</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Segoe UI', sans-serif;
            background-color: #f4f6f9;
        }
        .sidebar {
            height: 100vh;
            width: 250px;
            position: fixed;
            top: 0;
            left: 0;
            background-color: #1F4E96; /* Logo Blue */
            color: white;
            padding-top: 60px;
        }
        .sidebar h4 {
            text-align: center;
            margin-bottom: 30px;
            font-weight: bold;
            color: white;
        }
        .sidebar a {
            display: block;
            padding: 12px 20px;
            color: white;
            text-decoration: none;
            transition: 0.3s;
        }
        .sidebar a:hover, 
        .sidebar a.active {
            background-color: #163B70; /* Darker Logo Blue */
        }
        .main-content {
            margin-left: 250px;
            padding: 40px;
        }
        .header {
            background-color: white;
            padding: 20px;
            border-bottom: 3px solid #1F4E96; /* Blue accent */
        }
        .header h2 {
            color: #1F4E96;
            font-weight: bold;
        }
        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0,0,0,0.05);
            padding: 30px;
            overflow-x: auto;
        }
        table {
            width: 100%;
            min-width: 800px;
        }
        .btn-primary {
            background-color: #1F4E96;
            border-color: #1F4E96;
        }
        .btn-primary:hover {
            background-color: #163B70;
            border-color: #163B70;
        }
        .btn-danger {
            background-color: #dc3545;
            border-color: #dc3545;
        }
        .btn-action {
            margin-right: 5px;
        }
        @media (max-width: 768px) {
            .sidebar {
                position: relative;
                width: 100%;
                height: auto;
            }
            .main-content {
                margin-left: 0;
                padding: 20px;
            }
        }
    </style>
</head>
<body>

<div class="sidebar">
    <h4>Admin Panel</h4>
    <a href="dashboard.php">Dashboard</a>
    <a href="main_website_conatcts.php">Home Page Contacts</a>
  <a href="contact-us.php" class="active">Contacts Page Contacts</a>
  <a href="servicescontact.php">Service Page Contacts</a>
</div>

<div class="main-content">
    <div class="header">
        <h2>Contact Form Submissions</h2>
    </div>
    <div class="card mt-4">
        <?php
        ini_set('display_errors', 1);
        error_reporting(E_ALL);

        include 'config.php';

        // timezone
        date_default_timezone_set("Asia/Kolkata");

        // Delete submission
        if (isset($_GET['delete_id'])) {
            $idToDelete = intval($_GET['delete_id']);
            try {
                $stmt = $conn->prepare("DELETE FROM contact_messages WHERE id = ?");
                $stmt->execute([$idToDelete]);
                echo "<p class='alert alert-success'>✅ Submission ID $idToDelete deleted successfully.</p>";
            } catch (PDOException $e) {
                echo "<p class='alert alert-danger'>❌ Error deleting submission: " . $e->getMessage() . "</p>";
            }
        }

        // Fetch submissions
        try {
            $stmt = $conn->query("SELECT * FROM contact_messages ORDER BY id DESC");
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

            if (count($data) > 0) {
                echo "<table class='table table-striped table-hover'>
                        <thead class='table-dark'>
                            <tr>
                                <th>#</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Message</th>
                                <th>Date & Time</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>";
                foreach ($data as $row) {
                    $date = new DateTime($row['submitted_at'], new DateTimeZone('UTC'));
                    $date->setTimezone(new DateTimeZone('Asia/Kolkata'));
                    $formattedDate = $date->format("d-m-Y h:i A");

                    echo "<tr>
                            <td>{$row['id']}</td>
                            <td>{$row['name']}</td>
                            <td>{$row['email']}</td>
                            <td>{$row['phone']}</td>
                            <td>{$row['message']}</td>
                            <td>{$formattedDate}</td>
                            <td>
                                <a href='mailto:{$row['email']}?subject=Reply to your Inquiry&body=Hello {$row['name']}, regarding your request about {$row['message']}...' 
                                   class='btn btn-primary btn-sm btn-action'>Reply</a>
                                <a href='?delete_id={$row['id']}' 
                                   class='btn btn-danger btn-sm btn-action' 
                                   onclick=\"return confirm('Are you sure you want to delete this record?');\">Delete</a>
                            </td>
                        </tr>";
                }
                echo "</tbody></table>";
            } else {
                echo "<p class='alert alert-info'>No submissions found.</p>";
            }
        } catch (PDOException $e) {
            echo "<p class='alert alert-danger'>❌ Database Error: " . $e->getMessage() . "</p>";
        }
        ?>
    </div>
</div>
</body>
</html>
