<?php
/**
 * Contact Submissions Management
 */

session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/includes/auth.php';

checkAdminAuth();

$pdo = getDBConnection();

// Get all contacts
$contacts = $pdo->query("
    SELECT id, name, email, phone, service, message, status, created_at
    FROM contact_submissions
    ORDER BY created_at DESC
")->fetchAll();
?>
$pageTitle = 'Contacts';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - Admin Panel</title>
    <link rel="stylesheet" href="assets/admin-modern.css">
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>
    
    <div class="admin-main">
        <?php include 'includes/header-new.php'; ?>
        
        <div class="admin-container">
            <div class="card slide-up">
                <div class="card-header">
                    <h3 class="card-title">Contact Submissions (<?php echo count($contacts); ?>)</h3>
                </div>
                <div style="overflow-x: auto;">
                    <table class="data-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Service</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($contacts as $contact): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($contact['name']); ?></td>
                        <td><?php echo htmlspecialchars($contact['email']); ?></td>
                        <td><?php echo htmlspecialchars($contact['phone']); ?></td>
                        <td><?php echo htmlspecialchars($contact['service'] ?: 'N/A'); ?></td>
                        <td>
                            <span class="status-badge status-<?php echo $contact['status']; ?>">
                                <?php echo ucfirst($contact['status']); ?>
                            </span>
                        </td>
                        <td><?php echo date('M d, Y H:i', strtotime($contact['created_at'])); ?></td>
                            <td>
                                <div style="display: flex; gap: 0.5rem;">
                                    <a href="contact-view.php?id=<?php echo $contact['id']; ?>" class="btn btn-sm btn-secondary">View</a>
                                    <a href="contact-delete.php?id=<?php echo $contact['id']; ?>" 
                                       class="btn btn-sm btn-danger"
                                       onclick="return confirm('Delete this contact?')">Delete</a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                </div>
            </div>
        </div>
    </div>
    
    <script src="assets/admin.js"></script>
</body>
</html>

