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

// الحصول على دفعات الإنتاج
$stmt = $conn->query("SELECT pb.*, pr.recipe_name, p.name as product_name FROM production_batches pb LEFT JOIN product_recipes pr ON pb.recipe_id = pr.id LEFT JOIN products p ON pr.product_id = p.id ORDER BY pb.created_at DESC");
$batches = $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-red-600 to-red-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-red-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>
                <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>
                        <th class="px-6 py-3 text-right">الحالة</th>
                        <th class="px-6 py-3 text-right">التاريخ</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($batches) > 0): ?>
                        <?php foreach($batches as $b): 
                            $status_colors = [
                                'pending' => 'bg-yellow-100 text-yellow-800',
                                'in_progress' => 'bg-blue-100 text-blue-800',
                                'completed' => 'bg-green-100 text-green-800',
                                'cancelled' => 'bg-red-100 text-red-800'
                            ];
                            $status_names = [
                                'pending' => 'قيد الانتظار',
                                'in_progress' => 'جاري',
                                'completed' => 'مكتمل',
                                'cancelled' => 'ملغي'
                            ];
                        ?>
                        <tr class="border-t hover:bg-gray-50">
                            <td class="px-6 py-4 font-mono"><?php echo htmlspecialchars($b['batch_number']); ?></td>
                            <td class="px-6 py-4 font-medium"><?php echo htmlspecialchars($b['product_name']); ?></td>
                            <td class="px-6 py-4"><?php echo htmlspecialchars($b['recipe_name']); ?></td>
                            <td class="px-6 py-4 font-bold"><?php echo $b['quantity_produced']; ?> قطعة</td>
                            <td class="px-6 py-4"><?php echo number_format($b['total_cost'], 2); ?> ج.م</td>
                            <td class="px-6 py-4 font-bold text-green-600"><?php echo number_format($b['cost_per_unit'], 2); ?> ج.م</td>
                            <td class="px-6 py-4">
                                <span class="px-3 py-1 rounded-full text-xs <?php echo $status_colors[$b['status']]; ?>">
                                    <?php echo $status_names[$b['status']]; ?>
                                </span>
                            </td>
                            <td class="px-6 py-4 text-sm"><?php echo date('Y-m-d', strtotime($b['production_date'])); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="8" class="px-6 py-8 text-center text-gray-500">
                                لا توجد دفعات إنتاج حالياً. ابدأ بإنشاء دفعة إنتاج جديدة!
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="mt-6 bg-green-50 border-r-4 border-green-500 p-6 rounded-lg">
            <h3 class="text-xl font-bold text-green-900 mb-2">
                <i class="fas fa-check-circle ml-2"></i> النظام جاهز!
            </h3>
            <p class="text-green-800">
                قاعدة البيانات مكتملة 100%. يمكنك البدء بإضافة المواد والوصفات ثم إنشاء دفعات الإنتاج.
            </p>
        </div>
    </div>
</body>
</html>
