<?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);

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

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

$order = $stmt->fetch();

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

/*
|--------------------------------------------------------------------------
| ORDER ITEMS
|--------------------------------------------------------------------------
| FIX:
| Your order_items table does NOT use product_name.
| It uses:
| - name
| - quantity
| - price
|--------------------------------------------------------------------------
*/

$itemStmt = $pdo->prepare("
SELECT *
FROM order_items
WHERE order_id = ?
");

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

$items = $itemStmt->fetchAll();

$customerEmail = '';

$emailStmt = $pdo->prepare("
SELECT customer_email
FROM cash_receipts
ORDER BY id DESC
LIMIT 1
");

$emailStmt->execute();

$emailRow = $emailStmt->fetch();

if($emailRow){
    $customerEmail =
    (string)($emailRow['customer_email'] ?? '');
}

/*
|--------------------------------------------------------------------------
| SEND RECEIPT 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){

        $itemName =
        (string)($item['name'] ?? 'Item');

        $itemQty =
        (int)($item['quantity'] ?? 1);

        $itemPrice =
        (float)($item['price'] ?? 0);

        $receiptText .=
        "- ".
        $itemName.
        " | Qty: ".
        $itemQty.
        " | Price: $".
        number_format($itemPrice,2).
        "\n";
    }

    if($customerEmail !== ''){

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

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

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>View 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): ?>

<?php

$itemName =
(string)($item['name'] ?? 'Item');

$itemQty =
(int)($item['quantity'] ?? 1);

$itemPrice =
(float)($item['price'] ?? 0);

?>

<tr>

<td>
<?= htmlspecialchars($itemName) ?>
</td>

<td>
<?= $itemQty ?>
</td>

<td>
$<?= number_format($itemPrice,2) ?>
</td>

</tr>

<?php endforeach; ?>

</table>

</div>

</body>
</html>
