<?php include 'includes/header.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>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 */
    .sidebar {
      height: 100vh;
      width: 250px;
      position: fixed;
      top: 0;
      left: 0;
      background-color: #ffffff;
      color: #1a1a1a;
      padding-top: 10px;
      overflow-y: auto;
      border-right: 1px solid #ddd;
    }
    .sidebar .logo {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 15px 0;
    }
    .sidebar .logo img {
      max-width: 150px;
    }
    .sidebar a {
      display: block;
      padding: 12px 20px;
      color: #1a1a1a;
      text-decoration: none;
      transition: 0.3s;
      font-weight: 500;
    }
    .sidebar a:hover,
    .sidebar a.active {
      background-color: #e6f7ff;
      color: #007acc;
      border-radius: 6px;
    }

    /* Main Content */
    .main-content {
      margin-left: 250px;
      padding: 40px;
    }
    .header {
      background-color: #fff;
      padding: 20px;
      border-bottom: 3px solid #007acc;
      border-radius: 8px 8px 0 0;
    }
    .header h2 {
      color: #007acc;
      font-weight: 600;
      margin-bottom: 5px;
    }
    .header p {
      color: #555;
      margin: 0;
    }
    .card {
      background-color: #fff;
      border-radius: 12px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.05);
      padding: 30px;
      overflow-x: auto;
      margin-top: 30px;
    }
    table {
      width: 100%;
      min-width: 800px;
    }
    .btn-primary {
      background-color: #007acc;
      border-color: #007acc;
    }
    .btn-primary:hover {
      background-color: #005f99;
      border-color: #005f99;
    }
    .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>

<!-- Sidebar -->
<div class="sidebar">
  <div class="logo">
    <img src="https://demo.godsonindustries.com/sunrediant/assets/images/logo.png" alt="Logo">
  </div>
  <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>
  <!--<a href="Conatct_WebDev.php" class="active">Website Development Contacts</a>-->
  <a href="logout.php">Logout</a>
</div>

<!-- Main Content -->
<div class="main-content">
  <div class="header">
    <h2>Contact Submissions From Contact Us Page</h2>
    <p>Manage and review all website development form entries below.</p>
  </div>

  <div class="card">
    <?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 sunrediant_contacts 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 sunrediant_contacts 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>

