[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');
|
---|
[43c9090] | 52 | message.textContent = 'Дали сте сигурни дека сакате да откажете?';
|
---|
[743de55] | 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');
|
---|
[43c9090] | 109 | nameCell.textContent = request.name;
|
---|
[743de55] | 110 | row.appendChild(nameCell);
|
---|
| 111 | const surnameCell = document.createElement('td');
|
---|
[43c9090] | 112 | surnameCell.textContent = request.surname;
|
---|
[743de55] | 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');
|
---|
[43c9090] | 121 | usernameCell.textContent = request.username;
|
---|
[743de55] | 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 | thead.innerHTML = '';
|
---|
| 142 | }
|
---|
| 143 | let newColumns;
|
---|
| 144 | if(data==='carried_out'){
|
---|
| 145 | newColumns = ['Термин', 'Feedback-корисник', 'Feedback-терапевт', 'Статус'];
|
---|
| 146 | }
|
---|
| 147 | else{
|
---|
| 148 | newColumns = ['Термин', 'Име', 'Презиме', 'Купон','Дополнителни информации','Корисник','Откажи'];
|
---|
| 149 | }
|
---|
| 150 | const newHeadRow = document.createElement('tr');
|
---|
| 151 |
|
---|
| 152 | newColumns.forEach(columnText => {
|
---|
| 153 | const th = document.createElement('th');
|
---|
| 154 | th.textContent = columnText;
|
---|
| 155 | newHeadRow.appendChild(th);
|
---|
| 156 | });
|
---|
| 157 | thead.appendChild(newHeadRow);
|
---|
| 158 | }
|
---|
| 159 | function displayCarriedOutInTable(data){
|
---|
| 160 | const tableBody = document.getElementById('requestsTableBody');
|
---|
| 161 | tableBody.innerHTML = '';
|
---|
| 162 | toggleHead("carried_out");
|
---|
| 163 | data.forEach(request => {
|
---|
| 164 | const row = document.createElement('tr');
|
---|
| 165 | const termCell = document.createElement('td');
|
---|
[43c9090] | 166 | termCell.textContent = replaceWithSpace(request.dateTime);
|
---|
[743de55] | 167 | row.appendChild(termCell);
|
---|
| 168 | const userCell = document.createElement('td');
|
---|
| 169 | userCell.textContent = request.userNote;
|
---|
| 170 | row.appendChild(userCell);
|
---|
| 171 | const adminCell = document.createElement('td');
|
---|
[43c9090] | 172 | adminCell.textContent = request.adminNote;
|
---|
[743de55] | 173 | row.appendChild(adminCell);
|
---|
| 174 | const statusCell = document.createElement('td');
|
---|
| 175 | statusCell.textContent = request.status;
|
---|
| 176 | row.appendChild(statusCell);
|
---|
| 177 | tableBody.appendChild(row);
|
---|
| 178 | });
|
---|
| 179 | }
|
---|
| 180 | function handleRequestedClick(){
|
---|
| 181 | fetch(`/api/requests/listAll?username=${username}`)
|
---|
| 182 | .then(response => response.json())
|
---|
| 183 | .then(data => {
|
---|
| 184 | console.log('Success:', data);
|
---|
| 185 | displayRequestsInTable(data,[
|
---|
| 186 | {
|
---|
| 187 | text: 'Откажи',
|
---|
| 188 | className: 'btn-danger',
|
---|
| 189 | handler: () => cancelForm("request",data)
|
---|
| 190 | }
|
---|
| 191 | ]);
|
---|
| 192 | })
|
---|
| 193 | .catch(error => {
|
---|
| 194 | console.error('Error:', error);
|
---|
| 195 | });
|
---|
| 196 | }
|
---|
| 197 | function handleReservedClick(){
|
---|
| 198 | fetch(`/api/appointments/listAll?username=${username}&status=RESERVED`)
|
---|
| 199 | .then(response => response.json())
|
---|
| 200 | .then(data => {
|
---|
| 201 | console.log('Success:', data);
|
---|
| 202 | displayRequestsInTable(data,[
|
---|
| 203 | {
|
---|
| 204 | text: 'Откажи',
|
---|
| 205 | className: 'btn-danger',
|
---|
| 206 | handler: () => cancelForm("appointment",data)
|
---|
| 207 | }
|
---|
| 208 | ]);
|
---|
| 209 | })
|
---|
| 210 | .catch(error => {
|
---|
| 211 | console.error('Error:', error);
|
---|
| 212 | });
|
---|
| 213 | }
|
---|
| 214 | function handleCarriedOutClick(){
|
---|
| 215 | fetch(`/api/users/listAllCarriedOut?username=${username}`)
|
---|
| 216 | .then(response => response.json())
|
---|
| 217 | .then(data => {
|
---|
| 218 | displayCarriedOutInTable(data);
|
---|
| 219 | })
|
---|
| 220 | .catch(error => {
|
---|
| 221 | console.error('Error:', error);
|
---|
| 222 | });
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | document.getElementById('requested').addEventListener('click', handleRequestedClick);
|
---|
| 227 | document.getElementById('reserved').addEventListener('click', handleReservedClick);
|
---|
| 228 | document.getElementById('carried-out').addEventListener('click', handleCarriedOutClick); |
---|