-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
290 lines (249 loc) · 10.9 KB
/
script.js
File metadata and controls
290 lines (249 loc) · 10.9 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
// Fungsi untuk memuat item menu dari database
async function loadMenuItems() {
const menuItemsContainer = document.querySelector('.menu-items');
menuItemsContainer.innerHTML = 'Loading menu items...';
const urlParams = new URLSearchParams(window.location.search);
const isAdmin = urlParams.get('admin') === 'true';
const adminControls = document.querySelector('.admin-controls');
if (adminControls) {
adminControls.style.display = isAdmin ? 'block' : 'none';
}
const logoutLink = document.getElementById('logout-link');
if (logoutLink) {
logoutLink.style.display = isAdmin ? 'list-item' : 'none';
}
try {
const response = await fetch('get_menu.php');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const menuItems = await response.json();
menuItemsContainer.innerHTML = '';
if (menuItems.length === 0) {
menuItemsContainer.innerHTML = '<p>Tidak ada item menu yang tersedia.</p>';
} else {
menuItems.forEach(item => {
const itemDiv = document.createElement('div');
itemDiv.classList.add('item');
itemDiv.innerHTML = `
<img src="Gambar/${item.gambar || 'placeholder.png'}" alt="${item.nama_item}">
<h3>${item.nama_item}</h3>
<p>${item.deskripsi}</p>
<p><strong>Harga:</strong> Rp ${parseInt(item.harga).toLocaleString('id-ID')}</p>
<div class="quantity-control">
<button onclick="kurangi(this)">-</button>
<input type="number" value="0" min="0" data-nama="${item.nama_item}" data-harga="${item.harga}">
<button onclick="tambah(this)">+</button>
</div>
<div class="admin-buttons" style="display:${isAdmin ? 'block' : 'none'};">
<button class="btn-edit" data-id="${item.id}">Edit</button>
<button class="btn-delete" data-id="${item.id}">Delete</button>
</div>
`;
menuItemsContainer.appendChild(itemDiv);
});
if (isAdmin) {
document.querySelectorAll('.btn-delete').forEach(button => {
button.addEventListener('click', function() {
const id = this.getAttribute('data-id');
deleteMenuItem(id);
});
});
document.querySelectorAll('.btn-edit').forEach(button => {
button.addEventListener('click', function() {
const id = this.getAttribute('data-id');
showEditForm(id);
});
});
}
}
} catch (error) {
console.error('Gagal memuat item menu:', error);
menuItemsContainer.innerHTML = '<p>Maaf, gagal memuat menu. Silakan coba lagi nanti.</p>';
}
}
async function showEditForm(id) {
try {
// Ambil data item berdasarkan ID
const response = await fetch(`get_menu_item.php?id=${id}`);
const item = await response.json();
if (item) {
// Sembunyikan form tambah, tampilkan form edit
document.getElementById('add-form').style.display = 'none';
document.getElementById('edit-form').style.display = 'block';
// Isi form edit dengan data yang diambil
document.getElementById('edit-id-item').value = item.id;
document.getElementById('edit-nama-item').value = item.nama_item;
document.getElementById('edit-deskripsi').value = item.deskripsi;
document.getElementById('edit-harga').value = item.harga;
document.getElementById('edit-file-name-display').textContent = item.gambar || 'No file chosen';
const preview = document.getElementById('current-image-preview');
preview.innerHTML = `<img src="Gambar/${item.gambar}" alt="${item.nama_item}" style="max-width:150px; margin-top: 10px;">`;
}
} catch (error) {
console.error('Gagal mengambil data item untuk diedit:', error);
alert('Gagal memuat data item untuk diedit.');
}
}
document.getElementById('btn-cancel-edit').addEventListener('click', function() {
// Sembunyikan form edit, tampilkan form tambah
document.getElementById('add-form').style.display = 'block';
document.getElementById('edit-form').style.display = 'none';
});
// Event listener untuk tombol "Perbarui Menu"
document.getElementById('btn-update-item').addEventListener('click', async function() {
const id = document.getElementById('edit-id-item').value;
const namaItem = document.getElementById('edit-nama-item').value;
const deskripsi = document.getElementById('edit-deskripsi').value;
const harga = document.getElementById('edit-harga').value;
const gambarInput = document.getElementById('edit-gambar');
if (!namaItem || !harga) {
alert("Nama item dan harga tidak boleh kosong!");
return;
}
const formData = new FormData();
formData.append('id', id);
formData.append('nama_item', namaItem);
formData.append('deskripsi', deskripsi);
formData.append('harga', parseFloat(harga));
if (gambarInput.files.length > 0) {
formData.append('gambar', gambarInput.files[0]);
}
try {
const response = await fetch('update_menu.php', {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.success) {
alert(result.message);
document.getElementById('edit-form').style.display = 'none';
document.getElementById('add-form').style.display = 'block';
loadMenuItems();
} else {
alert("Gagal memperbarui item: " + result.message);
}
} catch (error) {
console.error('Error saat memperbarui item menu:', error);
alert('Terjadi kesalahan saat mencoba memperbarui item menu.');
}
});
async function deleteMenuItem(id) {
if (confirm("Apakah Anda yakin ingin menghapus item ini?")) {
try {
const response = await fetch('delete_menu.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: id })
});
const result = await response.json();
if (result.success) {
alert(result.message);
loadMenuItems();
} else {
alert("Gagal menghapus item: " + result.message);
}
} catch (error) {
console.error('Error saat menghapus item menu:', error);
alert('Terjadi kesalahan saat menghapus item.');
}
}
}
document.addEventListener('DOMContentLoaded', loadMenuItems);
// Fungsi untuk menambah item menu baru
document.getElementById('btn-add-item').addEventListener('click', async function() {
const namaItem = document.getElementById('add-nama-item').value;
const deskripsi = document.getElementById('add-deskripsi').value;
const harga = document.getElementById('add-harga').value;
const gambarInput = document.getElementById('add-gambar');
if (!gambarInput.files.length) {
alert("Mohon pilih file gambar.");
return;
}
if (!namaItem || !harga) {
alert("Nama item dan harga tidak boleh kosong!");
return;
}
const formData = new FormData();
formData.append('nama_item', namaItem);
formData.append('deskripsi', deskripsi);
formData.append('harga', parseFloat(harga));
formData.append('gambar', gambarInput.files[0]);
try {
const response = await fetch('add_menu.php', {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.success) {
alert(result.message);
document.getElementById('add-nama-item').value = '';
document.getElementById('add-deskripsi').value = '';
document.getElementById('add-harga').value = '';
gambarInput.value = '';
document.getElementById('file-name-display').textContent = 'No file chosen';
loadMenuItems();
} else {
alert("Gagal menambah item: " + result.message);
}
} catch (error) {
console.error('Error saat menambah item menu:', error);
alert('Terjadi kesalahan saat mencoba menambah item menu.');
}
});
// Fungsi tambah dan kurangi yang sudah ada dari menu.html asli
function tambah(el){
var input = el.parentNode.querySelector('input');
input.value = parseInt(input.value) + 1;
}
function kurangi(el){
var input = el.parentNode.querySelector('input');
if(parseInt(input.value) > 0){
input.value = parseInt(input.value) - 1;
}
}
document.getElementById('pesan-sekarang').addEventListener('click', function(){
var inputs = document.querySelectorAll('.menu-items input');
var pesan = "Halo Banana King%0ASaya ingin memesan:%0A";
var adaPesanan = false;
var totalHargaKeseluruhan = 0;
inputs.forEach(function(input){
var jumlah = parseInt(input.value);
if(jumlah > 0){
adaPesanan = true;
var nama = input.getAttribute('data-nama');
var harga = parseFloat(input.getAttribute('data-harga'));
var total = jumlah * harga;
totalHargaKeseluruhan += total;
pesan += "- " + nama + " (" + jumlah + " x Rp " + harga.toLocaleString('id-ID') + ") = Rp " + total.toLocaleString('id-ID') + "%0A";
}
});
var note = document.getElementById('note').value;
if(note){
pesan += "%0ANote: " + encodeURIComponent(note);
}
if(adaPesanan){
pesan += "%0ATotal Keseluruhan: Rp " + totalHargaKeseluruhan.toLocaleString('id-ID');
window.open('https://wa.me/6285722087434?text=' + pesan, '_blank');
} else {
alert("Silakan pilih minimal 1 produk untuk dipesan.");
}
});
document.addEventListener('DOMContentLoaded', function() {
const kirimWaButton = document.getElementById('kirim-wa');
if (kirimWaButton) {
kirimWaButton.addEventListener('click', function() {
var nama = document.getElementById('nama').value;
var email = document.getElementById('email').value;
var pesan = document.getElementById('pesan').value;
if(nama && email && pesan){
var text = "Halo Banana King%0ASaya ingin memberikan saran.%0ANama: " + encodeURIComponent(nama) + "%0AEmail: " + encodeURIComponent(email) + "%0APesan: " + encodeURIComponent(pesan);
window.open('https://wa.me/6285722087434?text=' + text, '_blank');
} else {
alert("Mohon isi semua kolom sebelum mengirim.");
}
});
}
});