-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
311 lines (281 loc) · 9.77 KB
/
Copy pathscript.js
File metadata and controls
311 lines (281 loc) · 9.77 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
// Globalne varijable
let map;
let markers = [];
let hoteli = [];
// Učitaj hotele pri učitavanju stranice
$(document).ready(function() {
ucitajHotele();
});
// Funkcija za učitavanje hotela
function ucitajHotele() {
$.ajax({
url: 'api.php',
type: 'POST',
data: { action: 'dohvati_hotele' },
dataType: 'json',
success: function(response) {
if (response.success) {
hoteli = response.data;
prikaziHotele(hoteli);
} else {
alert('Greška pri učitavanju hotela: ' + response.message);
}
},
error: function(xhr, status, error) {
console.error('AJAX greška:', error);
alert('Došlo je do greške pri učitavanju podataka');
}
});
}
// Funkcija za prikaz hotela u tablici
function prikaziHotele(data) {
let html = '';
data.forEach(function(hotel) {
html += `
<tr>
<td>${hotel.id}</td>
<td><strong>${hotel.naziv}</strong></td>
<td>${hotel.adresa}</td>
<td>${hotel.grad}</td>
<td>${hotel.zupanija}</td>
<td class="text-center">${hotel.kapacitet}</td>
<td class="text-center">${hotel.broj_soba}</td>
<td class="text-center">${hotel.broj_gostiju}</td>
<td class="text-center">
<span class="badge bg-${hotel.slobodno > 0 ? 'success' : 'danger'}">
${hotel.slobodno}
</span>
</td>
<td>
<div class="btn-group" role="group">
<button class="btn btn-sm btn-info text-white" onclick="otvoriBoravakModal(${hotel.id}, '${hotel.naziv}', ${hotel.broj_gostiju}, ${hotel.kapacitet})" title="Ažuriraj boravak">
<i class="bi bi-people"></i>
</button>
<button class="btn btn-sm btn-warning" onclick="otvoriUrediModal(${hotel.id})" title="Uredi">
<i class="bi bi-pencil"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="obrisiHotel(${hotel.id}, '${hotel.naziv}')" title="Obriši">
<i class="bi bi-trash"></i>
</button>
</div>
</td>
</tr>
`;
});
$('#hotelTableBody').html(html);
}
// Funkcija za dodavanje hotela
function dodajHotel() {
const form = $('#dodajHotelForm')[0];
if (!form.checkValidity()) {
form.reportValidity();
return;
}
const formData = new FormData(form);
formData.append('action', 'dodaj_hotel');
$.ajax({
url: 'api.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function(response) {
if (response.success) {
alert(response.message);
$('#dodajHotelModal').modal('hide');
form.reset();
ucitajHotele();
} else {
alert('Greška: ' + response.message);
}
},
error: function(xhr, status, error) {
console.error('AJAX greška:', error);
alert('Došlo je do greške pri dodavanju hotela');
}
});
}
// Funkcija za otvaranje modala za uređivanje
function otvoriUrediModal(id) {
$.ajax({
url: 'api.php',
type: 'GET',
data: { action: 'dohvati_hotel', id: id },
dataType: 'json',
success: function(response) {
if (response.success) {
const hotel = response.data;
$('#edit_id').val(hotel.id);
$('#edit_naziv').val(hotel.naziv);
$('#edit_adresa').val(hotel.adresa);
$('#edit_grad').val(hotel.grad);
$('#edit_zupanija').val(hotel.zupanija);
$('#edit_kapacitet').val(hotel.kapacitet);
$('#edit_broj_soba').val(hotel.broj_soba);
$('#edit_broj_gostiju').val(hotel.broj_gostiju);
$('#edit_latitude').val(hotel.latitude);
$('#edit_longitude').val(hotel.longitude);
$('#urediHotelModal').modal('show');
} else {
alert('Greška: ' + response.message);
}
},
error: function(xhr, status, error) {
console.error('AJAX greška:', error);
alert('Došlo je do greške pri učitavanju podataka hotela');
}
});
}
// Funkcija za uređivanje hotela
function urediHotel() {
const form = $('#urediHotelForm')[0];
if (!form.checkValidity()) {
form.reportValidity();
return;
}
const formData = new FormData(form);
formData.append('action', 'uredi_hotel');
$.ajax({
url: 'api.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function(response) {
if (response.success) {
alert(response.message);
$('#urediHotelModal').modal('hide');
ucitajHotele();
} else {
alert('Greška: ' + response.message);
}
},
error: function(xhr, status, error) {
console.error('AJAX greška:', error);
alert('Došlo je do greške pri ažuriranju hotela');
}
});
}
// Funkcija za brisanje hotela
function obrisiHotel(id, naziv) {
if (confirm(`Jeste li sigurni da želite obrisati hotel "${naziv}"?`)) {
$.ajax({
url: 'api.php',
type: 'POST',
data: { action: 'obrisi_hotel', id: id },
dataType: 'json',
success: function(response) {
if (response.success) {
alert(response.message);
ucitajHotele();
} else {
alert('Greška: ' + response.message);
}
},
error: function(xhr, status, error) {
console.error('AJAX greška:', error);
alert('Došlo je do greške pri brisanju hotela');
}
});
}
}
// Funkcija za otvaranje modala za ažuriranje boravka
function otvoriBoravakModal(id, naziv, trenutniBroj, kapacitet) {
$('#boravak_id').val(id);
$('#boravak_hotel').val(naziv);
$('#boravak_broj_gostiju').val(trenutniBroj);
$('#boravak_kapacitet').text(kapacitet);
$('#boravakModal').modal('show');
}
// Funkcija za ažuriranje boravka
function azurirajBoravak() {
const form = $('#boravakForm')[0];
if (!form.checkValidity()) {
form.reportValidity();
return;
}
const formData = new FormData(form);
formData.append('action', 'azuriraj_boravak');
$.ajax({
url: 'api.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function(response) {
if (response.success) {
alert(response.message);
$('#boravakModal').modal('hide');
ucitajHotele();
} else {
alert('Greška: ' + response.message);
}
},
error: function(xhr, status, error) {
console.error('AJAX greška:', error);
alert('Došlo je do greške pri ažuriranju boravka');
}
});
}
// Funkcija za preklapanje između tablice i mape
function toggleMapView() {
const tableView = document.getElementById('tableView');
const mapView = document.getElementById('mapView');
if (tableView.style.display === 'none') {
tableView.style.display = 'block';
mapView.style.display = 'none';
} else {
tableView.style.display = 'none';
mapView.style.display = 'block';
if (!map) {
initMap();
}
}
}
// Inicijalizacija Google Mape
function initMap() {
// Centar Hrvatske
const hrvatska = { lat: 45.1, lng: 15.2 };
map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: hrvatska,
mapTypeId: 'roadmap'
});
// Dodaj markere za sve hotele
hoteli.forEach(function(hotel) {
if (hotel.latitude && hotel.longitude) {
const position = {
lat: parseFloat(hotel.latitude),
lng: parseFloat(hotel.longitude)
};
const marker = new google.maps.Marker({
position: position,
map: map,
title: hotel.naziv,
icon: {
url: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png'
}
});
const infoWindow = new google.maps.InfoWindow({
content: `
<div style="padding: 10px;">
<h6><strong>${hotel.naziv}</strong></h6>
<p style="margin: 5px 0;">
<i class="bi bi-geo-alt"></i> ${hotel.adresa}, ${hotel.grad}<br>
<i class="bi bi-pin-map"></i> ${hotel.zupanija}<br>
<i class="bi bi-people"></i> Gostiju: ${hotel.broj_gostiju}/${hotel.kapacitet}<br>
<i class="bi bi-door-open"></i> Slobodno: ${hotel.slobodno}
</p>
</div>
`
});
marker.addListener('click', function() {
infoWindow.open(map, marker);
});
markers.push(marker);
}
});
}