-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookings.php
executable file
·154 lines (132 loc) · 6.62 KB
/
bookings.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FERN Hotel - Bookings Details</title>
<?php require('inc/links.php') ?>
</head>
<body class="bg-light">
<?php
require('inc/header.php');
if (!(isset($_SESSION['login']) && $_SESSION['login'] == true)) {
redirect('index.php');
}
?>
<div class="container">
<div class="row">
<div class="col-12 my-5 px-4">
<h2 class="fw-bold h-font">BOOKINGS</h2>
<div style="font-size:14px;">
<a href="index.php" class="text-secondary text-decoration-none">HOME</a>
<span class="text-secondary"> > </span>
<a href="#" class="text-secondary text-decoration-none">BOOKINGS</a>
</div>
</div>
<?php
$query = "SELECT bo.*, bd.*
FROM `payment` bo
INNER JOIN `booking` bd ON bo.payment_id = bd.payment_id
WHERE (bd.booking_status IN ('confirmed', 'cancelled'))
AND (bd.user_id = ?)
ORDER BY bd.booking_id DESC";
$result = select($query, [$_SESSION['id']], 'i');
while ($data = mysqli_fetch_assoc($result)) {
$checkin = date("d-m-y", strtotime($data['checkin']));
$checkout = date("d-m-y", strtotime($data['checkout']));
$date = date("d-m-y | h:ia", strtotime($data['datentime']));
$status_bg = "";
$btn = "";
$cancelbtn = "";
// Check booking status to determine button visibility
if ($data['booking_status'] == 'confirmed') {
$status_bg = "bg-success";
$btn = "<a href='generate_pdf.php?gen_pdf&id=$data[booking_id]' class='btn btn-dark btn-sm shadow-none'>Download PDF</a>";
$cancelbtn = "<button class='btn btn-danger btn-sm shadow-none cancel-booking' data-payment-id='$data[payment_id]'>Cancel Booking</button>";
} else if ($data['booking_status'] == 'pending') {
$status_bg = "bg-warning";
} else if ($data['booking_status'] == 'cancelled') {
$status_bg = "bg-danger";
} else {
$status_bg = "bg-info";
}
echo <<<bookings
<div class='col-md-4 px-4 mb-4'>
<div class='bg-white rounded shadow-md' style="padding: 12px;">
<h5 class='fw-bold'>$data[room_name]</h5>
<p>
<b>Check in: </b> $checkin <br>
<b>Check out: </b> $checkout
</p>
<p>
<b>Amount: </b> ₹$data[trans_amt] <br>
<b>Order ID: </b> $data[order_id]<br>
<b>Date: </b> $date
</p>
<p>
<span class='badge $status_bg' id='status'>$data[booking_status]</span>
</p>
$btn
$cancelbtn
</div>
</div>
bookings;
}
?>
</div>
</div>
<?php require('inc/footer.php'); ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Add event listener for all Cancel Booking buttons
const cancelButtons = document.querySelectorAll('.cancel-booking');
cancelButtons.forEach(button => {
button.addEventListener('click', function () {
const paymentId = this.getAttribute('data-payment-id');
if (confirm('Are you sure you want to cancel this booking?')) {
cancelBooking(paymentId);
}
});
});
// Cancel Booking Function
function cancelBooking(paymentId) {
let xhr = new XMLHttpRequest();
xhr.open("POST", "ajax/cancel_booking.php", true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
console.log('Server Response:', this.responseText); // Log the raw response for debugging
if (this.status === 200) {
try {
// Try parsing the response as JSON
const response = JSON.parse(this.responseText);
if (response.success) {
showAlert('success', 'Booking cancelled successfully!');
const bookingCard = document.querySelector(`[data-payment-id='${paymentId}']`).closest('.bg-white');
const badge = bookingCard.querySelector('.badge');
badge.classList.replace('bg-warning', 'bg-danger');
badge.classList.replace('bg-success', 'bg-danger');
badge.textContent = 'Cancelled';
const cancelButton = bookingCard.querySelector('.cancel-booking');
cancelButton.remove();
// Remove Download PDF button as well
const downloadButton = bookingCard.querySelector('.btn-dark');
if (downloadButton) {
downloadButton.remove();
}
} else {
showAlert('danger', response.message || 'Failed to cancel booking.');
}
} catch (error) {
console.error('Error parsing JSON:', error);
showAlert('danger', 'An error occurred while processing the response.');
}
} else {
showAlert('danger', 'Server error. Please try again later.');
}
};
xhr.send('payment_id=' + paymentId);
}
});
</script>
</body>
</html>