-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.js
140 lines (129 loc) · 4.69 KB
/
admin.js
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
/*
* Author: Matthew Fan
* Student ID: 18040157
*
* Description of file:
* This file provides direct access from the admin.html to the php server.
* According to the Admin function, the javascript will handle user input information
* and send it to the php server, then get the corresponding respond from the
* server, then convert the returned information to HTML elements.
*
* Functions: Function description will be written before the function definition
*/
/**
* This will handle the assign driver request from the admin.html
* It will get the reference number and an assign request to the server using POST method
* @param {bookingRefNo} Input from admin.html form
*/
function assignDriver(bookingRefNo) {
const xhr = createRequest();
if(xhr){
var obj = document.getElementById("assignConfirmOutput");
var requestbody = "referenceNumber="+encodeURIComponent(bookingRefNo);
xhr.open("POST", "./admin.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
obj.innerHTML = xhr.responseText;
searchBooking("");
}
}//end anonymous call-back function
xhr.send(requestbody);
}
}
/**
* This function will get a 2D array from the server, and create a HTML table based on it.
* @param {*} bookingList An array of bookings from the server
*/
function appendBookingTable(bookingList) {
const table = document.getElementById("tableBody");
table.innerHTML = "";
if(bookingList.length === 0) {
const eachRow = document.createElement("tr");
eachRow.appendChild(createTableColumn("No Booking Result match your input"));
table.appendChild(eachRow);
} else {
bookingList.forEach(element => {
const eachRow = document.createElement("tr");
eachRow.appendChild(createTableColumn(element.bookingRefNo));
eachRow.appendChild(createTableColumn(element.customerName));
eachRow.appendChild(createTableColumn(element.phoneNumber));
eachRow.appendChild(createTableColumn(element.suburb));
eachRow.appendChild(createTableColumn(element.destinationSuburb));
dateTime = element.pickUpDate + " / " + element.pickUpTime;
eachRow.appendChild(createTableColumn(dateTime));
eachRow.appendChild(createTableColumn(element.status));
eachRow.appendChild(createAssignButton(element.bookingRefNo, element.status));
table.appendChild(eachRow);
});
}
}
/**
* Create the columne of the generated table
* @param {*} element
* @returns
*/
function createTableColumn(element) {
const column = document.createElement("td");
column.appendChild(document.createTextNode(element));
return column;
}
/**
* Create the assign driver button for each row in the table
* @param {*} element
* @returns
*/
function createAssignButton(referenceNumber, status) {
const column = document.createElement("td");
const button = document.createElement("input");
button.className = "button is-rounded is large";
button.name = "assign";
button.type="button";
button.value="assign taxi";
//prevent assign button to be clicable for taken
if(status=="assigned") {
button.disabled="true"
}
button.addEventListener("click", () => assignDriver(referenceNumber), false);
column.appendChild(button);
return column;
}
/**
* Fetch the data from the input form in admin.html, and send it to the server
* This will use the search keywork from the input form
*
* After get the response from the server, the function will generate a table
* based on the information list returned from the server.
* @param {*} requestKeyword
*/
function searchBooking(requestKeyword) {
const xhr = createRequest();
let bookingList = [];
if(xhr) {
var obj = document.getElementById("targetDiv");
var url = "admin.php?requestKeyword="+requestKeyword;
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
bookingList = JSON.parse(xhr.responseText);
appendBookingTable(bookingList);
}
}
xhr.send(null);
}
}
/**
* Initialize XML
* @returns XML object
*/
function createRequest() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr;
} // end function createRequest()