<?php
// hps_receipt.php
require "db.php";

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid receipt");
}

$pid = (int)$_GET['id'];

/* -----------------------------
   FETCH PAYMENT
------------------------------*/
$pay = mysqli_fetch_assoc(mysqli_query($conn,"
    SELECT p.*, s.sale_no, c.name AS customer, c.mobile
    FROM hps_payments p
    JOIN hps_sales s ON s.id = p.sale_id
    JOIN hps_customers c ON c.id = s.customer_id
    WHERE p.id = $pid
"));

if (!$pay) {
    die("Receipt not found");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Payment Receipt</title>

<style>
body{
    font-family: Arial, sans-serif;
    font-size: 14px;
}
.receipt{
    width: 350px;
    margin: auto;
    border: 1px dashed #000;
    padding: 10px;
}
h3,h4{
    text-align:center;
    margin:5px 0;
}
table{
    width:100%;
}
td{
    padding:4px 0;
}
hr{
    border:0;
    border-top:1px dashed #000;
}
@media print{
    button{display:none;}
}
</style>
</head>
<body>

<div class="receipt">

<h3>YOUR SHOP NAME</h3>
<h4>Hire Purchase Receipt</h4>

<hr>

<table>
<tr>
<td><b>Receipt #</b></td>
<td align="right"><?=$pay['id']?></td>
</tr>
<tr>
<td><b>Date</b></td>
<td align="right"><?=$pay['pay_date']?></td>
</tr>
</table>

<hr>

<table>
<tr>
<td><b>Sale No</b></td>
<td align="right"><?=$pay['sale_no']?></td>
</tr>
<tr>
<td><b>Customer</b></td>
<td align="right"><?=$pay['customer']?></td>
</tr>
<tr>
<td><b>Mobile</b></td>
<td align="right"><?=$pay['mobile']?></td>
</tr>
</table>

<hr>

<table>
<tr>
<td><b>Payment Type</b></td>
<td align="right"><?=$pay['pay_type']?></td>
</tr>
<tr>
<td><b>Amount</b></td>
<td align="right"><b><?=number_format($pay['amount'])?></b></td>
</tr>
<tr>
<td><b>Remarks</b></td>
<td align="right"><?=$pay['remarks']?></td>
</tr>
</table>

<hr>

<p style="text-align:center;">
Thank you for your payment
</p>

<p style="text-align:center;font-size:12px;">
Software by HPS
</p>

</div>

<div style="text-align:center;margin-top:10px;">
<button onclick="window.print()">🖨 Print Receipt</button>
</div>

</body>
</html>
