<?php
// opd/tracking.php - Customer tracking page
require_once '../config.php';
require_once '../includes/steadfast_tracking.php';
session_start();

if (!isset($_SESSION['opd_patient'])) {
    header('Location: index.php');
    exit;
}

$patient_id = $_SESSION['opd_patient'];
$orders = $conn->query("SELECT * FROM orders WHERE patient_id = $patient_id ORDER BY created_at DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Orders | Track Shipments</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body style="background:#f0f2f5;padding:20px">
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h3><i class="fas fa-truck"></i> My Orders</h3>
        <a href="dashboard.php" class="btn btn-secondary">← Dashboard</a>
    </div>

    <?php if($orders->num_rows == 0): ?>
        <div class="alert alert-info">No orders found.</div>
    <?php else: ?>
        <?php while($order = $orders->fetch_assoc()): ?>
            <div class="card mb-3">
                <div class="card-header bg-white d-flex justify-content-between align-items-center">
                    <strong>Order #: <?php echo $order['order_number']; ?></strong>
                    <span class="badge bg-<?php echo $order['status'] == 'delivered' ? 'success' : ($order['status'] == 'cancelled' ? 'danger' : 'warning'); ?>">
                        <?php echo ucfirst($order['status']); ?>
                    </span>
                </div>
                <div class="card-body">
                    <p><strong>Date:</strong> <?php echo date('d M Y, h:i A', strtotime($order['created_at'])); ?></p>
                    <p><strong>Total:</strong> ৳<?php echo number_format($order['total_amount']); ?></p>
                    
                    <?php echo displayTrackingTimeline($order); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php endif; ?>
</div>

<script>
function refreshTracking() {
    location.reload();
}
</script>
</body>
</html>