<?php

declare(strict_types=1);

require_once '../../private/db.php';
require_once '../../private/auth.php';

require_login();

$paymentIntent =
trim((string)($_GET['pi'] ?? ''));

if($paymentIntent === ''){

die('Receipt not found');

}

$stmt = $pdo->prepare("
SELECT *
FROM orders
WHERE stripe_payment_intent = ?
ORDER BY id DESC
LIMIT 1
");

$stmt->execute([
$paymentIntent
]);

$order = $stmt->fetch();

if(!$order){

die('Receipt not found');

}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Receipt</title>

<style>

body{
font-family:Arial;
background:#f3f4f6;
padding:40px;
}

.receipt{
max-width:700px;
margin:auto;
background:#fff;
padding:40px;
border-radius:10px;
box-shadow:0 0 15px rgba(0,0,0,0.1);
}

h1{
margin-top:0;
}

.line{
margin-bottom:15px;
}

.print-btn{
padding:14px 20px;
background:#2563eb;
color:#fff;
border:none;
border-radius:8px;
cursor:pointer;
font-size:16px;
margin-top:20px;
}

@media print{

.print-btn{
display:none;
}

body{
background:#fff;
padding:0;
}

.receipt{
box-shadow:none;
}

}

</style>

</head>
<body>

<div class="receipt">

<h1>Executive Protection Institute</h1>

<div class="line">
<strong>Invoice:</strong>
<?= htmlspecialchars($order['order_number']) ?>
</div>

<div class="line">
<strong>Payment Method:</strong>
<?= htmlspecialchars($order['payment_method']) ?>
</div>

<div class="line">
<strong>Total:</strong>
$<?= number_format((float)$order['total'],2) ?>
</div>

<div class="line">
<strong>Status:</strong>
<?= htmlspecialchars($order['status']) ?>
</div>

<div class="line">
<strong>Stripe Payment Intent:</strong><br>
<?= htmlspecialchars($order['stripe_payment_intent']) ?>
</div>

<div class="line">
<strong>Date:</strong>
<?= htmlspecialchars($order['created_at'] ?? date('Y-m-d H:i:s')) ?>
</div>

<button
class="print-btn"
onclick="window.print()">
Print Receipt
</button>

</div>

<script>

window.onload = function(){

setTimeout(function(){

window.print();

},1000);

};

</script>

</body>
</html>
