ploxion-inventaire/app.blade.php

138 lines
11 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{{-- PL0XI0N inventaire 🗄️ — le STOCK MAÎTRE du cubion bouffe : tout ce qu'on a à disposition.
Lit/écrit window.jOSBouffe (modèle partagé) → toute modif se répercute LIVE dans frigo/cuisine/courses.
CRUD complet : ajouter, quantité ±, lieu, supprimer. 100% client. window.jOSApps['inventaire'].
PORTS xion : OUT stock (snapshot à chaque mutation, → frigo.stock/cuisine.stock) ·
IN stock ← courses.bought routé par jOSWire (idempotent : courses fait déjà B.add en direct). --}}
<style>
.inv{position:relative;height:100%;display:flex;flex-direction:column;background:var(--xr-bg,#0a0d16);color:var(--xr-text,#e7f0ff);font-family:var(--xr-font-ui,system-ui,sans-serif)}
.inv *{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
.inv__bar{flex:0 0 auto;display:flex;align-items:center;gap:8px;padding:10px 12px;border-bottom:1px solid var(--xr-border,#22324f)}
.inv__bar b{font-size:1.02rem} .inv__pill{font-size:.66rem;color:#8fb6ff;margin-left:auto}
.inv__add{flex:0 0 auto;display:flex;gap:6px;flex-wrap:wrap;padding:9px 12px;border-bottom:1px solid var(--xr-border,#22324f);background:rgba(255,255,255,.02)}
.inv__add input,.inv__add select{background:rgba(0,0,0,.32);border:1px solid var(--xr-border,#22324f);border-radius:8px;color:#eef;padding:8px 9px;font:inherit;font-size:.82rem;outline:none}
.inv__add input.n{flex:1;min-width:110px} .inv__add input.q{width:58px;text-align:center}
.inv__add button{background:linear-gradient(150deg,#4fe6d6,#3d9dff);color:#04121a;font-weight:800;border:0;border-radius:9px;padding:8px 13px;cursor:pointer}
.inv__list{flex:1;min-height:0;overflow-y:auto;padding:8px 10px 20px}
.inv__grp{font-size:.62rem;text-transform:uppercase;letter-spacing:.09em;color:#7f93b8;margin:12px 4px 6px}
.inv__it{display:flex;align-items:center;gap:9px;padding:8px 10px;border:1px solid var(--xr-border,#22324f);border-radius:11px;background:rgba(20,26,42,.55);margin-bottom:7px}
.inv__nm{flex:1;min-width:0;font-weight:600;font-size:.9rem} .inv__nm small{display:block;color:var(--xr-muted,#8aa0c0);font-weight:400;font-size:.66rem}
.inv__qty{display:flex;align-items:center;gap:6px}
.inv__qty button{width:28px;height:28px;border-radius:8px;border:1px solid var(--xr-border,#22324f);background:rgba(0,0,0,.3);color:var(--xr-text,#e7f0ff);font-size:1rem;cursor:pointer}
.inv__qv{min-width:52px;text-align:center;font-variant-numeric:tabular-nums;font-weight:700;font-size:.86rem}
.inv__loc{font-size:.62rem;padding:3px 8px;border-radius:20px;border:1px solid var(--xr-border,#22324f);cursor:pointer;white-space:nowrap;text-transform:capitalize}
.inv__loc[data-l=frigo]{color:#7fd0ff;border-color:rgba(90,180,255,.4)} .inv__loc[data-l=congelo]{color:#a9c7ff} .inv__loc[data-l=placard]{color:#ffd493;border-color:rgba(230,180,90,.35)}
.inv__x{background:none;border:0;color:rgba(255,150,150,.7);font-size:.95rem;cursor:pointer;padding:4px}
.inv__empty{text-align:center;color:var(--xr-muted,#8aa0c0);padding:30px 16px;font-size:.85rem}
.inv__note{font-size:.64rem;color:#6f83a8;padding:6px 12px}
</style>
<script>
(function () {
"use strict";
var UNITS = ['pièces', 'g', 'kg', 'ml', 'L', 'paquet', 'boîte', 'sachet'];
function describe() {
var B = window.jOSBouffe;
if (!B) return { v: 1, id: 'inventaire', title: 'Inventaire', icon: '🗄️', items: [{ p: 1, kind: 'text', label: 'inventaire', value: 'modèle bouffe indisponible' }] };
var all = B.all(), low = B.low();
var items = [{ p: 1, kind: 'stat', label: 'aliments', value: all.length }];
if (low.length) items.push({ p: 2, kind: 'stat', label: 'à racheter', value: low.length });
return { v: 1, id: 'inventaire', title: 'Inventaire', icon: '🗄️', items: items };
}
function mount(container, ctx) {
var B = window.jOSBouffe;
container.innerHTML =
'<div class="inv">'
+ ' <div class="inv__bar"><span style="font-size:1.2rem">🗄️</span><b>Inventaire</b><span class="inv__pill" data-count></span></div>'
+ ' <div class="inv__add">'
+ ' <input class="n" data-n placeholder="ajouter un aliment…" maxlength="40">'
+ ' <input class="q" data-q type="number" value="1" min="0" step="1">'
+ ' <select data-u>' + UNITS.map(function (u) { return '<option>' + u + '</option>'; }).join('') + '</select>'
+ ' <select data-l><option value="placard">placard</option><option value="frigo">frigo</option><option value="congelo">congélo</option></select>'
+ ' <button data-plus>+</button>'
+ ' </div>'
+ ' <div class="inv__list" data-list></div>'
+ ' <div class="inv__note">Partagé en direct avec frigo · cuisine · courses (un seul stock).</div>'
+ '</div>';
if (!B) { container.querySelector('[data-list]').innerHTML = '<div class="inv__empty">Modèle bouffe indisponible.</div>'; return { unmount: function () {} }; }
var root = container.querySelector('.inv'), listEl = root.querySelector('[data-list]'), countEl = root.querySelector('[data-count]');
var nEl = root.querySelector('[data-n]'), qEl = root.querySelector('[data-q]'), uEl = root.querySelector('[data-u]'), lEl = root.querySelector('[data-l]');
function esc(s) { return (s == null ? '' : String(s)).replace(/[&<>"]/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]; }); }
function locName(l) { return l === 'congelo' ? 'congélo' : l; }
var rx = (B && B.recettes) ? B.recettes : null;
if (rx) rx.load().then(function () { render(); }).catch(function () {});
function render() {
var all = B.all(); countEl.textContent = all.length + ' aliment' + (all.length > 1 ? 's' : '');
if (!all.length) { listEl.innerHTML = '<div class="inv__empty">Rien en stock. Ajoute ce que tu as. 🍎</div>'; return; }
var order = { frigo: 0, congelo: 1, placard: 2 }, groups = { frigo: [], congelo: [], placard: [] };
all.forEach(function (it) { (groups[it.loc] || (groups[it.loc] = [])).push(it); });
var html = '';
Object.keys(groups).sort(function (a, b) { return (order[a] == null ? 9 : order[a]) - (order[b] == null ? 9 : order[b]); }).forEach(function (loc) {
var g = groups[loc]; if (!g.length) return; g.sort(function (a, b) { return (a.name || '').localeCompare(b.name || ''); });
html += '<div class="inv__grp">' + (loc === 'frigo' ? '❄ frigo' : loc === 'congelo' ? '🧊 congélo' : '🗄 placard') + ' · ' + g.length + '</div>';
g.forEach(function (it) {
var used = (rx && rx.ready()) ? rx.usedIn(it.name).length : 0; // badge « utilisé dans N recettes »
html += '<div class="inv__it" data-id="' + it.id + '">'
+ '<div class="inv__nm">' + esc(it.name) + '<small>' + esc(it.cat || '') + (it.exp ? ' · ⏳ ' + it.exp + 'j' : '') + (used ? ' · 🍳 ' + used + ' recettes' : '') + '</small></div>'
+ '<div class="inv__qty"><button data-dec>−</button><span class="inv__qv">' + (it.qty != null ? it.qty : 0) + ' ' + esc(it.unit || '') + '</span><button data-inc>+</button></div>'
+ '<span class="inv__loc" data-loc data-l="' + it.loc + '" title="changer de lieu">' + locName(it.loc) + '</span>'
+ '<button class="inv__x" data-del title="supprimer">✕</button></div>';
});
});
listEl.innerHTML = html;
}
var LOOP = ['placard', 'frigo', 'congelo'];
listEl.addEventListener('click', function (e) {
var it = e.target.closest('[data-id]'); if (!it) return; var id = it.getAttribute('data-id');
var obj = B.all().filter(function (x) { return x.id === id; })[0]; if (!obj) return;
if (e.target.closest('[data-inc]')) B.set(id, { qty: (+obj.qty || 0) + 1 });
else if (e.target.closest('[data-dec]')) { var nq = (+obj.qty || 0) - 1; if (nq <= 0) B.remove(id); else B.set(id, { qty: nq }); }
else if (e.target.closest('[data-loc]')) { var ni = (LOOP.indexOf(obj.loc) + 1) % LOOP.length; B.set(id, { loc: LOOP[ni] }); }
else if (e.target.closest('[data-del]')) B.remove(id);
});
function addIt() { var name = (nEl.value || '').trim(); if (!name) return; var loc = lEl.value;
B.add({ name: name, qty: +qEl.value || 1, unit: uEl.value, loc: loc, cat: loc === 'placard' ? 'sec' : 'frais' });
nEl.value = ''; qEl.value = '1'; nEl.focus(); try { if (window.jXsfx) window.jXsfx.tap(); } catch (x) {} }
root.querySelector('[data-plus]').onclick = addIt;
nEl.addEventListener('keydown', function (e) { if (e.key === 'Enter') addIt(); });
// autocomplete du référentiel (bion) : suggère parmi 900+ ingrédients, pré-remplit unité + lieu.
// Sans match, l'ajout libre marche exactement comme avant (la liste aide, elle n'emprisonne pas).
var ac = (B.attachAutocomplete) ? B.attachAutocomplete(nEl, { unit: uEl, loc: lEl }) : null;
var off = B.on(function () { render(); }); // LIVE : re-render quand un AUTRE ploxion change le stock
// ---- PORTS xion (bus jOSPorts) : inventaire = stock maître du cubion bouffe ----
var offs = [];
function snapshot() { var all = B.all(); return { count: all.length, low: B.low().length,
items: all.slice(0, 60).map(function (it) { return { name: it.name, qty: it.qty, unit: it.unit, loc: it.loc }; }) }; }
function flashRow(name) { try { var it = B.find(name); if (!it) return;
var row = listEl.querySelector('[data-id="' + it.id + '"]'); if (!row) return;
row.style.transition = 'box-shadow .25s'; row.style.boxShadow = '0 0 0 2px rgba(79,230,214,.7)';
setTimeout(function () { try { row.style.boxShadow = ''; } catch (e) {} }, 1200); } catch (e) {} }
if (window.jOSPorts) {
// OUT stock → frigo.stock / cuisine.stock : chaque mutation réelle du stock est publiée sur le bus.
offs.push(B.on(function (items, reason) {
if (reason === 'load' || reason === 'local') return; // sync interne (boxion/localStorage), pas une donnée nouvelle
try { window.jOSPorts.emit('inventaire', 'stock', snapshot()); } catch (e) {}
}));
// IN stock ← courses.bought (routé par jOSWire) : l'achat re-entre dans le stock maître.
// courses fait déjà B.add en direct → idempotent : ajout seulement si absent (rejeu/tsoin), sinon flash.
// On ignore les payloads sans name (dont nos propres snapshots OUT, même topic) + les méta __wired/__via.
offs.push(window.jOSPorts.on('ploxion.inventaire.stock', function (p) {
try {
if (!p || typeof p !== 'object') return;
var name = (typeof p.name === 'string' ? p.name : '').trim(); if (!name) return;
if (!B.find(name)) B.add({ name: name, qty: +p.qty || 1, unit: p.unit || 'pièces',
loc: B.LOCS.indexOf(p.loc) > -1 ? p.loc : 'placard', cat: (p.loc === 'frigo' || p.loc === 'congelo') ? 'frais' : 'sec' });
flashRow(name);
} catch (e) {}
}));
}
render();
return { unmount: function () { try { off(); } catch (e) {} try { if (ac) ac.destroy(); } catch (e) {} offs.forEach(function (f) { try { f(); } catch (e) {} }); container.innerHTML = ''; } };
}
window.jOSApps = window.jOSApps || {};
window.jOSApps['inventaire'] = { manifest: { id: 'inventaire', name: 'Inventaire', icon: '🗄️' }, mount: mount, describe: describe };
})();
</script>