<?php
include 'config.php';

$sql = "SELECT o.*, c.name as customer_name 
        FROM orders o 
        JOIN customers c ON o.customer_id = c.id 
        ORDER BY o.created_at DESC 
        LIMIT 5";
$result = $conn->query($sql);

echo '<table class="table table-striped">
        <thead>
            <tr>
                <th>رقم الطلب</th>
                <th>العميل</th>
                <th>المجموع</th>
                <th>الحالة</th>
                <th>التاريخ</th>
                <th>الإجراءات</th>
            </tr>
        </thead>
        <tbody>';

if ($result->num_rows > 0) {
    $status_badge = [
        'pending' => 'warning',
        'accepted' => 'info',
        'on_the_way' => 'primary',
        'delivered' => 'success'
    ];

    $status_text = [
        'pending' => 'قيد الانتظار',
        'accepted' => 'تم القبول',
        'on_the_way' => 'في الطريق',
        'delivered' => 'تم التوصيل'
    ];

    while ($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>#{$row['id']}</td>
                <td>{$row['customer_name']}</td>
                <td>{$row['total']} ريال</td>
                <td><span class='badge bg-{$status_badge[$row['status']]}'>{$status_text[$row['status']]}</span></td>
                <td>" . date('Y-m-d H:i', strtotime($row['created_at'])) . "</td>
                <td><a href='orders.php?action=view&id={$row['id']}' class='btn btn-sm btn-primary'>عرض</a></td>
            </tr>";
    }
} else {
    echo "<tr><td colspan='6' class='text-center'>لا توجد طلبات</td></tr>";
}
echo '</tbody></table>';
?>
