<?php
require_once __DIR__ . '/../include/db.php';

$order_id = intval($_GET['order_id'] ?? 0);
if ($order_id <= 0) {
    die("Invalid Order ID");
}

/* ---------------- ORDER INFO ---------------- */
$orderStmt = $conn->prepare("
    SELECT id, buyer_name, style_no, order_date
    FROM orders
    WHERE id = ?
");
$orderStmt->bind_param("i", $order_id);
$orderStmt->execute();
$order = $orderStmt->get_result()->fetch_assoc();

if (!$order) {
    die("Order not found");
}

/* ---------------- DELIVERY HISTORY ---------------- */
$histStmt = $conn->prepare("
    SELECT 
        da.assign_date,
        da.delivered_qty,
        da.challan_number,
        da.challan_image,
        da.remarks,
        dm.name AS delivery_man
    FROM delivery_assignments da
    JOIN delivery_men dm ON dm.id = da.delivery_man_id
    WHERE da.order_id = ?
      AND da.delivered_qty > 0
    ORDER BY da.assign_date DESC
");
$histStmt->bind_param("i", $order_id);
$histStmt->execute();
$history = $histStmt->get_result();
?>
<!DOCTYPE html>
<html>
<head>
     <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Delivery History — Order #<?= $order_id ?></title>
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>

<body class="bg-light p-4">

<div class="container">

    <div class="mb-3">
        <h3>📦 Delivery History</h3>
        <p class="mb-1"><b>Order ID:</b> #<?= $order['id'] ?></p>
        <p class="mb-1"><b>Buyer:</b> <?= htmlspecialchars($order['buyer_name']) ?></p>
        <p class="mb-1"><b>Style:</b> <?= htmlspecialchars($order['style_no']) ?></p>
        <p class="mb-1"><b>Order Date:</b> <?= $order['order_date'] ?></p>
    </div>

    <?php if ($history->num_rows == 0): ?>
        <div class="alert alert-warning">
            No delivery history found for this order.
        </div>
    <?php else: ?>

        <table class="table table-bordered table-striped align-middle">
            <thead class="table-dark">
                <tr>
                    <th>#</th>
                    <th>Delivery Date</th>
                    <th>Delivery Man</th>
                    <th>Delivered Qty</th>
                    <th>Challan No</th>
                    <th>Remarks</th>
                    <th>Challan</th>
                </tr>
            </thead>
            <tbody>
            <?php $i=1; while($h = $history->fetch_assoc()): ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><?= date("d M Y", strtotime($h['assign_date'])) ?></td>
                    <td><?= htmlspecialchars($h['delivery_man']) ?></td>
                    <td><b><?= $h['delivered_qty'] ?></b></td>
                    <td><?= htmlspecialchars($h['challan_number']) ?></td>
                    <td><?= nl2br(htmlspecialchars($h['remarks'])) ?></td>
                   <td>
    <?php if ($h['challan_image']): ?>
        <img 
            src="../delivery/uploads/<?= htmlspecialchars($h['challan_image']) ?>"
            alt="Challan"
            style="max-height:80px; cursor:pointer;"
            class="img-thumbnail"
            onclick="window.open(this.src, '_blank')">
    <?php else: ?>
        <span class="text-muted">N/A</span>
    <?php endif; ?>
</td>

                </tr>
            <?php endwhile; ?>
            </tbody>
        </table>

    <?php endif; ?>

    <a href="delivery_history.php" class="btn btn-secondary mt-3">← Back</a>

</div>

</body>
</html>
