<?php
require_once "db.php";
$id = intval($_GET['id'] ?? 0);
if($id<=0) die("Invalid ID");

$stmt = $conn->prepare("SELECT dc.*, s.invoice_no, s.total AS sale_total, c.customer_name, c.customer_phone
                        FROM due_collection dc
                        LEFT JOIN sales s ON dc.sale_id = s.sale_id
                        LEFT JOIN customers c ON s.customer_id = c.customer_id
                        WHERE dc.collection_id=?");
$stmt->bind_param("i",$id);
$stmt->execute();
$res = $stmt->get_result();
$col = $res->fetch_assoc();
$stmt->close();
if(!$col) die("Collection not found");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title> Due Collection Print</title>
<style>
body{font-family:sans-serif;padding:0;}
table{width:100%;border-collapse:collapse;margin-top:12px}
td,th{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">
  <h2>Due Collection Receipt</h2>
<p><strong>Invoice No:</strong> <?php echo htmlspecialchars($col['invoice_no']); ?> &nbsp; <strong>Invoice Date:</strong> <?php echo htmlspecialchars($col['collection_date']); ?></p>
  <p><strong>Customer Name:</strong> <?php echo htmlspecialchars($col['customer_name']); ?> &nbsp; <strong>Phone:</strong> <?php echo htmlspecialchars($col['customer_phone']); ?></p>
<table>
<tr><th>Invoice Total Amount</th><td><?php echo number_format($col['sale_total'],2); ?></td></tr>
<tr><th>Due Collected Amount</th><td><?php echo number_format($col['collected_amount'],2); ?></td></tr>
</table>
<br>
<p><strong>Note:</strong> <?php echo htmlspecialchars($col['note']); ?></p>
</main>
 <?php
    include("global/global-print-footer.php");
?>
</body>
</html>
