This portal only for GCC Officers. Based on there roles and Group ID they can view there module`s Like PGR, QR Public Asset Feedback, Makkalai Thedi Mayor,etc...
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.

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