Compile function NOTE_PTRS_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NOTE_PTRS

DECLARE SUBROUTINE Set_Property, Set_Property, obj_Notes, obj_Appwindow, Notes_Services
DECLARE SUBROUTINE obj_Note_Ptrs, ErrMsg, Send_Event, obj_Tables, Start_Window, Database_Services
DECLARE FUNCTION Get_Property, Get_Status, Popup, Dialog_Box, obj_Tables, NextKey, Notes_Services


$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT APPCOLORS
$INSERT NOTES_EQU
$INSERT NOTE_PTRS_EQU

GoToEvent Event for CtrlEntId else
	// Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)
	
	EventFlow = 1
	
End Event

Event PUB_MARK_ALL_READ.CLICK()
	
	Notes_Services('MarkAllAsRead', @User4)
	GoSub Refresh
	
end event

Event PUB_ARCHIVE.CLICK()
	
	If Get_Property( @WINDOW: '.NOTE_DATA', 'SELPOS')<2> NE 0 then
		//debug
		CurrRow = ''
		NoteDataList = Get_Property(@WINDOW:'.NOTE_DATA','LIST')
		CurrRow = Get_Property(@WINDOW:'.NOTE_DATA','SELPOS')<2>
		
		CONVERT @VM TO @SVM IN NoteDataList
		CONVERT @FM TO @VM IN NoteDataList

		npParms = 'NOTE_PTRS':@RM:@USER4
		UserRec = obj_Tables('ReadRec',npParms)
		NoteID = NoteDataList<1, CurrRow, 7>
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
			RETURN
		END
		
		LOCATE NoteID IN UserRec<NOTE_PTRS_NOTE_IDS$> USING @VM SETTING Fpos THEN
			NewUserRec = UserRec
			NewUserRec<NOTE_PTRS_ARCHIVED$, FPos> = True$
			Database_Services('WriteDataRow', 'NOTE_PTRS', @USER4, NewUserRec, True$, '', '')
		END
	end
	GOSUB Refresh
	
end event

Event PUB_UNARCHIVE.CLICK()

	If Get_Property( @WINDOW: '.NOTE_DATA2', 'SELPOS')<2> NE 0 then
		//debug
		CurrRow = ''
		NoteDataList = Get_Property(@WINDOW:'.NOTE_DATA2','LIST')
		CurrRow = Get_Property(@WINDOW:'.NOTE_DATA2','SELPOS')<2>
		
		CONVERT @VM TO @SVM IN NoteDataList
		CONVERT @FM TO @VM IN NoteDataList

		npParms = 'NOTE_PTRS':@RM:@USER4
		UserRec = obj_Tables('ReadRec',npParms)
		NoteID = NoteDataList<1, CurrRow, 7>
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
			RETURN
		END
		
		LOCATE NoteID IN UserRec<NOTE_PTRS_NOTE_IDS$> USING @VM SETTING Fpos THEN
			NewUserRec = UserRec
			NewUserRec<NOTE_PTRS_ARCHIVED$, FPos> = False$
			Database_Services('WriteDataRow', 'NOTE_PTRS', @USER4, NewUserRec, True$, '', '')
		END
	end
	GOSUB Refresh
	
end event

Event PUB_GOTO_ARCHIVE.CLICK()
	
	Send_Event(@WINDOW,'PAGE',2)
	Gosub Refresh
	
end event

Event PUB_GOTO_INBOX.CLICK()
	
	Send_Event(@WINDOW,'PAGE',1)
	Gosub Refresh
	
end event

//-----------------------------------------------------------------------------
// INTERNAL GO-SUBS
//-----------------------------------------------------------------------------


* * * * * * *
Refresh:
* * * * * * *

	PrevInbox = Get_Property( @WINDOW: '.NOTE_DATA', 'SELPOS')<2>
	PrevArchive = Get_Property( @WINDOW: '.NOTE_DATA2', 'SELPOS')<2>
	
	InboxList = Notes_Services('GetInboxMessages', @USER4)
	CurrInbox = Get_Property(  @WINDOW:'.NOTE_DATA', 'ARRAY')
	If InboxList NE CurrInbox then
		Set_Property( @WINDOW:'.NOTE_DATA', 'ARRAY', InboxList)
		If PrevInbox NE 0 then Set_Property( @WINDOW: '.NOTE_DATA', 'SELPOS', PrevInbox)
	end
	
	ArchiveList = Notes_Services('GetArchivedMessages', @USER4)
	CurrArchive = Get_Property(  @WINDOW:'.NOTE_DATA2', 'ARRAY')
	If ArchiveList NE CurrArchive then
		Set_Property( @WINDOW:'.NOTE_DATA2', 'ARRAY', ArchiveList)
		If PrevArchive NE 0 then Set_Property( @WINDOW: '.NOTE_DATA2', 'SELPOS', PrevArchive)
	end
	
RETURN




