<?php
require_once __DIR__.'/includes/functions.php';
require_once __DIR__.'/includes/v292_admin_portal_helpers.php';
$admin=current_admin(); $user=current_user(); if(!$admin && !$user){ redirect('login.php'); }
$receiptId=(int)($_GET['receipt_id'] ?? $_GET['id'] ?? 0); $invoiceId=(int)($_GET['invoice_id'] ?? 0); $batchId=(int)($_GET['batch_id'] ?? 0);
$data=mj292_invoice_data($invoiceId,$batchId,$receiptId); $receipt=$data['receipt']; $invoice=$data['invoice']; $billUser=$data['user'];
if(!$receipt){ http_response_code(404); exit('Receipt not found.'); }
if(!$admin && $user && (int)$data['user_id'] !== (int)$user['id']){ http_response_code(403); exit('You cannot view this receipt.'); }
$receiptNo=(string)($receipt['receipt_no'] ?: ('RCP-'.$receipt['id'])); $currency=(string)($receipt['currency_code'] ?? 'SGD');
?><!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Receipt <?=e($receiptNo)?></title><style>body{font-family:Arial,sans-serif;background:#f5f7fb}.wrap{max-width:760px;margin:24px auto;background:#fff;border-radius:18px;padding:36px;box-shadow:0 15px 40px rgba(15,23,42,.1)}.top{display:flex;justify-content:space-between;border-bottom:2px solid #e5e7eb;padding-bottom:20px}.muted{color:#6b7280}.box{border:1px solid #e5e7eb;border-radius:14px;padding:16px;margin-top:20px}.right{text-align:right}.btn{display:inline-block;padding:10px 16px;border-radius:999px;background:#16a34a;color:#fff;text-decoration:none}.btn.secondary{background:#6b7280}.actions{max-width:760px;margin:20px auto;text-align:right}@media print{body{background:#fff}.actions{display:none}.wrap{box-shadow:none;margin:0;max-width:none}}</style></head><body><div class="actions"><a class="btn secondary" href="javascript:history.back()">Back</a> <a class="btn" href="javascript:window.print()">Print Receipt</a></div><div class="wrap"><div class="top"><div><h2><?=e(setting('invoice_seller_name',setting('site_name','MJ Marketplace')))?></h2><div class="muted"><?=e(setting('invoice_seller_email',''))?></div></div><div class="right"><h1>RECEIPT</h1><h3><?=e($receiptNo)?></h3></div></div><div class="box"><b>Received From</b><br><?=e($billUser['name'] ?? ('User #'.$data['user_id']))?><br><span class="muted"><?=e($billUser['email'] ?? '')?></span></div><div class="box"><b>Payment Details</b><br>Amount: <b><?=mj292_money0((float)$receipt['amount'],$currency)?></b><br>Invoice: <?=e($invoice['invoice_no'] ?? ('#'.($receipt['invoice_id'] ?? ''))) ?><br>Method: <?=e($receipt['payment_method'] ?? '')?><br>Reference: <?=e($receipt['reference_no'] ?? '')?><br>Date: <?=e(!empty($receipt['created_at']) ? date('d M Y, h:i A',strtotime($receipt['created_at'])) : date('d M Y'))?></div><p class="muted">This receipt was generated automatically after payment approval or full wallet payment.</p></div></body></html>
