File size: 4,601 Bytes
14d2101 |
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 |
document.addEventListener('DOMContentLoaded', () => {
const tbody = document.getElementById('inventoryBody');
const modal = document.getElementById('inventoryModal');
const form = document.getElementById('actionForm');
// Buttons
const editBtn = document.getElementById('editBtn');
const saveBtn = document.getElementById('saveBtn');
const cancelBtn = document.getElementById('cancelBtn');
const deleteBtn = document.getElementById('deleteBtn');
const sellBtn = document.getElementById('sellBtn');
const useBtn = document.getElementById('useBtn');
let currentId = null;
async function loadInventory() {
const res = await fetch('/api/inventory');
const items = await res.json();
tbody.innerHTML = items.map(item => `
<tr data-id="${item.id}">
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.type}</td>
<td>${item.color}</td>
<td>${item.quantity}</td>
<td>${item.price}</td>
<td>${item.note || ''}</td>
<td><button class="action-btn">Action</button></td>
</tr>
`).join('');
document.querySelectorAll('.action-btn').forEach(btn => {
btn.onclick = openModal;
});
}
function openModal(e) {
const row = e.target.closest('tr');
currentId = row.dataset.id;
// Fill form values
form.id.value = currentId;
form.name.value = row.children[1].textContent;
form.type.value = row.children[2].textContent;
form.color.value = row.children[3].textContent;
form.quantity.value = row.children[4].textContent;
form.price.value = row.children[5].textContent;
form.note.value = row.children[6].textContent;
setFormDisabled(true);
saveBtn.style.display = 'none';
modal.style.display = 'block';
}
function setFormDisabled(disabled) {
form.name.disabled = disabled;
form.type.disabled = disabled;
form.color.disabled = disabled;
form.quantity.disabled = disabled;
form.price.disabled = disabled;
form.note.disabled = disabled;
}
// Cancel closes modal
cancelBtn.onclick = () => {
modal.style.display = 'none';
setFormDisabled(true);
saveBtn.style.display = 'none';
};
// Edit button - enables fields
editBtn.onclick = () => {
setFormDisabled(false);
saveBtn.style.display = 'inline-block';
};
// Save edited item
form.onsubmit = async (e) => {
e.preventDefault();
const updated = {
name: form.name.value,
type: form.type.value,
color: form.color.value,
quantity: parseInt(form.quantity.value),
price: parseFloat(form.price.value),
note: form.note.value
};
const res = await fetch(`/api/inventory/${currentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updated)
});
if (res.ok) {
modal.style.display = 'none';
await loadInventory();
} else {
alert('Failed to update.');
}
};
// Delete
deleteBtn.onclick = async () => {
if (!confirm('Delete this item?')) return;
const res = await fetch(`/api/inventory/${currentId}`, { method: 'DELETE' });
if (res.ok) {
modal.style.display = 'none';
await loadInventory();
} else {
alert('Delete failed.');
}
};
// Sell
sellBtn.onclick = async () => {
const qty = prompt("Quantity to sell:", 1);
const quantity = parseInt(qty, 10);
if (!qty || isNaN(quantity) || quantity <= 0) return alert('Invalid quantity');
const res = await fetch(`/api/inventory/${currentId}/sell`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ quantity, payerType: "non-member" })
});
if (res.ok) {
modal.style.display = 'none';
await loadInventory();
} else {
alert('Sell failed.');
}
};
// Use
useBtn.onclick = async () => {
const qty = prompt("Quantity to use:", 1);
const quantity = parseInt(qty, 10);
if (!qty || isNaN(quantity) || quantity <= 0) return alert('Invalid quantity');
const res = await fetch(`/api/inventory/${currentId}/use`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ quantity })
});
if (res.ok) {
modal.style.display = 'none';
await loadInventory();
} else {
alert('Use failed.');
}
};
// Initial load
loadInventory();
});
|