Compile function REACTOR_LOG_EVENTS(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   Reactor_Log_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        10/24/22    djs     Added a RefreshForm GoSub to prevent comments from being lost upon
                            form refresh events.
        09/29/22    djs     Created initial commuter module.
                            

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler
#window REACTOR_LOG

$Insert EVENT_SETUP
$Insert LOGICAL
$Insert APP_INSERTS
$Insert REACTOR_LOG_EQUATES

EQU COL$PROB_CAT		TO 1		;* Used in .SERV_INFO
EQU COL$PROB_CAT_DESC	TO 2
EQU COL$SERV_ID			TO 3
EQU COL$SERV_ID_DESC	TO 4
EQU COL$SCHED			TO 5
EQU COL$ITEM_TYPE		TO 6
EQU COL$ITEM_ID			TO 7
EQU COL$ITEM_RI_NO		TO 8

Declare subroutine Post_Event, Database_Services
Declare function   Datetime, SRP_String, Database_Services

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
*    Transfer Param4 to Param3
*    Transfer Param5 to Param4
*    Transfer Param6 to Param5
*    Transfer Param7 to Param6
*    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Event Handlers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)
    
    GoSub SetupOLEControls
    IOOptions    = Get_Property(@Window, 'IOOPTIONS')
    IOOptions<6> = True$ ; // Don't clear on write
    Set_Property(@Window, 'IOOPTIONS', IOOptions)
    
end event


Event WINDOW.READ()
    
    GoSub FillForm
    
end event


Event WINDOW.WRITE()

    GoSub UpdateNotes
    QBF = Get_Property(@Window, "QBFSTATUS")
    If QBF EQ False$ then Set_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY', '')
    	
    IOOptions    = Get_Property(@Window, 'IOOPTIONS')
    IOOptions<6> = True$
    Set_Property(@Window, 'IOOPTIONS', IOOptions)
    
end event


Event WINDOW.CLEAR(bSaveKey, bSuppressWarning, bMaintainFocus, CtrlIDFocus)
    
    Set_Property(@Window, '@NOTES_USERNAMES', '')
    Set_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY', '')
    
end event


Event PUB_ADD_COMMENT.CLICK()

    Response  = Dialog_Box('NDW_ADD_COMMENT', @Window, True$)
    OkClicked = Response<1>
    NewNote   = Response<2>
    If OkClicked and NewNote NE '' then
        NotesArray                  = Get_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY')
        Notes                       = NotesArray<3>
        NumNotes                    = DCount(Notes, @VM)
        NotesArray<2, NumNotes + 1> = OConv(@User4, '[CONV_XLATE,LSL_USERS*FIRST_LAST]')
        NotesArray<1, NumNotes + 1> = OConv(Datetime(), 'DT')
        // Wrap text using CRLF$
        NoteLen                     = Len(NewNote)
        If NoteLen GT 115 then
            NumLoops = Int(NoteLen/115)
            For LoopIndex = 1 to NumLoops
                Dummy           = NewNote[LoopIndex * 115, 'F ']
                Loc             = Col2()
                NewNote[Loc, 0] = CRLF$
            Next LoopIndex
        end
        NotesArray<3, NumNotes + 1>     = NewNote
        NotesUsernames                  = Get_Property(@Window, '@NOTES_USERNAMES')
        NotesUsernames<0, NumNotes + 1> = @User4
        Set_Property(@Window, '@NOTES_USERNAMES', NotesUsernames)
        Set_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY', NotesArray)
        Set_Property(@Window, '@NOTESARRAY', NotesArray)
        
        RLSig = Get_Property(@Window:'.TECH_SIG', 'TEXT')
        If RLSig NE '' then GoSub UpdateNotes
    end
    
end event


