<?php
require_once '../../config/database.php';
$database = new Database();
$conn = $database->getConnection();
$message = '';

// الحصول على المنتجات
$stmt = $conn->query("SELECT id, name FROM products ORDER BY name");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

// الحصول على الوصفات
$stmt = $conn->query("SELECT pr.*, p.name as product_name FROM product_recipes pr LEFT JOIN products p ON pr.product_id = p.id ORDER BY pr.id DESC");
$recipes = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>وصفات المنتجات</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-gray-50">
    <div class="bg-gradient-to-r from-orange-600 to-orange-800 text-white p-6">
        <div class="max-w-7xl mx-auto flex justify-between items-center">
            <h1 class="text-3xl font-bold">📖 وصفات المنتجات</h1>
            <a href="index.php" class="bg-white text-orange-600 px-6 py-2 rounded-lg">العودة</a>
        </div>
    </div>
    <div class="max-w-7xl mx-auto p-6">
        <div class="bg-blue-50 border-r-4 border-blue-500 p-6 rounded-lg mb-6">
            <h3 class="text-xl font-bold text-blue-900 mb-2">
                <i class="fas fa-info-circle ml-2"></i> وصفات المنتجات
            </h3>
            <p class="text-blue-800 mb-3">
                هنا يمكنك إنشاء وصفة لكل منتج تحدد فيها:
            </p>
            <ul class="list-disc list-inside text-blue-700 space-y-1">
                <li>المواد الخام المطلوبة (زيوت، فيتامينات، عطور)</li>
                <li>مواد التعبئة (زجاجات، علب، أغطية)</li>
                <li>مواد التغليف (بابلز، كراتين، فلايرات)</li>
                <li>حجم الدفعة (عدد القطع)</li>
                <li>التكلفة الإجمالية (محسوبة تلقائياً)</li>
            </ul>
        </div>

        <div class="bg-white rounded-lg shadow overflow-hidden">
            <div class="p-6 bg-gray-50 border-b">
                <h2 class="text-xl font-bold">الوصفات الموجودة</h2>
            </div>
            <table class="w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-right">المنتج</th>
                        <th class="px-6 py-3 text-right">اسم الوصفة</th>
                        <th class="px-6 py-3 text-right">حجم الدفعة</th>
                        <th class="px-6 py-3 text-right">التكلفة الإجمالية</th>
                        <th class="px-6 py-3 text-right">تكلفة القطعة</th>
                        <th class="px-6 py-3 text-right">إجراءات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($recipes) > 0): ?>
                        <?php foreach($recipes as $r): ?>
                        <tr class="border-t hover:bg-gray-50">
                            <td class="px-6 py-4 font-medium"><?php echo htmlspecialchars($r['product_name']); ?></td>
                            <td class="px-6 py-4"><?php echo htmlspecialchars($r['recipe_name']); ?></td>
                            <td class="px-6 py-4"><?php echo $r['batch_size']; ?> قطعة</td>
                            <td class="px-6 py-4"><?php echo number_format($r['total_cost'], 2); ?> ج.م</td>
                            <td class="px-6 py-4 font-bold text-green-600"><?php echo number_format($r['cost_per_unit'], 2); ?> ج.م</td>
                            <td class="px-6 py-4">
                                <button class="bg-blue-600 text-white px-3 py-1 rounded mr-2">عرض</button>
                                <button class="bg-red-600 text-white px-3 py-1 rounded">حذف</button>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="px-6 py-8 text-center text-gray-500">
                                لا توجد وصفات حالياً. ابدأ بإنشاء وصفة لمنتجاتك!
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="mt-6 bg-yellow-50 border-r-4 border-yellow-500 p-6 rounded-lg">
            <h3 class="text-xl font-bold text-yellow-900 mb-2">
                <i class="fas fa-lightbulb ml-2"></i> كيفية الاستخدام
            </h3>
            <ol class="list-decimal list-inside text-yellow-800 space-y-2">
                <li>أضف جميع المواد الخام ومواد التعبئة والتغليف أولاً</li>
                <li>أنشئ وصفة جديدة لكل منتج</li>
                <li>حدد المكونات والكميات المطلوبة</li>
                <li>النظام سيحسب التكلفة تلقائياً</li>
                <li>استخدم الوصفة في دفعات الإنتاج</li>
            </ol>
        </div>
    </div>
</body>
</html>
