Files
2026-08-22 08:40:29 +02:00

443 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Auktionshaus</title>
<style>
body {
margin: 0;
background: #111;
color: #eee;
font-family: Arial, sans-serif;
padding: 20px;
}
h1 { margin-top: 0; }
a.back { color: #6fbf73; text-decoration: none; font-size: 14px; }
a.back:hover { text-decoration: underline; }
main { max-width: 900px; margin: 20px auto 0; }
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab-btn {
background: #222;
border: 1px solid #444;
color: #ccc;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.tab-btn.active { background: #2c7a3d; border-color: #2c7a3d; color: white; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.panel {
background: #1a1a1a;
border: 1px solid #333;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.panel h2 { margin-top: 0; }
.auction-item {
border-bottom: 1px solid #2a2a2a;
padding: 14px 0;
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.auction-item:last-child { border-bottom: none; }
.auction-name { font-weight: bold; font-size: 15px; }
.auction-meta { color: #888; font-size: 12px; margin-top: 2px; }
.auction-price { color: #f5d90a; font-weight: bold; font-size: 15px; }
.auction-actions { display: flex; gap: 6px; align-items: center; }
.auction-actions input {
width: 80px;
background: #222;
border: 1px solid #444;
color: #eee;
padding: 6px 8px;
border-radius: 4px;
}
input, select {
background: #222;
border: 1px solid #444;
color: #eee;
padding: 8px 10px;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
}
button {
background: #2c7a3d;
border: none;
color: white;
padding: 8px 14px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
button.secondary { background: #444; }
button.danger { background: #a83232; }
button:hover { opacity: 0.85; }
.form-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
gap: 10px;
margin-top: 12px;
}
.form-grid label { display: block; font-size: 12px; color: #999; margin-bottom: 4px; }
.form-grid input, .form-grid select { width: 100%; }
.status-badge {
display: inline-block;
border-radius: 4px;
padding: 2px 8px;
font-size: 11px;
margin-left: 6px;
}
.status-sold { background: #1a4d2a; color: #6fbf73; }
.status-expired { background: #444; color: #ccc; }
.status-cancelled { background: #5a2a2a; color: #e08a8a; }
.msg { font-size: 13px; color: #6fbf73; min-height: 18px; margin-top: 8px; }
.empty-hint { color: #666; padding: 10px 0; }
.hidden { display: none !important; }
</style>
</head>
<body>
<a class="back" href="/index.html">&larr; zurück zur Startseite</a>
<h1>🏛️ Auktionshaus</h1>
<main>
<div class="tabs">
<button class="tab-btn active" data-tab="browse">Angebote durchstöbern</button>
<button class="tab-btn" data-tab="create">Neues Angebot</button>
<button class="tab-btn" data-tab="mine">Meine Angebote</button>
</div>
<div class="tab-panel active" id="tab-browse">
<div class="panel">
<h2>Aktive Angebote</h2>
<div id="auctionList"></div>
<div class="empty-hint hidden" id="auctionEmpty">Aktuell keine aktiven Angebote.</div>
</div>
</div>
<div class="tab-panel" id="tab-create">
<div class="panel">
<h2>Item verkaufen</h2>
<div class="form-grid">
<div>
<label>Item aus deinem Inventar</label>
<select id="newItemSelect"></select>
</div>
<div>
<label>Menge</label>
<input type="number" id="newAmount" value="1" min="1">
</div>
<div>
<label>Startpreis ($)</label>
<input type="number" id="newStartPrice" value="100" min="1">
</div>
<div>
<label>Sofortkauf-Preis (optional)</label>
<input type="number" id="newBuyout" placeholder="leer = kein Sofortkauf">
</div>
<div>
<label>Laufzeit (Stunden)</label>
<input type="number" id="newHours" value="24" min="1" max="72">
</div>
</div>
<button style="margin-top:14px;" onclick="createAuction()">Angebot einstellen</button>
<div class="msg" id="createMsg"></div>
</div>
</div>
<div class="tab-panel" id="tab-mine">
<div class="panel">
<h2>Meine Angebote</h2>
<div id="mineList"></div>
<div class="empty-hint hidden" id="mineEmpty">Du hast noch keine Angebote eingestellt.</div>
</div>
</div>
</main>
<script>
const token = localStorage.getItem("token");
if (!token) {
alert("Bitte zuerst auf der Startseite einloggen.");
location.href = "/index.html";
}
async function authFetch(url, options = {}) {
options.headers = { ...(options.headers || {}), "Authorization": "Bearer " + token };
const res = await fetch(url, options);
if (res.status === 401) {
alert("Sitzung abgelaufen. Bitte erneut einloggen.");
location.href = "/index.html";
throw new Error("Nicht autorisiert");
}
return res;
}
function escapeHtml(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
let itemNames = {};
async function loadItemNames() {
const res = await fetch("/api/items");
const data = await res.json();
(data.items || []).forEach(i => { itemNames[i.id] = i.name; });
}
function itemLabel(id) {
return itemNames[id] || id;
}
function timeLeftLabel(endsAt) {
const diff = new Date(endsAt).getTime() - Date.now();
if (diff <= 0) return "läuft gleich ab";
const hours = Math.floor(diff / 3600000);
const mins = Math.floor((diff % 3600000) / 60000);
if (hours > 0) return `noch ${hours}h ${mins}min`;
return `noch ${mins}min`;
}
// -------------------------------------------------------------
// TABS
// -------------------------------------------------------------
document.querySelectorAll(".tab-btn").forEach(btn => {
btn.addEventListener("click", () => {
document.querySelectorAll(".tab-btn").forEach(b => b.classList.remove("active"));
document.querySelectorAll(".tab-panel").forEach(p => p.classList.remove("active"));
btn.classList.add("active");
document.getElementById("tab-" + btn.dataset.tab).classList.add("active");
if (btn.dataset.tab === "create") loadInventoryForSelect();
if (btn.dataset.tab === "mine") loadMyAuctions();
});
});
// -------------------------------------------------------------
// ANGEBOTE DURCHSTÖBERN
// -------------------------------------------------------------
async function loadAuctions() {
const res = await authFetch("/api/auctions");
const data = await res.json();
renderAuctions(data.auctions || []);
}
function renderAuctions(auctions) {
const list = document.getElementById("auctionList");
const empty = document.getElementById("auctionEmpty");
list.innerHTML = "";
if (auctions.length === 0) {
empty.classList.remove("hidden");
return;
}
empty.classList.add("hidden");
auctions.forEach(a => {
const minBid = a.current_bid ? a.current_bid + 1 : a.starting_price;
const div = document.createElement("div");
div.className = "auction-item";
div.innerHTML = `
<div>
<div class="auction-name">${itemLabel(a.item_id)} ${a.amount > 1 ? "x" + a.amount : ""}</div>
<div class="auction-meta">
Verkäufer: ${escapeHtml(a.seller_name)}${timeLeftLabel(a.ends_at)}
${a.bidder_name ? ` — höchstes Gebot von ${escapeHtml(a.bidder_name)}` : ""}
</div>
</div>
<div class="auction-price">${a.current_bid ? a.current_bid + "$" : a.starting_price + "$ (Start)"}</div>
<div class="auction-actions">
<input type="number" class="bidInput" data-id="${a.id}" placeholder="min. ${minBid}$" value="${minBid}">
<button class="bidBtn" data-id="${a.id}">Bieten</button>
${a.buyout_price ? `<button class="secondary buyoutBtn" data-id="${a.id}">Sofort für ${a.buyout_price}$</button>` : ""}
</div>
`;
list.appendChild(div);
});
list.querySelectorAll(".bidBtn").forEach(btn => {
btn.onclick = () => placeBid(btn.dataset.id);
});
list.querySelectorAll(".buyoutBtn").forEach(btn => {
btn.onclick = () => buyout(btn.dataset.id);
});
}
async function placeBid(id) {
const input = document.querySelector(`.bidInput[data-id="${id}"]`);
const amount = Number(input.value);
if (!amount || amount <= 0) return;
const res = await authFetch(`/api/auctions/${id}/bid`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ amount })
});
const data = await res.json();
if (data.ok) {
loadAuctions();
} else {
alert(data.error || "Gebot fehlgeschlagen.");
}
}
async function buyout(id) {
if (!confirm("Dieses Angebot jetzt sofort kaufen?")) return;
const res = await authFetch(`/api/auctions/${id}/buyout`, { method: "POST" });
const data = await res.json();
if (data.ok) {
alert("Gekauft! Das Item ist jetzt in deinem Inventar.");
loadAuctions();
} else {
alert(data.error || "Kauf fehlgeschlagen.");
}
}
// -------------------------------------------------------------
// NEUES ANGEBOT ERSTELLEN
// -------------------------------------------------------------
async function loadInventoryForSelect() {
const res = await authFetch("/api/me/inventory");
const data = await res.json();
const select = document.getElementById("newItemSelect");
const inv = (data.inventory || []).filter(i => i.amount > 0);
if (inv.length === 0) {
select.innerHTML = `<option value="">Kein Item im Inventar</option>`;
return;
}
select.innerHTML = inv.map(i =>
`<option value="${i.id}" data-max="${i.amount}">${itemLabel(i.id)} (${i.amount}x vorhanden)</option>`
).join("");
}
async function createAuction() {
const select = document.getElementById("newItemSelect");
const itemId = select.value;
if (!itemId) {
showMsg("createMsg", "Kein Item ausgewählt.", true);
return;
}
const amount = Number(document.getElementById("newAmount").value) || 1;
const startingPrice = Number(document.getElementById("newStartPrice").value) || 1;
const buyoutVal = document.getElementById("newBuyout").value.trim();
const buyoutPrice = buyoutVal ? Number(buyoutVal) : null;
const hours = Number(document.getElementById("newHours").value) || 24;
const res = await authFetch("/api/auctions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemId, amount, startingPrice, buyoutPrice, hours })
});
const data = await res.json();
if (data.ok) {
showMsg("createMsg", "Angebot eingestellt!");
loadInventoryForSelect();
} else {
showMsg("createMsg", "Fehler: " + (data.error || "unbekannt"), true);
}
}
// -------------------------------------------------------------
// MEINE ANGEBOTE
// -------------------------------------------------------------
async function loadMyAuctions() {
const res = await authFetch("/api/auctions/mine");
const data = await res.json();
renderMyAuctions(data.auctions || []);
}
function renderMyAuctions(auctions) {
const list = document.getElementById("mineList");
const empty = document.getElementById("mineEmpty");
list.innerHTML = "";
if (auctions.length === 0) {
empty.classList.remove("hidden");
return;
}
empty.classList.add("hidden");
const statusLabels = { active: "Aktiv", sold: "Verkauft", expired: "Abgelaufen (zurückgegeben)", cancelled: "Storniert" };
auctions.forEach(a => {
const div = document.createElement("div");
div.className = "auction-item";
div.innerHTML = `
<div>
<div class="auction-name">
${itemLabel(a.item_id)} ${a.amount > 1 ? "x" + a.amount : ""}
<span class="status-badge status-${a.status}">${statusLabels[a.status] || a.status}</span>
</div>
<div class="auction-meta">${timeLeftLabel(a.ends_at)} - Startpreis ${a.starting_price}$${a.current_bid ? `, aktuelles Gebot ${a.current_bid}$` : ""}</div>
</div>
${a.status === "active" && !a.current_bid ? `<button class="danger cancelBtn" data-id="${a.id}">Stornieren</button>` : ""}
`;
list.appendChild(div);
});
list.querySelectorAll(".cancelBtn").forEach(btn => {
btn.onclick = () => cancelAuction(btn.dataset.id);
});
}
async function cancelAuction(id) {
if (!confirm("Dieses Angebot wirklich stornieren? Du bekommst das Item zurück.")) return;
const res = await authFetch(`/api/auctions/${id}/cancel`, { method: "POST" });
const data = await res.json();
if (data.ok) {
loadMyAuctions();
} else {
alert(data.error || "Stornieren fehlgeschlagen.");
}
}
function showMsg(elId, text, isError) {
const el = document.getElementById(elId);
el.style.color = isError ? "#e06c6c" : "#6fbf73";
el.textContent = text;
setTimeout(() => { el.textContent = ""; }, 4000);
}
// -------------------------------------------------------------
// INIT
// -------------------------------------------------------------
async function init() {
await loadItemNames();
await loadAuctions();
setInterval(loadAuctions, 30000);
}
init();
</script>
</body>
</html>