-
Notifications
You must be signed in to change notification settings - Fork 0
/
managecustomer.html
169 lines (155 loc) · 5.65 KB
/
managecustomer.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
<!DOCTYPE html>
<html>
<head>
<title>Customer 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="cust-func">
<caption>Customer 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="contactInput" placeholder="Enter Customer Contact">
<button type="submit" class="search-cust">Search</button>
</form>
</td>
<td>
<button type="button" class="create-cust" onclick="window.location.href='createcustomer.html'">
<img src="assets/new-customer.png" width="20" height="20">  Create Customer
</td>
</tr>
</table>
<table id="customerTable">
<thead>
<tr class="cust-title">
<th style="width: 10%;">Customer ID</th>
<th style="width: 40%;">Customer Name</th>
<th style="width:15%;">Customer Contact</th>
<th style="width:20%;">Customer Email</th>
<th style="width:15%;">Customer Sales</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/customer';
const alterroute = 'http://localhost:3000/api/customer/contact/';
const searchForm = document.getElementById('searchForm');
const contactInput = document.getElementById('contactInput');
const table = document.getElementById('customerTable');
searchForm.addEventListener('submit', (event) => {
event.preventDefault();
const contact = contactInput.value.trim();
if (contact !== '') {
clearTableRows();
const searchUrl = alterroute + contact;
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() {
console.log(JSON.parse(this.responseText))
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.customer_id;
newRow.insertCell().textContent = row.customer_name;
newRow.insertCell().textContent = row.customer_contact;
newRow.insertCell().textContent = row.customer_email;
newRow.insertCell().textContent = row.customer_sales || '';
});
} else {
const newRow = table.insertRow();
newRow.insertCell().textContent = data.customer_id;
newRow.insertCell().textContent = data.customer_name;
newRow.insertCell().textContent = data.customer_contact;
newRow.insertCell().textContent = data.customer_email;
newRow.insertCell().textContent = data.customer_sales || '';
}
}
makeRequest(apiUrl);
});
</script>
</body>
</html>