<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h1 class="h3 mb-0">Kitchen Display</h1>
    <a href="<?= site_url('pos/kitchen') ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-clockwise"></i> Refresh</a>
</div>

<?php if (empty($orders)): ?>
    <div class="alert alert-secondary">No orders currently sent to the kitchen.</div>
<?php endif; ?>

<div class="row row-cols-1 row-cols-md-3 g-3">
    <?php foreach ($orders as $order): ?>
        <div class="col">
            <div class="card shadow-sm h-100">
                <div class="card-header d-flex justify-content-between">
                    <strong>Order #<?= $order->id ?></strong>
                    <span class="text-muted small"><?= $order->table_number ? 'Table ' . esc($order->table_number) : 'Takeaway' ?></span>
                </div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($order->items as $item): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center <?= $item->kitchen_status === 'ready' ? 'text-decoration-line-through text-muted' : '' ?>">
                            <span><?= (int) $item->quantity ?>× <?= esc($item->item_name) ?></span>
                            <?php if ($item->kitchen_status !== 'ready'): ?>
                                <form action="<?= site_url('pos/kitchen/items/' . $item->id . '/ready') ?>" method="post">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-success">Ready</button>
                                </form>
                            <?php else: ?>
                                <span class="badge bg-success">Ready</span>
                            <?php endif; ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<?= $this->endSection() ?>
