<?php
require_once __DIR__ . '/../includes/auth.php';
require_customer();
require_once __DIR__ . '/../includes/header_customer.php';
$pdo = require __DIR__ . '/../config/pdo.php';

$orderId = isset($_GET['order_id']) ? (int) $_GET['order_id'] : 0;
$orderStmt = $pdo->prepare('SELECT o.*, u.name, u.email, u.phone, u.address
    FROM orders o JOIN users u ON u.id = o.user_id
    WHERE o.id = ? AND o.user_id = ? LIMIT 1');
$orderStmt->execute([$orderId, current_user()['id']]);
$order = $orderStmt->fetch();

if (!$order) {
    echo '<div class="alert error">Order tidak ditemukan.</div>';
    require_once __DIR__ . '/../includes/footer_customer.php';
    exit;
}

$itemStmt = $pdo->prepare('SELECT oi.qty, oi.price, oi.subtotal, p.name
    FROM order_items oi JOIN products p ON p.id = oi.product_id
    WHERE oi.order_id = ?');
$itemStmt->execute([$orderId]);
$items = $itemStmt->fetchAll();
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h2 class="fw-bold">Nota Pembelian</h2>
    <button class="btn btn-sm" onclick="window.print()">Cetak</button>
</div>

<div class="card shadow-sm mb-4">
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <h5 class="fw-bold">Al-Qanuni Store</h5>
                <p>Oleh-oleh Haji & Umroh Premium</p>
            </div>
            <div class="col-md-6 text-md-end">
                <p>Kode: <strong><?= sanitize($order['code']); ?></strong></p>
                <p>Tanggal: <?= sanitize(date('d M Y', strtotime($order['created_at']))); ?></p>
                <p>Status: <?= sanitize($order['status']); ?></p>
            </div>
        </div>
        <hr>
        <h6 class="fw-bold">Customer</h6>
        <p><?= sanitize($order['name']); ?> | <?= sanitize($order['email']); ?> | <?= sanitize($order['phone']); ?></p>
        <p>Alamat: <?= sanitize($order['shipping_address'] ?: $order['address']); ?></p>
    </div>
</div>

<div class="table-responsive">
    <table class="table align-middle">
        <thead>
            <tr>
                <th>Produk</th>
                <th>Harga</th>
                <th>Qty</th>
                <th>Subtotal</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($items as $item): ?>
                <tr>
                    <td><?= sanitize($item['name']); ?></td>
                    <td><?= format_currency($item['price']); ?></td>
                    <td><?= (int) $item['qty']; ?></td>
                    <td><?= format_currency($item['subtotal']); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="3" class="text-end">Subtotal</th>
                <th><?= format_currency($order['subtotal']); ?></th>
            </tr>
            <tr>
                <th colspan="3" class="text-end">Ongkir</th>
                <th><?= format_currency($order['shipping_cost']); ?></th>
            </tr>
            <tr>
                <th colspan="3" class="text-end">Total</th>
                <th><?= format_currency($order['total']); ?></th>
            </tr>
        </tfoot>
    </table>
</div>

<?php require_once __DIR__ . '/../includes/footer_customer.php'; ?>
