[743de55] | 1 | function deleteEntry(url,term,separator) {
|
---|
| 2 | fetch(url)
|
---|
| 3 | .then(response => response.json())
|
---|
| 4 | .then(data => {
|
---|
| 5 | if (data.success) {
|
---|
| 6 | let retrievedData=data.data;
|
---|
| 7 | let idto=retrievedData.split("&")[1];
|
---|
| 8 | let infoto=retrievedData.split("&")[0];
|
---|
| 9 |
|
---|
| 10 | let statusField;
|
---|
| 11 | if(separator==='request'){
|
---|
| 12 | statusField="cancelledRequest";
|
---|
| 13 | }
|
---|
| 14 | else if(separator==='appointment'){
|
---|
| 15 |
|
---|
| 16 | statusField="cancelledAppointmentByUser";
|
---|
| 17 | }
|
---|
| 18 | const updateResponse = fetch(`/api/users/addTerm`, {
|
---|
| 19 | method: 'PUT',
|
---|
| 20 | headers: {
|
---|
| 21 | 'Content-Type': 'application/json',
|
---|
| 22 | },
|
---|
| 23 | body: JSON.stringify({
|
---|
| 24 | userId: idto,
|
---|
| 25 | term: term,
|
---|
| 26 | additionalInfo: infoto,
|
---|
| 27 | status: statusField
|
---|
| 28 | }),
|
---|
| 29 | });
|
---|
| 30 |
|
---|
| 31 | if (updateResponse.ok) {
|
---|
| 32 | console.log(`User updated successfully.`);
|
---|
| 33 | } else {
|
---|
| 34 | console.error(`Failed to update user. Status: ${updateResponse.status} - ${updateResponse.statusText}`);
|
---|
| 35 | }
|
---|
| 36 | location.reload();
|
---|
| 37 | } else {
|
---|
| 38 | alert('Failed to cancel the reservation.');
|
---|
| 39 | }
|
---|
| 40 | })
|
---|
| 41 | .catch(error => console.error('Error:', error));
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | function cancelForm(type,data){
|
---|
| 46 | let tempTerm=data[0].term;
|
---|
| 47 | const overlay = document.createElement('div');
|
---|
| 48 | overlay.setAttribute("id","popup");
|
---|
| 49 | const dialog = document.createElement('div');
|
---|
| 50 | dialog.setAttribute("id","dialogPopUp");
|
---|
| 51 | const message = document.createElement('p');
|
---|
| 52 | message.textContent = 'Are you sure you want to cancel the reservation?';
|
---|
| 53 | dialog.appendChild(message);
|
---|
| 54 | const yesButton = document.createElement('button');
|
---|
| 55 | yesButton.textContent = 'Yes';
|
---|
| 56 | yesButton.style.marginRight = '10px';
|
---|
| 57 | yesButton.classList.add('btn', 'btn-danger');
|
---|
| 58 |
|
---|
| 59 | const noButton = document.createElement('button');
|
---|
| 60 | noButton.textContent = 'No';
|
---|
| 61 | noButton.classList.add('btn', 'btn-secondary');
|
---|
| 62 | dialog.appendChild(yesButton);
|
---|
| 63 | dialog.appendChild(noButton);
|
---|
| 64 |
|
---|
| 65 | overlay.appendChild(dialog);
|
---|
| 66 |
|
---|
| 67 | document.body.appendChild(overlay);
|
---|
| 68 |
|
---|
| 69 | yesButton.addEventListener('click', () => {
|
---|
| 70 |
|
---|
| 71 | let url;
|
---|
| 72 | let separator;
|
---|
| 73 | if (type === 'request') {
|
---|
| 74 | url = `/api/requests/cancelReservation?username=${username}&term=${tempTerm}`;
|
---|
| 75 | separator="request";
|
---|
| 76 | } else if (type === 'appointment') {
|
---|
| 77 | url = `/api/appointments/cancelAppointment?username=${username}&term=${tempTerm}`;
|
---|
| 78 | separator="appointment";
|
---|
| 79 | }
|
---|
| 80 | deleteEntry(url,tempTerm,separator);
|
---|
| 81 | document.body.removeChild(overlay);
|
---|
| 82 | });
|
---|
| 83 |
|
---|
| 84 | noButton.addEventListener('click', () => {
|
---|
| 85 | document.body.removeChild(overlay);
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 | function replaceWithSpace(dateTimeString) {
|
---|
| 89 | return dateTimeString.replace('T', ' ');
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | function getCookieValue(name) {
|
---|
| 93 | const value = `; ${document.cookie}`;
|
---|
| 94 | const parts = value.split(`; ${name}=`);
|
---|
| 95 | if (parts.length === 2) return parts.pop().split(';').shift();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | const username = getCookieValue('username');
|
---|
| 99 | function displayRequestsInTable(requests,buttonsConfig) {
|
---|
| 100 | const tableBody = document.getElementById('requestsTableBody');
|
---|
| 101 | tableBody.innerHTML = '';
|
---|
| 102 | toggleHead();
|
---|
| 103 | requests.forEach(request => {
|
---|
| 104 | const row = document.createElement('tr');
|
---|
| 105 | const termCell = document.createElement('td');
|
---|
| 106 | termCell.textContent = replaceWithSpace(request.term);
|
---|
| 107 | row.appendChild(termCell);
|
---|
| 108 | const nameCell = document.createElement('td');
|
---|
| 109 | nameCell.textContent = request.name; // Format date
|
---|
| 110 | row.appendChild(nameCell);
|
---|
| 111 | const surnameCell = document.createElement('td');
|
---|
| 112 | surnameCell.textContent = request.surname; // Format date
|
---|
| 113 | row.appendChild(surnameCell);
|
---|
| 114 | const couponCodeCell = document.createElement('td');
|
---|
| 115 | couponCodeCell.textContent = request.couponCode;
|
---|
| 116 | row.appendChild(couponCodeCell);
|
---|
| 117 | const additionalInfoCell = document.createElement('td');
|
---|
| 118 | additionalInfoCell.textContent = request.additionalInfo;
|
---|
| 119 | row.appendChild(additionalInfoCell);
|
---|
| 120 | const usernameCell = document.createElement('td');
|
---|
| 121 | usernameCell.textContent = request.username; // Format date
|
---|
| 122 | row.appendChild(usernameCell);
|
---|
| 123 | const buttonCell = document.createElement('td');
|
---|
| 124 |
|
---|
| 125 | buttonsConfig.forEach(config => {
|
---|
| 126 | const button = document.createElement('button');
|
---|
| 127 | button.textContent = config.text;
|
---|
| 128 | button.classList.add('btn', config.className);
|
---|
| 129 | button.addEventListener('click', config.handler);
|
---|
| 130 | buttonCell.appendChild(button);
|
---|
| 131 | });
|
---|
| 132 |
|
---|
| 133 | row.appendChild(buttonCell);
|
---|
| 134 | tableBody.appendChild(row);
|
---|
| 135 | });
|
---|
| 136 | }
|
---|
| 137 | function toggleHead(data){
|
---|
| 138 | const thead = document.getElementById('table-head');
|
---|
| 139 | const oldHead = document.getElementById('initial-head');
|
---|
| 140 | if (thead) {
|
---|
| 141 | console.log("cleaned")
|
---|
| 142 | thead.innerHTML = '';
|
---|
| 143 | }
|
---|
| 144 | let newColumns;
|
---|
| 145 | if(data==='carried_out'){
|
---|
| 146 | newColumns = ['Термин', 'Feedback-корисник', 'Feedback-терапевт', 'Статус'];
|
---|
| 147 | }
|
---|
| 148 | else{
|
---|
| 149 | newColumns = ['Термин', 'Име', 'Презиме', 'Купон','Дополнителни информации','Корисник','Откажи'];
|
---|
| 150 | }
|
---|
| 151 | const newHeadRow = document.createElement('tr');
|
---|
| 152 |
|
---|
| 153 | newColumns.forEach(columnText => {
|
---|
| 154 | const th = document.createElement('th');
|
---|
| 155 | th.textContent = columnText;
|
---|
| 156 | newHeadRow.appendChild(th);
|
---|
| 157 | });
|
---|
| 158 | thead.appendChild(newHeadRow);
|
---|
| 159 | }
|
---|
| 160 | function displayCarriedOutInTable(data){
|
---|
| 161 | const tableBody = document.getElementById('requestsTableBody');
|
---|
| 162 | tableBody.innerHTML = '';
|
---|
| 163 | toggleHead("carried_out");
|
---|
| 164 | data.forEach(request => {
|
---|
| 165 | const row = document.createElement('tr');
|
---|
| 166 | const termCell = document.createElement('td');
|
---|
| 167 | termCell.textContent = replaceWithSpace(request.dateTime); // Format date
|
---|
| 168 | row.appendChild(termCell);
|
---|
| 169 | const userCell = document.createElement('td');
|
---|
| 170 | userCell.textContent = request.userNote;
|
---|
| 171 | row.appendChild(userCell);
|
---|
| 172 | const adminCell = document.createElement('td');
|
---|
| 173 | adminCell.textContent = request.adminNote; // Format date
|
---|
| 174 | row.appendChild(adminCell);
|
---|
| 175 | const statusCell = document.createElement('td');
|
---|
| 176 | statusCell.textContent = request.status;
|
---|
| 177 | row.appendChild(statusCell);
|
---|
| 178 | tableBody.appendChild(row);
|
---|
| 179 | });
|
---|
| 180 | }
|
---|
| 181 | function handleRequestedClick(){
|
---|
| 182 | fetch(`/api/requests/listAll?username=${username}`)
|
---|
| 183 | .then(response => response.json())
|
---|
| 184 | .then(data => {
|
---|
| 185 | console.log('Success:', data);
|
---|
| 186 | displayRequestsInTable(data,[
|
---|
| 187 | {
|
---|
| 188 | text: 'Откажи',
|
---|
| 189 | className: 'btn-danger',
|
---|
| 190 | handler: () => cancelForm("request",data)
|
---|
| 191 | }
|
---|
| 192 | ]);
|
---|
| 193 | })
|
---|
| 194 | .catch(error => {
|
---|
| 195 | console.error('Error:', error);
|
---|
| 196 | });
|
---|
| 197 | }
|
---|
| 198 | function handleReservedClick(){
|
---|
| 199 | fetch(`/api/appointments/listAll?username=${username}&status=RESERVED`)
|
---|
| 200 | .then(response => response.json())
|
---|
| 201 | .then(data => {
|
---|
| 202 | console.log('Success:', data);
|
---|
| 203 | displayRequestsInTable(data,[
|
---|
| 204 | {
|
---|
| 205 | text: 'Откажи',
|
---|
| 206 | className: 'btn-danger',
|
---|
| 207 | handler: () => cancelForm("appointment",data)
|
---|
| 208 | }
|
---|
| 209 | ]);
|
---|
| 210 | })
|
---|
| 211 | .catch(error => {
|
---|
| 212 | console.error('Error:', error);
|
---|
| 213 | });
|
---|
| 214 | }
|
---|
| 215 | function handleCarriedOutClick(){
|
---|
| 216 | fetch(`/api/users/listAllCarriedOut?username=${username}`)
|
---|
| 217 | .then(response => response.json())
|
---|
| 218 | .then(data => {
|
---|
| 219 | displayCarriedOutInTable(data);
|
---|
| 220 | })
|
---|
| 221 | .catch(error => {
|
---|
| 222 | console.error('Error:', error);
|
---|
| 223 | });
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | document.getElementById('requested').addEventListener('click', handleRequestedClick);
|
---|
| 228 | document.getElementById('reserved').addEventListener('click', handleReservedClick);
|
---|
| 229 | document.getElementById('carried-out').addEventListener('click', handleCarriedOutClick); |
---|