LuminaWebsite/assets/js/shop.js
Maaxxs 17dea6d41f Lumishop partial approval, Lumipunkte on dashboard and in sidebar box
Members may now request more than is in stock (or items that are sold
out); only their points have to cover the full request. When approving,
the manager can approve fewer than requested. The quantity field is
prefilled with what is in stock, the cost updates as you type, and only
the approved part is charged and taken from stock. The order keeps the
requested quantity, so the member sees e.g. "3 von 10 bewilligt". The
order list also shows the current stock and flags when it isn't enough.

The column upgrade check now covers every table that has upgrades,
instead of only users and roles. Tables that are about to be created
fresh are skipped.

The dashboard gets a card with your Lumipunkte and your rank. In the
sidebar, the points move from under your name into a gold box above the
profile.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 10:56:59 +02:00

193 lines
8.4 KiB
JavaScript

(function () {
'use strict';
function formatPoints(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
}
// ---- Dialoge ----
function closeModal(modal) { modal.hidden = true; }
document.querySelectorAll('.modal-overlay').forEach(function (modal) {
var close = modal.querySelector('.modal-close');
if (close) close.addEventListener('click', function () { closeModal(modal); });
modal.addEventListener('click', function (event) {
if (event.target === modal) closeModal(modal);
});
});
document.addEventListener('keydown', function (event) {
if (event.key !== 'Escape') return;
document.querySelectorAll('.modal-overlay:not([hidden])').forEach(closeModal);
});
// ---- Shop: Beantragen ----
var buyModal = document.getElementById('shop-buy-modal');
if (buyModal) {
var buyItem = document.getElementById('shop-buy-item');
var buyName = document.getElementById('shop-buy-name');
var buyQuantity = document.getElementById('shop-buy-quantity');
var buyTotal = document.getElementById('shop-buy-total');
var price = 0;
var updateTotal = function () {
var qty = parseInt(buyQuantity.value, 10) || 0;
buyTotal.textContent = formatPoints(Math.max(0, qty) * price);
};
var openBuy = function (button, keepValues) {
buyItem.value = button.dataset.itemId;
buyName.textContent = button.dataset.itemName;
price = parseInt(button.dataset.price, 10) || 0;
buyQuantity.max = button.dataset.max;
if (!keepValues) {
buyQuantity.value = 1;
document.getElementById('shop-buy-note').value = '';
}
updateTotal();
buyModal.hidden = false;
buyQuantity.focus();
buyQuantity.select();
};
document.querySelectorAll('.shop-buy').forEach(function (button) {
button.addEventListener('click', function () { openBuy(button, false); });
});
buyQuantity.addEventListener('input', updateTotal);
// Nach einem Fehler ist der Dialog schon offen - Preis und Hoechstmenge nachladen.
if (!buyModal.hidden) {
var current = document.querySelector('.shop-buy[data-item-id="' + buyItem.value + '"]');
if (current) openBuy(current, true); else buyModal.hidden = true;
}
}
// ---- Shop: Suche und Kategorie ----
var search = document.getElementById('shop-search');
var grid = document.getElementById('shop-items');
if (search && grid) {
var category = document.getElementById('shop-category');
var noMatch = document.getElementById('shop-no-match');
var filter = function () {
var term = search.value.trim().toLowerCase();
var cat = category ? category.value : '';
var visible = 0;
grid.querySelectorAll('.shop-card').forEach(function (card) {
var show = (term === '' || card.dataset.name.indexOf(term) !== -1)
&& (cat === '' || card.dataset.category === cat);
card.hidden = !show;
if (show) visible++;
});
if (noMatch) noMatch.hidden = visible > 0;
};
search.addEventListener('input', filter);
if (category) category.addEventListener('change', filter);
}
// ---- Antraege: Grund abfragen (Ablehnen / Stornieren) ----
document.querySelectorAll('form[data-prompt]').forEach(function (form) {
form.addEventListener('submit', function (event) {
var reason = window.prompt(form.dataset.prompt, '');
if (reason === null) {
event.preventDefault();
return;
}
form.querySelector('input[name="reason"]').value = reason;
});
});
// ---- Antraege: Teilbewilligung - Kosten passend zur eingegebenen Anzahl anzeigen ----
document.querySelectorAll('.shop-approve-quantity').forEach(function (input) {
var form = input.closest('form');
var orderId = form.querySelector('input[name="order_id"]').value;
var price = document.querySelector('[data-order-price="' + orderId + '"]');
var amount = price ? price.lastChild : null;
var unit = parseInt(input.dataset.unitPrice, 10) || 0;
var update = function () {
var qty = Math.min(parseInt(input.value, 10) || 0, parseInt(input.max, 10));
if (amount) amount.textContent = ' ' + formatPoints(Math.max(0, qty) * unit);
};
input.addEventListener('input', update);
update();
});
// ---- Verwaltung: Gegenstand anlegen / bearbeiten ----
var itemModal = document.getElementById('shop-item-modal');
if (itemModal) {
var form = document.getElementById('shop-item-form');
var title = document.getElementById('shop-item-title');
var mode = form.elements.stock_mode;
var applyMode = function () {
itemModal.querySelectorAll('[data-mode-field]').forEach(function (field) {
field.hidden = field.dataset.modeField !== mode.value;
});
var hint = itemModal.querySelector('[data-mode-hint]');
if (hint) hint.textContent = mode.value === 'warehouse' ? '(Pflicht - daraus kommt der Bestand)' : '(optional, für das Bild)';
form.elements.warehouse_item.required = mode.value === 'warehouse';
};
mode.addEventListener('change', applyMode);
var fill = function (data) {
data = data || {};
form.elements.item_id.value = data.id || '';
form.elements.name.value = data.name || '';
form.elements.category_id.value = data.category_id || '';
form.elements.description.value = data.description || '';
form.elements.price.value = data.price !== undefined ? data.price : '';
mode.value = data.stock_mode || 'manual';
form.elements.stock.value = data.stock !== undefined ? data.stock : 0;
form.elements.warehouse_item.value = data.warehouse_item || '';
form.elements.active.checked = data.active === undefined ? true : !!data.active;
title.textContent = data.id ? 'Gegenstand bearbeiten' : 'Gegenstand anlegen';
applyMode();
};
document.querySelectorAll('[data-open-modal="shop-item-modal"]').forEach(function (button) {
button.addEventListener('click', function () {
fill(button.dataset.item ? JSON.parse(button.dataset.item) : null);
itemModal.hidden = false;
form.elements.name.focus();
});
});
if (itemModal.dataset.old) {
fill(JSON.parse(itemModal.dataset.old));
} else {
applyMode();
}
}
// ---- Verwaltung: aus dem Lagerhaus uebernehmen ----
var importModal = document.getElementById('shop-import-modal');
if (importModal) {
document.querySelectorAll('[data-open-modal="shop-import-modal"]').forEach(function (button) {
button.addEventListener('click', function () { importModal.hidden = false; });
});
var checks = importModal.querySelectorAll('.shop-import-check');
var all = document.getElementById('shop-import-all');
var priceFor = function (check) {
return importModal.querySelector('input[name="prices[' + check.value + ']"]');
};
// Wer einen Gegenstand auswaehlt, muss einen Preis angeben.
checks.forEach(function (check) {
check.addEventListener('change', function () {
priceFor(check).required = check.checked;
all.checked = Array.prototype.every.call(checks, function (c) { return c.checked; });
});
});
all.addEventListener('change', function () {
checks.forEach(function (check) {
check.checked = all.checked;
priceFor(check).required = all.checked;
});
});
document.getElementById('shop-import-apply').addEventListener('click', function () {
var value = document.getElementById('shop-import-price').value;
if (value === '') return;
checks.forEach(function (check) {
if (check.checked) priceFor(check).value = value;
});
});
}
})();