You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
13 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head th:replace="~{fragments/head-admin :: head}"></head>
  4. <meta charset="ISO-8859-1">
  5. </head>
  6. <body>
  7. <style>
  8. .small-input {
  9. width: 150px;
  10. /* Adjust the width as needed */
  11. }
  12. .small-table th,
  13. .small-table td {
  14. font-size: 14px;
  15. /* Adjust font size as needed */
  16. }
  17. /* Reduce padding for table cells */
  18. .small-table td,
  19. .small-table th {
  20. padding: 5px;
  21. /* Adjust padding as needed */
  22. }
  23. </style>
  24. <body>
  25. <!-- Page Container -->
  26. <div id="page-container"
  27. class="sidebar-o sidebar-dark enable-page-overlay side-scroll page-header-fixed page-header-dark">
  28. <!-- Side Overlay aside (right)-->
  29. <aside th:replace="~{fragments/aside-right :: aside}"></aside>
  30. <!-- END Side Overlay (right)-->
  31. <!-- Sidebar Nav-->
  32. <nav th:replace="~{fragments/nav-admin :: nav}"></nav>
  33. <!-- END Sidebar -->
  34. <!-- Header -->
  35. <header th:replace="~{fragments/header-admin :: header}"></header>
  36. <!-- END Header -->
  37. <!-- Main Container -->
  38. <main id="main-container">
  39. <!-- Page Content -->
  40. <!-- Card Container -->
  41. <div class="content">
  42. <div class="block block-rounded">
  43. <div class="block-header bg-primary text-white">
  44. <h3 class="block-title">Reasons For Booking</h3>
  45. </div>
  46. <div class="block-content">
  47. <h4>Booking Reasons Details<span>*</span></h4>
  48. <div class="row">
  49. <div class="col-md-4">
  50. <div class="form-group">
  51. <label for="bookingReason">Booking Reason:</label>
  52. <div class="input-group">
  53. <select class="form-control" id="bookingReason" name="bookingReason">
  54. </select>
  55. <div class="input-group-append">
  56. <button type="button" class="btn btn-primary" id="addReasonField">
  57. <i class="fas fa-plus fa-fw fa-lg"></i>
  58. </button>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <div class="col-md-4">
  64. <div id="maxdaysContainer" style="display:none">
  65. <label for="maxdays">Max Days:</label>
  66. <input type="text" id="maxdays" class="form-control">
  67. </div>
  68. </div>
  69. <!-- Plus icon wrapped inside a box -->
  70. <div class="row" id="inputBox" style="display:none">
  71. <div class="col-md-4">
  72. <div class="form-group">
  73. <label for="bookingReason">Add New Reason:</label>
  74. <input type="text" class="form-control" id="newReason"
  75. placeholder="New Reason" required>
  76. </div>
  77. </div>
  78. <div class="col-md-4" id="maxDaysContainer">
  79. <div class="form-group">
  80. <label for="maxdays">Max Days:</label>
  81. <input type="text" class="form-control" id="newmaxdays"
  82. placeholder="Max Days" required>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. <br>
  88. <div id="documentDetailsTable" style="width:100%;display:none">
  89. <h4>Booking Reasons Document Details<span>*</span></h4>
  90. <div class="row">
  91. <div>
  92. <div class="col-md-9">
  93. <table class="table" <thead>
  94. <tr>
  95. <th>Field Show</th>
  96. <th>Field Label</th>
  97. <th>Document Show</th>
  98. <th>Document Label</th>
  99. </tr>
  100. </thead>
  101. <tbody id="documentDetailsBody">
  102. </tbody>
  103. </table>
  104. </div>
  105. <div class="col-md-3">
  106. <button type="button" class="btn btn-success" id="addDocumentRow"><i
  107. class="fas fa-plus"></i></button>
  108. </div>
  109. </div>
  110. <div style="text-align:right">
  111. <button type="button" id="resetbutton" class="btn btn-danger">Reset</button>
  112. <button type="button" id="reasondoc" class="btn btn-primary">Submit</button>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. </main>
  120. <div th:replace="~{fragments/resetpassword :: div}"></div>
  121. <script th:replace="~{fragments/loginscript :: script}"></script>
  122. <script>
  123. var rowcount = 0;
  124. $(document).ready(function () {
  125. $('#addReasonField').click(function () {
  126. // Show the input box when plus icon is clicked
  127. $('#inputBox').show();
  128. $('#documentDetailsTable').show();
  129. });
  130. $('#bookingReason').change(function () {
  131. if ($(this).val() === "0") {
  132. // Disable the plus icon button
  133. $('#addReasonField').prop('disabled', false);
  134. } else {
  135. // Enable the plus icon button if no selection is made
  136. $('#addReasonField').prop('disabled', true);
  137. $('#inputBox').hide();
  138. }
  139. var selectedReasonId = $(this).val();
  140. if (selectedReasonId) {
  141. // Show the table
  142. $('#documentDetailsTable').show();
  143. // Clear existing table rows
  144. $('#documentDetailsBody').empty();
  145. // Populate table fields based on selected reason
  146. populateTableFields(selectedReasonId);
  147. }
  148. });
  149. // Add new row when plus button is clicked
  150. $('#addDocumentRow').click(function () {
  151. rowcount += 1;
  152. var newRow = `
  153. <tr class="new-row">
  154. <td>
  155. <input type="hidden" class="form-control" name="reasonDocId_${rowcount}" value="0">
  156. <label class="radio-inline"><input type="radio" name="addTextField_${rowcount}" value="1" checked> Yes</label>
  157. <label class="radio-inline"><input type="radio" name="addTextField_${rowcount}" value="0"> No</label>
  158. </td>
  159. <td><input type="text" class="form-control" name="fieldName_${rowcount}" placeholder="Field Label"></td>
  160. <td>
  161. <label class="radio-inline"><input type="radio" name="addDocumentField_${rowcount}" value="1" checked> Yes</label>
  162. <label class="radio-inline"><input type="radio" name="addDocumentField_${rowcount}" value="0"> No</label>
  163. </td>
  164. <td><input type="text" class="form-control" name="docFieldName_${rowcount}" placeholder="Document Label"></td>
  165. <td><button type="button" class="btn btn-danger deleteRow"><i class="fas fa-minus"></i></button></td>
  166. </tr>`;
  167. $('#documentDetailsBody').append(newRow);
  168. });
  169. $('#resetbutton').click(function () {
  170. window.location.reload();
  171. });
  172. // Delete row when minus button is clicked
  173. $(document).on('click', '.deleteRow', function () {
  174. $(this).closest('tr').remove();
  175. if (rowcount > 0) {
  176. rowcount -= 1;
  177. }
  178. });
  179. });
  180. $(document).ready(function () {
  181. // AJAX request to fetch all reasons from the API
  182. $.ajax({
  183. url: '/communityhall/admin/api/getReasons', // Your API endpoint
  184. type: 'GET',
  185. success: function (reasons) {
  186. // Clear existing options
  187. $('#bookingReason').empty();
  188. $('#bookingReason').append('<option value="0" selected>Select a reason</option>');
  189. // Append options to the dropdown
  190. reasons.forEach(function (reason) {
  191. $('#bookingReason').append('<option value="' + reason.reasonId + '" data-maxdays="' + reason.maxdays + '">' + reason.reason + '</option>');
  192. });
  193. },
  194. error: function (xhr, status, error) {
  195. }
  196. });
  197. // Event listener for dropdown change
  198. $('#bookingReason').on('change', function () {
  199. var selectedOption = $(this).find('option:selected');
  200. var maxdays = selectedOption.data('maxdays');
  201. // Display maxdays in the text field
  202. if (maxdays !== undefined) {
  203. $('#maxdays').val(maxdays);
  204. $('#maxdaysContainer').show();
  205. } else {
  206. $('#maxdays').val(''); // Clear the field if no valid reason is selected
  207. $('#maxdaysContainer').hide();
  208. }
  209. });
  210. });
  211. function populateTableFields(selectedReasonId) {
  212. // AJAX request to fetch table fields from the API
  213. $.ajax({
  214. url: '/communityhall/admin/api/getReasonDocumentDetails?reasonId=' + selectedReasonId,
  215. type: 'GET',
  216. success: function (documentDetails) {
  217. // Log the documentDetails to debug
  218. $('#documentDetailsBody').empty();
  219. // Append rows for each document detail
  220. rowcount = documentDetails.length;
  221. documentDetails.forEach(function (detail, index) {
  222. // Create the new row
  223. var newRow = `
  224. <tr>
  225. <td>
  226. <input type="hidden" class="form-control" name="reasonDocId_${index}" value="${detail.reasonDocId}">
  227. <label class="radio-inline"><input type="radio" name="addTextField_${index}" value="1"> Yes</label>
  228. <label class="radio-inline"><input type="radio" name="addTextField_${index}" value="0"> No</label>
  229. </td>
  230. <td><input type="text" class="form-control" name="fieldName_${index}" value="${detail.fieldName}"></td>
  231. <td>
  232. <label class="radio-inline"><input type="radio" name="addDocumentField_${index}" value="1"> Yes</label>
  233. <label class="radio-inline"><input type="radio" name="addDocumentField_${index}" value="0"> No</label>
  234. </td>
  235. <td><input type="text" class="form-control" name="docFieldName_${index}" value="${detail.docFieldName}"></td>
  236. </tr>`;
  237. // Convert the newRow string into a jQuery object
  238. var $row = $(newRow);
  239. // Set the radio button values based on the detail object's properties
  240. $row.find(`input[name="addTextField_${index}"][value="${detail.addTextField ? '1' : '0'}"]`).prop('checked', true);
  241. $row.find(`input[name="addDocumentField_${index}"][value="${detail.addDocumentField ? '1' : '0'}"]`).prop('checked', true);
  242. // Append the row to the table body
  243. $('#documentDetailsBody').append($row);
  244. });
  245. },
  246. error: function (xhr, status, error) {
  247. }
  248. });
  249. }
  250. $(document).ready(function () {
  251. // Add an event listener for the "add reason doc" button
  252. $('#reasondoc').click(function () {
  253. saveReasonAndDocument();
  254. });
  255. });
  256. function saveReasonAndDocument() {
  257. var reason = $('#bookingReason').val();
  258. var maxdays = $('#maxdays').val();
  259. var newreason = $('#newReason').val().trim().replace(/\s+/g, ' '); // Trim and remove extra spaces
  260. var newmaxdays = $('#newmaxdays').val();
  261. var data = {
  262. "updatedBy": "Test",
  263. "reasonDocumentMasterList": []
  264. };
  265. // Validate the reason
  266. if (reason !== "0") {
  267. data.reasonId = reason;
  268. data.maxdays = maxdays;
  269. data.reason = $('#bookingReason option:selected').text().trim().replace(/\s+/g, ' '); // Trim and remove extra spaces
  270. } else if (newreason === "" || newmaxdays === "") {
  271. Swal.fire({
  272. icon: 'warning',
  273. title: 'Validation Error',
  274. text: 'New Reason and MaxDays should not be empty.'
  275. });
  276. return; // Prevent further execution
  277. } else {
  278. data.reason = newreason;
  279. data.maxdays = newmaxdays;
  280. }
  281. // Validate the document details
  282. var valid = true;
  283. $('#documentDetailsBody tr').each(function () {
  284. var reasonDocId = $(this).find('input[name^="reasonDocId_"]').val();
  285. var addTextField = $(this).find('input[name^="addTextField_"]:checked').val();
  286. var addDocumentField = $(this).find('input[name^="addDocumentField_"]:checked').val();
  287. var fieldName = $(this).find('input[name^="fieldName_"]').val().trim().replace(/\s+/g, ' '); // Trim and remove extra spaces
  288. var docFieldName = $(this).find('input[name^="docFieldName_"]').val().trim().replace(/\s+/g, ' '); // Trim and remove extra spaces
  289. // Check if fieldName is empty
  290. if (addTextField === '1' && fieldName === "") {
  291. Swal.fire({
  292. icon: 'warning',
  293. title: 'Validation Error',
  294. text: 'Field Name should not be empty.'
  295. });
  296. valid = false;
  297. return false; // Break the loop
  298. }
  299. // Check if docFieldName is empty and addDocumentField is selected
  300. if (addDocumentField === '1' && docFieldName === "") {
  301. Swal.fire({
  302. icon: 'warning',
  303. title: 'Validation Error',
  304. text: "Document Label should not be empty if 'Document Show' is Yes."
  305. });
  306. valid = false;
  307. return false; // Break the loop
  308. }
  309. var reasonDoc = {
  310. "fieldName": fieldName,
  311. "addTextField": addTextField === '1',
  312. "docFieldName": docFieldName,
  313. "addDocumentField": addDocumentField === '1'
  314. };
  315. // Include reasonDocId if it's not zero (indicating an existing entry)
  316. if (reasonDocId && reasonDocId !== "0") {
  317. reasonDoc.reasonDocId = reasonDocId;
  318. }
  319. data.reasonDocumentMasterList.push(reasonDoc);
  320. });
  321. // If validation failed, stop further execution
  322. if (!valid) {
  323. return;
  324. }
  325. $.ajax({
  326. url: '/communityhall/admin/api/saveReason',
  327. type: 'POST',
  328. contentType: 'application/json',
  329. data: JSON.stringify(data),
  330. success: function (response) {
  331. window.location.reload(); // Reload the page on success
  332. },
  333. error: function (xhr, status, error) {
  334. console.error('Error saving reason and document details:', error);
  335. }
  336. });
  337. }
  338. </script>
  339. </body>
  340. </html>