<?php
/**
 * View Contact Submission
 */

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

checkAdminAuth();

$id = intval($_GET['id'] ?? 0);
$pdo = getDBConnection();
$message = '';

// Handle status update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['status'])) {
    $status = sanitizeInput($_POST['status']);
    $updateStmt = $pdo->prepare("UPDATE contact_submissions SET status = :status WHERE id = :id");
    $updateStmt->execute([':status' => $status, ':id' => $id]);
    $message = 'Status updated successfully!';
}

$contact = $pdo->prepare("SELECT * FROM contact_submissions WHERE id = :id");
$contact->execute([':id' => $id]);
$contact = $contact->fetch();

if (!$contact) {
    header('Location: contacts.php');
    exit;
}

// Mark as read if still new
if ($contact['status'] === 'new') {
    $updateStmt = $pdo->prepare("UPDATE contact_submissions SET status = 'read' WHERE id = :id");
    $updateStmt->execute([':id' => $id]);
    $contact['status'] = 'read';
}
?>
$pageTitle = 'View Contact';
?>
<!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="form-group">
                <label>Name</label>
                <div class="view-value"><?php echo htmlspecialchars($contact['name']); ?></div>
            </div>
            <div class="form-group">
                <label>Email</label>
                <div class="view-value">
                    <a href="mailto:<?php echo htmlspecialchars($contact['email']); ?>">
                        <?php echo htmlspecialchars($contact['email']); ?>
                    </a>
                </div>
            </div>
            <div class="form-group">
                <label>Phone</label>
                <div class="view-value">
                    <a href="tel:<?php echo htmlspecialchars($contact['phone']); ?>">
                        <?php echo htmlspecialchars($contact['phone']); ?>
                    </a>
                </div>
            </div>
            <div class="form-group">
                <label>Service Interest</label>
                <div class="view-value"><?php echo htmlspecialchars($contact['service'] ?: 'N/A'); ?></div>
            </div>
            <div class="form-group">
                <label>Message</label>
                <div class="view-value"><?php echo nl2br(htmlspecialchars($contact['message'])); ?></div>
            </div>
            <div class="form-group">
                <label>Status</label>
                <form method="POST" style="display: inline-block;">
                    <select name="status" onchange="this.form.submit()" style="padding: 0.5rem; border-radius: 6px;">
                        <option value="new" <?php echo $contact['status'] === 'new' ? 'selected' : ''; ?>>New</option>
                        <option value="read" <?php echo $contact['status'] === 'read' ? 'selected' : ''; ?>>Read</option>
                        <option value="replied" <?php echo $contact['status'] === 'replied' ? 'selected' : ''; ?>>Replied</option>
                        <option value="archived" <?php echo $contact['status'] === 'archived' ? 'selected' : ''; ?>>Archived</option>
                    </select>
                </form>
            </div>
            <div class="form-group">
                <label>Submitted</label>
                <div class="view-value"><?php echo date('F d, Y \a\t H:i', strtotime($contact['created_at'])); ?></div>
            </div>
            
            <?php if ($message): ?>
                <div class="message success" style="margin-bottom: 1rem;"><?php echo htmlspecialchars($message); ?></div>
            <?php endif; ?>
            
                <div style="margin-top: 2rem; display: flex; gap: 1rem;">
                    <a href="contacts.php" class="btn btn-primary">Back to Contacts</a>
                    <a href="contact-delete.php?id=<?php echo $contact['id']; ?>" 
                       class="btn btn-danger"
                       onclick="return confirm('Delete this contact submission?')">Delete</a>
                </div>
            </div>
        </div>
    </div>
    
    <style>
        .view-value {
            padding: 0.75rem;
            background: var(--bg-primary);
            border-radius: var(--radius-md);
            color: var(--text-primary);
        }
        .view-value a {
            color: var(--secondary-color);
            text-decoration: none;
        }
        .view-value a:hover {
            text-decoration: underline;
        }
    </style>
    
    <script src="assets/admin.js"></script>
</body>
</html>

