-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard_suppliers.php
More file actions
553 lines (467 loc) · 19 KB
/
Copy pathdashboard_suppliers.php
File metadata and controls
553 lines (467 loc) · 19 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
include_once "backend/session.php";
header('X-Frame-Options: SAMEORIGIN');
if (!isset($_SESSION['userid']) || $_SESSION['usertype'] != 3) {
echo "<script>location.href = 'login_main.php';</script>";
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>
<body>
<style>
#actions {
display: flex;
justify-content: flex-end;
margin-top: 20px;
right: 10%; /* 100% - 70% = 30% */
position: absolute;
}
#btnAddSupplier {
margin-left: 10px;
font-size: 20px;
padding: 10px;
}
#btnAddSupplier {
background-color: #4CAF50; /* Green */
border: none;
color: white;
text-align: center;
text-decoration: none;
font-size: 16px;
transition-duration: 0.4s;
cursor: pointer;
}
#btnAddSupplier:hover {
background-color: white;
color: black;
border: 2px solid #4CAF50;
}
table#supplierlist input[type='number'] {
text-align: center;
}
.sortable {
cursor: pointer;
background-image: url('assets/images/no_sort.png');
background-repeat: no-repeat;
background-position: calc(100% - 30px) center;
background-size: 30px 30px;
}
.sortable.asc {
background-image: url('assets/images/asc.png');
background-repeat: no-repeat;
}
.sortable.desc {
background-image: url('assets/images/desc.png');
background-repeat: no-repeat;
}
table#supplierlist td:nth-child(2) {
width: 70%; /* Adjust this value to give more width to the second column */
}
table#supplierlist td:nth-child(3),
table#supplierlist td:nth-child(4) {
width: fit-content;
margin: 5px;
}
table#supplierlist th.sortable:hover {
color: #007BFF; /* Change the text color to blue when hovering */
background-color: #ddd; /* Change the background color to light gray when hovering */
}
.edit-button {
border-radius: 12px; /* This will make the button have round corners */
padding: 10px 20px; /* This will make the button bigger */
display: flex; /* This will align the icon and the text side by side */
align-items: center; /* This will vertically center the icon and the text */
justify-content: center; /* This will horizontally center the icon and the text */
margin: auto; /* This will center the button in the cell */
}
.edit-button:hover {
background-color: #ddd; /* This will change the background color to light gray when hovering */
cursor: pointer; /* This will change the cursor to a hand pointer when hovering */
}
.edit-icon {
height: 20px; /* Adjust this value to change the size of the icon */
width: 20px; /* Adjust this value to change the size of the icon */
margin-right: 5px; /* This will add some space between the icon and the text */
}
.delete-icon {
height: 35px; /* Adjust this value to change the size of the icon */
width: 35px; /* Adjust this value to change the size of the icon */
margin-right: 5px; /* This will add some space between the icon and the text */
transition: background-color 0.3s ease; /* This will animate the background color change */
cursor: pointer; /* This will change the cursor to a hand pointer when hovering */
border: 2px solid transparent; /* Add a transparent border */
padding: 5px; /* This will add space between the border and the image */
}
.delete-icon:hover {
background-color: #ddd; /* This will change the background color to light gray when hovering */
border-color: #000; /* This will change the border color to black when hovering */
}
#dialog-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 1000;
}
#dialog-box {
position: fixed;
top: 45%;
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
width: 80%;
max-width: 800px;
z-index: 1001;
opacity: 0; /* Start with the dialog invisible */
transition: opacity 0.5s; /* Animate changes to the opacity over 0.5 seconds */
border-radius: 30px;
}
#dialog-box label {
display: block;
margin-top: 10px;
}
#dialog-box input {
width: 100%;
padding: 10px;
margin-top: 5px;
}
#dialog-box button {
margin-top: 10px;
padding: 10px 20px;
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
#food-type option:hover, #supplier-type option:hover {
background-color: #ddd;
}
input[readonly] {
background-color: #ddd;
}
input[type="text"], input[type="number"] {
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
width: 100%;
}
table#supplierlist {
margin-left: auto;
margin-right: auto;
margin-top: 10px;
width: 80%;
border: 1px solid black;
}
table#supplierlist th {
background-color: gray;
color: white;
font-weight: bold;
height: 50px;
font-size: 20px;
text-align: center;
}
table#supplierlist td {
text-align: center;
color: black;
font-weight: bold;
background-color: lightyellow;
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
margin: 10px;
width: 20%;
height: 50px;
font-size: 20px;
}
#supplier-description {
width: 100%; /* Make the textarea fit the width of the dialog */
height: 300px; /* Increase the height of the textarea */
resize: none; /* Disable user resizing */
border-radius: 5px; /* Add rounded corners for a modern look */
border: 1px solid #ccc; /* Add a border */
padding: 10px; /* Add some padding */
box-sizing: border-box; /* Make sure the padding and border are included in the element's total width and height */
font-size: 16px; /* Increase the font size */
font-weight: normal !important;
}
table#supplierlist td:nth-child(2) {
font-weight: normal !important;
text-align: justify;
padding-left: 10px; /* adjust as needed */
}
#dialog-confirm p {
color: red;
}
</style>
<div id='table-container'>
<table id='supplierlist'>
<tr>
<th class='sortable' onclick='sortTable(0)'>Supplier Name</th>
<th>Supplier Detail</th>
<th>Action</th>
<th>🗑️</th>
</tr>
</table>
<div id='actions'>
<button id='btnAddSupplier' onclick='newSupplier()'>Add new supplier</button>
</div>
</div>
<script>
$(document).ready(updateTable());
const numbericColumns = [];
const tableName = "supplierlist";
var defaultDir = "asc";
var lastSortedColumn = 0;
var supplierid = null;
function sortTable(n, dir = null) {
var table, rows, i, x, y, shouldSwitch;
table = document.getElementById(tableName);
var switching = true;
lastSortedColumn = n;
// if dir is not null, set the default direction to dir
if (dir != null) {
if (dir == "refresh") {
defaultDir = defaultDir === "asc" ? "asc" : "desc";
} else {
defaultDir = dir === "asc" ? "asc" : "desc";
}
} else {
var ths = table.getElementsByTagName("th");
for (var i = 0; i < ths.length; i++) {
// head has class asc or desc
if (ths[i].classList.contains("asc") || ths[i].classList.contains("desc")) {
// defualt direction is the opposite of the current direction
defaultDir = ths[i].classList.contains("asc") ? "desc" : "asc";
}
}
}
while (switching) {
switching = false;
rows = table.rows;
// Loop to go through all rows
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
// Compare the two adjacent rows
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
// Check if the two rows should switch place
if (numbericColumns.includes(n)) {
if (defaultDir == "asc") {
if (parseFloat(x.innerHTML) > parseFloat(y.innerHTML)) {
shouldSwitch = true;
break;
}
} else {
if (parseFloat(x.innerHTML) < parseFloat(y.innerHTML)) {
shouldSwitch = true;
break;
}
}
} else {
if (defaultDir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
} else {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
}
}
if (shouldSwitch) {
// If a switch is needed, perform the switch and mark that a switch has been done
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
var headers = table.getElementsByTagName("TH");
for (i = 0; i < headers.length; i++) {
headers[i].classList.remove("asc");
headers[i].classList.remove("desc");
}
headers[n].classList.add(defaultDir);
}
sortTable(lastSortedColumn);
function updateTable() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/getSupplierList.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var data = JSON.parse(xhr.responseText);
var table = document.getElementById('supplierlist');
var rows = table.getElementsByTagName('tr');
var i = 1;
while (i < rows.length) {
table.deleteRow(i);
}
for (var i = 0; i < data.length; i++) {
var row = table.insertRow(i + 1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = data[i].SupplierName;
cell2.innerHTML = data[i].SupplierDescription;
cell3.innerHTML = "<button class='edit-button' onclick='editSupplier(\"" + data[i].SupplierId + "\")'><img src='assets/images/edit.png' class='edit-icon'>Edit</button>";
cell4.innerHTML = "<img src='assets/images/unlink.png' class='delete-icon' onclick='deleteSupplier(\"" + data[i].SupplierId + "\")'>";
}
sortTable(lastSortedColumn, "refresh");
}
else if (xhr.readyState == 4 && xhr.status != 200) {
alert('Failed to update the table. \nReason: ' + xhr.responseText);
}
}
}
var isEventListenerAdded = false;
function showDialog($supplierid = null, $suppliername = null, $supplierdescription = null) {
// Show the dialog box
fadeIn();
function fadeIn() {
var dialogOverlay = document.getElementById('dialog-overlay');
var dialogBox = document.getElementById('dialog-box');
dialogOverlay.style.display = 'block';
dialogBox.style.animation = 'fadeIn 0.5s';
dialogBox.style.opacity = 1;
}
function fadeOut() {
var dialogBox = document.getElementById('dialog-box');
dialogBox.style.opacity = 0;
setTimeout(function() {
document.getElementById('dialog-overlay').style.display = 'none';
}, 500);
}
var action = $suppliername == null ? 'add' : 'edit';
supplierid = $supplierid;
if (!isEventListenerAdded) {
document.getElementById('dialog-overlay').addEventListener('click', function(event) {
if (event.target.id === 'dialog-overlay') {
fadeOut();
}
});
document.getElementById('close-dialog').addEventListener('click', fadeOut);
document.getElementById('supplier-form').addEventListener('submit', function(event) {
event.preventDefault();
sendForm();
});
isEventListenerAdded = true;
}
document.getElementById('supplier-name').value = $suppliername;
document.getElementById('supplier-description').value = $supplierdescription;
if (action === 'edit') {
document.getElementById('send-supplier').innerText = 'Edit Supplier';
document.getElementById('dialog-header-label').innerText = 'Edit Supplier';
} else {
document.getElementById('send-supplier').innerText = 'Add Supplier';
document.getElementById('dialog-header-label').innerText = 'Add New Supplier';
}
}
function newSupplier() {
showDialog();
}
function editSupplier(supplierid) {
// get food detail
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/getSupplierDetail.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('supplierid=' + supplierid);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var supplier = JSON.parse(xhr.responseText);
console.log(supplier);
showDialog(supplier.SupplierId, supplier.SupplierName, supplier.SupplierDescription);
console.log(supplier.SupplierId, supplier.SupplierName, supplier.SupplierDescription);
}
}
}
function sendForm() {
var action = document.getElementById('send-supplier').innerText === 'Add Supplier' ? 'add' : 'edit';
var suppliername = document.getElementById('supplier-name').value;
var supplierdescription = document.getElementById('supplier-description').value;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/modifySupplier.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// console.log('supplierid=' + supplierid + '&suppliername=' + suppliername + '&supplierdescription=' + supplierdescription + '&action=' + action);
xhr.send('supplierid=' + supplierid + '&suppliername=' + suppliername + '&supplierdescription=' + supplierdescription + '&action=' + action);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
updateTable();
document.getElementById('dialog-overlay').style.display = 'none';
} else if (xhr.readyState == 4 && xhr.status != 200) {
alert('Failed to ' + action + ' the supplier. \nReason: ' + xhr.responseText);
}
}
}
function deleteSupplier(supplierid) {
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
position: { my: "center", at: "center", of: window, collision: "fit", using: function(pos) {
var topOffset = $(window).height()/4;
$(this).css({
top: pos.top - topOffset,
left: pos.left
});
}},
buttons: {
"Delete": function() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/modifySupplier.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('supplierid=' + supplierid + '&action=delete');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
updateTable();
} else if (xhr.readyState == 4 && xhr.status != 200) {
alert('Failed to delete the supplier. \nReason: ' + xhr.responseText);
}
}
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
}
</script>
<div id="dialog-confirm" title="Delete supplier?" style="display: none;">
<p>Are you sure you want to delete this supplier?</p>
<p>It will also delete all the foods associated with this supplier.</p>
<p>This action cannot be undone.</p>
</div>
<div id="dialog-overlay">
<div id="dialog-box">
<h2 id="dialog-header-label">Add New Food</h2>
<form id="supplier-form">
<label for="supplier-name">Food Name:</label>
<input type="text" id="supplier-name" name="supplier-name" required>
<label for="supplier-description">Supplier Detail:</label>
<textarea id="supplier-description" name="supplier-description" required></textarea>
<br/><br/>
<div id="dialog-buttons">
<button type="submit" id="send-supplier">Add Supplier</button>
<button type="button" id="close-dialog">Close</button>
</div>
</form>
</div>
</div>
</body>
</html>