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

$captain_id = $_SESSION['captain_id'];

// Get completed orders
$completed_orders = $conn->query("
    SELECT o.*, c.name as customer_name, c.phone as customer_phone, c.address as customer_address
    FROM orders o 
    JOIN customers c ON o.customer_id = c.id 
    WHERE o.captain_id = '$captain_id' AND o.status = 'delivered'
    ORDER BY o.created_at DESC
");
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>الطلبات المنجزة - Max Captain</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body {
            font-family: 'Tajawal', sans-serif;
            background-color: #f8f9fa;
            padding-bottom: 80px;
        }
        .navbar {
            background: #F57C00;
            color: white;
        }
        .order-card {
            background: white;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 15px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            border-right: 4px solid #28a745;
        }
        .bottom-nav {
            position: fixed;
            bottom: 0;
            right: 0;
            left: 0;
            background: white;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
            padding: 10px;
        }
        .nav-item {
            text-align: center;
        }
        .nav-link {
            color: #666;
        }
        .nav-link.active {
            color: #F57C00;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg">
        <div class="container-fluid">
            <span class="navbar-brand text-white">
                <i class="bi bi-check-circle"></i> الطلبات المنجزة
            </span>
        </div>
    </nav>

    <div class="container mt-4">
        <?php if ($completed_orders->num_rows > 0): ?>
            <?php while ($order = $completed_orders->fetch_assoc()): ?>
                <div class="order-card">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <h5>طلب #<?php echo $order['id']; ?></h5>
                        <span class="badge bg-success">تم التوصيل</span>
                    </div>
                    
                    <div class="mb-2">
                        <strong><i class="bi bi-person"></i> العميل:</strong> 
                        <?php echo $order['customer_name']; ?>
                    </div>
                    <div class="mb-2">
                        <strong><i class="bi bi-telephone"></i> الهاتف:</strong> 
                        <?php echo $order['customer_phone']; ?>
                    </div>
                    <div class="mb-2">
                        <strong><i class="bi bi-geo-alt"></i> العنوان:</strong> 
                        <?php echo $order['customer_address']; ?>
                    </div>
                    <div class="mb-2">
                        <strong><i class="bi bi-currency-dollar"></i> المجموع:</strong> 
                        <?php echo $order['total']; ?> ريال
                    </div>
                    <div class="mb-2">
                        <strong><i class="bi bi-clock"></i> التاريخ:</strong> 
                        <?php echo date('Y-m-d H:i', strtotime($order['created_at'])); ?>
                    </div>
                    
                    <div class="mt-3">
                        <a href="order_details.php?id=<?php echo $order['id']; ?>" class="btn btn-info btn-sm">
                            <i class="bi bi-list"></i> تفاصيل الطلب
                        </a>
                    </div>
                </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="alert alert-info text-center">
                <i class="bi bi-info-circle"></i> لا توجد طلبات منجزة
            </div>
        <?php endif; ?>
    </div>

    <!-- Bottom Navigation -->
    <nav class="bottom-nav">
        <div class="row text-center">
            <div class="col-3">
                <a href="dashboard.php" class="nav-link">
                    <i class="bi bi-house-door fs-4"></i>
                    <div>الرئيسية</div>
                </a>
            </div>
            <div class="col-3">
                <a href="orders.php" class="nav-link">
                    <i class="bi bi-list-check fs-4"></i>
                    <div>الطلبات</div>
                </a>
            </div>
            <div class="col-3">
                <a href="completed_orders.php" class="nav-link active">
                    <i class="bi bi-check-circle fs-4"></i>
                    <div>المنجزة</div>
                </a>
            </div>
            <div class="col-3">
                <a href="profile.php" class="nav-link">
                    <i class="bi bi-person fs-4"></i>
                    <div>الملف</div>
                </a>
            </div>
        </div>
    </nav>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Bottom Navigation -->
<nav class="bottom-nav">
    <div class="row text-center">
        <div class="col-3">
            <a href="dashboard.php" class="nav-link">
                <i class="bi bi-house-door fs-4"></i>
                <div>الرئيسية</div>
            </a>
        </div>
        <div class="col-3">
            <a href="orders.php" class="nav-link">
                <i class="bi bi-list-check fs-4"></i>
                <div>الطلبات</div>
            </a>
        </div>
        <div class="col-3">
            <a href="completed_orders.php" class="nav-link">
                <i class="bi bi-check-circle fs-4"></i>
                <div>المنجزة</div>
            </a>
        </div>
        <div class="col-3">
            <a href="profile.php" class="nav-link">
                <i class="bi bi-person fs-4"></i>
                <div>الحساب</div>
            </a>
        </div>
    </div>
</nav>
</body>
</html>