-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurchase_scene.js
175 lines (150 loc) · 6.66 KB
/
purchase_scene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { World } from './World.js';
import { Wholesale } from './Wholesale.js';
World.getInstance().events.subscribe('load_purchase_scene', () => {
World.getInstance().events.emit('stop_game');
const wholesale = new Wholesale();
function renderApp() {
document.getElementById('html-content').innerHTML = ''; // Reset content
const appHTML = `
<h2>Großhandel Shop</h2>
<table id="product-table" class="table">
<thead>
<tr>
<th>Emoji</th>
<th>Name</th>
<th>Besonderheit</th>
<th>Haltbarkeit</th>
<th>Qualität</th>
<th>Geschmack</th>
<th>Bestand</th>
<th>Preis (€)</th>
<th>Menge</th>
<th>Aktion</th>
</tr>
</thead>
<tbody>
<!-- Produkte werden hier dynamisch hinzugefügt -->
</tbody>
</table>
<h2>Warenkorb</h2>
<div class="cart">
<ul id="cart-list">
<li>Warenkorb ist leer.</li>
</ul>
<p><strong>Gesamtsumme:</strong> <span id="total-price">0</span> €</p>
<p><strong>Vermögen:</strong> <span id="wealth">0</span> €</p>
<p id="error-message" style="color: red; display: none;">Du hast nicht genug Geld für Deinen Warenkorb</p>
<button id="checkout-button" class="btn-green">Einkaufen</button>
</div>
`;
document.getElementById('html-content').innerHTML = appHTML;
const productTableBody = document.querySelector('#product-table tbody');
const cartList = document.getElementById('cart-list');
const totalPriceSpan = document.getElementById('total-price');
const checkoutButton = document.getElementById('checkout-button');
const wealthSpan = document.getElementById('wealth');
const errorMessage = document.getElementById('error-message');
wealthSpan.textContent = World.getInstance().getWealth();
// Produkte anzeigen
function renderProducts() {
const inventory = wholesale.pos.listInventory();
productTableBody.innerHTML = '';
inventory.forEach(product => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${product.emoji}</td>
<td>${product.name}</td>
<td>${product.attributeIds.join(',')}</td>
<td>${product.expiryDate}</td>
<td>${product.quality}</td>
<td>${product.taste}</td>
<td>${product.stock}</td>
<td>${product.sellPrice} €</td>
<td><input type="number" class="table-input" min="1" max="${product.stock}" value="1" id="quantity-${product.id}"></td>
<td><button class="btn-blue" onclick="window.addToCart('${product.id}')">+</button></td>
`;
productTableBody.appendChild(row);
});
}
// Produkt in den Warenkorb legen
window.addToCart = function (productId) {
const quantityInput = document.getElementById(`quantity-${productId}`);
const quantity = parseInt(quantityInput.value);
if (isNaN(quantity) || quantity <= 0) {
alert('Bitte eine gültige Menge eingeben!');
return;
}
try {
wholesale.pos.addToCart(productId, quantity, {});
resetQuantities();
renderCart();
} catch (error) {
alert(`Fehler beim Hinzufügen zum Warenkorb: ${error.message}`);
}
};
// Produkt aus dem Warenkorb entfernen
window.removeFromCart = function (productId) {
try {
wholesale.pos.removeFromCart(productId, 1);
resetQuantities();
renderCart();
} catch (error) {
alert(`Fehler beim Entfernen aus dem Warenkorb: ${error.message}`);
}
};
// Warenkorb anzeigen
function renderCart() {
const cart = wholesale.pos.listCart();
cartList.innerHTML = '';
if (cart.length === 0) {
cartList.innerHTML = '<li>Warenkorb ist leer.</li>';
totalPriceSpan.textContent = '0';
errorMessage.style.display = 'none';
return;
}
cart.forEach(item => {
const li = document.createElement('li');
li.innerHTML = `${item.name} (x${item.quantity}) - ${item.totalPrice.toFixed(2)} €
<button class="btn-blue" onclick="window.removeFromCart('${item.id}')">-1</button>`;
cartList.appendChild(li);
});
const total = wholesale.pos.calculateTotal();
const wealth = World.getInstance().getWealth();
totalPriceSpan.textContent = total;
if (total > wealth) {
errorMessage.style.display = 'block';
checkoutButton.disabled = true;
} else {
errorMessage.style.display = 'none';
checkoutButton.disabled = false;
}
}
// Input-Felder auf 1 zurücksetzen
function resetQuantities() {
const inputs = document.querySelectorAll("#product-table input[type='number']");
inputs.forEach(input => input.value = 1);
}
// Rechnung erstellen
checkoutButton.addEventListener('click', () => {
if (wholesale.pos.listCart().length === 0) {
alert('Warenkorb ist leer!');
return;
}
try {
const { invoice, items } = wholesale.pos.checkout('Wholesale Supplier', 'Kunde ABC', 'Retail');
console.log('Rechnung erstellt!', invoice, items);
// save products in foodStall
items.forEach(itemOn => {
World.getInstance().getFoodStall().getPos().addProductToInventory(itemOn.product, itemOn.quantity, itemOn.price)
})
renderCart();
World.getInstance().events.emit('load_pricelist_scene');
} catch (error) {
alert(`Fehler beim Erstellen der Rechnung: ${error.message}`);
}
});
// Initiale Anzeige
renderProducts();
}
renderApp();
});