<?php
include 'config.php';
checkCustomerAuth();

$customer_id = $_SESSION['customer_id'];

// Get customer orders
$orders = $conn->query("
    SELECT o.*, cp.name as captain_name, cp.phone as captain_phone
    FROM orders o 
    LEFT JOIN captains cp ON o.captain_id = cp.id 
    WHERE o.customer_id = '$customer_id' 
    ORDER BY o.created_at DESC
");

if ($orders->num_rows > 0) {
    while ($order = $orders->fetch_assoc()) {
        ?>
        <div class="order-card" id="order-<?php echo $order['id']; ?>">
            <div class="order-header">
                <div class="d-flex justify-content-between align-items-center">
                    <h6 class="mb-0">طلب #<?php echo $order['id']; ?></h6>
                    <span class="badge bg-<?php 
                        $status_badge = [
                            'pending' => 'warning',
                            'accepted' => 'info',
                            'on_the_way' => 'primary',
                            'delivered' => 'success'
                        ];
                        echo $status_badge[$order['status']];
                    ?>">
                        <?php 
                        $status_text = [
                            'pending' => 'قيد الانتظار',
                            'accepted' => 'تم القبول',
                            'on_the_way' => 'في الطريق',
                            'delivered' => 'تم التوصيل'
                        ];
                        echo $status_text[$order['status']];
                        ?>
                    </span>
                </div>
            </div>
            
            <div class="row">
                <div class="col-md-6">
                    <p class="mb-1"><strong>المجموع:</strong> <?php echo $order['total']; ?>ريال</p>
                    <p class="mb-1"><strong>التاريخ:</strong> <?php echo date('Y-m-d H:i', strtotime($order['created_at'])); ?></p>
                </div>
                <div class="col-md-6">
                    <?php if ($order['captain_name']): ?>
                        <p class="mb-1"><strong>الكابتن:</strong> <?php echo $order['captain_name']; ?></p>
                        <p class="mb-1"><strong>هاتف الكابتن:</strong> <?php echo $order['captain_phone']; ?></p>
                    <?php else: ?>
                        <p class="mb-1 text-muted">لم يتم تعيين كابتن بعد</p>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="mt-3">
                <button class="btn btn-sm btn-outline-primary view-order-details" 
                        data-order-id="<?php echo $order['id']; ?>">
                    <i class="bi bi-eye"></i> عرض التفاصيل
                </button>
                
                <?php if ($order['status'] == 'pending'): ?>
                    <span class="badge bg-warning ms-2">
                        <i class="bi bi-clock"></i> جاري التحضير
                    </span>
                <?php elseif ($order['status'] == 'accepted'): ?>
                    <span class="badge bg-info ms-2">
                        <i class="bi bi-check-circle"></i> تم القبول
                    </span>
                <?php elseif ($order['status'] == 'on_the_way'): ?>
                    <span class="badge bg-primary ms-2">
                        <i class="bi bi-truck"></i> في الطريق
                    </span>
                <?php elseif ($order['status'] == 'delivered'): ?>
                    <span class="badge bg-success ms-2">
                        <i class="bi bi-check2-circle"></i> تم التسليم
                    </span>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
} else {
    ?>
    <div class="empty-orders">
        <i class="bi bi-cart display-1 text-muted"></i>
        <h4 class="text-muted mt-3">لا توجد طلبات سابقة</h4>
        <p class="text-muted">لم تقم بإجراء أي طلبات حتى الآن</p>
        <a href="home.php" class="btn btn-primary">
            <i class="bi bi-arrow-left"></i> تصفح الوجبات
        </a>
    </div>
    <?php
}
?>