forked from cehyman/BusSystemWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.ejs
180 lines (159 loc) · 5.41 KB
/
book.ejs
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
<!DOCTYPE html>
<html>
<head>
<title>Routes</title>
<link rel="stylesheet" href="style.css" />
<script src="logOut.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<a href="/homePage">Home</a><br /><br />
<h1>Welcome to the Booking page.</h1>
<p>Here you can book and "pay" for rides on the bus</p>
<a href="/routes">Look at routes in more detail here</a><br /><br />
<label id="labelUsername"></label><br />
<input id="buttonMyAcc" type="button" onclick="location.href = '/account';" value="My Account" />
<input id="buttonLogOut" type="button" onclick="logOut();" value="Log Out" />
<br /><br /><br />
<h2>Book a ride:</h2>
<label>Departure Location:</label>
<select id="dropdownDepart">
</select><br /><br />
<label>Destination:</label>
<select id="dropdownDestination">
</select><br /><br />
<label>Arrival Time:</label>
<input id="inputArrivalTime" type="time" /><br /><br />
<input id="buttonSubmit" type="button" onclick="bookRide();" value="Submit" />
<h2 id="headerRoute"></h2>
<table id="tableRoute"></table>
<h3 id="bookingStatus"></h3>
</body>
</html>
<script>
//function that is called when the user presses the submit button
function bookRide() {
var dDepart = document.getElementById("dropdownDepart");
var dDest = document.getElementById("dropdownDestination");
var iTime = document.getElementById("inputArrivalTime");
var hRoute = document.getElementById("headerRoute");
var table = document.getElementById("tableRoute");
var depart = dDepart.value;
var dest = dDest.value;
var time = iTime.value;
//clear the result section
hRoute.innerHTML = "";
//clear the table
while (table.hasChildNodes()) {
table.removeChild(table.firstChild);
}
if (depart == dest || depart == 'Select a route' || dest == 'Select a route' || time == '') {
hRoute.innerHTML = "Please select a valid route.";
return;
}
var dbData;
//Post to server with dDepart, dDest, and iTime
$.ajax({
url: "http://localhost:8080/bookRide",
type: "POST",
data: {
depart: depart,
dest: dest,
time: time
},
dataType: 'json',
success: function (result) {
const SUCCESS = 0;
const NO_ROUTE = 1;
const NO_SEATS = 2;
hRoute.innerHTML = "" + result.code;
if (result.code == SUCCESS) {
dbData = result.full;
var totalPrice = 0;
for (var i = 0; i < dbData.length; i++) {
var row = table.insertRow(i);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = dbData[i].depart_stop;
cell2.innerHTML = dbData[i].depart_time;
totalPrice += parseFloat(dbData[i].price);
}
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "Bus Stop";
cell2.innerHTML = "Departure Time";
hRoute.innerHTML = "Booked route from " + depart + " to " + dest + " for total $" + totalPrice + ", All Stops: ";
generateBookings(dbData);
} else if (result.code == NO_ROUTE) {
hRoute.innerHTML = "No route from " + depart + " to " + dest + " arriving before " + time;
} else if (result.code == NO_SEATS) {
hRoute.innerHTML = "No seats available for route from " + depart + " to " + dest + " arriving before " + time;
} else {
hRoute.innerHTML = "Error contacting database. error: " + result.code;
}
}
});
}
function generateBookings(dbData) {
$.ajax({
url: "http://localhost:8080/generateBookings",
type: "POST",
data: {
bookings: dbData,
username: sessionStorage.getItem("username")
},
dataType: 'json',
success: function (result) {
if (result.code != 0) {
var bookingStatus = document.getElementById("bookingStatus");
bookingStatus.innerHTML = "Booking failed: " + JSON.stringify(result.code);
}
}
})
}
//function that is called when page is loaded to populate the dropdowns
function populateDropdowns() {
var dDepart = document.getElementById("dropdownDepart");
var dDest = document.getElementById("dropdownDestination");
var labelUser = document.getElementById("labelUsername");
labelUser.innerHTML = 'Logged in as ' + sessionStorage.getItem('username');
var dbData;
$.ajax({
url: "http://localhost:8080/viewRoutes",
type: "GET",
dataType: 'json',
success: function (result) {
const NONEMPTY = 0;
const EMPTY = 1;
if (result.code == NONEMPTY) {
dbData = result.routes;
var option = document.createElement("option");
option.text = "Select a route";
dDepart.appendChild(option);
option = document.createElement("option");
option.text = "Select a route";
dDest.appendChild(option);
for (var i = 0; i < dbData.length; i++) {
option = document.createElement("option");
option.value = "" + dbData[i].route_id;
option.innerHTML = "" + dbData[i].route_id;
dDepart.appendChild(option);
option = document.createElement("option");
option.value = "" + dbData[i].route_id;
option.innerHTML = "" + dbData[i].route_id;
dDest.appendChild(option);
}
} else if (result.code == EMPTY) {
var option = document.createElement("option");
option.text = "No routes found";
dRoute.add(option);
} else {
alert("Error: " + result.code);
}
}
});
//get the list of locations from database and add them to dropdown here
}
populateDropdowns();
</script>