<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}

require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

// Handle Delete
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $stmt = $pdo->prepare("DELETE FROM contact_messages WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: messages.php?success=delete');
    exit();
}

// Handle Mark as Read
if (isset($_GET['mark_read'])) {
    $id = (int)$_GET['mark_read'];
    $stmt = $pdo->prepare("UPDATE contact_messages SET is_read = 1 WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: messages.php?success=read');
    exit();
}

// Ensure the messages table exists before querying
ensure_contact_messages_table($pdo);
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();

$stmt = $pdo->query("SELECT * FROM contact_messages ORDER BY created_at DESC");
$messages = $stmt->fetchAll();

$pageTitle = $t['messages_title'];
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php
    $currentPage = 'messages';
    include 'includes/sidebar.php';
    ?>

    <div class="main-content">
        <div class="topbar">
            <h1>✉️ <?php echo $pageTitle; ?></h1>
        </div>

        <div class="table-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th><?php echo $t['msg_from']; ?></th>
                            <th><?php echo $t['contact_message']; ?></th>
                            <th style="width:150px;"><?php echo $t['msg_date']; ?></th>
                            <th style="width:120px;"><?php echo $t['msg_status']; ?></th>
                            <th style="width:180px;text-align:center;"><?php echo $t['users_table_actions']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($messages)): ?>
                            <tr><td colspan="5" class="text-center py-5 text-muted">😶 <?php echo $t['msg_no_messages']; ?></td></tr>
                        <?php else: ?>
                            <?php foreach ($messages as $msg): ?>
                                <tr class="<?php echo $msg['is_read'] ? 'opacity-75' : 'fw-bold'; ?>">
                                    <td>
                                        <div class="text-dark"><?php echo htmlspecialchars($msg['name']); ?></div>
                                        <a href="mailto:<?php echo htmlspecialchars($msg['email']); ?>" class="text-muted small"><?php echo htmlspecialchars($msg['email']); ?></a>
                                    </td>
                                    <td class="small" style="white-space: pre-wrap;"><?php echo htmlspecialchars($msg['message']); ?></td>
                                    <td class="text-muted small"><?php echo date('Y-m-d H:i', strtotime($msg['created_at'])); ?></td>
                                    <td>
                                        <?php if ($msg['is_read']): ?>
                                            <span class="badge bg-success"><?php echo $t['msg_read']; ?></span>
                                        <?php else: ?>
                                            <span class="badge bg-warning text-dark"><?php echo $t['msg_unread']; ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td style="text-align:center;">
                                        <?php if (!$msg['is_read']): ?>
                                            <a href="messages.php?mark_read=<?php echo $msg['id']; ?>" class="btn btn-outline-success btn-sm me-1">✓ <?php echo $t['msg_mark_read']; ?></a>
                                        <?php endif; ?>
                                        <a href="messages.php?delete=<?php echo $msg['id']; ?>" class="btn btn-delete btn-sm" data-confirm="<?php echo htmlspecialchars($t['msg_delete_confirm']); ?>">🗑️</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>فح