$(document).ready(function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), 'Accept': 'application/json' } }); const phoneInstances = {}; function initPhoneInput(selector) { const input = document.querySelector(selector); if (!input) return null; // If it was already initialized, don't do it again if (phoneInstances[selector]) return phoneInstances[selector]; phoneInstances[selector] = window.intlTelInput(input, { initialCountry: "gh", onlyCountries: ["gh"], nationalMode: true, // ALLOW users to type 024... natively autoPlaceholder: "polite", utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/utils.js" }); // Auto-format on blur using the plugin's built-in engine input.addEventListener("blur", function () { if (phoneInstances[selector].isValidNumber()) { // Reformats to local standard (e.g., 024 123 4567) for UI phoneInstances[selector].setNumber(phoneInstances[selector].getNumber()); } }); return phoneInstances[selector]; } // Initialize inputs when their respective modals open $('#editUserModal').on('shown.bs.modal', () => initPhoneInput("#editPhone")); $('#addUserModal').on('shown.bs.modal', () => initPhoneInput("#addUserphone")); $('#editAllowedApps, #inputPermissions').select2({ dropdownParent: $('#editUserModal'), placeholder: "Select options, multiple allowed" }); $('#allowedApps').select2({ dropdownParent: $('#addUserModal'), placeholder: "Select options, multiple allowed" }); const optionsMap = { district_user: [ "PPD Head", "Works Head", "MIS", "PPD Staff", "Works Staff", "Devt Planning Officer", "MCE", "DCE", "Urban Roads Department Head", "District Fire Officer", "District Disaster Prevention Department", "Head of District Health Department", "Representative of the Lands Commission", "Representative of the Environmental Protection Agency", "Rep from Traditional Council", "Chairman of the Works Sub Committee", "Chairman of Development Sub Planning Committee", "Nominated Elected Assembly Memebers" ], national_luspa: ["Director", "IT Head", "Staff"], regional_luspa: ["Director", "Staff"] }; function populatePositions(userType, targetDropdown) { targetDropdown.empty().append(''); if (optionsMap[userType]) { $.each(optionsMap[userType], function(index, value) { targetDropdown.append($("").attr("value", value.toLowerCase()).text(value)); }); } } let initialUserType = $("input[name='user_type']:checked").val(); if(initialUserType) populatePositions(initialUserType, $("#uaPostionAdd")); // Handle Radio Changes $("input[name='user_type']").change(function() { let userType = $(this).val(); let isDistrict = (userType === 'district_user'); let isRegional = (userType === 'regional_luspa'); $('#regionID, #editRegionID').prop('disabled', !isDistrict && !isRegional); $('#districtID, #editdistrictID').prop('disabled', !isDistrict); populatePositions(userType, $("#uaPostionAdd")); }); $('.regionIDD').change(function() { let region_id = $(this).val(); let $districtSelect = $('.districtIDD'); $districtSelect.empty().append(''); if(!region_id) return; $.ajax({ url: base_url + '/admin/districts/' + region_id, type: 'GET', success: function(data) { if(data.districts) { $.each(data.districts, function(id, row) { $districtSelect.append($("