-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
235 lines (206 loc) · 7.21 KB
/
admin.js
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
function loadCategories() {
// Fetch categories from categories.json
fetch("categories.json")
.then(function (response) {
return response.json();
})
.then(function (data) {
const categoryDropdown = document.getElementById('newCategory');
// Clear existing options
categoryDropdown.innerHTML = '';
// Add new options from the fetched data
if (data.categories) {
data.categories.forEach(function (category) {
const option = document.createElement('option');
option.value = category;
option.text = category;
categoryDropdown.add(option);
});
}
});
}
function addCategoryDirect() {
const newCategoryDirect = document.getElementById('newCategoryDirect').value;
if (newCategoryDirect) {
// Make a fetch request to add the new category directly
fetch("addCategory.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `action=addCategory&category=${encodeURIComponent(newCategoryDirect)}`,
})
.then(function (response) {
return response.json();
})
.then(function (data) {
if (data.success) {
// Optionally, update the DOM or provide user feedback
alert('Category added successfully.');
// Update the categories dropdown or perform other actions as needed
loadCategories();
} else {
alert(`Failed to add category. ${data.message}`);
}
})
.catch(function (error) {
console.error("There was a problem with the fetch operation:", error);
alert('Failed to add category. Please try again.');
});
} else {
alert('Please fill in the New Category field.');
}
}
// Function to load existing items from items.json
function loadData() {
fetch("items.json")
.then(function (response) {
return response.json();
})
.then(function (itemsData) {
const dataOutput = document.getElementById("data-output");
// Clear existing items
dataOutput.innerHTML = "";
// Add new items from the fetched data
if (itemsData.items) {
itemsData.items.forEach(function (item) {
const detailsHTML = `<td>${item.name}</td><td>${item.category}</td><td>${generateDetailsHTML(
item.details
)}</td><td><button onclick="deleteItem('${item.id}')" class="action-button">Delete</button></td>`;
const newRow = document.createElement("tr");
newRow.innerHTML = detailsHTML;
newRow.id = item.id;
dataOutput.appendChild(newRow);
});
}
});
}
// Function to generate HTML for item details
function generateDetailsHTML(details) {
let detailsHTML = "";
details.forEach(function (detail) {
detailsHTML += `${detail.detail_name}: ${detail.detail_value}<br>`;
});
return detailsHTML;
}
function deleteItem(itemId) {
fetch("update.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ action: "deleteItem", deletedItemId: itemId }),
})
.then(function (response) {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then(function (data) {
console.log(data); // Log the response for debugging
if (data.success) {
// Remove the deleted item from the DOM
removeItemFromDOM(itemId);
alert('Item deleted successfully.');
} else {
alert(`Failed to delete item. ${data.message}`);
}
})
.catch(function (error) {
console.error("There was a problem with the fetch operation:", error);
alert('Failed to delete item. Please try again.');
});
}
// Helper function to remove the deleted item from the DOM
function removeItemFromDOM(itemId) {
const deletedItem = document.getElementById(itemId);
if (deletedItem) {
deletedItem.remove();
}
}
// Declare a global array to store details
var detailsArray = [];
document.addEventListener("DOMContentLoaded", function () {
main();
loadCategories();
});
function addDetail() {
// Get input values
var detailName = document.getElementById("newDetailName").value;
var detailValue = document.getElementById("newDetailValue").value;
// Create a new detail object
var newDetail = {
detail_name: detailName,
detail_value: detailValue,
};
// Add the new detail to the detailsArray
storeDetails(newDetail);
// Add the new detail to the details-container
var detailsContainer = document.getElementById("details-container");
// Create a new div element to hold the detail information
var detailDiv = document.createElement("div");
detailDiv.innerHTML = `${detailName}: ${detailValue}`;
var removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.onclick = function() {
removeDetail(removeButton);
};
detailDiv.appendChild(removeButton);
// Append the detailDiv to the detailsContainer
detailsContainer.appendChild(detailDiv);
// Clear the input fields
document.getElementById("newDetailName").value = "";
document.getElementById("newDetailValue").value = "";
}
function addItem() {
var name = document.getElementById("newName").value;
var category = document.getElementById("newCategory").value;
// Get details from the global array
var details = detailsArray;
var data = {
action: "addItem",
newName: name,
newCategory: category,
details: details,
};
// Use fetch to send data to the server
fetch("update.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
alert("Item added successfully!");
// Optionally, you can reset the form or perform other actions
// Clear the details array after adding the item
detailsArray = [];
// Clear the input fields
document.getElementById("newName").value = "";
document.getElementById("newCategory").value = "";
document.getElementById("details-container").innerHTML = "";
document.getElementById("newDetailName").value = "";
document.getElementById("newDetailValue").value = "";
// Refresh the data on the page
loadData();
} else {
alert("Failed to add item. Please try again.");
}
})
.catch((error) => {
console.error("Error:", error);
alert("An error occurred. Please try again.");
});
}
// Function to store details in the detailsArray
function storeDetails(newDetail) {
detailsArray.push(newDetail);
}
// Function to remove a dynamically added detail
function removeDetail(button) {
button.parentElement.remove();
}