<?php
require_once __DIR__ . '/../config/config.php';
require_login();

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT r.*, s.name supplier_name, s.address supplier_address, u.username created_by_name, po.po_number
                        FROM return_to_supplier r
                        JOIN suppliers s ON s.id = r.supplier_id
                        JOIN users u ON u.id = r.created_by
                        LEFT JOIN purchase_orders po ON po.id = r.po_id
                        WHERE r.id = ?");
$stmt->execute([$id]);
$ret = $stmt->fetch();
if (!$ret) { die('Return not found.'); }

$lineStmt = $pdo->prepare("SELECT rti.*, i.name item_name, un.abbreviation unit_abbr FROM return_to_supplier_items rti
                            JOIN items i ON i.id = rti.item_id LEFT JOIN units un ON un.id = i.unit_id
                            WHERE rti.return_id = ?");
$lineStmt->execute([$id]);
$lines = $lineStmt->fetchAll();

$printTitle = 'Return to Supplier';
$printSubtitle = $ret['return_number'];
include __DIR__ . '/../includes/print_header.php';
?>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm mb-6">
  <div>
    <p class="text-slate-500 mb-1">Supplier</p>
    <p class="font-semibold"><?= e($ret['supplier_name']) ?></p>
    <p><?= e($ret['supplier_address']) ?></p>
  </div>
  <div class="text-right">
    <?php if ($ret['po_number']): ?><p><span class="text-slate-500">Related PO:</span> <?= e($ret['po_number']) ?></p><?php endif; ?>
    <p><span class="text-slate-500">Date:</span> <?= date('d M Y', strtotime($ret['created_at'])) ?></p>
    <p><span class="text-slate-500">Prepared By:</span> <?= e($ret['created_by_name']) ?></p>
    <p><span class="text-slate-500">Status:</span> <span class="capitalize"><?= e($ret['status']) ?></span></p>
  </div>
</div>

<div class="overflow-x-auto"><table class="w-full text-sm mb-6 border-collapse">
  <thead><tr class="border-b-2 border-slate-800 text-left text-xs uppercase text-slate-500">
    <th class="py-2">Item</th><th class="py-2">Qty</th><th class="py-2">Unit Cost</th><th class="py-2">Value</th><th class="py-2">Reason</th></tr></thead>
  <tbody>
  <?php $total = 0; foreach ($lines as $l): $val = $l['quantity']*$l['unit_cost']; $total += $val; ?>
    <tr class="border-b border-slate-100">
      <td class="py-2"><?= e($l['item_name']) ?></td>
      <td class="py-2"><?= number_format($l['quantity'],2) ?> <?= e($l['unit_abbr']) ?></td>
      <td class="py-2">₦<?= number_format($l['unit_cost'],2) ?></td>
      <td class="py-2">₦<?= number_format($val,2) ?></td>
      <td class="py-2"><?= e($l['reason']) ?></td>
    </tr>
  <?php endforeach; ?>
  </tbody>
  <tfoot><tr class="font-semibold"><td colspan="3" class="py-2 text-right">Total Value</td><td class="py-2">₦<?= number_format($total,2) ?></td><td></td></tr></tfoot>
</table></div>

<?php if ($ret['reason']): ?><p class="text-sm mb-6"><span class="text-slate-500">Overall Reason:</span> <?= e($ret['reason']) ?></p><?php endif; ?>

<div class="grid grid-cols-1 sm:grid-cols-2 gap-6 mt-10 text-sm">
  <div class="border-t border-slate-400 pt-2">Prepared By: <?= e($ret['created_by_name']) ?></div>
  <div class="border-t border-slate-400 pt-2">Supplier Acknowledgement: ______________________</div>
</div>
<?php include __DIR__ . '/../includes/print_footer.php'; ?>
