2025-05-25 16:54:16 +02:00
|
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
2025-05-25 16:25:22 +02:00
|
|
|
|
try {
|
2025-05-25 16:54:16 +02:00
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
if (!token) {
|
2025-05-25 16:25:22 +02:00
|
|
|
|
window.location.href = '/login.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-25 16:54:16 +02:00
|
|
|
|
|
|
|
|
|
const response = await fetch('/api/order-history', {
|
|
|
|
|
headers: { 'Authorization': `Bearer ${token}` }
|
|
|
|
|
});
|
2025-05-25 16:25:22 +02:00
|
|
|
|
|
2025-05-25 16:54:16 +02:00
|
|
|
|
if (!response.ok) throw new Error('Błąd ładowania historii');
|
2025-05-25 16:25:22 +02:00
|
|
|
|
|
2025-05-25 16:54:16 +02:00
|
|
|
|
const orders = await response.json();
|
2025-05-25 16:25:22 +02:00
|
|
|
|
const container = document.getElementById('order-history');
|
2025-05-25 16:54:16 +02:00
|
|
|
|
container.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
orders.forEach(order => {
|
2025-05-25 17:52:16 +02:00
|
|
|
|
const orderDate = new Date(order.data_zamowienia).toLocaleDateString();
|
|
|
|
|
const itemsList = order.items.map(item => `
|
|
|
|
|
<div class="card dark-card mb-2">
|
|
|
|
|
<div class="card-body">
|
|
|
|
|
<h6 class="card-subtitle mb-2 text-muted">${item.tytul}</h6>
|
|
|
|
|
<p class="card-text">Autor: ${item.autor}</p>
|
|
|
|
|
<p class="card-text">Ilość: ${item.ilosc} × ${item.cena} PLN</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
|
|
|
|
|
const orderHTML = `
|
|
|
|
|
<div class="mb-4">
|
|
|
|
|
<div class="card dark-card">
|
|
|
|
|
<div class="card-header">
|
|
|
|
|
<h5 class="card-title">Zamówienie #${order.id}</h5>
|
|
|
|
|
<p class="card-text">Data: ${orderDate}</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="card-body">
|
|
|
|
|
<h6 class="text-accent">Pozycje:</h6>
|
|
|
|
|
${itemsList}
|
|
|
|
|
<hr class="bg-secondary">
|
|
|
|
|
<div class="d-flex justify-content-between">
|
|
|
|
|
<p class="fw-bold">Suma całkowita:</p>
|
|
|
|
|
<p class="fw-bold">${order.suma_totalna} PLN</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="d-flex justify-content-between">
|
|
|
|
|
<p>Status:</p>
|
|
|
|
|
<p class="text-accent">${order.status || 'Przyjęto do realizacji'}</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
container.insertAdjacentHTML('beforeend', orderHTML);
|
|
|
|
|
});
|
2025-05-25 16:54:16 +02:00
|
|
|
|
|
2025-05-25 16:25:22 +02:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error:', error);
|
2025-05-25 16:54:16 +02:00
|
|
|
|
alert('Nie udało się załadować historii zamówień');
|
2025-05-25 16:25:22 +02:00
|
|
|
|
}
|
2025-05-25 16:54:16 +02:00
|
|
|
|
});
|
2025-05-25 17:52:16 +02:00
|
|
|
|
|