<?php
// نسخة تجريبية بدون فحص صلاحيات
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once '../../config/database.php';

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

echo "<!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 p-8'>
    <div class='max-w-4xl mx-auto'>
        <h1 class='text-3xl font-bold mb-6'>🏭 اختبار نظام التصنيع</h1>";

// اختبار الجداول
$tables = [
    'raw_materials' => 'المواد الخام',
    'packaging_materials' => 'مواد التعبئة',
    'wrapping_materials' => 'مواد التغليف',
    'product_recipes' => 'وصفات المنتجات',
    'recipe_ingredients' => 'مكونات الوصفات',
    'production_batches' => 'دفعات الإنتاج',
    'material_movements' => 'حركات المواد',
    'material_alerts' => 'التنبيهات'
];

echo "<div class='bg-white rounded-lg shadow-md p-6 mb-6'>
        <h2 class='text-xl font-bold mb-4'>📊 حالة الجداول</h2>
        <div class='space-y-2'>";

foreach ($tables as $table => $name) {
    try {
        $stmt = $conn->query("SELECT COUNT(*) FROM $table");
        $count = $stmt->fetchColumn();
        echo "<div class='flex items-center justify-between p-3 bg-green-50 rounded'>
                <span class='font-medium'>✅ $name ($table)</span>
                <span class='text-green-600 font-bold'>$count صف</span>
              </div>";
    } catch (Exception $e) {
        echo "<div class='flex items-center justify-between p-3 bg-red-50 rounded'>
                <span class='font-medium'>❌ $name ($table)</span>
                <span class='text-red-600 text-sm'>" . $e->getMessage() . "</span>
              </div>";
    }
}

echo "</div></div>";

// إحصائيات
echo "<div class='grid grid-cols-2 md:grid-cols-4 gap-4 mb-6'>";

$stats = [
    ['table' => 'raw_materials', 'icon' => 'flask', 'color' => 'purple', 'name' => 'مواد خام'],
    ['table' => 'packaging_materials', 'icon' => 'box', 'color' => 'blue', 'name' => 'تعبئة'],
    ['table' => 'wrapping_materials', 'icon' => 'gift', 'color' => 'green', 'name' => 'تغليف'],
    ['table' => 'product_recipes', 'icon' => 'book', 'color' => 'orange', 'name' => 'وصفات']
];

foreach ($stats as $stat) {
    try {
        $stmt = $conn->query("SELECT COUNT(*) FROM {$stat['table']}");
        $count = $stmt->fetchColumn();
        
        echo "<div class='bg-white rounded-lg shadow-md p-4 text-center'>
                <i class='fas fa-{$stat['icon']} text-{$stat['color']}-600 text-3xl mb-2'></i>
                <p class='text-gray-500 text-sm'>{$stat['name']}</p>
                <p class='text-2xl font-bold text-gray-800'>$count</p>
              </div>";
    } catch (Exception $e) {
        echo "<div class='bg-white rounded-lg shadow-md p-4 text-center'>
                <i class='fas fa-{$stat['icon']} text-red-600 text-3xl mb-2'></i>
                <p class='text-gray-500 text-sm'>{$stat['name']}</p>
                <p class='text-red-600 text-sm'>خطأ</p>
              </div>";
    }
}

echo "</div>";

// معلومات النظام
echo "<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-3'>
            <i class='fas fa-info-circle ml-2'></i> حالة النظام
        </h3>
        <div class='space-y-2 text-blue-800'>
            <p>✅ قاعدة البيانات: <span class='font-bold'>مكتملة 100%</span></p>
            <p>✅ الجداول: <span class='font-bold'>8 جداول</span></p>
            <p>✅ الأعمدة: <span class='font-bold'>80+ عمود</span></p>
            <p>⏳ الصفحات: <span class='font-bold'>قيد الإنشاء</span></p>
        </div>
      </div>";

// روابط
echo "<div class='bg-white rounded-lg shadow-md p-6'>
        <h3 class='text-xl font-bold mb-4'>🔗 الروابط</h3>
        <div class='space-y-2'>
            <a href='index.php' class='block p-3 bg-orange-50 hover:bg-orange-100 rounded text-orange-700 font-medium'>
                <i class='fas fa-home ml-2'></i> الصفحة الرئيسية (يحتاج تسجيل دخول)
            </a>
            <a href='../login.php' class='block p-3 bg-blue-50 hover:bg-blue-100 rounded text-blue-700 font-medium'>
                <i class='fas fa-sign-in-alt ml-2'></i> تسجيل الدخول
            </a>
            <a href='../dashboard.php' class='block p-3 bg-purple-50 hover:bg-purple-100 rounded text-purple-700 font-medium'>
                <i class='fas fa-tachometer-alt ml-2'></i> لوحة التحكم
            </a>
        </div>
      </div>";

echo "</div>
</body>
</html>";
?>
