<?php
/**
 * فحص حقول جدول المنتجات
 */

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

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

echo "<!DOCTYPE html>
<html lang='ar' dir='rtl'>
<head>
    <meta charset='UTF-8'>
    <title>فحص حقول المنتجات</title>
    <style>
        body { font-family: Arial; padding: 20px; background: #f5f5f5; }
        .box { background: white; padding: 20px; margin: 10px 0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .success { background: #d4edda; color: #155724; }
        .error { background: #f8d7da; color: #721c24; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 10px; text-align: right; border: 1px solid #dee2e6; }
        th { background: #f8f9fa; font-weight: bold; }
    </style>
</head>
<body>";

echo "<h1>🔍 فحص حقول جدول products</h1>";

try {
    // Get table structure
    $query = "DESCRIBE products";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<div class='box'>";
    echo "<h2>📋 الحقول الموجودة في الجدول:</h2>";
    echo "<table>";
    echo "<tr><th>اسم الحقل</th><th>النوع</th><th>Null</th><th>Key</th><th>Default</th></tr>";
    
    $required_fields = [
        'name', 'description', 'price', 'discount_price', 'category_id',
        'stock_quantity', 'status', 'sku', 'image', 'gallery_images',
        'size', 'certification', 'short_description', 'detailed_description',
        'how_to_use', 'additional_tips'
    ];
    
    $existing_fields = [];
    foreach ($columns as $col) {
        $existing_fields[] = $col['Field'];
        $is_required = in_array($col['Field'], $required_fields);
        $style = $is_required ? "background: #d4edda;" : "";
        
        echo "<tr style='{$style}'>";
        echo "<td><strong>{$col['Field']}</strong></td>";
        echo "<td>{$col['Type']}</td>";
        echo "<td>{$col['Null']}</td>";
        echo "<td>{$col['Key']}</td>";
        echo "<td>{$col['Default']}</td>";
        echo "</tr>";
    }
    echo "</table>";
    echo "</div>";
    
    // Check missing fields
    $missing_fields = array_diff($required_fields, $existing_fields);
    
    if (!empty($missing_fields)) {
        echo "<div class='box error'>";
        echo "<h2>❌ حقول مفقودة:</h2>";
        echo "<ul>";
        foreach ($missing_fields as $field) {
            echo "<li><strong>{$field}</strong></li>";
        }
        echo "</ul>";
        echo "<p>⚠️ هذه الحقول مطلوبة في صفحة التعديل ولكنها غير موجودة في الجدول!</p>";
        echo "</div>";
    } else {
        echo "<div class='box success'>";
        echo "<h2>✅ جميع الحقول المطلوبة موجودة!</h2>";
        echo "</div>";
    }
    
} catch (Exception $e) {
    echo "<div class='box error'>";
    echo "<h2>❌ خطأ:</h2>";
    echo "<p>" . $e->getMessage() . "</p>";
    echo "</div>";
}

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