Event WINDOW.OMNIEVENT(Message, Param1, Param2, Param3, Param4)

    CurrRLKey = Get_Property(@Window:'.REACTOR_LOG_NO', 'TEXT')
    OrigRLKey = Get_Property(@Window, '@ORIG_KEY')
    If CurrRLKey NE OrigRLKey then 
        GoSub FillForm
    end else
        GoSub RefreshForm
    end
    
       
    // Check for reactor log signature and set all controls to readonly if set.
    Signature = Get_Property(@Window:'.TECH_SIG', 'TEXT')
    CtrlMap = Get_Property(@Window, 'CTRLMAP')
    For each Ctrl in CtrlMap using @FM setting fPos
        CtrlType = Get_Property(Ctrl, 'TYPE')
        If Not( (Ctrl EQ @Window:'.OLE_EDT_NOTES') or (Ctrl EQ @Window:'.PUB_ADD_COMMENT') or (Ctrl EQ @Window:'.NEW_BUTTON') or (Ctrl EQ @Window:'.PRINT_ROOT_CAUSE') or (Ctrl EQ @Window:'.BUTTON_18') or  (Ctrl EQ @Window:'.LU_RL_NO') or (CtrlType EQ 'STATIC') or (Ctrl EQ @Window:'.TECH_SIGN') or (CtrlType EQ 'WINDOW') or (Ctrl EQ @Window:'.PHX_BTN_CLEAR') or (Ctrl EQ @Window:'.TAB') or (IndexC(Ctrl, 'QBF', 1) GT 0) or (Ctrl EQ @Window:'.REACTOR_LOG_NO') or (CtrlType EQ 'GROUPBOX') or (Ctrl EQ @Window:'.INJ_SETTING_HIST') or (Ctrl EQ @Window:'.PUB_RATIO_HISTORY') ) then
            Set_Property(Ctrl, 'ENABLED', (Signature EQ ''))
        end
    Next Ctrl
    Set_Property('REACTOR_LOG.MENU.FILE.SAVE_ROW', 'ENABLED', (Signature EQ '')) 

    If Signature EQ '' then
        Set_Property(@Window:'.TECH_SIGN', 'TEXT', 'Sign')
        LOReviewed = Get_Property(@WINDOW:'.LO_REVIEWED','CHECK')
        LONotApp   = Get_Property(@WINDOW:'.LO_NA','CHECK')

        IF LOReviewed OR LONotApp THEN
            Set_Property(@WINDOW:'.TECH_SIGN','ENABLED',1)
        END ELSE
            Set_Property(@WINDOW:'.TECH_SIGN','ENABLED',0)
        END

        TechSig = Get_Property(@WINDOW:'.TECH_SIG','DEFPROP')
        PostSig = Get_Property(@WINDOW:'.RI_POST_BY','DEFPROP')

        IF TechSig NE '' OR PostSig NE '' THEN
            Set_Property(@WINDOW:'.REACTOR','ENABLED',0)
        END ELSE
            Set_Property(@WINDOW:'.REACTOR','ENABLED',1)
        END
    end else
        Set_Property(@Window:'.TECH_SIGN', 'TEXT', 'Unsign')
        Set_Property(@Window:'.TECH_SIGN', 'ENABLED', True$)
        Set_Property(@Window, 'SAVEWARN', False$)
    end
    
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SetupOLEControls:
    
    OLECtrl       = @Window:'.OLE_EDT_NOTES'
    Set_Property(OLECtrl, "OLE.Dimension", 3:@FM:5)
    TitleList     = 'Datetime':@VM:'User':@VM:'Note'
    Set_Property(OLECtrl, 'OLE.TitleList', TitleList)
    DataColumn    = 120
    Set_Property(OLECtrl, "OLE.DataColumn[1]", DataColumn)
    Set_Property(OLECtrl, "OLE.DataColumn[2]", DataColumn)
    DataColumn<4> = True$ ; // Autosize Notes column
    Set_Property(OLECtrl, "OLE.DataColumn[3]", DataColumn)
    Set_Property(OLECtrl, "OLE.CellProtection[All; All]", 'RO')
    Set_Property(OLECtrl, "OLE.HeaderColumn[1]", 40:@FM:False$)
    Set_Property(OLECtrl, "OLE.CellMultilined[All; All]", True$)
    Set_Property(OLECtrl, "OLE.AllowDeletions", False$)

return


FillForm:
    
    RLNo       = Get_Property(@Window:'.REACTOR_LOG_NO', 'TEXT')
    AtRecord   = Database_Services('ReadDataRow', 'REACTOR_LOG', RLNo)
    Notes      = AtRecord<REACTOR_LOG_NOTES$>
    // OI 10 seems to messing up delimiters...
    Convert @TM to @VM in Notes
    NotesDTMs  = AtRecord<REACTOR_LOG_NOTES_DTMS$>
    NotesUsers = AtRecord<REACTOR_LOG_NOTES_USERS$>
    NumNotes   = DCount(Notes, @VM)
    // Wrap text using CRLF$
    For NoteIndex = 1 to NumNotes
        Note    = Notes<0, NoteIndex>
        NoteLen = Len(Note)
        If NoteLen GT 115 then
            NumLoops = Int(NoteLen/115)
            For LoopIndex = 1 to NumLoops
                Dummy        = Note[LoopIndex * 115, 'F ']
                Loc          = Col2()
                Note[Loc, 0] = CRLF$
            Next LoopIndex
            Notes<0, NoteIndex> = Note
        end
    Next NoteIndex
    Set_Property(@Window, '@NOTES_USERNAMES', NotesUsers)
    NotesArray = OConv(NotesDTMs, 'DT') : @FM : OConv(NotesUsers, '[CONV_XLATE,LSL_USERS*FIRST_LAST]') : @FM : Notes
    Set_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY', NotesArray)
    Set_Property(@Window, '@NOTESARRAY', NotesArray)
    RLKey      = Get_Property(@Window:'.REACTOR_LOG_NO', 'TEXT')
    Set_Property(@Window, '@ORIG_KEY', RLKey)
    
return


RefreshForm:
    
    NotesArray = Get_Property(@Window, '@NOTESARRAY')
    Set_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY', NotesArray)
    
return


UpdateNotes:
    
    NotesArray                         = Get_Property(@Window:'.OLE_EDT_NOTES', 'OLE.ARRAY')
    NotesDTMs                          = NotesArray<1>
    Notes                              = NotesArray<3>
    // Remove word wrapping delimeters
    Swap @TM with '' in Notes
    AtRecord                           = Get_Property(@Window, 'ATRECORD')
    AtRecord<REACTOR_LOG_NOTES$>       = Notes
    AtRecord<REACTOR_LOG_NOTES_DTMS$>  = IConv(NotesDTMs, 'DT')
    AtRecord<REACTOR_LOG_NOTES_USERS$> = Get_Property(@Window, '@NOTES_USERNAMES')
    Set_Property(@Window, 'ATRECORD', AtRecord)
    @Record                            = AtRecord
    Database_Services('WriteDataRow', 'REACTOR_LOG', @ID, AtRecord, True$)
    
return


