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

$orders = $pdo->query('SELECT o.id, o.code, o.total, o.status, o.created_at, u.name AS customer_name
    FROM orders o
    JOIN users u ON u.id = o.user_id
    ORDER BY o.created_at DESC')->fetchAll();
?>

<h2 class="fw-bold mb-4">Order Online</h2>

<div class="card shadow-sm">
    <div class="card-body table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Kode</th>
                    <th>Customer</th>
                    <th>Total</th>
                    <th>Status</th>
                    <th>Tanggal</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($orders as $order): ?>
                    <tr>
                        <td><?= sanitize($order['code']); ?></td>
                        <td><?= sanitize($order['customer_name']); ?></td>
                        <td><?= format_currency($order['total']); ?></td>
                        <td><?= sanitize($order['status']); ?></td>
                        <td><?= sanitize(date('d M Y', strtotime($order['created_at']))); ?></td>
                        <td>
                            <a class="btn btn-sm" href="<?= base_url('admin/order_manage.php?id=' . (int) $order['id']); ?>">Kelola</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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