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

<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-cup-straw" style="color: var(--brand);"></i> Edit Menu Item
            </div>
            <div class="card-body">
                <form action="<?= site_url('pos/menu/' . $item->id) ?>" method="post" enctype="multipart/form-data">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Name</label>
                        <input type="text" class="form-control" name="name" value="<?= esc(old('name', $item->name)) ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Description <span class="text-muted fw-normal">(optional)</span></label>
                        <textarea class="form-control" name="description" id="editItemDescription" rows="2" placeholder="A short menu description"><?= esc(old('description', $item->description ?? '')) ?></textarea>
                        <button type="button" class="btn btn-sm btn-outline-primary mt-1" id="editItemDraftBtn"><i class="bi bi-stars me-1"></i>Draft with AI</button>
                    </div>
                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Category</label>
                            <input type="text" class="form-control" name="category" value="<?= esc(old('category', $item->category ?? '')) ?>" placeholder="e.g. Mains">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Price (GHS)</label>
                            <input type="number" step="0.01" min="0" class="form-control" name="price" value="<?= esc(old('price', $item->price)) ?>" required>
                        </div>
                    </div>
                    <div class="row align-items-end mb-4">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Photo <span class="text-muted fw-normal">(JPG, PNG, or WEBP — max 2MB)</span></label>
                            <input type="file" class="form-control" name="photo" accept="image/jpeg,image/png,image/webp">
                        </div>
                        <div class="col-md-6">
                            <?php if ($item->photo_path): ?>
                                <div class="text-muted small mb-1">Current photo</div>
                                <img src="<?= site_url('uploads/' . $item->photo_path) ?>" alt="<?= esc($item->name) ?>" style="max-height: 60px; max-width: 60px; object-fit: cover; border-radius: 6px;" class="border p-1">
                            <?php else: ?>
                                <div class="text-muted small">No photo uploaded yet.</div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2 me-1"></i>Save Changes</button>
                        <a href="<?= site_url('pos/menu') ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

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

    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() ?>
