-
Notifications
You must be signed in to change notification settings - Fork 0
/
manageitem.html
276 lines (248 loc) · 9.94 KB
/
manageitem.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html>
<head>
<title>Item Information</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<nav class="navbar">
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="userhome.html">Home</a>
<a href="managecustomer.html">Customer Management</a>
<a href="transaction.html">Transaction</a>
<a href="manageitem.html">Stock Management</a>
<a href="updatepassword.html">Update Password</a>
</div>
<span class="open" onclick="openNav()">  ☰  
<div class="logo">
<a href="">
<img src="assets/sale-sign.png" alt="SDS_logo">
<span>  SALES PILOT  </span>
<img src="assets/cashier-machine.png" alt="SDS_logo">
</a>
</div>
</span>
<a class="logout" href="#" onclick="logout()">Log Out  </a>
</nav>
<table class="stock-func">
<caption>Stock Management</caption>
<tr>
<td>
<button onclick="window.location.href='userhome.html'" class="go-back">
<img src="assets/back.png" width="20" height="20">  Main Menu
</button>
</td>
<td>
<form id="searchForm">
<input type="text" id="itemIdInput" placeholder="Enter Item ID">
<button type="submit" class="search-stock">Search</button>
</form>
</td>
<td>
<button type="button" class="create-stock" onclick="window.location.href='createitem.html'">
<img src="assets/add-item.png" width="20" height="20">  Create Item
</td>
</tr>
</table>
<table id="itemTable">
<thead>
<tr class="item-title">
<th>Item ID</th>
<th>Item Name</th>
<th>Item Type</th>
<th>Item Quantity</th>
<th>Add Quantity</th>
<th>Item Price</th>
<th>Update Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// Open Side Navigation Bar
function openNav() {
document.getElementById("mySidenav").style.width = "280px";
}
// Close Side Navigation Bar
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
// Logout and clear data from local storage
function logout() {
localStorage.removeItem("token");
window.location.href = "login.html";
}
const token = localStorage.getItem("token");
if (!token) {
window.location.href = "login.html";
}
window.addEventListener('DOMContentLoaded', (event) => {
const apiUrl = 'http://localhost:3000/api/item/';
const searchForm = document.getElementById('searchForm');
const itemIdInput = document.getElementById('itemIdInput');
const table = document.getElementById('itemTable');
searchForm.addEventListener('submit', (event) => {
event.preventDefault();
const itemId = itemIdInput.value.trim();
if (itemId !== '') {
clearTableRows();
const searchUrl = apiUrl + itemId;
makeRequest(searchUrl);
} else {
makeRequest(apiUrl); // Load all data when the search input is empty
location.reload();
}
});
function makeRequest(url) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
const data = JSON.parse(this.responseText);
updateTable(data);
} else if (this.status === 404){
clearTableRows();
displayErrorMessage();
} else{
clearTableRows();
displayNoResultsMessage();
}
}
};
xhr.open('GET', url, true);
xhr.setRequestHeader('Authorization', `Bearer ${token}`); // Add authorization header
xhr.send();
}
function displayNoResultsMessage() {
alert('No search results found.');
location.reload();
}
function displayErrorMessage() {
alert('An error occurred while fetching data.');
location.reload();
}
function clearTableRows() {
const rows = table.getElementsByTagName('tr');
while (rows.length > 1) {
table.deleteRow(1);
}
}
function updateTable(data) {
if (Array.isArray(data)) {
data.forEach((row) => {
const newRow = table.insertRow();
newRow.insertCell().textContent = row.item_id;
newRow.insertCell().textContent = row.item_name;
newRow.insertCell().textContent = row.item_type;
newRow.insertCell().textContent = row.item_quantity;
const addQuantityCell = newRow.insertCell();
const addQuantityInput = document.createElement('input');
addQuantityInput.type = 'number';
addQuantityInput.min = 1;
addQuantityCell.appendChild(addQuantityInput);
newRow.insertCell().textContent = row.item_price;
const updatePriceCell = newRow.insertCell();
const updatePriceInput = document.createElement('input');
updatePriceInput.type = 'number';
updatePriceInput.min = 0.01;
updatePriceCell.appendChild(updatePriceInput);
const updateButton = document.createElement('button');
updateButton.textContent = 'Update';
updateButton.className = 'update-button'; // Add CSS class to the button
updateButton.addEventListener('click', () => {
const itemId = row.item_id;
const quantityToAdd = parseInt(addQuantityInput.value);
if (quantityToAdd > 0) {
updateItemQuantity(itemId, quantityToAdd);
}
const priceToUpdate = parseFloat(updatePriceInput.value);
if (!isNaN(priceToUpdate)) {
updateItemPrice(itemId, priceToUpdate);
}
});
newRow.insertCell().appendChild(updateButton);
});
} else {
const newRow = table.insertRow();
newRow.insertCell().textContent = data.item_id;
newRow.insertCell().textContent = data.item_name;
newRow.insertCell().textContent = data.item_type;
newRow.insertCell().textContent = data.item_quantity;
const addQuantityCell = newRow.insertCell();
const addQuantityInput = document.createElement('input');
addQuantityInput.type = 'number';
addQuantityInput.min = 1;
addQuantityCell.appendChild(addQuantityInput);
newRow.insertCell().textContent = data.item_price;
const updatePriceCell = newRow.insertCell();
const updatePriceInput = document.createElement('input');
updatePriceInput.type = 'number';
updatePriceInput.min = 0.01;
updatePriceCell.appendChild(updatePriceInput);
const updateButton = document.createElement('button');
updateButton.textContent = 'Update';
updateButton.addEventListener('click', () => {
const itemId = data.item_id;
const quantityToAdd = parseInt(addQuantityInput.value);
if (quantityToAdd > 0) {
updateItemQuantity(itemId, quantityToAdd);
}
const priceToUpdate = parseFloat(updatePriceInput.value);
if (!isNaN(priceToUpdate)) {
updateItemPrice(itemId, priceToUpdate);
}
});
newRow.insertCell().appendChild(updateButton);
}
}
function updateItemQuantity(id, quantityToAdd) {
const apiUrl = `http://localhost:3000/api/item/quantity/${id}`;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
const updatedItem = JSON.parse(this.responseText);
console.log(updatedItem); // Do something with the updated item data
location.reload(); // Refresh the page after the update
} else {
const error = JSON.parse(this.responseText);
console.error(error); // Handle the error response
}
}
};
xhr.open('PUT', apiUrl, true);
xhr.setRequestHeader('Authorization', `Bearer ${token}`); // Add authorization header
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ item_quantity: quantityToAdd }));
}
function updateItemPrice(id, priceToUpdate) {
const apiUrl = `http://localhost:3000/api/item/price/${id}`;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
const updatedItem = JSON.parse(this.responseText);
console.log(updatedItem); // Do something with the updated item data
alert("Item updated successfully!")
location.reload(); // Refresh the page after the update
} else {
const error = JSON.parse(this.responseText);
console.error(error); // Handle the error response
alert("Unable to update, please try again!");
location.reload();
}
}
};
xhr.open('PUT', apiUrl, true);
xhr.setRequestHeader('Authorization', `Bearer ${token}`); // Add authorization header
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ item_price: priceToUpdate }));
}
makeRequest(apiUrl);
});
</script>
</body>
</html>