<?php
// order-tracking.php
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/functions.php';

$invoice_no = $_GET['invoice'] ?? '';

if ($invoice_no) {
    $order = getOrder($pdo, $invoice_no);
    if ($order) {
        $order_items = getOrderItems($pdo, $order['id']);
    }
}

include __DIR__ . '/includes/header.php';
?>

<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="tracking-card">
                <h4 class="text-center mb-4">Order Tracking</h4>
                
                <form method="GET" class="tracking-form">
                    <div class="input-group">
                        <input type="text" name="invoice" class="form-control" placeholder="Enter Invoice Number" value="<?php echo $invoice_no; ?>">
                        <button type="submit" class="btn btn-primary">Track Order</button>
                    </div>
                </form>
                
                <?php if (isset($order) && $order): ?>
                <div class="tracking-result mt-4">
                    <div class="order-status-track">
                        <div class="status-step <?php echo in_array($order['order_status'], ['pending', 'processing', 'shipped', 'delivered']) ? 'active' : ''; ?>">
                            <div class="step-icon"><i class="fa-solid fa-clock"></i></div>
                            <div class="step-label">Pending</div>
                        </div>
                        <div class="status-line <?php echo in_array($order['order_status'], ['processing', 'shipped', 'delivered']) ? 'active' : ''; ?>"></div>
                        <div class="status-step <?php echo in_array($order['order_status'], ['processing', 'shipped', 'delivered']) ? 'active' : ''; ?>">
                            <div class="step-icon"><i class="fa-solid fa-spinner"></i></div>
                            <div class="step-label">Processing</div>
                        </div>
                        <div class="status-line <?php echo in_array($order['order_status'], ['shipped', 'delivered']) ? 'active' : ''; ?>"></div>
                        <div class="status-step <?php echo in_array($order['order_status'], ['shipped', 'delivered']) ? 'active' : ''; ?>">
                            <div class="step-icon"><i class="fa-solid fa-truck"></i></div>
                            <div class="step-label">Shipped</div>
                        </div>
                        <div class="status-line <?php echo $order['order_status'] == 'delivered' ? 'active' : ''; ?>"></div>
                        <div class="status-step <?php echo $order['order_status'] == 'delivered' ? 'active' : ''; ?>">
                            <div class="step-icon"><i class="fa-solid fa-check"></i></div>
                            <div class="step-label">Delivered</div>
                        </div>
                    </div>
                    
                    <div class="order-details mt-4">
                        <div class="row">
                            <div class="col-md-6">
                                <h6>Order Information</h6>
                                <p><strong>Invoice:</strong> <?php echo $order['invoice_no']; ?></p>
                                <p><strong>Date:</strong> <?php echo date('d M Y, h:i A', strtotime($order['created_at'])); ?></p>
                                <p><strong>Status:</strong> 
                                    <span class="badge bg-<?php echo $order['order_status'] == 'delivered' ? 'success' : ($order['order_status'] == 'cancelled' ? 'danger' : 'warning'); ?>">
                                        <?php echo ucfirst($order['order_status']); ?>
                                    </span>
                                </p>
                                <p><strong>Total:</strong> <?php echo number_format($order['total']); ?> TK</p>
                            </div>
                            <div class="col-md-6">
                                <h6>Customer Information</h6>
                                <p><strong>Name:</strong> <?php echo $order['name']; ?></p>
                                <p><strong>Phone:</strong> <?php echo $order['phone']; ?></p>
                                <p><strong>Address:</strong> <?php echo $order['address']; ?></p>
                            </div>
                        </div>
                        
                        <h6 class="mt-3">Order Items</h6>
                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>Product</th>
                                        <th>Qty</th>
                                        <th>Price</th>
                                        <th>Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($order_items as $item): ?>
                                    <tr>
                                        <td><?php echo $item['product_name']; ?></td>
                                        <td><?php echo $item['quantity']; ?></td>
                                        <td><?php echo number_format($item['price']); ?> TK</td>
                                        <td><?php echo number_format($item['total']); ?> TK</td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <?php elseif ($invoice_no): ?>
                <div class="alert alert-danger mt-4">Order not found. Please check your invoice number.</div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<style>
.tracking-card {
    background: #fff;
    border-radius: 12px;
    padding: 30px;
    border: 1px solid #e2e8f0;
}

.tracking-form .input-group {
    max-width: 500px;
    margin: 0 auto;
}

.tracking-form .form-control {
    border-radius: 8px 0 0 8px;
    border: 2px solid #e2e8f0;
    padding: 10px 15px;
}

.tracking-form .btn {
    border-radius: 0 8px 8px 0;
}

.order-status-track {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
}

.status-step {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    opacity: 0.4;
    transition: all 0.3s ease;
}

.status-step.active {
    opacity: 1;
}

.step-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #94a3b8;
    transition: all 0.3s ease;
}

.status-step.active .step-icon {
    background: #047dc3;
    color: #fff;
}

.step-label {
    font-size: 12px;
    color: #94a3b8;
}

.status-step.active .step-label {
    color: #047dc3;
    font-weight: 600;
}

.status-line {
    width: 60px;
    height: 3px;
    background: #e2e8f0;
    margin: 0 5px;
    margin-bottom: 20px;
}

.status-line.active {
    background: #047dc3;
}

.order-details {
    background: #f8fafc;
    padding: 20px;
    border-radius: 8px;
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>