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.

1581 lines
51 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html lang="en" dir="ltr">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
  5. <head th:replace="~{fragments/modules/base/head :: head}"></head>
  6. <style>
  7. body {
  8. background-color: #f8f9fa;
  9. /* font-family: Arial, sans-serif;*/
  10. }
  11. .header {
  12. font-size: 22px;
  13. font-weight: bold;
  14. margin-bottom: 20px;
  15. }
  16. .table {
  17. border: 1px solid #dee2e6;
  18. }
  19. .table th,
  20. .table td {
  21. border: 1px solid #dee2e6;
  22. padding: 10px;
  23. }
  24. .section-title {
  25. font-size: 18px;
  26. font-weight: bold;
  27. margin-top: 20px;
  28. border-bottom: 2px solid #007bff;
  29. padding-bottom: 5px;
  30. }
  31. .form-group {
  32. margin-top: 15px;
  33. }
  34. .radio-group {
  35. display: flex;
  36. gap: 20px;
  37. margin-top: 10px;
  38. }
  39. #assetModal .modal-body {
  40. max-height: 80vh;
  41. overflow-y: auto;
  42. }
  43. #floordetailsModal .modal-body {
  44. max-height: 80vh;
  45. overflow-y: auto;
  46. }
  47. #roomMappingModal .modal-body {
  48. max-height: 80vh;
  49. overflow-y: auto;
  50. min-height: 40vh;
  51. }
  52. /* .responsive-section {
  53. margin-left: -256px;
  54. } */
  55. /* Wider negative margin if needed */
  56. /* .responsive-section-wide {
  57. margin-left: -281px;
  58. } */
  59. /* Mobile responsiveness */
  60. @media (max-width: 768px) {
  61. .responsive-section,
  62. .responsive-section-wide {
  63. margin-left: 0 !important;
  64. padding: 0 10px;
  65. }
  66. .btn.mb-2 {
  67. width: 100%;
  68. margin-top: 10px;
  69. }
  70. .d-flex.justify-content-between {
  71. flex-direction: column;
  72. align-items: flex-start;
  73. }
  74. .table {
  75. display: block;
  76. width: 100%;
  77. overflow-x: auto;
  78. white-space: nowrap;
  79. }
  80. }
  81. </style>
  82. <body>
  83. <div class="wrapper">
  84. <header th:replace="~{fragments/modules/base/header :: header}"></header>
  85. <div class="page-wrap">
  86. <div th:replace="~{fragments/modules/base/menu :: menu}"></div>
  87. <div class="main-content" style="margin-top: 6px;padding-left: 0px;">
  88. <div class="container">
  89. <div class="col-md-12">
  90. <div class="d-flex justify-content-between align-items-center responsive-section">
  91. <!-- <h4 style="margin-left: -256px;">Buildings</h4> -->
  92. <h4 >Buildings</h4>
  93. <button type="button" class="btn btn-primary"
  94. style="background-color: #0D587B; border: none; border-radius: 6px; padding: 6px 16px;"
  95. data-bs-toggle="modal" data-bs-target="#assetModal">
  96. + Add Building
  97. </button>
  98. </div>
  99. <hr class="responsive-section"
  100. style="border: 1px solid #ccc; margin-top: 10px;">
  101. </div>
  102. <div class="row mt-4 responsive-section-wide">
  103. <div class="col-md-12">
  104. <div class="card">
  105. <div class="card-block">
  106. <table id="CompletedCPSDatatable"
  107. class="table table-striped table-bordered table-hover dataTable" role="grid"
  108. style="width: 100%">
  109. <thead class="thead-dark">
  110. <tr>
  111. <th style="width: 5%;">S.NO</th>
  112. <th style="width: 10%;">Building Name</th>
  113. <th style="width: 15%;">Total Floor</th>
  114. <th style="width: 10%;">Building Material</th>
  115. <th style="width: 15%;">Roof Material</th>
  116. <th style="width: 10%;">Action</th>
  117. </tr>
  118. </thead>
  119. <tbody>
  120. </tbody>
  121. </table>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. <!-- Card 2: Other Details -->
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. <!-- Modal -->
  132. <div class="modal fade" id="assetModal" tabindex="-1"
  133. data-bs-backdrop="static" data-bs-keyboard="false"
  134. aria-labelledby="assetModalLabel" aria-hidden="true">
  135. <div class="modal-dialog modal-lg" role="document">
  136. <div class="modal-content">
  137. <div class="modal-header">
  138. <h5 class="modal-title">Building Management</h5>
  139. <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
  140. <span aria-hidden="true">&times;</span>
  141. </button>
  142. </div>
  143. <div class="modal-body">
  144. <span id="EditSchoolId" style="display:none;"></span>
  145. <div class="row">
  146. <div class="col-md-12 mb-3 d-flex justify-content-end">
  147. <!-- <label class="mb-0">Total No of Buildings: <span id="lastBuildingId"></span></label> -->
  148. <button type="button" class="btn btn-success btn-sm" id="addBuildingBtn">+ Add
  149. Building</button>
  150. </div>
  151. </div>
  152. <!-- Dynamic Asset Selection Section -->
  153. <div class="col-md-12" id="assetContainer"></div>
  154. <div class="modal-footer">
  155. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
  156. <button type="button" class="btn btn-primary" id="buildingSave">Save</button>
  157. <button type="button" class="btn btn-primary" id="buildingupdate">Update</button>
  158. </div>
  159. </div>
  160. </div>
  161. </div>
  162. </div>
  163. <!-- Floor Details Modal -->
  164. <div class="modal fade" id="floordetailsModal" tabindex="-1" role="dialog" aria-labelledby="floordetailsModalLabel"
  165. aria-hidden="true" data-backdrop="static" data-keyboard="false">
  166. <div class="modal-dialog modal-lg" role="document">
  167. <div class="modal-content">
  168. <div class="modal-header">
  169. <h5 class="modal-title" id="detailsModalLabel">Floor Details</h5>
  170. <button type="button" class="close" data-dismiss="modal"
  171. onclick="$('#floordetailsModal').modal('hide');" aria-label="Close">
  172. <span aria-hidden="true">&times;</span>
  173. </button>
  174. </div>
  175. <div class="modal-body">
  176. <table class="table table-striped table-bordered" id="floorDetailsTable">
  177. <thead class="thead-dark">
  178. <tr>
  179. <th class="text-center">S.No</th>
  180. <th class="text-center">Floor Name</th>
  181. <th class="text-center">Rooms</th>
  182. <th class="text-center">Status</th>
  183. <th class="text-center">Action</th>
  184. </tr>
  185. </thead>
  186. <tbody>
  187. <!-- Data will be inserted here -->
  188. </tbody>
  189. </table>
  190. </div>
  191. <div class="modal-footer">
  192. <button type="button" class="btn btn-secondary" data-dismiss="modal"
  193. onclick="$('#floordetailsModal').modal('hide');">Close</button>
  194. </div>
  195. </div>
  196. </div>
  197. </div>
  198. <!-- Room Mapping Modal -->
  199. <div class="modal fade" id="roomMappingModal" tabindex="-1" role="dialog" aria-labelledby="roomMappingModalLabel"
  200. aria-hidden="true">
  201. <div class="modal-dialog modal-lg" role="document">
  202. <div class="modal-content">
  203. <div class="modal-header">
  204. <h5 class="modal-title"><span id="BuildingRoomMapping">&nbsp;</span>&nbsp;Room Mapping</h5>
  205. <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
  206. <span aria-hidden="true">&times;</span>
  207. </button>
  208. </div>
  209. <div class="modal-body">
  210. Add&nbsp;<span id="roomFloorName"></span>&nbsp;Details
  211. <form>
  212. <!-- Hidden Inputs to Store Building ID and Floor ID -->
  213. <input type="hidden" id="roomBuildingId">
  214. <input type="hidden" id="roomFloorId">
  215. <div class="row">
  216. <div class="col-md-4 form-group">
  217. <label>Select Type of Rooms <span class="text-danger">*</span></label>
  218. <div class="dropdown">
  219. <button class="form-control dropdown-toggle" type="button" id="room_type_dropdown"
  220. data-bs-toggle="dropdown" aria-expanded="false">
  221. Select Rooms Type
  222. </button>
  223. <ul class="dropdown-menu" id="room_type_options"
  224. aria-labelledby="room_type_dropdown"
  225. style="max-height: 400px; overflow-y: auto; width: 100%;">
  226. <!-- Options will be added dynamically -->
  227. </ul>
  228. </div>
  229. <input type="hidden" id="selected_room_types"> <!-- Stores selected values -->
  230. </div>
  231. </div>
  232. <!-- Dynamic Inputs for Selected room Types -->
  233. <div class="row mt-3" id="room_capacity_container"></div>
  234. </form>
  235. </div>
  236. <div class="modal-footer">
  237. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
  238. <button type="button" class="btn btn-primary" id="saveRoomMapping">Save Room Mapping</button>
  239. </div>
  240. </div>
  241. </div>
  242. </div>
  243. <script th:replace="~{fragments/modules/base/script :: script}"></script>
  244. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
  245. </body>
  246. <script>
  247. //let buildings = [];
  248. //let lastBuildingId = 0;
  249. //Full rearranged and cleaned-up script for Building Management Page
  250. $(document).ready(function () {
  251. function getQueryParam(param) {
  252. const urlParams = new URLSearchParams(window.location.search);
  253. return urlParams.get(param);
  254. }
  255. //if(!window.buildingBound){
  256. let schoolId;
  257. // buildings = [];
  258. let lastBuildingId = 0;
  259. schoolId = getQueryParam('schoolId');
  260. // Load saved buildings on page load
  261. loadSavedBuilding(schoolId);
  262. // Reset modal when shown
  263. $('#assetModal').on('show.bs.modal', function () {
  264. $('#assetContainer').empty();
  265. buildings = [];
  266. //lastBuildingId = parseInt($('#lastBuildingId').text()) || 0;
  267. //console.log("lastBuildingId====="+lastBuildingId);
  268. $('#addBuildingBtn').show();
  269. $('#buildingupdate').hide();
  270. $('#buildingSave').hide();
  271. });
  272. /* $(document).on('click', '.edit-btn', function () {
  273. console.log('Edit button clicked'); // Debugging line
  274. $('#addBuildingBtn').hide();
  275. $('#buildingupdate').show();
  276. }); */
  277. $(document).on('click', '.view-btn', function () {
  278. console.log('View button clicked'); // Debugging line
  279. $('#addBuildingBtn').hide();
  280. // Load data for viewing
  281. //$('#assetModal').modal('show');
  282. });
  283. // Add building
  284. $(document).on('click', '#addBuildingBtn', function () {
  285. //lastBuildingId1 = parseInt($('#lastBuildingId').text()) || 0;
  286. //console.log("lastBuildingId1====="+lastBuildingId1);
  287. addNewBuilding();
  288. $('#buildingSave').show();
  289. });
  290. // Save buildings
  291. $('#buildingSave').on('click', saveBuildings);
  292. // View and Edit buttons
  293. $(document).on('click', '.view-btn', handleView);
  294. //$(document).on('click', '.edit-btn', handleEdit);
  295. $(document).on('click', '.edit-btn', function () {
  296. const buildingId = $(this).data('building-number');
  297. handleEdit(buildingId);
  298. $('#buildingupdate').show();
  299. $('#addBuildingBtn').hide();
  300. $('#buildingSave').hide();
  301. });
  302. //window.buildingBound=true;
  303. //}
  304. });
  305. function addNewBuilding() {
  306. const assetContainer = $('#assetContainer');
  307. const lastId = $('.building-section').length;
  308. const buildingId = lastId + 1;
  309. //console.log("lastBuildingId="+lastBuildingId);
  310. buildings.push(buildingId);
  311. let buildingMaterials = '', roofMaterials = '';
  312. $.ajax({
  313. url: '/gcc/api/gccschool/dropdown/getBuildingMaterial', method: 'GET', async: false, success: res => {
  314. if (res.status === "OK") {
  315. res.data.forEach(item => {
  316. buildingMaterials += `<option value="${item.bm_id}">${item.content}</option>`;
  317. });
  318. }
  319. }
  320. });
  321. $.ajax({
  322. url: '/gcc/api/gccschool/dropdown/getRoofMaterial', method: 'GET', async: false, success: res => {
  323. if (res.status === "OK") {
  324. res.data.forEach(item => {
  325. roofMaterials += `<option value="${item.rm_id}">${item.content}</option>`;
  326. });
  327. }
  328. }
  329. });
  330. //ak
  331. const buildingSection = $(`
  332. <div class="building-section border p-3 mb-3" data-id="${buildingId}">
  333. <h5 class="building-title">Building </h5>
  334. <div class="row">
  335. <div class="col-md-6 form-group">
  336. <label>Building Name <span class="text-danger">*</span></label>
  337. <input class="form-control" id="buildingName${buildingId}" placeholder="Enter Building Names">
  338. </div>
  339. <div class="col-md-6 form-group">
  340. <label>No. of Floors <span class="text-danger">*</span></label>
  341. <input type="text" class="form-control noOfFloorsInput" id="noOfFloors${buildingId}" placeholder="Enter No. of Floors">
  342. </div>
  343. <div class="col-md-6 form-group">
  344. <label>Building Material <span class="text-danger">*</span></label>
  345. <select class="form-control" id="buildingMaterial${buildingId}">
  346. <option value="">Select Building Material</option>
  347. ${buildingMaterials}
  348. </select>
  349. </div>
  350. <div class="col-md-6 form-group">
  351. <label>Roof Material <span class="text-danger">*</span></label>
  352. <select class="form-control" id="roofMaterial${buildingId}">
  353. <option value="">Select Roof Material</option>
  354. ${roofMaterials}
  355. </select>
  356. </div>
  357. </div>
  358. <button type="button" class="btn btn-danger mt-2 remove-building">Remove</button>
  359. </div>
  360. `);
  361. assetContainer.append(buildingSection);
  362. }
  363. $(document).on('input', '.noOfFloorsInput', function () {
  364. const value = $(this).val().trim();
  365. // Accept only numbers, ++, or --
  366. const isValid = /^(\d+|\+\+|--)$/.test(value);
  367. if (!isValid) {
  368. $(this).val(value.slice(0, -1)); // Remove last character
  369. }
  370. });
  371. // Remove building section
  372. $(document).on('click', '.remove-building', function () {
  373. const section = $(this).closest('.building-section');
  374. const id = parseInt(section.data('id'));
  375. section.remove();
  376. buildings = buildings.filter(b => b !== id);
  377. //reorderBuildings();
  378. // Check if any building-section remains
  379. const remainingSections = $('.building-section');
  380. if (remainingSections.length === 0) {
  381. $('#buildingSave').hide();
  382. } else {
  383. $('#buildingSave').show();
  384. }
  385. });
  386. function reorderBuildings() {
  387. $('.building-section').each((i, el) => {
  388. const newId = i + 1;
  389. $(el).attr('data-id', newId);
  390. $(el).find('.building-title').text(`Building ${newId}`);
  391. });
  392. }
  393. function saveBuildings() {
  394. let buildingList = [];
  395. let allValid = true;
  396. $('.building-section').each((i, el) => {
  397. const section = $(el);
  398. const id = section.data('id');
  399. const name = section.find(`#buildingName${id}`).val()?.trim() || '';
  400. const floors = section.find(`#noOfFloors${id}`).val()?.toString().trim() || '';
  401. const material = section.find(`#buildingMaterial${id}`).val() || '';
  402. const roof = section.find(`#roofMaterial${id}`).val() || '';
  403. if (!name) {
  404. Swal.fire({
  405. icon: 'warning',
  406. title: `Building ${id}: Missing Field`,
  407. text: 'Please enter Building Name.',
  408. showConfirmButton: true,
  409. allowOutsideClick: false
  410. });
  411. allValid = false;
  412. return false;
  413. }
  414. if (!floors) {
  415. Swal.fire({
  416. icon: 'warning',
  417. title: `Building ${id}: Missing Field`,
  418. text: 'Please enter No. of Floors.',
  419. showConfirmButton: true,
  420. allowOutsideClick: false
  421. });
  422. allValid = false;
  423. return false;
  424. }
  425. if (!material) {
  426. Swal.fire({
  427. icon: 'warning',
  428. title: `Building ${id}: Missing Field`,
  429. text: 'Please select Building Material.',
  430. showConfirmButton: true,
  431. allowOutsideClick: false
  432. });
  433. allValid = false;
  434. return false;
  435. }
  436. if (!roof) {
  437. Swal.fire({
  438. icon: 'warning',
  439. title: `Building ${id}: Missing Field`,
  440. text: 'Please select Roof Material.',
  441. showConfirmButton: true,
  442. allowOutsideClick: false
  443. });
  444. allValid = false;
  445. return false;
  446. }
  447. buildingList.push({
  448. building_name: name,
  449. total_floor: floors,
  450. building_material: material,
  451. roof_material: roof,
  452. school_id: schoolId // You can dynamically fetch this if needed
  453. });
  454. });
  455. if (!allValid) return;
  456. // Submit valid data
  457. $.ajax({
  458. url: '/gcc/api/gccschool/buildingasset/saveBuildings',
  459. type: 'POST',
  460. contentType: 'application/json',
  461. data: JSON.stringify(buildingList),
  462. success: res => {
  463. console.log("Server Response:", res);
  464. if (res === 'Building Name Alredy Exist') {
  465. Swal.fire({
  466. icon: 'info',
  467. title: 'Already Exists',
  468. text: 'Building name already exists. Please enter a different name.',
  469. showConfirmButton: true,
  470. allowOutsideClick: false
  471. });
  472. } else if (res === 'Building Saved sucessfully') {
  473. Swal.fire({
  474. icon: 'success',
  475. title: 'Success',
  476. text: 'Building saved successfully!',
  477. showConfirmButton: true
  478. }).then(() => location.reload());
  479. } else {
  480. Swal.fire({
  481. icon: 'error',
  482. title: 'Error',
  483. text: 'Something went wrong. Please try again.',
  484. showConfirmButton: true
  485. });
  486. }
  487. }
  488. /*success: res => {
  489. console.log("dsgdgs");
  490. console.log(res);
  491. Swal.fire({
  492. icon: 'success',
  493. title: 'Success',
  494. text: res,
  495. showConfirmButton: true
  496. }).then(() => location.reload());
  497. },
  498. error: () => {
  499. Swal.fire({
  500. icon: 'error',
  501. title: 'Error',
  502. text: 'Failed to save buildings.',
  503. showConfirmButton: true
  504. });
  505. }*/
  506. });
  507. }
  508. $(document).on('input', '[id^="buildingName"]', function() {
  509. this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
  510. });
  511. var buttons = [
  512. {
  513. extend: 'pageLength',
  514. className: 'btn btn-warning'
  515. },
  516. {
  517. title: 'Data export',
  518. extend: 'copyHtml5',
  519. text: 'Copy',
  520. className: 'btn btn-secondary',
  521. id: 'copyBtn'
  522. },
  523. {
  524. extend: 'excelHtml5',
  525. text: 'Excel',
  526. className: 'btn btn-secondary',
  527. id: 'excelBtn',
  528. title: 'Petition List'
  529. },
  530. {
  531. extend: 'csvHtml5',
  532. text: 'CSV',
  533. className: 'btn btn-secondary',
  534. id: 'csvBtn',
  535. title: 'Petition List'
  536. },
  537. {
  538. extend: 'pdfHtml5',
  539. text: 'PDF',
  540. className: 'btn btn-secondary',
  541. id: 'pdfBtn',
  542. title: 'Petition List'
  543. },
  544. {
  545. extend: 'print',
  546. text: 'Print',
  547. className: 'btn btn-secondary',
  548. id: 'printBtn',
  549. title: 'Petition List',
  550. exportOptions: {
  551. columns: ':visible:not(.no-print)' // Exclude columns with class 'no-print' from print view
  552. }
  553. }
  554. ];
  555. function loadSavedBuilding(schoolId) {
  556. $.get('/gcc/api/gccschool/buildingasset/getSavedBuildings', {school_id: schoolId}, res => {
  557. //$('#lastBuildingId').text(res.lastId);
  558. if (res.status === "OK") {
  559. if ($.fn.DataTable.isDataTable('#CompletedCPSDatatable')) {
  560. $('#CompletedCPSDatatable').DataTable().clear().destroy(true);
  561. }
  562. $('#CompletedCPSDatatable').DataTable({
  563. data: res.data,
  564. columns: [
  565. {data: null, render: (d, t, r, m) => m.row + 1},
  566. {data: 'building_name'},
  567. {data: 'total_floor'},
  568. {data: 'building_material_name'},
  569. {data: 'roof_material_name'},
  570. {
  571. data: 'buildng_id',
  572. render: (data, t, row) => `
  573. <div class="text-center">
  574. <button class="btn btn-sm btn-primary view-btn" data-building-number="${data}" data-building-name="${row.building_name}"><i class="bi bi-eye"></i></button>
  575. <button class="btn btn-sm btn-success edit-btn" data-building-number="${data}" data-building-name="${row.building_name}"><i class="bi bi-pencil"></i></button>
  576. <button class="btn btn-sm btn-danger delete-btn" data-building-number="${data}" data-building-name="${row.building_name}" data-school-id="${row.school_id}">
  577. <i class="bi bi-trash"></i>
  578. </button>
  579. </div>`
  580. }
  581. ],
  582. lengthMenu: [
  583. [50, 100, 200, 500, -1],
  584. ['50 rows', '100 rows', '200 rows', '500 rows', 'Show all']
  585. ],
  586. dom: 'Bftip',
  587. autoWidth: false,
  588. //fixedColumns: {left: 0, right: 0},
  589. scrollY: '60vh',
  590. scrollX:true,
  591. fixedHeader:false,
  592. //scrollCollapse: true,
  593. buttons: [buttons],
  594. responsive: false,
  595. searching: true,
  596. select: false,
  597. processing: false,
  598. });
  599. } else {
  600. $('#CompletedCPSDatatable tbody').html('<tr><td colspan="6" class="text-center text-danger">No buildings found</td></tr>');
  601. }
  602. });
  603. }
  604. function handleView() {
  605. const buildingId = $(this).data('building-number');
  606. $('#assetModal').modal('show');
  607. $('#assetContainer').html('<p>Loading...</p>');
  608. $.get('/gcc/api/gccschool/buildingasset/getBuildingbyid', {building_id: buildingId}, res => {
  609. if (res.status === "OK") {
  610. const d = res.data;
  611. const viewHtml = `
  612. <div class="border p-3">
  613. <h5>Building Details</h5>
  614. <div class="row">
  615. <div class="col-md-6"><label>Building Name</label><input class="form-control" value="${d.building_name}" disabled></div>
  616. <div class="col-md-6"><label>Total Floors</label><input class="form-control" value="${d.total_floor}" disabled></div>
  617. <div class="col-md-6"><label>Building Material</label><input class="form-control" value="${d.buildingmaterial}" disabled></div>
  618. <div class="col-md-6"><label>Roof Material</label><input class="form-control" value="${d.roofmaterial}" disabled></div>
  619. </div>
  620. </div>
  621. `;
  622. $('#assetContainer').html(viewHtml);
  623. // ✅ Hide buttons only after content is loaded
  624. $('#addBuildingBtn').hide();
  625. $('#buildingSave').hide(); // hide Save button
  626. $('.modal-footer .btn-secondary').show(); // keep Close visible
  627. }
  628. });
  629. }
  630. function handleEdit(buildingId) {
  631. //const buildingId = $(this).data('building-number');
  632. $('#assetModal').modal('show');
  633. $('#addBuildingBtn').hide();
  634. $('#assetContainer').html('<p>Loading...</p>');
  635. $.get('/gcc/api/gccschool/buildingasset/getBuildingbyid', {building_id: buildingId}, res => {
  636. if (res.status === "OK") {
  637. const d = res.data;
  638. $('#EditSchoolId').val(d.school_id);
  639. console.log("d.school_id="+d.school_id);
  640. const html = `
  641. <div class="building-section border p-3" data-id="${buildingId}">
  642. <h5>Edit Building</h5>
  643. <div class="row mb-3">
  644. <div class="col-md-4">
  645. <label>Building Name</label>
  646. <input class="form-control" id="buildingName${buildingId}" value="${d.building_name}">
  647. </div>
  648. <div class="col-md-4">
  649. <label>Total Floors</label>
  650. <select class="form-control" id="noOfFloors${buildingId}" data-selected="${d.total_floor}" disabled></select>
  651. </div>
  652. <div class="col-md-4 d-flex align-items-end" style="gap: 1rem;">
  653. <button type="button" class="btn btn-success me-2" id="flooredit">+</button>
  654. <button type="button" class="btn btn-danger" id="floordelete">-</button>
  655. </div>
  656. </div>
  657. <div class="row">
  658. <div class="col-md-6">
  659. <label>Building Material</label>
  660. <select class="form-control" id="buildingMaterial${buildingId}">
  661. <option selected value="${d.buildingmaterial}">${d.buildingmaterial}</option>
  662. </select>
  663. </div>
  664. <div class="col-md-6">
  665. <label>Roof Material</label>
  666. <select class="form-control" id="roofMaterial${buildingId}">
  667. <option selected value="${d.roofmaterial}">${d.roofmaterial}</option>
  668. </select>
  669. </div>
  670. </div>
  671. </div>`;
  672. $('#assetContainer').html(html);
  673. $('.modal-footer').show();
  674. populateDropdowns(buildingId, d.building_material, d.roof_material);
  675. populatefloorDropdowns(buildingId);
  676. }
  677. setTimeout(() => {
  678. const totalFloors = getTotalFloors(buildingId);
  679. console.log(`Total number of floors in edit: ${totalFloors}`);
  680. // Optionally display it somewhere on the page/modal
  681. }, 300);
  682. });
  683. }
  684. $(document).on('input', '.noOfFloorsInput', function () {
  685. let value = parseInt($(this).val()) || 0;
  686. if (value > 9) {
  687. $(this).val(''); // Set back to 9
  688. Swal.fire({
  689. icon: 'warning',
  690. title: 'Limit Exceeded',
  691. text: 'You can enter a maximum of 9 floors only.',
  692. allowOutsideClick: false,
  693. allowEscapeKey: false,
  694. allowEnterKey: false,
  695. showConfirmButton: true
  696. });
  697. }
  698. });
  699. /*function populateDropdowns(buildingId, selectedBM, selectedRM) {
  700. $.get('/gcc/api/gccschool/dropdown/getBuildingMaterial', res => {
  701. if (res.status === "OK") {
  702. let options = res.data.map(item => `<option value="${item.bm_id}" ${item.bm_id == selectedBM ? 'selected' : ''}>${item.content}</option>`);
  703. $(`#buildingMaterial${buildingId}`).html(options);
  704. }
  705. });
  706. $.get('/gcc/api/gccschool/dropdown/getRoofMaterial', res => {
  707. if (res.status === "OK") {
  708. let options = res.data.map(item => `<option value="${item.rm_id}" ${item.rm_id == selectedRM ? 'selected' : ''}>${item.content}</option>`);
  709. $(`#roofMaterial${buildingId}`).html(options);
  710. }
  711. });
  712. }*/
  713. function populatefloorDropdowns(buildingId) {
  714. $.get('/gcc/api/gccschool/buildingasset/getFloorsByBuildingId', { buildingId }, res => {
  715. if (res.status === "OK") {
  716. const floorSelect = $(`#noOfFloors${buildingId}`);
  717. const selectedFloor = parseInt(floorSelect.data('selected'), 10);
  718. floorSelect.empty();
  719. res.floors.forEach(f => {
  720. //const floor = f.floor_name;
  721. const selected = f.floor_name === selectedFloor ? 'selected' : '';
  722. floorSelect.append(`<option value="${f.floors_id}" data-exe="${f.execution_order}" ${selected}>${f.floor_name}</option>`);
  723. });
  724. }
  725. });
  726. }
  727. function populateDropdowns(buildingId, selectedBuildingMaterialId, selectedRoofMaterialId) {
  728. $.get('/gcc/api/gccschool/buildingasset/getAllMaterials', res => {
  729. if (res.status === "OK") {
  730. const buildingMaterialSelect = $(`#buildingMaterial${buildingId}`);
  731. const roofMaterialSelect = $(`#roofMaterial${buildingId}`);
  732. buildingMaterialSelect.empty();
  733. roofMaterialSelect.empty();
  734. res.buildingMaterials.forEach(item => {
  735. const selected = item.execution_order === selectedBuildingMaterialId ? 'selected' : '';
  736. buildingMaterialSelect.append(`<option value="${item.execution_order}" ${selected}>${item.content}</option>`);
  737. });
  738. res.roofMaterials.forEach(item => {
  739. const selected = item.execution_order === selectedRoofMaterialId ? 'selected' : '';
  740. roofMaterialSelect.append(`<option value="${item.execution_order}" ${selected}>${item.content}</option>`);
  741. });
  742. }
  743. });
  744. }
  745. function getTotalFloors(buildingId) {
  746. const floorSelect = $(`#noOfFloors${buildingId}`);
  747. const totalFloors = floorSelect.find('option').length;
  748. return totalFloors;
  749. }
  750. //for update
  751. /* $(document).on('click', '#buildingupdate', function () {
  752. const buildingId = $('.building-section').data('id');
  753. const buildingName = $(`#buildingName${buildingId}`).val();
  754. const totalFloors = getTotalFloors(buildingId);
  755. // const totalFloor = $(`#noOfFloors${buildingId}`).data('selected');
  756. const buildingMaterial = $(`#buildingMaterial${buildingId}`).val();
  757. const roofMaterial = $(`#roofMaterial${buildingId}`).val();
  758. let edit_schoolId = $('#EditSchoolId').val();
  759. console.log("edit_schoolId="+edit_schoolId);
  760. //schoolId = schoolId; // Make sure you have a hidden input or dropdown with school ID
  761. console.log("buildingId",buildingId)
  762. console.log("buildingName",buildingName)
  763. //console.log("totalFloor",totalFloor)
  764. console.log("buildingMaterial",buildingMaterial)
  765. console.log("roofMaterial",roofMaterial)
  766. console.log("Total Floors:", totalFloors);
  767. // Optional: Input validation
  768. if (!buildingName || !totalFloors || !buildingMaterial || !roofMaterial ) {
  769. Swal.fire({
  770. icon: 'warning',
  771. title: 'Missing Fields',
  772. text: 'Please fill all the required fields before updating.'
  773. });
  774. return;
  775. }
  776. $.ajax({
  777. url: '/gcc/api/gccschool/buildingasset/updateBuilding',
  778. type: 'POST',
  779. data: {
  780. building_id: buildingId,
  781. building_name: buildingName,
  782. total_floor: totalFloors,
  783. building_material: buildingMaterial,
  784. roof_material: roofMaterial,
  785. school_id: edit_schoolId
  786. },
  787. success: function (res) {
  788. if (res.toLowerCase() === 'success') {
  789. Swal.fire({
  790. icon: 'success',
  791. title: 'Updated',
  792. text: 'Building updated successfully!'
  793. }).then(() => {
  794. location.reload(); // Refresh after success
  795. });
  796. } else {
  797. Swal.fire({
  798. icon: 'error',
  799. title: 'Error',
  800. text: 'Something went wrong while updating.'
  801. });
  802. }
  803. },
  804. error: function () {
  805. Swal.fire({
  806. icon: 'error',
  807. title: 'Error',
  808. text: 'Failed to communicate with server.'
  809. });
  810. }
  811. });
  812. }); */
  813. $(document).on('click', '#buildingupdate', function () {
  814. const buildingId = $('.building-section').data('id');
  815. const buildingName = $(`#buildingName${buildingId}`).val();
  816. const totalFloors = getTotalFloors(buildingId);
  817. const buildingMaterial = $(`#buildingMaterial${buildingId}`).val();
  818. const roofMaterial = $(`#roofMaterial${buildingId}`).val();
  819. const schoolId = $('#EditSchoolId').val();
  820. if (!buildingName || !totalFloors || !buildingMaterial || !roofMaterial) {
  821. Swal.fire('Missing Fields', 'Please fill all fields', 'warning');
  822. return;
  823. }
  824. // STEP 1: Update building basic info
  825. $.post('/gcc/api/gccschool/buildingasset/updateBuilding', {
  826. building_id: buildingId,
  827. building_name: buildingName,
  828. total_floor: totalFloors,
  829. building_material: buildingMaterial,
  830. roof_material: roofMaterial,
  831. school_id: schoolId
  832. }).done(function () {
  833. // STEP 2: Fetch existing floors
  834. $.get('/gcc/api/gccschool/buildingasset/getFloorsByBuildingId', {
  835. buildingId: buildingId
  836. }).done(function (res) {
  837. if (res.status === "OK") {
  838. const existingFloors = res.floors.length;
  839. if (totalFloors < existingFloors) {
  840. // STEP 3: Reduce floors
  841. const floorToRemove = res.floors[existingFloors - 1]; // Get the last floor
  842. $.post('/gcc/api/gccschool/buildingasset/editBuildings', {
  843. building_id: buildingId,
  844. floor_id: floorToRemove.floors_id
  845. });
  846. } else if (totalFloors > existingFloors) {
  847. // STEP 4: Add missing floors
  848. for (let i = existingFloors; i < totalFloors; i++) {
  849. $.post('/gcc/api/gccschool/buildingasset/addFloor', {
  850. building_id: buildingId,
  851. exe_order: i
  852. });
  853. }
  854. }
  855. Swal.fire({
  856. icon: 'success',
  857. title: 'Updated',
  858. text: 'Building updated successfully!',
  859. allowOutsideClick: false, // Disable clicking outside the modal to close it
  860. allowEscapeKey: false, // Disable escape key to close the modal
  861. allowEnterKey: false, // Disable enter key to close the modal
  862. showConfirmButton: true
  863. }).then(() => location.reload());
  864. }
  865. });
  866. }).fail(function () {
  867. Swal.fire('Error', 'Failed to update building.', 'error');
  868. });
  869. });
  870. //for add
  871. // Attach click listener to floorEditBtn
  872. /* $(document).on('click', '#flooredit', function () {
  873. console.log("hello floor add")
  874. const buildingId = $('.building-section').data('id');
  875. const floorSelect = $(`#noOfFloors${buildingId}`);
  876. const selectedOption = floorSelect.find('option:selected');
  877. const exe_order = selectedOption.attr('data-exe');
  878. Swal.fire({
  879. title: 'Are you sure?',
  880. text: `Do you want to update the floor with execution order ${exe_order}?`,
  881. icon: 'question',
  882. showCancelButton: true,
  883. confirmButtonColor: '#3085d6',
  884. cancelButtonColor: '#d33',
  885. confirmButtonText: 'Yes, update it!',
  886. cancelButtonText: 'Cancel'
  887. }).then((result) => {
  888. if (result.isConfirmed) {
  889. $.post('/gcc/api/gccschool/buildingasset/addFloor', {
  890. building_id: buildingId,
  891. exe_order: exe_order
  892. }, function (response) {
  893. Swal.fire(
  894. 'Updated!',
  895. 'The floor was updated successfully.',
  896. 'success'
  897. ).then(() => {
  898. // Optionally refresh the table or UI
  899. //reloadbuildingSection(); // OR refreshDataTable();
  900. // ✅ Reuse your existing function
  901. //handleEdit.call({ dataset: { buildingNumber: buildingId } });
  902. handleEdit(buildingId); // simple and clean
  903. });
  904. // Optionally, refresh data or do something else
  905. });
  906. } else {
  907. Swal.fire(
  908. 'Cancelled',
  909. 'Floor update was cancelled.',
  910. 'info'
  911. );
  912. }
  913. });
  914. }); */
  915. //Add floor
  916. /*$(document).on('click', '#flooredit', function () {
  917. const buildingId = $('.building-section').data('id');
  918. const totalFloors = $('#noOfFloors' + buildingId + ' option').length;
  919. $.post('/gcc/api/gccschool/buildingasset/addFloor', {
  920. building_id: buildingId,
  921. exe_order: totalFloors // execution order starts from 1, 2, 3...
  922. }, function (res) {
  923. if (res === 'Sucess') {
  924. Swal.fire('Success', 'Floor added successfully', 'success');
  925. populatefloorDropdowns(buildingId); // Reload dropdown
  926. } else {
  927. Swal.fire('Error', 'Failed to add floor', 'error');
  928. }
  929. });
  930. });*/
  931. $(document).on('click', '#flooredit', function () {
  932. const buildingId = $('.building-section').data('id');
  933. const totalFloors = $('#noOfFloors' + buildingId + ' option').length;
  934. Swal.fire({
  935. title: 'Are you sure?',
  936. text: `Do you want to add a new floor? Current floors: ${totalFloors}`,
  937. icon: 'question',
  938. showCancelButton: true,
  939. confirmButtonText: 'Yes, add it',
  940. cancelButtonText: 'Cancel', allowOutsideClick: false, // Disable clicking outside the modal to close it
  941. allowEscapeKey: false, // Disable escape key to close the modal
  942. allowEnterKey: false, // Disable enter key to close the modal
  943. showConfirmButton: true
  944. }).then((result) => {
  945. if (result.isConfirmed) {
  946. // User clicked "Yes"
  947. $.post('/gcc/api/gccschool/buildingasset/addFloor', {
  948. building_id: buildingId,
  949. exe_order: totalFloors
  950. }, function (res) {
  951. if (res === 'Sucess') {
  952. Swal.fire({
  953. title: 'Success!',
  954. text: 'Floor added successfully.',
  955. icon: 'success',
  956. allowOutsideClick: false, // Disable clicking outside the modal to close it
  957. allowEscapeKey: false, // Disable escape key to close the modal
  958. allowEnterKey: false, // Disable enter key to close the modal
  959. showConfirmButton: true
  960. });
  961. populatefloorDropdowns(buildingId); // reload dropdown
  962. } else {
  963. Swal.fire({
  964. title: 'Error!',
  965. text: 'Failed to add floor.',
  966. icon: 'error',
  967. allowOutsideClick: false, // Disable clicking outside the modal to close it
  968. allowEscapeKey: false, // Disable escape key to close the modal
  969. allowEnterKey: false, // Disable enter key to close the modal
  970. showConfirmButton: true
  971. });
  972. }
  973. });
  974. } else {
  975. Swal.fire({
  976. title: 'Cancelled',
  977. text: 'No changes were made.',
  978. icon: 'info',
  979. allowOutsideClick: false, // Disable clicking outside the modal to close it
  980. allowEscapeKey: false, // Disable escape key to close the modal
  981. allowEnterKey: false, // Disable enter key to close the modal
  982. showConfirmButton: true
  983. });
  984. }
  985. });
  986. });
  987. // Delete floor
  988. /* $(document).on('click', '#floordelete', function () {
  989. const buildingId = $('.building-section').data('id');
  990. const selectedFloorId = $('#noOfFloors' + buildingId).val();
  991. if (!selectedFloorId) {
  992. Swal.fire('Warning', 'No floor selected to delete', 'warning');
  993. return;
  994. }
  995. $.post('/gcc/api/gccschool/buildingasset/editBuildings', {
  996. building_id: buildingId,
  997. floor_id: selectedFloorId
  998. }, function (res) {
  999. if (res === 'Success') {
  1000. Swal.fire('Success', 'Floor deleted successfully', 'success');
  1001. populatefloorDropdowns(buildingId); // Reload dropdown
  1002. } else {
  1003. Swal.fire('Error', 'Failed to delete floor', 'error');
  1004. }
  1005. });
  1006. }); */
  1007. /* $(document).on('click', '#floordelete', function () {
  1008. const buildingId = $('.building-section').data('id');
  1009. const selectedFloorId = $('#noOfFloors' + buildingId).val();
  1010. if (!selectedFloorId) {
  1011. Swal.fire('Warning', 'No floor selected to delete', 'warning');
  1012. return;
  1013. }
  1014. $.post('/gcc/api/gccschool/buildingasset/editBuildings', {
  1015. building_id: buildingId,
  1016. floor_id: selectedFloorId
  1017. }, function (res) {
  1018. if (res === 'Success') {
  1019. Swal.fire('Success', 'Floor deleted successfully', 'success');
  1020. populatefloorDropdowns(buildingId); // Reload dropdown
  1021. } else {
  1022. Swal.fire('Error', 'Failed to delete floor', 'error');
  1023. }
  1024. });
  1025. });
  1026. */
  1027. /*$(document).on('click', '#flooredit', function () {
  1028. const buildingId = $('.building-section').data('id');
  1029. const floorSelect = $(#noOfFloors${buildingId});
  1030. const selectedOption = floorSelect.find('option:selected');
  1031. const exe_order = selectedOption.attr('data-exe');
  1032. console.log("exe_order",exe_order);
  1033. $.post('/gcc/api/gccschool/buildingasset/addFloor', {
  1034. building_id: buildingId,
  1035. exe_order: exe_order
  1036. }, function (response) {
  1037. //console.log('Floor add/edit response:', response);
  1038. //alert('Floor updated successfully!');
  1039. // optionally refresh something or update UI
  1040. });
  1041. });*/
  1042. // Click handler for the "Edit Floor" button
  1043. // for delete
  1044. /*$(document).on('click', '#floordelete', function () {
  1045. const buildingId = $('.building-section').data('id');
  1046. const floorId = $(`#noOfFloors${buildingId}`).val();
  1047. console.log(floorId)
  1048. $.ajax({
  1049. url: '/gcc/api/gccschool/buildingasset/editBuildings', // adjust your controller path
  1050. method: 'POST',
  1051. data: {
  1052. building_id: buildingId,
  1053. floor_id: floorId
  1054. },
  1055. success: function (response) {
  1056. Swal.fire({
  1057. icon: 'success',
  1058. title: 'Success',
  1059. text: response.message || "Floor edited successfully"
  1060. });
  1061. },
  1062. error: function () {
  1063. Swal.fire({
  1064. icon: 'error',
  1065. title: 'Error',
  1066. text: 'Could not edit the floor.'
  1067. });
  1068. }
  1069. });
  1070. });*/
  1071. /* $(document).on('click', '#floordelete', function () {
  1072. console.log("hello floor delete")
  1073. const buildingId = $('.building-section').data('id');
  1074. const floorSelect = $(`#noOfFloors${buildingId}`);
  1075. const floorId = floorSelect.val();
  1076. //const floorName = floorSelect.find('option:selected').text();
  1077. if (!floorId) {
  1078. Swal.fire({
  1079. icon: 'warning',
  1080. title: 'No Floor Selected',
  1081. text: 'Please select a floor to delete or edit.'
  1082. });
  1083. return;
  1084. }
  1085. Swal.fire({
  1086. title: 'Are you sure?',
  1087. text: `Do you really want to delete `,
  1088. icon: 'warning',
  1089. showCancelButton: true,
  1090. confirmButtonColor: '#d33',
  1091. cancelButtonColor: '#3085d6',
  1092. confirmButtonText: 'Yes, proceed',
  1093. cancelButtonText: 'Cancel'
  1094. }).then((result) => {
  1095. if (result.isConfirmed) {
  1096. $.ajax({
  1097. url: '/gcc/api/gccschool/buildingasset/editBuildings', // update as needed /updateBuilding
  1098. method: 'POST',
  1099. data: {
  1100. building_id: buildingId,
  1101. floors_id: floorId
  1102. },
  1103. success: function (response) {
  1104. Swal.fire({
  1105. icon: 'success',
  1106. title: 'Success',
  1107. text: response.message || "Floor updated successfully"
  1108. }).then(() => {
  1109. // Optionally refresh the table or UI
  1110. //reloadbuildingSection(); // OR refreshDataTable();
  1111. //location.reload();
  1112. handleEdit(buildingId);
  1113. });
  1114. // Optionally refresh dropdown or UI
  1115. },
  1116. error: function () {
  1117. Swal.fire({
  1118. icon: 'error',
  1119. title: 'Error',
  1120. text: 'Could not update the floor.'
  1121. });
  1122. }
  1123. });
  1124. } else {
  1125. Swal.fire(
  1126. 'Cancelled',
  1127. 'No changes were made.',
  1128. 'info'
  1129. );
  1130. }
  1131. });
  1132. });
  1133. */
  1134. $(document).on('click', '#floordelete', function () {
  1135. const buildingId = $('.building-section').data('id');
  1136. const floorSelect = $(`#noOfFloors${buildingId}`);
  1137. const floorId = floorSelect.val();
  1138. //const floorName = floorSelect.find('option:selected').text();
  1139. if (!floorId) {
  1140. Swal.fire({
  1141. icon: 'warning',
  1142. title: 'No Floor Selected',
  1143. text: 'Please select a floor to delete or edit.'
  1144. });
  1145. return;
  1146. }
  1147. Swal.fire({
  1148. title: 'Are you sure?',
  1149. text: `Do you really want to delete `,
  1150. icon: 'warning',
  1151. showCancelButton: true,
  1152. confirmButtonColor: '#d33',
  1153. cancelButtonColor: '#3085d6',
  1154. confirmButtonText: 'Yes, proceed',
  1155. cancelButtonText: 'Cancel',
  1156. allowOutsideClick: false, // Disable clicking outside the modal to close it
  1157. allowEscapeKey: false, // Disable escape key to close the modal
  1158. allowEnterKey: false, // Disable enter key to close the modal
  1159. showConfirmButton: true
  1160. }).then((result) => {
  1161. if (result.isConfirmed) {
  1162. $.ajax({
  1163. url: '/gcc/api/gccschool/buildingasset/editBuildings', // update as needed
  1164. method: 'POST',
  1165. data: {
  1166. building_id: buildingId,
  1167. floor_id: floorId
  1168. },
  1169. success: function (response) {
  1170. Swal.fire({
  1171. icon: 'success',
  1172. title: 'Success',
  1173. text: response.message || "Floor updated successfully"
  1174. }).then(() => {
  1175. // Optionally refresh the table or UI
  1176. //reloadbuildingSection(); // OR refreshDataTable();
  1177. //location.reload();
  1178. handleEdit(buildingId);
  1179. });
  1180. // Optionally refresh dropdown or UI
  1181. },
  1182. error: function () {
  1183. Swal.fire({
  1184. icon: 'error',
  1185. title: 'Error',
  1186. text: 'Could not update the floor.',
  1187. allowOutsideClick: false, // Disable clicking outside the modal to close it
  1188. allowEscapeKey: false, // Disable escape key to close the modal
  1189. allowEnterKey: false, // Disable enter key to close the modal
  1190. showConfirmButton: true
  1191. });
  1192. }
  1193. });
  1194. } else {
  1195. Swal.fire({
  1196. title: 'Cancelled',
  1197. text: 'No changes were made.',
  1198. icon: 'info',
  1199. confirmButtonText: 'OK',
  1200. allowOutsideClick: false, // Disable clicking outside the modal to close it
  1201. allowEscapeKey: false, // Disable escape key to close the modal
  1202. allowEnterKey: false, // Disable enter key to close the modal
  1203. showConfirmButton: true
  1204. });
  1205. }
  1206. });
  1207. });
  1208. // this delete is for edit modal
  1209. // Use event delegation
  1210. $(document).on('click', '#buildingDelete', function () {
  1211. const buildingId = $('.building-section').data('id');
  1212. const selectedFloor = $(`#noOfFloors${buildingId}`).val();
  1213. const highestFloor = $(`#noOfFloors${buildingId} option:first`).val(); // Gets first option (i.e., highest)
  1214. console.log("selectedFloor", selectedFloor);
  1215. console.log("highestFloor", highestFloor);
  1216. console.log("buildingId", buildingId);
  1217. if (selectedFloor !== highestFloor) {
  1218. Swal.fire({
  1219. icon: 'warning',
  1220. title: 'Not Selected Highest Floor',
  1221. text: `Please select the highest floor (${highestFloor}) to delete this building!`,
  1222. confirmButtonText: 'OK'
  1223. });
  1224. return;
  1225. }
  1226. if (confirm("Are you sure you want to delete this building?")) {
  1227. // deleteBuilding(buildingId); // your actual delete logic
  1228. }
  1229. });
  1230. function deleteBuilding(buildingId) {
  1231. const isDelete = true;
  1232. console.log("buildingId",buildingId);
  1233. $.ajax({
  1234. url: '/gcc/api/gccschool/buildingasset/', // your delete endpoint
  1235. type: 'POST',
  1236. data: {
  1237. building_id: buildingId,
  1238. is_delete: isDelete
  1239. },
  1240. success: function (res) {
  1241. if (res.status === "OK") {
  1242. alert("Building deleted successfully.");
  1243. // $('#assetModal').modal('hide');
  1244. // Refresh your building list here
  1245. } else {
  1246. alert("Failed to delete building.");
  1247. }
  1248. },
  1249. error: function () {
  1250. alert("Error occurred while deleting the building.");
  1251. }
  1252. });
  1253. }
  1254. // this delete is for whole building
  1255. /*$(document).on('click', '.delete-btn', function () {
  1256. const buildingId = $(this).data('building-number');
  1257. const buildingName = $(this).data('building-name');
  1258. const isDelete = true;// or false if you're toggling
  1259. const school_id = $(this).data('school-id');
  1260. console.log("buildingId",buildingId);
  1261. Swal.fire({
  1262. title: `Delete ${buildingName}?`,
  1263. text: "Are you sure you want to delete this building?",
  1264. icon: 'warning',
  1265. showCancelButton: true,
  1266. confirmButtonText: 'Yes, Delete',
  1267. cancelButtonText: 'Cancel',
  1268. allowOutsideClick: false, // Disable clicking outside the modal to close it
  1269. allowEscapeKey: false, // Disable escape key to close the modal
  1270. allowEnterKey: false, // Disable enter key to close the modal
  1271. showConfirmButton: true
  1272. }).then((result) => {
  1273. if (result.isConfirmed) {
  1274. $.ajax({
  1275. url: '/gcc/api/gccschool/buildingasset/deleteBuilding',
  1276. method: 'POST',
  1277. data: {
  1278. building_id: buildingId,
  1279. is_delete: isDelete
  1280. },
  1281. success: function (response) {
  1282. Swal.fire({
  1283. icon: 'success',
  1284. title: 'Deleted!',
  1285. text: response,
  1286. allowOutsideClick: false, // Disable clicking outside the modal to close it
  1287. allowEscapeKey: false, // Disable escape key to close the modal
  1288. allowEnterKey: false, // Disable enter key to close the modal
  1289. showConfirmButton: true
  1290. }).then(() => {
  1291. // Optionally refresh the table or UI
  1292. //reloadbuildingSection(school_id); // OR refreshDataTable();
  1293. location.reload();
  1294. //loadSavedBuilding(school_id);
  1295. });
  1296. },
  1297. error: function () {
  1298. Swal.fire({
  1299. icon: 'error',
  1300. title: 'Error',
  1301. text: 'Failed to delete building.',
  1302. confirmButtonText: 'OK'
  1303. });
  1304. }
  1305. });
  1306. }
  1307. });
  1308. });*/
  1309. $(document).on('click', '.delete-btn', function () {
  1310. const buildingId = $(this).data('building-number');
  1311. const buildingName = $(this).data('building-name');
  1312. const isDelete = true;
  1313. const school_id = $(this).data('school-id');
  1314. Swal.fire({
  1315. title: `Delete ${buildingName}?`,
  1316. text: "Are you sure you want to delete this building?",
  1317. icon: 'warning',
  1318. showCancelButton: true,
  1319. confirmButtonText: 'Yes, Delete',
  1320. cancelButtonText: 'Cancel',
  1321. allowOutsideClick: false,
  1322. allowEscapeKey: false,
  1323. allowEnterKey: false,
  1324. showConfirmButton: true
  1325. }).then((result) => {
  1326. if (result.isConfirmed) {
  1327. $.ajax({
  1328. url: '/gcc/api/gccschool/buildingasset/deleteBuilding',
  1329. method: 'POST',
  1330. data: {
  1331. building_id: buildingId,
  1332. is_delete: isDelete
  1333. },
  1334. success: function (response) {
  1335. Swal.fire({
  1336. icon: 'success',
  1337. title: 'Deleted!',
  1338. text: response,
  1339. allowOutsideClick: false,
  1340. allowEscapeKey: false,
  1341. allowEnterKey: false,
  1342. showConfirmButton: true
  1343. }).then(() => {
  1344. location.reload();
  1345. });
  1346. },
  1347. error: function () {
  1348. Swal.fire({
  1349. icon: 'error',
  1350. title: 'Error',
  1351. text: 'Failed to delete building.',
  1352. confirmButtonText: 'OK',
  1353. allowOutsideClick: false,
  1354. allowEscapeKey: false,
  1355. allowEnterKey: false,
  1356. showConfirmButton: true
  1357. }).then(() => {
  1358. });
  1359. }
  1360. });
  1361. } else if (result.dismiss === Swal.DismissReason.cancel) {
  1362. Swal.fire({
  1363. title: 'Cancelled',
  1364. text: 'No changes were made.',
  1365. icon: 'info',
  1366. confirmButtonText: 'OK',
  1367. allowOutsideClick: false,
  1368. allowEscapeKey: false,
  1369. allowEnterKey: false,
  1370. showConfirmButton: true
  1371. }).then(() => {
  1372. });
  1373. }
  1374. });
  1375. });
  1376. </script>
  1377. </html>