<?php
header('Content-Type: text/html; charset=utf-8');

require_once 'database.php';

$db = Database::getInstance();
$conn = $db->getConnection();

if (!$conn) {
    die("❌ فشل الاتصال بقاعدة البيانات");
}

echo "<h2>🔍 هيكل جدول inspection_requests</h2>";

$result = $conn->query("DESCRIBE inspection_requests");
if ($result) {
    echo "<table border='1' cellpadding='8' cellspacing='0'>";
    echo "汽水<th>العمود</th><th>النوع</th><th>Null</th><th>مفتاح</th><th>افتراضي</th> </tr>";
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo "汽水";
        echo "<td><strong>" . htmlspecialchars($row['Field']) . "</strong></td>";
        echo "<td>" . htmlspecialchars($row['Type']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Null']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Key']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Default'] ?? 'NULL') . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "❌ فشل جلب هيكل الجدول";
}

echo "<h2>🔍 هيكل جدول users</h2>";
$result2 = $conn->query("DESCRIBE users");
if ($result2) {
    echo "<table border='1' cellpadding='8' cellspacing='0'>";
    echo "汽水<th>العمود</th><th>النوع</th><th>Null</th><th>مفتاح</th><th>افتراضي</th> </tr>";
    while ($row = $result2->fetch(PDO::FETCH_ASSOC)) {
        echo "汽水";
        echo "<td><strong>" . htmlspecialchars($row['Field']) . "</strong></td>";
        echo "<td>" . htmlspecialchars($row['Type']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Null']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Key']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Default'] ?? 'NULL') . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

echo "<h2>🔍 هيكل جدول inspections</h2>";
try {
    $result3 = $conn->query("DESCRIBE inspections");
    if ($result3) {
        echo "<table border='1' cellpadding='8' cellspacing='0'>";
        echo "汽水<th>العمود</th><th>النوع</th><th>Null</th><th>مفتاح</th><th>افتراضي</th> </tr>";
        while ($row = $result3->fetch(PDO::FETCH_ASSOC)) {
            echo "汽水";
            echo "<td><strong>" . htmlspecialchars($row['Field']) . "</strong></td>";
            echo "<td>" . htmlspecialchars($row['Type']) . "</td>";
            echo "<td>" . htmlspecialchars($row['Null']) . "</td>";
            echo "<td>" . htmlspecialchars($row['Key']) . "</td>";
            echo "<td>" . htmlspecialchars($row['Default'] ?? 'NULL') . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>⚠️ جدول inspections غير موجود</p>";
    }
} catch (Exception $e) {
    echo "<p>⚠️ جدول inspections غير موجود: " . $e->getMessage() . "</p>";
}

// جلب بيانات تجريبية من inspection_requests
echo "<h2>📋 بيانات من inspection_requests (أول 3 سجلات)</h2>";
try {
    $dataResult = $conn->query("SELECT * FROM inspection_requests LIMIT 3");
    if ($dataResult && $dataResult->rowCount() > 0) {
        echo "<table border='1' cellpadding='8' cellspacing='0'>";
        // جلب الأعمدة
        $columns = array_keys($dataResult->fetch(PDO::FETCH_ASSOC));
        echo "<tr>";
        foreach ($columns as $col) {
            echo "<th>" . htmlspecialchars($col) . "</th>";
        }
        echo "</tr>";
        
        // إعادة الاستعلام
        $dataResult = $conn->query("SELECT * FROM inspection_requests LIMIT 3");
        while ($row = $dataResult->fetch(PDO::FETCH_ASSOC)) {
            echo "<tr>";
            foreach ($columns as $col) {
                echo "<td>" . htmlspecialchars($row[$col] ?? 'NULL') . "</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>لا توجد بيانات في جدول inspection_requests</p>";
    }
} catch (Exception $e) {
    echo "<p>خطأ: " . $e->getMessage() . "</p>";
}
?>