<?php
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../config/security.php';
require_admin();

$orders = db()->query('SELECT * FROM orders ORDER BY created_at DESC')->fetchAll();

$title = 'Ventas - BICHOTTA';
include __DIR__ . '/../includes/admin-header.php';
?>
<h1>Gestión de Ventas</h1>
<table class="table">
  <thead>
    <tr>
      <th>Pedido</th>
      <th>Cliente</th>
      <th>Total</th>
      <th>Método pago</th>
      <th>Estado pago</th>
      <th>Acciones</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($orders as $order): ?>
      <tr>
        <td><?php echo sanitize($order['order_number']); ?></td>
        <td><?php echo sanitize($order['customer_name']); ?></td>
        <td>$<?php echo number_format($order['total'], 0, ',', '.'); ?></td>
        <td><?php echo sanitize($order['payment_method']); ?></td>
        <td><?php echo sanitize($order['payment_status']); ?></td>
        <td><a class="button btn-secondary" href="order-view.php?id=<?php echo (int)$order['id']; ?>">Ver</a></td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
<?php admin_footer();
