<?php

declare(strict_types=1);

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

require_login();

$orderId = (int)($_GET['id'] ?? 0);

/*
|--------------------------------------------------------------------------
| LOAD ORDER
|--------------------------------------------------------------------------
*/

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

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

$order = $stmt->fetch();

if(!$order){
die('Receipt not found');
}

/*
|--------------------------------------------------------------------------
| LOAD ORDER ITEMS + PRODUCT NAMES
|--------------------------------------------------------------------------
*/

$itemStmt = $pdo->prepare("
SELECT

order_items.*,

products.name AS product_name

FROM order_items

LEFT JOIN products
ON products.id = order_items.product_id

WHERE order_items.order_id = ?
");

$itemStmt->execute([$orderId]);

$items = $itemStmt->fetchAll();

/*
|--------------------------------------------------------------------------
| FIX:
| customer email now comes directly from orders table
|--------------------------------------------------------------------------
*/

$customerEmail =
(string)($order['customer_email'] ?? '');

if(isset($_POST['send_receipt'])){

$receiptText =
"Executive Protection Institute

Invoice:
".$order['order_number']."

Payment Method:
".$order['payment_method']."

Total:
$".number_format((float)$order['total'],2)."

Items Purchased:
";

foreach($items as $item){

$receiptText .=
"- ".
($item['product_name'] ?? 'Unknown Product').
" | Qty: ".
((int)($item['quantity'] ?? 1)).
" | Price: $".
number_format(
(float)($item['price'] ?? 0),
2
).
"\n";
}

if(
$customerEmail !== ''
&&
filter_var($customerEmail, FILTER_VALIDATE_EMAIL)
){

send_receipt_email(
$customerEmail,
'Receipt '.$order['order_number'],
$receiptText
);

echo "<script>alert('Receipt Sent');</script>";

}
}

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

<style>

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

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

table{
width:100%;
border-collapse:collapse;
margin-top:20px;
}

table th,
table td{
border:1px solid #ddd;
padding:12px;
text-align:left;
}

.send-btn{
background:red;
color:#fff;
border:none;
padding:14px 20px;
border-radius:8px;
cursor:pointer;
font-size:16px;
margin-bottom:20px;
}

</style>

</head>
<body>

<div class="receipt">

<form method="POST">

<button
type="submit"
name="send_receipt"
class="send-btn">
Send Receipt
</button>

</form>

<h1>Executive Protection Institute</h1>

<p>
<strong>Invoice:</strong>
<?= htmlspecialchars((string)$order['order_number']) ?>
</p>

<p>
<strong>Payment Method:</strong>
<?= htmlspecialchars((string)$order['payment_method']) ?>
</p>

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

<p>
<strong>Status:</strong>
<?= htmlspecialchars((string)$order['status']) ?>
</p>

<p>
<strong>Customer Email:</strong>
<?= htmlspecialchars($customerEmail) ?>
</p>

<h2>Items Purchased</h2>

<table>

<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>

<?php foreach($items as $item): ?>

<tr>

<td>
<?= htmlspecialchars((string)($item['product_name'] ?? 'Unknown Product')) ?>
</td>

<td>
<?= (int)($item['quantity'] ?? 1) ?>
</td>

<td>
$<?= number_format((float)($item['price'] ?? 0),2) ?>
</td>

</tr>

<?php endforeach; ?>

</table>

</div>

</body>
</html>
