<?php
session_start();
require_once '../../config/database.php';

// Check admin authentication
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../login.php');
    exit;
}

$database = new Database();
$conn = $database->getConnection();

// Get all consultations
try {
    $stmt = $conn->query("SELECT * FROM consultations ORDER BY order_position ASC, created_at DESC");
    $consultations = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $total_consultations = count($consultations);
    $active_consultations = count(array_filter($consultations, fn($c) => $c['is_active'] == 1));
} catch (PDOException $e) {
    $consultations = [];
    $total_consultations = 0;
    $active_consultations = 0;
}
?>
<!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">
<link rel="stylesheet" href="../assets/css/admin-layout.css">
<link rel="stylesheet" href="../assets/css/admin-improvements.css">
<style>
body { font-family: 'Tajawal', sans-serif; }
.consultation-card { transition: all 0.3s ease; }
.consultation-card:hover { transform: translateY(-5px); box-shadow: 0 10px 25px rgba(0,0,0,0.1); }
</style>
</head>
<body class="bg-gray-50 min-h-screen">

<?php include '../includes/sidebar.php'; ?>

<div style="margin-right: 280px; padding: 24px;">
    
    <!-- Header -->
    <header class="bg-white shadow-sm border-b border-gray-200 rounded-lg mb-6 p-6">
        <div class="flex items-center justify-between">
            <div>
                <h1 class="text-3xl font-bold text-gray-900">📞 إدارة الاستشارات والمكالمات</h1>
                <p class="text-sm text-gray-500 mt-1">إدارة خدمات الاستشارات والمكالمات المتاحة للعملاء</p>
            </div>
            <div class="flex gap-3">
                <a href="bookings.php" class="bg-blue-500 text-white px-6 py-3 rounded-lg hover:bg-blue-600 transition flex items-center gap-2">
                    <i class="fas fa-calendar-check"></i>
                    <span>الحجوزات</span>
                </a>
                <a href="add.php" class="bg-gradient-to-r from-pink-500 to-pink-600 text-white px-6 py-3 rounded-lg hover:from-pink-600 hover:to-pink-700 transition flex items-center gap-2">
                    <i class="fas fa-plus"></i>
                    <span>إضافة استشارة</span>
                </a>
            </div>
        </div>
    </header>

    <!-- Statistics -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm">إجمالي الاستشارات</p>
                    <p class="text-3xl font-bold text-gray-900 mt-2"><?php echo $total_consultations; ?></p>
                </div>
                <div class="bg-blue-100 p-4 rounded-full">
                    <i class="fas fa-phone-alt text-blue-600 text-2xl"></i>
                </div>
            </div>
        </div>
        
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm">الاستشارات النشطة</p>
                    <p class="text-3xl font-bold text-green-600 mt-2"><?php echo $active_consultations; ?></p>
                </div>
                <div class="bg-green-100 p-4 rounded-full">
                    <i class="fas fa-check-circle text-green-600 text-2xl"></i>
                </div>
            </div>
        </div>
        
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm">غير نشطة</p>
                    <p class="text-3xl font-bold text-gray-400 mt-2"><?php echo $total_consultations - $active_consultations; ?></p>
                </div>
                <div class="bg-gray-100 p-4 rounded-full">
                    <i class="fas fa-pause-circle text-gray-400 text-2xl"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Consultations Grid -->
    <?php if (empty($consultations)): ?>
        <div class="bg-white rounded-lg shadow p-12 text-center">
            <div class="text-6xl mb-4">📞</div>
            <h3 class="text-xl font-bold text-gray-700 mb-2">لا توجد استشارات حالياً</h3>
            <p class="text-gray-500 mb-6">ابدأ بإضافة أول استشارة</p>
            <a href="add.php" class="inline-block bg-pink-600 text-white px-6 py-3 rounded-lg hover:bg-pink-700 transition">
                <i class="fas fa-plus mr-2"></i>إضافة استشارة
            </a>
        </div>
    <?php else: ?>
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            <?php foreach ($consultations as $consultation): 
                $discount_percentage = 0;
                if ($consultation['discount_price'] && $consultation['price'] > 0) {
                    $discount_percentage = round((($consultation['price'] - $consultation['discount_price']) / $consultation['price']) * 100);
                }
            ?>
            <div class="bg-white rounded-lg shadow consultation-card overflow-hidden">
                <!-- Image -->
                <div class="relative h-48 bg-gradient-to-br from-pink-400 to-pink-600 flex items-center justify-center">
                    <?php if ($consultation['image']): ?>
                        <img src="../../<?php echo htmlspecialchars($consultation['image']); ?>" 
                             alt="<?php echo htmlspecialchars($consultation['title']); ?>"
                             class="w-full h-full object-cover">
                    <?php else: ?>
                        <i class="fas fa-phone-alt text-white text-6xl"></i>
                    <?php endif; ?>
                    
                    <?php if ($discount_percentage > 0): ?>
                    <div class="absolute top-3 right-3 bg-red-500 text-white px-3 py-1 rounded-full text-sm font-bold">
                        خصم <?php echo $discount_percentage; ?>%
                    </div>
                    <?php endif; ?>
                    
                    <div class="absolute top-3 left-3">
                        <span class="px-3 py-1 rounded-full text-xs font-bold <?php echo $consultation['is_active'] ? 'bg-green-500 text-white' : 'bg-gray-500 text-white'; ?>">
                            <?php echo $consultation['is_active'] ? 'نشط' : 'غير نشط'; ?>
                        </span>
                    </div>
                </div>
                
                <!-- Content -->
                <div class="p-5">
                    <h3 class="text-lg font-bold text-gray-900 mb-2"><?php echo htmlspecialchars($consultation['title']); ?></h3>
                    <p class="text-gray-600 text-sm mb-4 line-clamp-2"><?php echo htmlspecialchars(substr($consultation['description'], 0, 100)); ?>...</p>
                    
                    <div class="flex items-center gap-4 text-sm text-gray-500 mb-4">
                        <span><i class="far fa-clock mr-1"></i><?php echo $consultation['duration']; ?> دقيقة</span>
                        <?php if ($consultation['consultant_name']): ?>
                        <span><i class="far fa-user mr-1"></i><?php echo htmlspecialchars($consultation['consultant_name']); ?></span>
                        <?php endif; ?>
                    </div>
                    
                    <div class="flex items-center justify-between mb-4">
                        <div>
                            <?php if ($consultation['discount_price']): ?>
                                <span class="text-gray-400 line-through text-sm"><?php echo number_format($consultation['price'], 0); ?> ج.م</span>
                                <span class="text-2xl font-bold text-pink-600 block"><?php echo number_format($consultation['discount_price'], 0); ?> ج.م</span>
                            <?php else: ?>
                                <span class="text-2xl font-bold text-pink-600"><?php echo number_format($consultation['price'], 0); ?> ج.م</span>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <div class="flex gap-2">
                        <a href="edit.php?id=<?php echo $consultation['id']; ?>" class="flex-1 bg-blue-500 text-white text-center py-2 rounded-lg hover:bg-blue-600 transition">
                            <i class="fas fa-edit"></i> تعديل
                        </a>
                        <button onclick="deleteConsultation(<?php echo $consultation['id']; ?>)" class="bg-red-500 text-white px-4 py-2 rounded-lg hover:bg-red-600 transition">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<script>
function deleteConsultation(id) {
    if (!confirm('هل أنت متأكد من حذف هذه الاستشارة؟')) return;
    
    fetch('delete.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ id: id })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('✅ تم الحذف بنجاح');
            location.reload();
        } else {
            alert('❌ ' + data.message);
        }
    });
}
</script>
</body>
</html>
