﻿<?php
require_once '../config/database.php';
require_once '../models/review.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();
$review_model = new Review($db);

$all_reviews = $review_model->getAllReviews();
$total_reviews = count($all_reviews);
$average_rating = $total_reviews > 0 ? array_reduce($all_reviews, function($total, $review) {
    return $total + $review['rating'];
}, 0) / $total_reviews : 0;

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['approve_review'])) {
        $review_id = $_POST['review_id'];
        $query = "UPDATE reviews SET is_approved = 1 WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->execute([$review_id]);
        $message = 'تم اعتماد التقييم بنجاح!';
        $all_reviews = $review_model->getAllReviews();
    } elseif (isset($_POST['toggle_visibility'])) {
        $review_id = $_POST['review_id'];
        $query = "UPDATE reviews SET is_visible = NOT is_visible WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->execute([$review_id]);
        $message = 'تم تحديث حالة الظهور بنجاح!';
        $all_reviews = $review_model->getAllReviews();
    } elseif (isset($_POST['delete_review'])) {
        $review_id = $_POST['review_id'];
        if ($review_model->delete($review_id)) {
            $message = 'تم حذف التقييم بنجاح!';
            $all_reviews = $review_model->getAllReviews();
        }
    }
}
?>
<!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>
                    <div>
                        <h1 class="text-2xl font-bold text-gray-900">إدارة التقييمات</h1>
                        <p class="text-sm text-gray-500">إجمالي: <?php echo $total_reviews; ?> | متوسط: <?php echo number_format($average_rating, 1); ?>★</p>
                    </div>
                </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; ?>

            <!-- Statistics Cards -->
            <?php
                $approved_count = count(array_filter($all_reviews, fn($r) => $r['is_approved']));
                $visible_count = count(array_filter($all_reviews, fn($r) => $r['is_visible']));
                $pending_count = count(array_filter($all_reviews, fn($r) => !$r['is_approved']));
            ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-4 mb-6">
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">إجمالي التقييمات</p>
                            <p class="text-2xl font-bold text-gray-900"><?php echo $total_reviews; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-star text-purple-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">متوسط التقييم</p>
                            <p class="text-2xl font-bold text-yellow-600"><?php echo number_format($average_rating, 1); ?>★</p>
                        </div>
                        <div class="w-12 h-12 bg-yellow-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-star-half-alt text-yellow-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">معتمدة</p>
                            <p class="text-2xl font-bold text-green-600"><?php echo $approved_count; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-check-circle text-green-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">في الانتظار</p>
                            <p class="text-2xl font-bold text-yellow-600"><?php echo $pending_count; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-yellow-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-clock text-yellow-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">مرئية</p>
                            <p class="text-2xl font-bold text-blue-600"><?php echo $visible_count; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-eye text-blue-600 text-xl"></i>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Reviews Table -->
            <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">جميع التقييمات</h3>
                </div>
                <div class="overflow-x-auto">
                    <?php if (!empty($all_reviews)): ?>
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">العميل</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">المنتج</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">التقييم</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">التعليق</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الحالة</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">التاريخ</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <?php foreach ($all_reviews as $review): ?>
                                    <tr class="hover:bg-gray-50">
                                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?php echo htmlspecialchars($review['user_name']); ?></td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?php echo htmlspecialchars($review['product_name']); ?></td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="flex items-center">
                                                <?php for ($i = 1; $i <= 5; $i++): ?>
                                                    <span class="text-lg <?php echo $i <= $review['rating'] ? 'text-yellow-400' : 'text-gray-300'; ?>">★</span>
                                                <?php endfor; ?>
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 text-sm text-gray-900 max-w-xs truncate"><?php echo htmlspecialchars($review['comment']); ?></td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="flex flex-col space-y-1">
                                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <?php echo $review['is_approved'] ? 'bg-green-100 text-green-800' : 'bg-yellow-100 text-yellow-800'; ?>">
                                                    <?php echo $review['is_approved'] ? 'معتمد' : 'في الانتظار'; ?>
                                                </span>
                                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <?php echo $review['is_visible'] ? 'bg-blue-100 text-blue-800' : 'bg-gray-100 text-gray-800'; ?>">
                                                    <?php echo $review['is_visible'] ? 'مرئي' : 'مخفي'; ?>
                                                </span>
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('Y-m-d', strtotime($review['created_at'])); ?></td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm space-y-1">
                                            <?php if (!$review['is_approved']): ?>
                                                <form method="POST" class="inline-block">
                                                    <input type="hidden" name="review_id" value="<?php echo $review['id']; ?>">
                                                    <button type="submit" name="approve_review" class="text-green-600 hover:text-green-900 block">
                                                        <i class="fas fa-check ml-1"></i> اعتماد
                                                    </button>
                                                </form>
                                            <?php endif; ?>
                                            <form method="POST" class="inline-block">
                                                <input type="hidden" name="review_id" value="<?php echo $review['id']; ?>">
                                                <button type="submit" name="toggle_visibility" class="text-blue-600 hover:text-blue-900 block">
                                                    <i class="fas fa-eye ml-1"></i> <?php echo $review['is_visible'] ? 'إخفاء' : 'إظهار'; ?>
                                                </button>
                                            </form>
                                            <form method="POST" class="inline-block" onsubmit="return confirm('هل أنت متأكد من حذف هذا التقييم؟');">
                                                <input type="hidden" name="review_id" value="<?php echo $review['id']; ?>">
                                                <button type="submit" name="delete_review" class="text-red-600 hover:text-red-900 block">
                                                    <i class="fas fa-trash ml-1"></i> حذف
                                                </button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php else: ?>
                        <div class="p-8 text-center text-gray-500">
                            <i class="fas fa-star text-4xl mb-4"></i>
                            <p>لا توجد تقييمات حالياً</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
