<?php
require_once __DIR__ . '/../system/customer/auth.php';
require_once __DIR__ . '/../system/customer/middleware.php';

customer_auth();

$user = customer_user();

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

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

$stmt->execute([$id, $user['email']]);

$order = $stmt->fetch();

if (!$order) {
    exit('Order tidak ditemukan.');
}

$stmt = $pdo->prepare("
    SELECT *
    FROM order_items
    WHERE order_id = ?
");
$stmt->execute([$id]);

$items = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Order Detail</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-100 p-6">

<div class="max-w-5xl mx-auto bg-white rounded-3xl shadow p-6">

<div class="flex justify-between mb-6">
    <div>
        <h1 class="text-3xl font-black">
            <?= e($order['order_code']) ?>
        </h1>

        <p class="text-slate-500 mt-2">
            <?= e($order['created_at']) ?>
        </p>
    </div>

    <a href="orders.php" class="text-green-700 font-bold">
        ← Orders
    </a>
</div>

<div class="grid md:grid-cols-3 gap-4 mb-8">

<div class="bg-slate-100 rounded-2xl p-4">
<p class="text-slate-500">Order Status</p>
<h2 class="text-xl font-black"><?= e($order['order_status']) ?></h2>
</div>

<div class="bg-slate-100 rounded-2xl p-4">
<p class="text-slate-500">Payment</p>
<h2 class="text-xl font-black"><?= e($order['payment_status']) ?></h2>
</div>

<div class="bg-slate-100 rounded-2xl p-4">
<p class="text-slate-500">Total</p>
<h2 class="text-xl font-black text-green-700"><?= rupiah((int)$order['total']) ?></h2>
</div>

</div>

<div class="space-y-4">

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

<div class="border rounded-2xl p-4 flex justify-between">
    <div>
        <h3 class="font-bold"><?= e($item['product_name']) ?></h3>
        <p class="text-sm text-slate-500">
            Qty: <?= (int)$item['qty'] ?>
        </p>
    </div>

    <strong>
        <?= rupiah((int)$item['subtotal']) ?>
    </strong>
</div>

<?php endforeach; ?>

</div>

</div>

</body>
</html>
