<?php
session_start();
include "Adminn/db.php";

if(!isset($_GET['order_id'])){
    header("Location: index.php");
    exit();
}

$order_id = intval($_GET['order_id']);

/* ================= FETCH ORDER ================= */
$order_query = mysqli_query($conn,"SELECT * FROM orders WHERE id='$order_id'");
$order = mysqli_fetch_assoc($order_query);

if(!$order){
    die("Invalid Order ID");
}

/* ================= FETCH ORDER ITEMS ================= */
$items_query = mysqli_query($conn,"
    SELECT order_items.*, products.name, products.currency 
    FROM order_items 
    JOIN products ON order_items.product_id = products.id
    WHERE order_items.order_id = '$order_id'
");

$items = [];
$grand_total = 0;

while($row = mysqli_fetch_assoc($items_query)){
    $items[] = $row;
    $grand_total += $row['price'] * $row['quantity'];
}

/* ================= BUILD WHATSAPP MESSAGE ================= */
$message  = "NEW ORDER CONFIRMATION\n";
$message .= "Order ID: #".$order_id."\n";
$message .= "Name: ".$order['customer_name']."\n";
$message .= "Phone: ".$order['phone']."\n";
$message .= "Email: ".$order['email']."\n";
$message .= "Address: ".$order['address'].", ".$order['city']."\n";
$message .= "Payment Method: ".$order['payment_method']."\n";
$message .= "-------------------------\n";
$message .= "Items Purchased:\n";

foreach($items as $item){
    $line_total = $item['price'] * $item['quantity'];
    $message .= $item['name']." x".$item['quantity']." = "
              .number_format($line_total)." ".$item['currency']."\n";
}

$message .= "-------------------------\n";
$message .= "Grand Total: ".number_format($grand_total)."To Be calculated according to the currency";

$whatsapp_number = "2347045755264"; // CHANGE TO YOUR NUMBER
$whatsapp_link = "https://wa.me/".$whatsapp_number."?text=".urlencode($message);
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Receipt</title>

<style>
body{
    background:#f4f6f9;
    font-family:Segoe UI;
    margin:0;
}

.receipt{
    max-width:800px;
    margin:40px auto;
    background:white;
    padding:30px;
    border-radius:12px;
    box-shadow:0 10px 30px rgba(0,0,0,0.08);
}

h2{
    text-align:center;
    color:rgb(242,139,0);
}

.order-id{
    text-align:center;
    margin-bottom:20px;
    color:#666;
}

.notice{
    background:#fff8e6;
    border-left:5px solid rgb(242,139,0);
    padding:15px;
    margin-bottom:20px;
    font-size:15px;
}

.details{
    line-height:1.8;
    margin-bottom:20px;
}

table{
    width:100%;
    border-collapse:collapse;
}

th{
    background:rgb(242,139,0);
    color:white;
    padding:12px;
}

td{
    padding:12px;
    border-bottom:1px solid #eee;
    text-align:center;
}

.total-row td{
    font-weight:bold;
    background:#fafafa;
}

.btn{
    display:block;
    width:100%;
    padding:15px;
    margin-top:15px;
    text-align:center;
    text-decoration:none;
    color:white;
    border-radius:30px;
    font-weight:600;
    cursor:pointer;
    border:none;
}

.whatsapp{
    background:#25D366;
}

.print{
    background:#444;
}

@media print{
    .btn, .notice{
        display:none;
    }
}
</style>
</head>

<body>

<div class="receipt">

<h2>Order Successful!</h2>
<div class="order-id">Order ID: #<?php echo $order_id; ?></div>

<div class="notice">
<strong>Important:</strong><br>
To ensure your order is attended to quickly, please click the WhatsApp confirmation button below and submit your purchase details. Orders are processed only after WhatsApp confirmation.
</div>

<div class="details">
<strong>Name:</strong> <?php echo $order['customer_name']; ?><br>
<strong>Email:</strong> <?php echo $order['email']; ?><br>
<strong>Phone:</strong> <?php echo $order['phone']; ?><br>
<strong>Address:</strong> <?php echo $order['address'].", ".$order['city']; ?><br>
<strong>Payment Method:</strong> <?php echo $order['payment_method']; ?>
</div>

<table>
<tr>
    <th>Product</th>
    <th>Qty</th>
    <th>Price</th>
    <th>Total</th>
</tr>

<?php foreach($items as $item): ?>
<tr>
    <td><?php echo $item['name']; ?></td>
    <td><?php echo $item['quantity']; ?></td>
    <td><?php echo number_format($item['price'])." ".$item['currency']; ?></td>
    <td><?php echo number_format($item['price'] * $item['quantity'])." ".$item['currency']; ?></td>
</tr>
<?php endforeach; ?>

<tr class="total-row">
    <td colspan="3">Grand Total</td>
    <td><?php echo number_format($grand_total); ?>According to your currencies</td>
</tr>
</table>

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

<a href="<?php echo $whatsapp_link; ?>" class="btn whatsapp" target="_blank">
Confirm & Submit Purchase on WhatsApp
</a>

</div>

</body>
</html>