<?php
require_once '../config/database.php';

session_start();

// Check admin access
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: login.php');
    exit;
}

$database = new Database();
$db = $database->getConnection();

// Get all messages
$query = "SELECT * FROM contact_messages ORDER BY created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute();
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['mark_read'])) {
        $msg_id = $_POST['message_id'];
        $query = "UPDATE contact_messages SET is_read = 1 WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->execute([$msg_id]);
        $message = 'تم تحديث حالة الرسالة!';
        header("Location: messages.php");
        exit;
    } elseif (isset($_POST['delete_message'])) {
        $msg_id = $_POST['message_id'];
        $query = "DELETE FROM contact_messages WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->execute([$msg_id]);
        $message = 'تم حذف الرسالة بنجاح!';
        header("Location: messages.php");
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>الرسائل - Roz Skin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { font-family: 'Tajawal', sans-serif; }
    </style>
</head>
<body class="bg-gray-50">
    <div class="min-h-screen">
        <!-- Header -->
        <header class="bg-white shadow-sm border-b border-gray-200 sticky top-0 z-20">
            <div class="flex items-center justify-between px-6 py-4">
                <div class="flex items-center space-x-4 space-x-reverse">
                    <a href="dashboard.php" class="text-gray-600 hover:text-gray-900">
                        <i class="fas fa-arrow-right text-xl"></i>
                    </a>
                    <h1 class="text-2xl font-bold text-gray-900">رسائل العملاء</h1>
                </div>
            </div>
        </header>

        <div class="p-6">
            <?php if ($message): ?>
                <div class="bg-green-50 border-r-4 border-green-400 p-4 mb-6">
                    <p class="text-sm text-green-700"><?php echo $message; ?></p>
                </div>
            <?php endif; ?>

            <!-- Messages List -->
            <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                <div class="px-6 py-5 border-b border-gray-200">
                    <h3 class="text-xl font-semibold text-gray-900">جميع الرسائل (<?php echo count($messages); ?>)</h3>
                </div>
                <div class="divide-y divide-gray-200">
                    <?php if (!empty($messages)): ?>
                        <?php foreach ($messages as $msg): ?>
                            <div class="p-6 hover:bg-gray-50 <?php echo !$msg['is_read'] ? 'bg-blue-50' : ''; ?>">
                                <div class="flex items-start justify-between">
                                    <div class="flex-1">
                                        <div class="flex items-center space-x-3 space-x-reverse mb-2">
                                            <div class="w-10 h-10 bg-gradient-to-br from-cyan-500 to-blue-600 rounded-full flex items-center justify-center text-white font-bold">
                                                <?php echo strtoupper(substr($msg['name'], 0, 1)); ?>
                                            </div>
                                            <div>
                                                <h4 class="text-lg font-semibold text-gray-900"><?php echo htmlspecialchars($msg['name']); ?></h4>
                                                <p class="text-sm text-gray-500"><?php echo htmlspecialchars($msg['email']); ?></p>
                                            </div>
                                            <?php if (!$msg['is_read']): ?>
                                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                                    جديد
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                        <div class="mr-13">
                                            <p class="text-sm font-medium text-gray-700 mb-1">الموضوع: <?php echo htmlspecialchars($msg['subject']); ?></p>
                                            <p class="text-sm text-gray-600"><?php echo htmlspecialchars($msg['message']); ?></p>
                                            <p class="text-xs text-gray-400 mt-2">
                                                <i class="fas fa-clock ml-1"></i>
                                                <?php echo date('Y-m-d H:i', strtotime($msg['created_at'])); ?>
                                            </p>
                                        </div>
                                    </div>
                                    <div class="flex flex-col space-y-2 mr-4">
                                        <?php if (!$msg['is_read']): ?>
                                            <form method="POST" class="inline-block">
                                                <input type="hidden" name="message_id" value="<?php echo $msg['id']; ?>">
                                                <button type="submit" name="mark_read" class="text-blue-600 hover:text-blue-900 text-sm">
                                                    <i class="fas fa-check ml-1"></i> تحديد كمقروء
                                                </button>
                                            </form>
                                        <?php endif; ?>
                                        <a href="mailto:<?php echo htmlspecialchars($msg['email']); ?>" class="text-green-600 hover:text-green-900 text-sm">
                                            <i class="fas fa-reply ml-1"></i> رد
                                        </a>
                                        <form method="POST" class="inline-block" onsubmit="return confirm('هل أنت متأكد من حذف هذه الرسالة؟');">
                                            <input type="hidden" name="message_id" value="<?php echo $msg['id']; ?>">
                                            <button type="submit" name="delete_message" class="text-red-600 hover:text-red-900 text-sm">
                                                <i class="fas fa-trash ml-1"></i> حذف
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="p-12 text-center text-gray-500">
                            <i class="fas fa-envelope text-4xl mb-4"></i>
                            <p>لا توجد رسائل حالياً</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
