@@ -4,6 +4,11 @@ var dbUser;
44var dbVehicles ;
55var socket ;
66
7+ // Status Codes Global Variables
8+ var statusCodesCache = [ ] ;
9+ var statusCodeMap = { } ;
10+ var filteredStatusCodes = [ ] ;
11+
712// Hide modal utility function (global scope)
813function hideModal ( modalId ) {
914 const $modal = $ ( `#${ modalId } ` ) ;
@@ -167,22 +172,32 @@ $(function () {
167172 $ ( document ) . ready ( function ( ) {
168173 window . forceModalCleanup ( ) ;
169174
170- // Simple modal functions like modern dashboard
171- window . openNewVehicleModal = function ( ) {
172- $ ( '#newVehicleModal' ) . modal ( 'show' ) ;
173- } ;
174-
175- window . closeNewVehicleModal = function ( ) {
176- $ ( '#newVehicleModal' ) . modal ( 'hide' ) ;
177- } ;
178-
179- window . openNewPersonaModal = function ( ) {
180- $ ( '#newCivModal' ) . modal ( 'show' ) ;
181- } ;
182-
183- window . closeNewPersonaModal = function ( ) {
184- $ ( '#newCivModal' ) . modal ( 'hide' ) ;
185- } ;
175+ // Simple modal functions like modern dashboard
176+ window . openNewVehicleModal = function ( ) {
177+ $ ( '#newVehicleModal' ) . modal ( 'show' ) ;
178+ } ;
179+
180+ window . closeNewVehicleModal = function ( ) {
181+ $ ( '#newVehicleModal' ) . modal ( 'hide' ) ;
182+ } ;
183+
184+ window . openNewPersonaModal = function ( ) {
185+ $ ( '#newCivModal' ) . modal ( 'show' ) ;
186+ } ;
187+
188+ window . closeNewPersonaModal = function ( ) {
189+ $ ( '#newCivModal' ) . modal ( 'hide' ) ;
190+ } ;
191+
192+ // Status Codes Modal Functions
193+ window . openStatusCodesModal = function ( ) {
194+ loadStatusCodes ( ) ;
195+ $ ( '#statusCodesModal' ) . modal ( 'show' ) ;
196+ } ;
197+
198+ window . closeStatusCodesModal = function ( ) {
199+ $ ( '#statusCodesModal' ) . modal ( 'hide' ) ;
200+ } ;
186201 } ) ;
187202
188203 // Form validation and popover handling
@@ -4667,7 +4682,181 @@ function handleDeleteCallNote(noteId) {
46674682 } ,
46684683 error : function ( xhr ) {
46694684 console . error ( 'Error deleting call note:' , xhr . responseText ) ;
4670- showToast ( 'Failed to delete note: ' + ( xhr . responseJSON ?. message || 'Unknown error' ) , 'danger' ) ;
4685+ showToast ( 'Failed to delete note: ' + ( xhr . responseJSON ?. message || 'Unknown error' ) , 'danger' ) ;
4686+ }
4687+ } ) ;
4688+ }
4689+
4690+ // ===== STATUS CODES FUNCTIONALITY =====
4691+
4692+ // Load status codes from API
4693+ function loadStatusCodes ( ) {
4694+ const communityId = dbUser . user ?. lastAccessedCommunity ?. communityID ;
4695+ if ( ! communityId ) {
4696+ $ ( '#noStatusCodes' ) . text ( 'Please join a community to view status codes.' ) . show ( ) ;
4697+ $ ( '#statusCodesContent' ) . hide ( ) ;
4698+ return ;
4699+ }
4700+
4701+ if ( statusCodesCache . length > 0 ) {
4702+ displayStatusCodes ( statusCodesCache ) ;
4703+ return ;
4704+ }
4705+
4706+ $ . ajax ( {
4707+ url : `${ POLICE_CAD_API_URL } /api/v1/community/${ communityId } ` ,
4708+ method : 'GET' ,
4709+ headers : {
4710+ 'Authorization' : `Bearer ${ dbUser . token || '' } `
4711+ } ,
4712+ success : function ( data ) {
4713+ statusCodesCache = data ?. community ?. tenCodes || [ ] ;
4714+
4715+ // Build statusCodeMap
4716+ statusCodeMap = { } ;
4717+ statusCodesCache . forEach ( tc => {
4718+ statusCodeMap [ tc . _id ] = tc . code ;
4719+ } ) ;
4720+
4721+ filteredStatusCodes = statusCodesCache ;
4722+
4723+ if ( statusCodesCache . length === 0 ) {
4724+ $ ( '#noStatusCodes' ) . show ( ) ;
4725+ $ ( '#statusCodesContent' ) . hide ( ) ;
4726+ } else {
4727+ $ ( '#noStatusCodes' ) . hide ( ) ;
4728+ $ ( '#statusCodesContent' ) . show ( ) ;
4729+ displayStatusCodes ( statusCodesCache ) ;
4730+ }
4731+
4732+ // Load current status after statusCodesCache is populated
4733+ loadCurrentStatus ( ) ;
4734+ } ,
4735+ error : function ( xhr ) {
4736+ console . error ( 'Error loading status codes:' , xhr . responseText ) ;
4737+ $ ( '#noStatusCodes' ) . text ( 'Failed to load status codes: ' + ( xhr . responseJSON ?. message || 'Unknown error' ) ) . show ( ) ;
4738+ $ ( '#statusCodesContent' ) . hide ( ) ;
4739+ }
4740+ } ) ;
4741+ }
4742+
4743+ // Display status codes in modern card grid
4744+ function displayStatusCodes ( codes ) {
4745+ const codesToDisplay = filteredStatusCodes . length > 0 ? filteredStatusCodes : codes ;
4746+
4747+ $ ( '#statusCodesGrid' ) . empty ( ) ;
4748+
4749+ codesToDisplay . forEach ( code => {
4750+ const codeHtml = `
4751+ <div class="heroui-code-card" onclick="selectStatusCode('${ code . _id } ')">
4752+ <div class="heroui-code-number">${ code . code } </div>
4753+ <div class="heroui-code-description">${ code . description } </div>
4754+ </div>
4755+ ` ;
4756+ $ ( '#statusCodesGrid' ) . append ( codeHtml ) ;
4757+ } ) ;
4758+ }
4759+
4760+ // Handle status code selection
4761+ function selectStatusCode ( statusCodeID ) {
4762+ if ( ! statusCodeID ) {
4763+ console . error ( 'Invalid statusCodeID:' , statusCodeID ) ;
4764+ showToast ( 'Invalid status code selected.' , 'danger' ) ;
4765+ return ;
4766+ }
4767+
4768+ // Verify status code in cache
4769+ const statusCode = statusCodesCache . find ( sc => sc . _id === statusCodeID ) ;
4770+ if ( ! statusCode ) {
4771+ console . error ( 'Status code not found in cache:' , statusCodeID ) ;
4772+ showToast ( 'Selected status code not found.' , 'danger' ) ;
4773+ return ;
4774+ }
4775+
4776+ updateStatus ( statusCodeID ) ;
4777+ $ ( '#statusCodesModal' ) . modal ( 'hide' ) ;
4778+ }
4779+
4780+ // Update user status
4781+ function updateStatus ( statusCode ) {
4782+ const userId = dbUser . _id ;
4783+ const communityId = dbUser . user ?. lastAccessedCommunity ?. communityID ;
4784+ const data = {
4785+ departmentID : dbUser . user ?. lastAccessedCommunity ?. departmentID ,
4786+ tenCodeID : statusCode
4787+ } ;
4788+
4789+ $ . ajax ( {
4790+ url : `${ POLICE_CAD_API_URL } /api/v1/community/${ communityId } /members/${ userId } /tenCode` ,
4791+ method : 'PUT' ,
4792+ data : JSON . stringify ( data ) ,
4793+ contentType : 'application/json' ,
4794+ headers : {
4795+ 'Authorization' : `Bearer ${ dbUser . token || '' } `
4796+ } ,
4797+ success : function ( res ) {
4798+ const status = statusCodeMap [ statusCode ] || 'Unknown' ;
4799+ $ ( '#currentStatus' ) . text ( status ) ;
4800+ $ ( '#statusSetBy' ) . text ( `Set By: ${ dbUser . user . username } ` ) ;
4801+ showToast ( `Status updated to: ${ status } ` , 'success' ) ;
4802+ } ,
4803+ error : function ( xhr ) {
4804+ console . error ( 'Error updating status:' , xhr . responseText ) ;
4805+ showToast ( 'Failed to update status: ' + ( xhr . responseJSON ?. message || 'Unknown error' ) , 'danger' ) ;
46714806 }
46724807 } ) ;
4673- }
4808+ }
4809+
4810+ // Load current status
4811+ function loadCurrentStatus ( ) {
4812+ const userId = dbUser . _id ;
4813+ const communityId = dbUser . user ?. lastAccessedCommunity ?. communityID ;
4814+
4815+ $ . ajax ( {
4816+ url : `${ POLICE_CAD_API_URL } /api/v1/community/${ communityId } ` ,
4817+ method : 'GET' ,
4818+ headers : {
4819+ 'Authorization' : `Bearer ${ dbUser . token || '' } `
4820+ } ,
4821+ success : function ( data ) {
4822+ const tenCodeID = data ?. community ?. members [ userId ] ?. tenCodeID ;
4823+ if ( tenCodeID && statusCodeMap [ tenCodeID ] ) {
4824+ $ ( '#currentStatus' ) . text ( statusCodeMap [ tenCodeID ] ) ;
4825+ $ ( '#statusSetBy' ) . text ( `Set By: ${ dbUser . user . username } ` ) ;
4826+ } else {
4827+ $ ( '#currentStatus' ) . text ( 'None' ) ;
4828+ $ ( '#statusSetBy' ) . text ( 'Set By: None' ) ;
4829+ }
4830+ } ,
4831+ error : function ( xhr ) {
4832+ console . error ( 'Error loading current status:' , xhr . responseText ) ;
4833+ $ ( '#currentStatus' ) . text ( 'Unknown' ) ;
4834+ $ ( '#statusSetBy' ) . text ( 'Set By: None' ) ;
4835+ }
4836+ } ) ;
4837+ }
4838+
4839+ // Initialize status codes functionality
4840+ $ ( document ) . ready ( function ( ) {
4841+ // Load status codes and current status on page load
4842+ if ( dbUser && dbUser . user ?. lastAccessedCommunity ) {
4843+ // Load status codes first, then current status
4844+ loadStatusCodes ( ) ;
4845+ }
4846+
4847+ // Search functionality
4848+ $ ( '#statusCodeSearch' ) . on ( 'input' , function ( ) {
4849+ const searchTerm = $ ( this ) . val ( ) . toLowerCase ( ) ;
4850+
4851+ if ( searchTerm === '' ) {
4852+ filteredStatusCodes = statusCodesCache ;
4853+ } else {
4854+ filteredStatusCodes = statusCodesCache . filter ( code =>
4855+ code . code . toLowerCase ( ) . includes ( searchTerm ) ||
4856+ code . description . toLowerCase ( ) . includes ( searchTerm )
4857+ ) ;
4858+ }
4859+
4860+ displayStatusCodes ( filteredStatusCodes ) ;
4861+ } ) ;
4862+ } ) ;
0 commit comments