Apply remaining pricelist and local-first updates

This commit is contained in:
Mikhail Chusavitin
2026-02-06 13:01:40 +03:00
parent 8e5c4f5a7c
commit 80ec7bc6b8
19 changed files with 767 additions and 30 deletions

View File

@@ -16,7 +16,7 @@
</div>
<div id="save-buttons" class="hidden flex items-center space-x-2">
<button onclick="refreshPrices()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">
Пересчитать цену
Обновить цены
</button>
<button onclick="saveConfig()" class="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700">
Сохранить
@@ -34,6 +34,14 @@
class="w-20 px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500"
onchange="updateServerCount()">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Прайслист</label>
<select id="pricelist-select"
class="w-56 px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500"
onchange="updatePricelistSelection()">
<option value="">Загрузка...</option>
</select>
</div>
<div class="text-sm text-gray-500">
<span id="server-count-info">Всего: <span id="total-server-count">1</span> сервер(а)</span>
</div>
@@ -224,6 +232,7 @@ let cart = [];
let categoryOrderMap = {}; // Category code -> display_order mapping
let autoSaveTimeout = null; // Timeout for debounced autosave
let serverCount = 1; // Server count for the configuration
let selectedPricelistId = null; // Selected pricelist (server ID)
// Autocomplete state
let autocompleteInput = null;
@@ -296,6 +305,7 @@ document.addEventListener('DOMContentLoaded', async function() {
serverCount = config.server_count || 1;
document.getElementById('server-count').value = serverCount;
document.getElementById('total-server-count').textContent = serverCount;
selectedPricelistId = config.pricelist_id || null;
if (config.items && config.items.length > 0) {
cart = config.items.map(item => ({
@@ -322,6 +332,7 @@ document.addEventListener('DOMContentLoaded', async function() {
return;
}
await loadActivePricelists();
await loadAllComponents();
renderTab();
updateCartUI();
@@ -361,6 +372,44 @@ function updateServerCount() {
triggerAutoSave();
}
async function loadActivePricelists() {
const select = document.getElementById('pricelist-select');
if (!select) return;
try {
const resp = await fetch('/api/pricelists?active_only=true&per_page=200');
const data = await resp.json();
const pricelists = data.pricelists || [];
if (pricelists.length === 0) {
select.innerHTML = '<option value="">Нет активных прайслистов</option>';
selectedPricelistId = null;
return;
}
select.innerHTML = pricelists.map(pl => {
return `<option value="${pl.id}">${pl.version}</option>`;
}).join('');
const existing = selectedPricelistId && pricelists.some(pl => Number(pl.id) === Number(selectedPricelistId));
if (existing) {
select.value = String(selectedPricelistId);
} else {
selectedPricelistId = Number(pricelists[0].id);
select.value = String(selectedPricelistId);
}
} catch (e) {
select.innerHTML = '<option value="">Ошибка загрузки</option>';
}
}
function updatePricelistSelection() {
const select = document.getElementById('pricelist-select');
const next = parseInt(select.value);
selectedPricelistId = Number.isFinite(next) && next > 0 ? next : null;
triggerAutoSave();
}
function getCategoryFromLotName(lotName) {
const parts = lotName.split('_');
return parts[0] || '';
@@ -1133,7 +1182,8 @@ async function saveConfig(showNotification = true) {
items: cart,
custom_price: customPrice,
notes: '',
server_count: serverCountValue
server_count: serverCountValue,
pricelist_id: selectedPricelistId
})
});
@@ -1327,6 +1377,17 @@ async function refreshPrices() {
if (config.price_updated_at) {
updatePriceUpdateDate(config.price_updated_at);
}
if (config.pricelist_id) {
selectedPricelistId = config.pricelist_id;
const select = document.getElementById('pricelist-select');
if (select) {
const hasOption = Array.from(select.options).some(opt => Number(opt.value) === Number(selectedPricelistId));
if (!hasOption) {
await loadActivePricelists();
}
select.value = String(selectedPricelistId);
}
}
// Re-render UI
renderTab();