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

// إحصائيات
$stats = [];
$stmt = $conn->query("SELECT COUNT(*) as total, SUM(quantity * unit_cost) as value FROM raw_materials");
$stats['raw'] = $stmt->fetch(PDO::FETCH_ASSOC);

$stmt = $conn->query("SELECT COUNT(*) as total, SUM(quantity * unit_cost) as value FROM packaging_materials");
$stats['packaging'] = $stmt->fetch(PDO::FETCH_ASSOC);

$stmt = $conn->query("SELECT COUNT(*) as total, SUM(quantity * unit_cost) as value FROM wrapping_materials");
$stats['wrapping'] = $stmt->fetch(PDO::FETCH_ASSOC);

$stmt = $conn->query("SELECT COUNT(*) FROM product_recipes");
$stats['recipes'] = $stmt->fetchColumn();

$stmt = $conn->query("SELECT COUNT(*) FROM production_batches WHERE status = 'completed'");
$stats['batches'] = $stmt->fetchColumn();
?>
<!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-teal-600 to-teal-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-teal-600 px-6 py-2 rounded-lg">العودة</a>
        </div>
    </div>
    <div class="max-w-7xl mx-auto p-6">
        <!-- إحصائيات المخزون -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
            <div class="bg-white rounded-lg shadow-md p-6">
                <div class="flex items-center justify-between mb-4">
                    <div>
                        <p class="text-gray-500 text-sm">المواد الخام</p>
                        <p class="text-2xl font-bold"><?php echo $stats['raw']['total']; ?> مادة</p>
                        <p class="text-green-600 font-bold"><?php echo number_format($stats['raw']['value'], 2); ?> ج.م</p>
                    </div>
                    <i class="fas fa-flask text-purple-600 text-4xl"></i>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-6">
                <div class="flex items-center justify-between mb-4">
                    <div>
                        <p class="text-gray-500 text-sm">مواد التعبئة</p>
                        <p class="text-2xl font-bold"><?php echo $stats['packaging']['total']; ?> مادة</p>
                        <p class="text-green-600 font-bold"><?php echo number_format($stats['packaging']['value'], 2); ?> ج.م</p>
                    </div>
                    <i class="fas fa-box text-blue-600 text-4xl"></i>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-6">
                <div class="flex items-center justify-between mb-4">
                    <div>
                        <p class="text-gray-500 text-sm">مواد التغليف</p>
                        <p class="text-2xl font-bold"><?php echo $stats['wrapping']['total']; ?> مادة</p>
                        <p class="text-green-600 font-bold"><?php echo number_format($stats['wrapping']['value'], 2); ?> ج.م</p>
                    </div>
                    <i class="fas fa-gift text-green-600 text-4xl"></i>
                </div>
            </div>
        </div>

        <!-- إحصائيات الإنتاج -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
            <div class="bg-white rounded-lg shadow-md p-6">
                <div class="flex items-center justify-between mb-4">
                    <div>
                        <p class="text-gray-500 text-sm">الوصفات</p>
                        <p class="text-3xl font-bold text-orange-600"><?php echo $stats['recipes']; ?></p>
                    </div>
                    <i class="fas fa-book text-orange-600 text-4xl"></i>
                </div>
                <p class="text-sm text-gray-600">عدد وصفات المنتجات المسجلة</p>
            </div>

            <div class="bg-white rounded-lg shadow-md p-6">
                <div class="flex items-center justify-between mb-4">
                    <div>
                        <p class="text-gray-500 text-sm">دفعات الإنتاج</p>
                        <p class="text-3xl font-bold text-red-600"><?php echo $stats['batches']; ?></p>
                    </div>
                    <i class="fas fa-industry text-red-600 text-4xl"></i>
                </div>
                <p class="text-sm text-gray-600">عدد دفعات الإنتاج المكتملة</p>
            </div>
        </div>

        <!-- القيمة الإجمالية -->
        <div class="bg-gradient-to-r from-green-500 to-green-600 rounded-lg shadow-lg p-8 text-white mb-6">
            <div class="text-center">
                <p class="text-xl mb-2">القيمة الإجمالية للمخزون</p>
                <p class="text-5xl font-bold">
                    <?php 
                    $total_value = $stats['raw']['value'] + $stats['packaging']['value'] + $stats['wrapping']['value'];
                    echo number_format($total_value, 2); 
                    ?> ج.م
                </p>
            </div>
        </div>

        <!-- معلومات -->
        <div class="bg-blue-50 border-r-4 border-blue-500 p-6 rounded-lg">
            <h3 class="text-xl font-bold text-blue-900 mb-2">
                <i class="fas fa-chart-line ml-2"></i> التقارير المتاحة
            </h3>
            <ul class="list-disc list-inside text-blue-800 space-y-2">
                <li>تقرير المخزون الحالي</li>
                <li>تقرير التكاليف</li>
                <li>تقرير دفعات الإنتاج</li>
                <li>تقرير الموردين</li>
                <li>تحليل الأرباح المتوقعة</li>
            </ul>
        </div>
    </div>
</body>
</html>
