<?php
session_start();
if (!isset($_SESSION['username'])) { echo "Access denied"; exit(); }
require_once "db.php";
if (!isset($_GET['id'])) { echo "Invalid request"; exit(); }
$id = intval($_GET['id']);

$stmt = $conn->prepare("
  SELECT dp.*, p.purchase_date, p.invoice_no, p.total AS purchase_total, s.supplier_name, s.phone, s.address
  FROM due_payments dp
  LEFT JOIN purchases p ON dp.purchase_id = p.purchase_id
  LEFT JOIN suppliers s ON dp.supplier_id = s.supplier_id
  WHERE dp.due_payment_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$payment = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$payment) { echo "Payment not found."; exit(); }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Payment Receipt #<?php echo $payment['due_payment_id']; ?></title>
<style>
body{font-family:Arial;margin:20px}
table{width:100%;border-collapse:collapse;margin-top:12px}
th,td{border:1px solid #000;padding:8px;text-align:left}
</style>
</head>
<body onload="window.print()" class="global-print">
  <?php
    include("global/global-print-heading.php");
?>
  <main class="print-body-content">
    <p><strong>Invoice No:</strong> <?php echo htmlspecialchars($payment['invoice_no']); ?> &nbsp; <strong>Invoice Date:</strong> <?php echo htmlspecialchars($payment['purchase_date']); ?></p>
  <p><strong>Supplier:</strong> <?php echo htmlspecialchars($payment['supplier_name']); ?> &nbsp; <strong>Phone:</strong> <?php echo htmlspecialchars($payment['phone']); ?></p>
  <table>
    <tr><th>Description</th><th>Amount (৳)</th></tr>
    <tr><td>Invoice Total</td><td><?php echo number_format($payment['purchase_total'],2); ?></td></tr>
    <tr><td>Due Payment Amount (<?php echo htmlspecialchars($payment['method']); ?>)</td><td><?php echo number_format($payment['amount'],2); ?></td></tr>
  </table>
  <p><strong>Note:</strong> <?php echo htmlspecialchars($payment['note']); ?></p>
</main>
   <?php
    include("global/global-print-footer.php");
?>
</body>
</html>
