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

<h1 class="h3 mb-3">Menu Items</h1>

<div class="row">
    <div class="col-md-8 mb-3">
        <div class="card shadow-sm">
            <div class="table-scroll">
                <table class="table table-hover mb-0 align-middle">
                    <thead class="table-light"><tr><th></th><th>Name</th><th>Category</th><th>Price</th><th>Available</th><th class="text-end">Actions</th></tr></thead>
                    <tbody>
                        <?php if (empty($items)): ?>
                            <tr><td colspan="6" class="text-center text-muted py-4">No menu items yet.</td></tr>
                        <?php endif; ?>
                        <?php foreach ($items as $item): ?>
                            <tr>
                                <td style="width: 48px;">
                                    <?php if ($item->photo_path): ?>
                                        <img src="<?= site_url('uploads/' . $item->photo_path) ?>" alt="<?= esc($item->name) ?>" style="width:40px;height:40px;object-fit:cover;border-radius:6px;">
                                    <?php else: ?>
                                        <div class="d-flex align-items-center justify-content-center bg-light text-muted" style="width:40px;height:40px;border-radius:6px;"><i class="bi bi-cup-straw"></i></div>
                                    <?php endif; ?>
                                </td>
                                <td class="fw-semibold"><?= esc($item->name) ?><?php if ($item->description): ?><div class="text-muted small fw-normal"><?= esc($item->description) ?></div><?php endif; ?></td>
                                <td><?= esc($item->category) ?></td>
                                <td><?= format_money($item->price) ?></td>
                                <td>
                                    <form action="<?= site_url('pos/menu/' . $item->id . '/toggle') ?>" method="post">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm <?= $item->is_available ? 'btn-outline-success' : 'btn-outline-secondary' ?>">
                                            <?= $item->is_available ? 'Available' : 'Unavailable' ?>
                                        </button>
                                    </form>
                                </td>
                                <td class="text-end">
                                    <a href="<?= site_url('pos/menu/' . $item->id . '/edit') ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-pencil"></i></a>
                                    <form action="<?= site_url('pos/menu/' . $item->id . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this item?');">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-4 mb-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <h2 class="h6">New Menu Item</h2>
                <form action="<?= site_url('pos/menu') ?>" method="post" enctype="multipart/form-data">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label">Name</label>
                        <input type="text" class="form-control" name="name" value="<?= esc(old('name', '')) ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Description <span class="text-muted small">(optional)</span></label>
                        <textarea class="form-control" name="description" id="newItemDescription" rows="2" placeholder="A short menu description"><?= esc(old('description', '')) ?></textarea>
                        <button type="button" class="btn btn-sm btn-outline-primary mt-1" id="newItemDraftBtn"><i class="bi bi-stars me-1"></i>Draft with AI</button>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Category</label>
                        <input type="text" class="form-control" name="category" placeholder="e.g. Mains" value="<?= esc(old('category', '')) ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Price (GHS)</label>
                        <input type="number" step="0.01" min="0" class="form-control" name="price" value="<?= esc(old('price', '')) ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Photo <span class="text-muted small">(optional, max 2MB)</span></label>
                        <input type="file" class="form-control" name="photo" accept="image/jpeg,image/png,image/webp">
                    </div>
                    <button type="submit" class="btn btn-primary">Add Item</button>
                </form>
            </div>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
document.getElementById('newItemDraftBtn').addEventListener('click', function () {
    const btn = this;
    const name = document.querySelector('input[name="name"]').value.trim();
    const textarea = document.getElementById('newItemDescription');

    if (! name) {
        alert('Enter a name first.');
        return;
    }

    btn.disabled = true;
    btn.innerHTML = '<span class="spinner-border spinner-border-sm"></span>';

    window.maxyieldsPost('<?= site_url('ai/draft-description') ?>', { context: 'menu_item', name: name, prompt: textarea.value })
        .then(function (data) {
            if (data.success) {
                textarea.value = data.text;
            } else {
                alert(data.error);
            }
        })
        .catch(function () {
            alert('Could not reach the server — please try again.');
        })
        .finally(function () {
            btn.disabled = false;
            btn.innerHTML = '<i class="bi bi-stars me-1"></i>Draft with AI';
        });
});
</script>
<?= $this->endSection() ?>
