-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
226 lines (190 loc) · 6.55 KB
/
main.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
let title = document.getElementById('title');
let price = document.getElementById('price');
let taxes = document.getElementById('taxes');
let ads = document.getElementById('ADS');
let discount = document.getElementById('discount');
let total = document.getElementById('total');
let quantity = document.getElementById('quantity');
let category = document.getElementById('category');
let create = document.getElementById('create');
let mood = 'create'; // this show when "UpDate && Create"
let temp;
// console.log(title,price,taxes,ads,discount,total,quantity,category,create);
// ! get total
function getTotal() {
if (price.value != '') {
let result = (+price.value + +taxes.value + +ads.value) - +discount.value
total.innerHTML = result;
total.style.background = 'green';
} else {
total.innerHTML = '';
total.style.background = 'red';
}
}
// todo : create product
let items;
if (localStorage.Product != null) { // contain product
items = JSON.parse(localStorage.Product);
} else {
items = [];
}
create.onclick = function () {
let newProduct = {
title: title.value,
price: price.value,
taxes: taxes.value,
ads: ads.value,
discount: discount.value,
total: total.innerHTML,
quantity: quantity.value,
category: category.value,
}
if (title.value != ''
&& price.value != ''
&& category.value != ''
&& newProduct.quantity < 100) {
if (mood === 'create') {
//todo : count
if (newProduct.quantity > 1) {
for (let i = 0; i < newProduct.quantity; i++) {
items.push(newProduct);
}
} else {
items.push(newProduct);
}
} else { // mood == 'update
items[temp] = newProduct;
mood = 'create'
create.innerHTML = 'Create'
quantity.style.display = 'inline'
}
//! to clear inputs
clearData();
}
//* to save data at localStorage
localStorage.setItem('Product', JSON.stringify(items));
//* Read Data
showData();
}
//? clear inputs
function clearData() {
title.value = '';
price.value = '';
taxes.value = '';
ads.value = '';
total.innerHTML = '';
discount.value = '';
quantity.value = '';
category.value = '';
}
//! Read
function showData() {
getTotal();
let table = '';
for (let i = 0; i < items.length; i++) {
table += `
<tr>
<td class="id">${i+1}</td>
<td class="title">${items[i].title}</td>
<td class="price">${items[i].price}</td>
<td class="taxes">${items[i].taxes}</td>
<td class="ads">${items[i].ads}</td>
<td class="total">${items[i].total}</td>
<td class="category">${items[i].category}</td>
<td class="update"><button onclick="updateData(${i})" id="update">Update</button></td>
<td class="delete"><button onclick="deleteData(${i})" id="delete">Delete</button></td>
</tr>
`
}
document.getElementById('tbody').innerHTML = table;
}
showData();
// todo: Delete
function deleteData(idItem) {
items.splice(idItem, 1);
localStorage.setItem('Product', JSON.stringify(items));
showData();
}
// todo: Delete All
let btnDeleteAll = document.getElementById('deleteAll');
btnDeleteAll.onclick = function () {
localStorage.clear();
items.splice(0, items.length);
showData();
}
//* UpDate
function updateData(idItem) {
title.value = items[idItem].title;
price.value = items[idItem].price;
taxes.value = items[idItem].taxes;
ads.value = items[idItem].ads;
discount.value = items[idItem].discount;
getTotal();
quantity.style.display = 'none';
category.value = items[idItem].category;
create.innerHTML = 'UpDate'
mood = 'update'
temp = idItem;
scroll({
top: 0,
behavior: 'smooth'
})
}
//! Search
let mood2 = 'title'; // this show when "title && category"
function searchMood(id) {
let search = document.getElementById('search');
if (id == 'STitle') {
mood2 = 'title';
search.placeholder = 'Search By Title';
} else {
mood2 = 'category';
search.placeholder = 'Search By Category';
}
search.focus();
search.value = '';
showData();
}
function searchData(value) {
let table = '';
if (mood2 == 'title') {
for (let i = 0; i < items.length; i++) {
if (items[i].title.toLowerCase().includes(value.toLowerCase())) {
table += `
<tr>
<td class="id">${i}</td>
<td class="title">${items[i].title}</td>
<td class="price">${items[i].price}</td>
<td class="taxes">${items[i].taxes}</td>
<td class="ads">${items[i].ads}</td>
<td class="total">${items[i].total}</td>
<td class="category">${items[i].category}</td>
<td class="update"><button onclick="updateData(${i})" id="update">Update</button></td>
<td class="delete"><button onclick="deleteData(${i})" id="delete">Delete</button></td>
</tr>
`
}
}
}
else {
for (let i = 0; i < items.length; i++) {
if (items[i].category.toLowerCase().includes(value.toLowerCase())) {
table += `
<tr>
<td class="id">${i}</td>
<td class="title">${items[i].title}</td>
<td class="price">${items[i].price}</td>
<td class="taxes">${items[i].taxes}</td>
<td class="ads">${items[i].ads}</td>
<td class="total">${items[i].total}</td>
<td class="category">${items[i].category}</td>
<td class="update"><button onclick="updateData(${i})" id="update">Update</button></td>
<td class="delete"><button onclick="deleteData(${i})" id="delete">Delete</button></td>
</tr>
`
}
}
}
document.getElementById('tbody').innerHTML = table;
}
//! Clean Data