-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookingManagement.php
516 lines (441 loc) · 19.7 KB
/
bookingManagement.php
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
session_start();
if (!isset($_SESSION['id']) || !isset($_SESSION['role'])) {
header('Location: login.php');
exit;
}
include_once('includes/header.php');
?>
<style>
#time-intervals {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
</style>
<div id="page-wrapper">
<div class="row">
<div class="col-12">
<h1 class="page-header text-center">Booking Management</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<style>
.myElement{margin-top: -80px; }
@media (max-width: 767px) {
.myElement {margin-top: 80px; }
}
/* Media query for large screens (lg) */
@media (min-width: 1200px) {
.myElement {margin-top: -120px; }
}
</style>
<!-- /.row -->
<div class="row">
<div class="card">
<div class="card-body">
<div class="calendar-body py-5">
<div class="calendar">
<div class="header">
<button id="prevBtn"><</button>
<h2 id="monthYear"></h2>
<button id="nextBtn">></button>
</div>
<div class="days"></div>
</div>
</div>
<div class="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h2 class="page-header text-center">Select your time slot</h2>
</div>
</div>
<div class="row">
<div class="col-lg-12 text-center">
<div id="time-intervals" class="d-flex justify-content-center align-items-center mx-auto">
<!-- here is the buttons -->
</div>
</div>
</div>
</div>
<!-- Modal for the booking form -->
<div class="modal fade" role="dialog" id="bookingModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Booking Form</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="selectedDate">Selected Date:</label>
<input type="text" class="form-control" id="selectedDate" readonly>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="selectedDate">Enter Name:</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" name="name" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="selectedDate">Enter Contact:</label>
<input type="text" class="form-control" id="contact" placeholder="Enter contact" name="contact" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="startTime">Start Time:</label>
<input type="text" class="form-control" id="startTime" readonly>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="endTime">End Time:</label>
<select class="form-control" id="endTime" required>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="advance">Enter Advance:</label>
<input type="text" class="form-control" id="advance" placeholder="Enter advance" name="advance" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="total">Enter Total:</label>
<input type="text" class="form-control" id="total" placeholder="Enter total" name="total" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="total">Payment Method:</label>
<select class="form-control" id="method" required>
<option value="bkash"> BKash </option>
<option value="nagad"> Nagad </option>
<option value="cash"> Cash </option>
</select>
</div>
</div>
</div>
<button type="button" class="btn btn-success" onclick="submitBookingForm()">Book</button>
</form>
</div>
</div>
</div>
</div>
<!-- modal for confirmation -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Booking Confirmed</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal Body -->
<div class="modal-body" id="modalContent">
<!-- Content will be dynamically added here -->
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<a href="bookingManagement.php" class="btn btn-secondary" >Close</a>
</div>
</div>
</div>
</div>
<script>
// Use JavaScript to open the modal, pass a value, and refresh the page when the modal is closed
document.getElementById('openModalButton').addEventListener('click', function () {
// Get the value you want to pass
var valueToPass = 'This is the value I want to pass';
// Update the modal content with the value
document.getElementById('modalContent').innerText = valueToPass;
// Show the modal
$('#myModal').modal('show');
});
// Refresh the page when the modal is fully hidden
$('#myModal').on('hidden.bs.modal', function () {
location.reload();
});
</script>
<?php
include 'connection.php';
$sql = "SELECT * FROM slot_management WHERE status=1";
$result = mysqli_query($conn, $sql);
echo "<script> var periods = [];";
$row = mysqli_fetch_array($result);
echo "var admin = '".$_SESSION['id']."';";
echo "periods.push('".$row['start_time']."');"."periods.push('".$row['end_time']."');";
while($r = mysqli_fetch_array($result)){
echo "periods.push('".$r['end_time']."');";
}
// echo "console.log(periods);</script>";
echo "</script>";
?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const daysContainer = document.querySelector('.days');
const monthYearElement = document.getElementById('monthYear');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
let flag = true;
let currentDate = new Date();
let currentMonth = currentDate.getMonth();
let currentYear = currentDate.getFullYear();
if (flag) {
renderCalendar(currentMonth, currentYear);
flag = false;
}
function renderCalendar(month, year) {
daysContainer.innerHTML = '';
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const totalDays = lastDay.getDate();
monthYearElement.textContent = `${getMonthName(month)} ${year}`;
for (let i = 1; i <= totalDays; i++) {
const day = document.createElement('div');
day.classList.add('day');
day.textContent = i;
day.addEventListener('click', () => selectDate(i));
daysContainer.appendChild(day);
day.classList.remove('today');
const currentDate = new Date();
if (i == currentDate.getDate() && month == currentDate.getMonth() && year == currentDate.getFullYear()) {
selectDate(i);
day.classList.add('today');
var temp = document.getElementById('selectedDate');
temp.value = currentDate.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
}
const todayIndex = new Date().getDate() - 1;
}
function getMonthName(month) {
const months = [
'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
];
return months[month];
}
function selectDate(day) {
const selectedDay = new Date(currentYear, currentMonth, day);
var inputDate = new Date(selectedDay);
var month = String(inputDate.getMonth() + 1).padStart(2, '0');
var mysqlDate = inputDate.getFullYear() + '-' + month + '-' + inputDate.getDate();
var live_slots = [];
var slotsArray = [];
$.ajax({
url: 'api/get_slot_status.php',
type: 'POST',
dataType: 'json',
data: {
date: mysqlDate,
},
success: function(response) {
slotsArray = response;
const today = new Date();
var dateWithoutTime = new Date(today.getFullYear(), today.getMonth(), today.getDate());
var fg = true;
if (selectedDay < dateWithoutTime) {
fg = false;
}
const selectedElement = document.querySelector('.selected');
if (selectedElement) {
selectedElement.classList.remove('selected');
}
const clickedDayElement = daysContainer.children[day - 1];
clickedDayElement.classList.add('selected');
const selectedDateStr = selectedDay.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
var temp = document.getElementById('selectedDate');
temp.value = selectedDateStr;
var time_interval = document.getElementById('time-intervals');
time_interval.innerHTML = "";
for (let i=0; i<periods.length-1; i++){
var startTime = periods[i];
var endTime = periods[i+1];
if (fg) {
var isBooked = false;
for (let j=0; j<slotsArray.length; j++){
if(periods[i]==slotsArray[j].start){
isBooked = true;
while(slotsArray[j].end!=periods[i]){
i++;
}
var span = `<span class="btn btn-danger m-2 col-lg-3 col-md-4 col-sm-6 col-xs-6 text-center" style= "padding: 10px; margin: 5px; " >${slotsArray[j].start} - ${periods[i]} booked by <br> ${slotsArray[j].name}</span>`;
i--;
break;
}
}
if(isBooked === false){
var span = `<span class="btn btn-primary m-2 col-lg-3 col-md-4 col-sm-6 col-xs-6 text-center" style= "padding: 10px; margin: 5px; " onclick="openBookingForm('${mysqlDate}' ,'${periods[i]}', '${periods[i+1]}')">${periods[i]} - ${periods[i+1]} <br> --- </span>`;
}
}
else {
var isBooked = false;
if(slotsArray.length == 0){
var span = `<span class="btn btn-warning m-2 col-lg-3 col-md-4 col-sm-6 col-xs-6 text-center" style= "padding: 10px; margin: 5px; " > No Booking </span>`;
time_interval.innerHTML += span;
break;
}
for (let j=0; j<slotsArray.length; j++){
if(periods[i]==slotsArray[j].start){
isBooked = true;
while(slotsArray[j].end!=periods[i]){
i++;
}
var span = `<span class="btn btn-danger m-2 col-lg-3 col-md-4 col-sm-6 col-xs-6 text-center" style= "padding: 10px; margin: 5px; " >${slotsArray[j].start} - ${periods[i]} booked by <br> ${slotsArray[j].name}</span>`;
i--;
break;
}
}
if(isBooked === false){
var span = `<span class="btn btn-primary m-2 col-lg-3 col-md-4 col-sm-6 col-xs-6 text-center" style= "padding: 10px; margin: 5px; " >${periods[i]} - ${periods[i+1]} <br> --- </span>`;
}
}
time_interval.innerHTML += span;
}
},
});
}
prevBtn.addEventListener('click', () => {
currentMonth--;
if (currentMonth < 0) {
currentMonth = 11;
currentYear--;
}
renderCalendar(currentMonth, currentYear);
});
nextBtn.addEventListener('click', () => {
currentMonth++;
if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
renderCalendar(currentMonth, currentYear);
});
});
function openBookingForm(chosenDate, startTime, endTime) {
document.getElementById('startTime').value = startTime;
populateEndTimeDropdown(chosenDate, startTime);
$('#bookingModal').modal('show');
}
function populateEndTimeDropdown(chosenDate, startTime) {
var endTimeDropdown = document.getElementById('endTime');
endTimeDropdown.innerHTML = '';
var x=periods.length+1;
let flag_end_time = false;
$.ajax({
url: 'api/get_end_time.php',
type: 'GET',
dataType: 'json',
data: {
date: chosenDate,
},
success: function(response) {
for (let i=0; i<periods.length; i++){
if(periods[i]==startTime){
x=i;
}
if(x<i){
for (let j=0; j<response.length; j++){
if (periods[i] === response[j]){
// console.log("DEBUG: "+periods[i]+" == "+response[j]);
flag_end_time = true;
break;
}
}
if (flag_end_time){
break;
}
var option = document.createElement('option');
option.value = periods[i];
option.text = periods[i];
endTimeDropdown.add(option);
}
}
}
});
}
function submitBookingForm() {
let date = document.getElementById('selectedDate').value;
let startTime = document.getElementById('startTime').value;
let endTime = document.getElementById('endTime').value;
let name = document.getElementById('name').value;
let contact = document.getElementById('contact').value;
let advance = document.getElementById('advance').value;
let total = document.getElementById('total').value;
let method = document.getElementById('method').value;
var inputDate = new Date(date);
var month = String(inputDate.getMonth() + 1).padStart(2, '0');
var mysqlDate = inputDate.getFullYear() + '-' + month + '-' + inputDate.getDate();
if (name === "") {
alert("Please enter a valid Name");
} else if (contact === "") {
alert("Please provide a Contact Number");
} else if(advance===""){
alert("Please provide a advance");
}else if(total===""){
alert("Please provide a Total");
}else {
$.ajax({
url: 'api/submit_booking.php',
type: 'POST',
data: {
date: mysqlDate,
startTime: startTime,
endTime: endTime,
name: name,
contact: contact,
ref_id: admin,
advance: advance,
total: total,
method: method
},
success: function(response) {
if (response==='1'){
var valueToPass = 'Booking confirmed for '+name+' on '+date+' from '+startTime+' to '+endTime;
// Update the modal content with the value
document.getElementById('modalContent').innerText = valueToPass;
// Show the modal
$('#bookingModal').modal('hide');
$('#myModal').modal('show');
}
else {
alert('An error occurred. Please try again later.');
}
},
});
}
}
</script>
</section>
</div>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /#page-wrapper -->
<?php include_once('includes/footer.php'); ?>