<?php
include 'db.php';

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

// Optional: Only print newly added items if passed
$new_item_ids = [];
if (!empty($_GET['items'])) {
    $raw = explode(',', $_GET['items']);
    $new_item_ids = array_map('intval', $raw);
}

$order = $conn->query("SELECT * FROM hold_orders WHERE id = $hold_id")->fetch_assoc();

// 🟡 Item filtering
$items_query = "SELECT * FROM hold_items WHERE hold_id = $hold_id";
if (!empty($new_item_ids)) {
    $ids_list = implode(',', $new_item_ids);
    $items_query .= " AND item_id IN ($ids_list)";
}
$items = $conn->query($items_query);
?>

<!DOCTYPE html>
<html>
<head>
  <title>KOT</title>
  <style>
    body {
      font-family: monospace;
      font-size: 15px;
      margin: 0;
      padding: 10px;
      width: 80mm;
    }
    h2 {
      font-size: 18px;
      text-align: center;
      margin-bottom: 10px;
    }
    p {
      margin: 0 0 6px;
    }
    table {
      width: 100%;
      border-collapse: collapse;
    }
    td {
      padding: 4px 0;
    }
    .text-right {
      text-align: right;
    }
    .line {
      border-top: 1px dashed #000;
      margin: 10px 0;
    }
  </style>
</head>
<body>

  <h2>KITCHEN ORDER TICKET</h2>
  <p><strong>Order #:</strong> <?= htmlspecialchars($order['id']) ?></p>
  <p><strong>Customer:</strong> <?= htmlspecialchars($order['customer_name']) ?></p>
  <p><strong>Time:</strong> <?= date('H:i', strtotime($order['created_at'])) ?></p>
  <?php if (!empty($order['order_mode'])): ?>
    <p><strong>Mode:</strong> <?= strtoupper($order['order_mode']) ?></p>
  <?php endif; ?>

  <?php if (!empty($order['table_id'])):
    $table = $conn->query("SELECT table_name FROM dinein_tables WHERE id = " . intval($order['table_id']))->fetch_assoc();
    if ($table):
  ?>
    <p><strong>Table:</strong> <?= htmlspecialchars($table['table_name']) ?></p>
  <?php endif; endif; ?>

  <?php if (!empty($order['order_taker_id'])):
    $order_taker = $conn->query("SELECT name FROM order_takers WHERE id = " . intval($order['order_taker_id']))->fetch_assoc();
    if ($order_taker):
  ?>
    <p><strong>Order Taker:</strong> <?= htmlspecialchars($order_taker['name']) ?></p>
  <?php endif; endif; ?>

  <div class="line"></div>

  <table>
    <?php if ($items->num_rows > 0): ?>
      <?php while($row = $items->fetch_assoc()): ?>
        <tr>
          <td>
            <?= htmlspecialchars($row['item_name']) ?>
            <?php if (!empty($row['modifiers'])): ?>
              <?php 
                $modifiers = json_decode($row['modifiers'], true);
                if (is_array($modifiers) && !empty($modifiers)) {
                  $modifier_names = array_map(function($m) { return $m['name'] ?? ''; }, $modifiers);
                  $modifier_text = implode(', ', array_filter($modifier_names));
                  if (!empty($modifier_text)) {
                    echo '<br><small style="color: #666;">(' . htmlspecialchars($modifier_text) . ')</small>';
                  }
                }
              ?>
            <?php endif; ?>
            <?php if (!empty($row['remarks'])): ?>
              <br><small style="color: #ff6b35; font-style: italic;">💬 <?= htmlspecialchars($row['remarks']) ?></small>
            <?php endif; ?>
          </td>
          <td class="text-right">x <?= intval($row['quantity']) ?></td>
        </tr>
      <?php endwhile; ?>
    <?php else: ?>
      <tr><td colspan="2">No items found.</td></tr>
    <?php endif; ?>
  </table>

  <div class="line"></div>
  <p style="text-align:center;">--- END OF KOT ---</p>

  <script>
    window.onload = function () {
      window.print();
      setTimeout(() => window.close(), 500);
    };
  </script>

</body>
</html>
