<?php
require 'db.php';
include 'layout.php';

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$return = $conn->query("SELECT pr.*, p.purchase_number, v.name AS vendor FROM purchase_returns pr JOIN purchases p ON pr.purchase_id = p.id JOIN vendors v ON pr.vendor_id = v.id WHERE pr.id = $id")->fetch_assoc();
if (!$return) {
    echo "<div class='alert alert-danger'>Purchase return not found.</div>";
    exit;
}
$items = $conn->query("SELECT pri.*, i.name as item_name FROM purchase_return_items pri JOIN items i ON pri.item_id = i.id WHERE pri.purchase_return_id = $id");
?>
<div class="page-wrapper">
  <div class="content">
    <h4>Purchase Return Details</h4>
    <div class="mb-3">
      <strong>Return Date:</strong> <?= htmlspecialchars($return['return_date']) ?><br>
      <strong>Purchase #:</strong> <?= htmlspecialchars($return['purchase_number']) ?><br>
      <strong>Vendor:</strong> <?= htmlspecialchars($return['vendor']) ?><br>
      <strong>Total Amount:</strong> <?= number_format($return['total_amount'], 2) ?><br>
      <strong>Remarks:</strong> <?= htmlspecialchars($return['remarks']) ?><br>
    </div>
    <table class="table table-bordered">
      <thead class="table-light">
        <tr>
          <th>Item</th>
          <th>Return Qty</th>
          <th>Unit Cost</th>
          <th>Total</th>
          <th>Tax Rate</th>
        </tr>
      </thead>
      <tbody>
        <?php while ($item = $items->fetch_assoc()): ?>
        <tr>
          <td><?= htmlspecialchars($item['item_name']) ?></td>
          <td><?= $item['quantity'] ?></td>
          <td><?= number_format($item['unit_cost'], 2) ?></td>
          <td><?= number_format($item['total_cost'], 2) ?></td>
          <td><?= number_format($item['tax_rate'], 2) ?>%</td>
        </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
    <a href="purchase_return_report.php" class="btn btn-secondary mt-3">Back to Report</a>
  </div>
</div> 