<?php
require_once 'config.php';

$order = null;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['track_order'])) {
    $order_id = (int)$_POST['order_id'];
    $phone = trim($_POST['phone']);
    if ($order_id && $phone) {
        $stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ? AND customer_phone = ?");
        $stmt->execute([$order_id, $phone]);
        $order = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$order) $error = "Order not found or phone number mismatch.";
    } else {
        $error = "Please enter Order ID and Phone number.";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Track Your Order – HomoeoCare</title>
    <link rel="stylesheet" href="style.css">
    <style>
        .track-form { max-width: 500px; margin: 30px auto; background: #f9fff9; padding: 30px; border-radius: 28px; }
        .track-form input { width: 100%; margin-bottom: 15px; }
        .order-details { background: #fefdf8; border-radius: 24px; padding: 20px; margin-top: 30px; }
        .status-badge { display: inline-block; padding: 6px 16px; border-radius: 60px; font-weight: bold; }
        .status-pending { background: #ffc107; color: #333; }
        .status-confirmed { background: #28a745; color: white; }
        .status-shipped { background: #17a2b8; color: white; }
        .status-delivered { background: #6f42c1; color: white; }
        .status-cancelled { background: #dc3545; color: white; }
        .tracking-link { word-break: break-all; background: #eef5ea; padding: 8px; border-radius: 12px; }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1>📦 Track Your Order</h1>
        <nav><a href="index.php">Home</a> <a href="shop.php">Shop</a> <a href="cart.php">Cart</a></nav>
    </header>

    <div class="track-form">
        <h2>Enter Order Details</h2>
        <form method="post">
            <div class="form-group"><label>Order ID *</label><input type="number" name="order_id" required></div>
            <div class="form-group"><label>Phone Number (used when ordering) *</label><input type="tel" name="phone" required></div>
            <button type="submit" name="track_order" class="btn-buy">Track Order</button>
        </form>
        <?php if ($error): ?><div class="alert error" style="background:#ffe0e0; color:#b00;"><?= $error ?></div><?php endif; ?>
    </div>

    <?php if ($order): ?>
        <div class="order-details">
            <h2>Order #<?= $order['id'] ?></h2>
            <p><strong>Date:</strong> <?= date('d M Y, h:i A', strtotime($order['created_at'])) ?></p>
            <p><strong>Total Amount:</strong> <?= number_format($order['total_amount'], 2) ?> BDT</p>
            <p><strong>Payment Method:</strong> <?= htmlspecialchars($order['payment_method']) ?></p>
            <p><strong>Status:</strong> 
                <span class="status-badge status-<?= $order['order_status'] ?>"><?= ucfirst($order['order_status']) ?></span>
            </p>
            <?php if ($order['courier_tracking_link']): ?>
                <p><strong>📦 Courier Tracking:</strong> <a href="<?= htmlspecialchars($order['courier_tracking_link']) ?>" target="_blank" class="tracking-link">Click to track your shipment</a></p>
            <?php else: ?>
                <p><strong>📦 Courier Tracking:</strong> Not yet available. Will be updated once shipped.</p>
            <?php endif; ?>
            <h3>Order Items:</h3>
            <?php
                $items = json_decode($order['order_details'], true);
                if ($items):
            ?>
                <table class="cart-table">
                    <thead><tr><th>Medicine</th><th>Details</th><th>Qty</th><th>Price</th></tr></thead>
                    <tbody>
                    <?php foreach ($items as $item): ?>
                        <tr>
                            <td><?= htmlspecialchars($item['medicine_name']) ?></td>
                            <td>
                                <?php if ($item['type'] === 'single'): ?>
                                    M/<?= $item['potency'] ?>
                                <?php else: ?>
                                    Combo M/<?= $item['potency_start'] ?>–M/<?= $item['potency_end'] ?>
                                <?php endif; ?>
                            </td>
                            <td><?= $item['quantity'] ?></td>
                            <td><?= number_format($item['type']==='single' ? $item['price'] : $item['package_price'], 2) ?> BDT</td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p>Order details not available.</p>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>
</body>
</html>