<?php
require_once __DIR__ . '/../include/db.php';
session_start();
if (!isset($_SESSION['delivery_man_id'])) { exit("Login required"); }

$order_id = intval($_GET['order_id'] ?? 0);
$delivery_man_id = intval($_SESSION['delivery_man_id']);

// get totals for this order & this delivery_man
$stmt = $conn->prepare("
    SELECT IFNULL(SUM(assigned_qty),0) AS assigned, IFNULL(SUM(delivered_qty),0) AS delivered
    FROM delivery_assignments
    WHERE order_id = ? AND delivery_man_id = ?
");
$stmt->bind_param("ii", $order_id, $delivery_man_id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

$assigned  = intval($data['assigned']);
$delivered = intval($data['delivered']);
$available = $assigned - $delivered;
if ($available < 0) $available = 0;
?>
<div class='p-3'>
    <p><strong>Assigned Qty:</strong> <?= $assigned ?></p>
    <p><strong>Already Delivered:</strong> <?= $delivered ?></p>
    <p><strong>Available to Deliver:</strong> <?= $available ?></p>

    <input type="hidden" name="order_id" value="<?= $order_id ?>">
    <div class="mb-2">
        <label class="form-label fw-bold">Deliver Now</label>
        <input type="number" name="deliver_now" min="1" max="<?= $available ?>" class="form-control" required>
    </div>
</div>
