Compile function NDW_ROTR_PARAMETERS_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        :   NDW_ROTR_PARAMETER_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/15/18    djs     Created initial commuter module.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_ROTR_PARAMETERS

$Insert ROTR_PARM_REV_HIST_EQUATES
$Insert RLIST_EQUATES
$Insert SECURITY_RIGHTS_EQU
$Insert LOGICAL
$Insert RTI_STYLE_EQUATES

Equ Col.Reactor    to 1
Equ Col.EnableROTR to 2
Equ Col.MaintStop  to 3
Equ Col.XRuns      to 4
Equ Col.NCRLimit   to 5
Equ Col.ZeroNCR    to 6
Equ Col.UCLLimit   to 7
Equ Col.FailLimit  to 8

Equ PROTECTED$     to 8
Equ WM_USER$       to 1024
Equ ETM_INSERTROW$ to (WM_USER$ + 2004)
Equ ETM_DELETEROW$ to (WM_USER$ + 2005)

Declare subroutine Set_Property, RList, Database_Services, SRP_Show_Window, Error_Services, Errmsg, Send_Message
Declare subroutine Post_Event
Declare function   Get_Property, Dialog_Box, Database_Services, Error_Services, Security_Check, Send_Message

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
    
    *******************************************************
    * Trap INS/DEL key to prevent edit table row deletion *
    *******************************************************
    eventOp    = TRUE$  ; * // Turn tracking on
    eventOp<4> = TRUE$  ; * // Track Synchronously
    Call Send_Message(@Window:'.EDT_ROTR_PARMS', 'QUALIFY_EVENT', ETM_INSERTROW$, eventOp)
    Call Send_Message(@Window:'.EDT_ROTR_PARMS', 'QUALIFY_EVENT', ETM_DELETEROW$, eventOp)
    Call Send_Message(@Window:'.EDT_REV_HIST', 'QUALIFY_EVENT', ETM_INSERTROW$, eventOp)
    Call Send_Message(@Window:'.EDT_REV_HIST', 'QUALIFY_EVENT', ETM_DELETEROW$, eventOp)
    
    Query = 'SELECT REACTOR WITH REACT_NO GT 0 AND WITH REACT_NO LT 200 BY REACT_NO'
    RList(Query, TARGET_SAVELIST$, 'REACTOR_LIST', 'Current List of Reactors', False$)
    ReactorList = Database_Services('ReadDataRow', 'SYSLISTS', 'REACTOR_LIST')
    // Remove field containing list description
    ReactorList = Delete(ReactorList, 1, 0, 0)
    Set_Property(@Window, '@REACTOR_LIST', ReactorList)
    
    ParametersList = ''
    
    For each ReactorID in ReactorList setting rowIndex
        Database_Services('ActivateRecord', 'REACTOR', ReactorID)
        ParametersList<rowIndex, Col.Reactor>    = ReactorID
        ParametersList<rowIndex, Col.EnableROTR> = {ENABLE_ROTR}
        ParametersList<rowIndex, Col.MaintStop>  = {ROTR_MAINT_RESET}
        ParametersList<rowIndex, Col.XRuns>      = {X_PREV_RUNS}
        ParametersList<rowIndex, Col.NCRLimit>   = {NCR_LIMIT}
        ParametersList<rowIndex, Col.ZeroNCR>    = {ZERO_NCR_MIN}
        ParametersList<rowIndex, Col.UCLLimit>   = {UCL_LIMIT}
        ParametersList<rowIndex, Col.FailLimit>  = {ROTR_FAIL_LIMIT}       
    Next ReactorID
    
    Set_Property(@Window : '.EDT_ROTR_PARMS', 'LIST', ParametersList)
    Set_Property(@Window, '@ORIGPARMS', ParametersList)
    
    GoSub FillRevHist    
    GoSub Setup_OLE_Controls

    ParameterStyles	= Send_Message(@Window : '.EDT_ROTR_PARMS', 'COLSTYLE', 0, '')
    ParameterStyles<Col.EnableROTR>	= BitOr(ParameterStyles<Col.EnableROTR>,DTCS_CHECKBOX$)
    ParameterStyles<Col.EnableROTR>	= BitOr(ParameterStyles<Col.EnableROTR>,DTCS_CHECKBOXCENTER$)
    Void	= Send_Message(@Window : '.EDT_ROTR_PARMS', 'COLSTYLE', 2, ParameterStyles)
    //ParameterStyles	= Send_Message(@Window : '.EDT_ROTR_PARMS', 'COLSTYLE', 0, '')
    ParameterStyles<Col.MaintStop>	= BitOr(ParameterStyles<Col.MaintStop>,DTCS_CHECKBOX$)
    ParameterStyles<Col.MaintStop>	= BitOr(ParameterStyles<Col.MaintStop>,DTCS_CHECKBOXCENTER$)
    Void	= Send_Message(@Window : '.EDT_ROTR_PARMS', 'COLSTYLE', 3, ParameterStyles)
    
    CanEdit = Security_Check('Spec', WRITE$)
    Set_Property(@Window, '@CAN_EDIT', CanEdit)
    If CanEdit then
        For column = 2 to 8
            ParameterStyles<column> = BitAnd( ParameterStyles<column>, BitNot(PROTECTED$) )
        Next column
        ParameterStyles	= Send_Message(@Window : '.EDT_ROTR_PARMS', 'COLSTYLE', 0, ParameterStyles)
    end

    FormSize = ''
    SRP_Show_Window(@Window, 'SYSTEM', 'C', 'C', 1, '', False$, False$, FormSize)
    Eventflow = 1
    
End Event


Event TAB_ROTR.CLICK()

    Page = Get_Property(@Window : '.TAB_ROTR', 'VALUE')
    Set_Property(@Window, 'VPOSITION', Page)
    
end event


Event PUB_COPY.CLICK()
    
    Response    = Dialog_Box('NDW_REACTOR_SELECTION', @Window)
    ReactorFrom = Response<1>
    ReactorTo   = Response<2>
    ParamsCtrl  = @Window : '.EDT_ROTR_PARMS'   
    SelRow = Get_Property(ParamsCtrl, 'SELPOS')<2>
    If (SelRow NE '') and (ReactorFrom NE '') and (ReactorTo NE '') then
        ParametersList = Get_Property(ParamsCtrl, 'LIST')
        ReactorList = Get_Property(@Window, '@REACTOR_LIST')
        CopyParameters = ParametersList<SelRow>
        For ReactorID = ReactorFrom to ReactorTo
            Locate ReactorID in ReactorList using @FM setting rowIndex then
                ParametersList<rowIndex, Col.EnableROTR> = CopyParameters<1, Col.EnableROTR>
                ParametersList<rowIndex, Col.MaintStop>  = CopyParameters<1, Col.MaintStop>
                ParametersList<rowIndex, Col.XRuns>      = CopyParameters<1, Col.XRuns>
                ParametersList<rowIndex, Col.NCRLimit>   = CopyParameters<1, Col.NCRLimit>
                ParametersList<rowIndex, Col.ZeroNCR>    = CopyParameters<1, Col.ZeroNCR>
                ParametersList<rowIndex, Col.UCLLimit>   = CopyParameters<1, Col.UCLLimit>
                ParametersList<rowIndex, Col.FailLimit>  = CopyParameters<1, Col.FailLimit>
            end
        Next ReactorID
        Set_Property(ParamsCtrl, 'LIST', ParametersList)
        Set_Property(@Window : '.PUB_SAVE', 'ENABLED', True$)
        Set_Property(@Window, 'SAVEWARN', True$)
    end
            
end event


Event PUB_SAVE.CLICK()

    SaveWarn = Get_Property(@Window, 'SAVEWARN')
    If (SaveWarn EQ True$) then
    GoSub UpdateRevHist
        If Result EQ False$ then
            Eventflow = 0
            Return
        end
    end

    ParamsCtrl     = @Window : '.EDT_ROTR_PARMS'
    ParametersList = Get_Property(ParamsCtrl, 'LIST')
    ReactorList    = Get_Property(@Window, '@REACTOR_LIST')
    For each ReactorID in ReactorList setting rowIndex
        Database_Services('ActivateRecord', 'REACTOR', ReactorID)
        {ENABLE_ROTR}      = ParametersList<rowIndex, Col.EnableROTR>
        {ROTR_MAINT_RESET} = ParametersList<rowIndex, Col.MaintStop>
        {X_PREV_RUNS}      = ParametersList<rowIndex, Col.XRuns>
        {NCR_LIMIT}        = ParametersList<rowIndex, Col.NCRLimit>
        {ZERO_NCR_MIN}     = ParametersList<rowIndex, Col.ZeroNCR>
        {UCL_LIMIT}        = ParametersList<rowIndex, Col.UCLLimit>
        {ROTR_FAIL_LIMIT}  = ParametersList<rowIndex, Col.FailLimit>
        Database_Services('WriteDataRow', 'REACTOR', ReactorID, @Record)
    Next ReactorID
    GoSub FillRevHist
    
end event


Event EDT_ROTR_PARMS.GOTFOCUS(PrevFocusID)

    CanEdit = Get_Property(@Window, '@CAN_EDIT')
    SelRow  = Get_Property(CtrlEntID, 'SELPOS')
    If SelRow NE '' and CanEdit then
        Set_Property(@Window : '.PUB_COPY', 'ENABLED', True$)
    end
    Eventflow = 1
    
end event


Event EDT_ROTR_PARMS.LOSTFOCUS(Flag, FocusID)
    
    SelRow = Get_Property(CtrlEntID, 'SELPOS')
    If SelRow EQ '' then
        Set_Property(@Window : '.PUB_COPY', 'ENABLED', False$)
    end
    Eventflow = 1
    
end event


Event EDT_ROTR_PARMS.POSCHANGED(NextColumn, NextRow)

    CanEdit = Get_Property(@Window, '@CAN_EDIT')
    If NextRow NE '' and CanEdit then
        Set_Property(@Window : '.PUB_COPY', 'ENABLED', True$)
    end
    Eventflow = 1

end event


Event EDT_ROTR_PARMS.CHANGED(NewData)

    NewParmsList  = Get_Property(@Window : '.EDT_ROTR_PARMS', 'LIST')
    OrigParmsList = Get_Property(@Window, '@ORIGPARMS')
    If NewParmsList NE OrigParmsList then
        Set_Property(@Window, 'SAVEWARN', True$)
        Set_Property(@Window : '.PUB_SAVE', 'ENABLED', True$)
    end else
        Set_Property(@Window, 'SAVEWARN', False$)
        Set_Property(@Window : '.PUB_SAVE', 'ENABLED', False$)
    end
    Eventflow = 1
    
end event


Event EDT_ROTR_PARMS.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, Altkey)
    
    NewParmsList  = Get_Property(@Window : '.EDT_ROTR_PARMS', 'LIST')
    OrigParmsList = Get_Property(@Window, '@ORIGPARMS')
    If NewParmsList NE OrigParmsList then
        Set_Property(@Window, 'SAVEWARN', True$)
        Set_Property(@Window : '.PUB_SAVE', 'ENABLED', True$)
    end else
        Set_Property(@Window, 'SAVEWARN', False$)
        Set_Property(@Window : '.PUB_SAVE', 'ENABLED', False$)
    end
    Eventflow = 1
    
end event


Event EDT_ROTR_PARMS.CLICK()
    
    // Check if user has access to enable/disable ROTR
    CanEdit  = Get_Property(@Window, '@CAN_EDIT')
    // Check if user clicked a cell
    ClickPos = Get_Property(CtrlEntID, "CLICKPOS")    
    If (CanEdit EQ True$) and (ClickPos EQ 3) then
        SelCell = Get_Property(CtrlEntID, "NOTIFYPOS")
        Col = SelCell<1>
        // If user clicked column 2 or 3, then notify OI that the data in the control has changed to trigger the
        // EDT_ROTR_PARMS "CHANGED" event. The clicking of the checkbox does not natively trigger this event.
        If (Col EQ 2) or (Col EQ 3) then Post_Event(CtrlEntID, 'CHANGED')
    end
end event


Event EDT_ROTR_PARMS.INSERTROW(RowNum)
    
    Eventflow = 0
    
end event


Event EDT_ROTR_PARMS.DELETEROW(RowNum, RowData)
    
    Eventflow = 0
    
end event


Event EDT_REV_HIST.INSERTROW(RowNum)
    
    Eventflow = 0
    
end event


Event EDT_REV_HIST.DELETEROW(RowNum, RowData)
    
    Eventflow = 0
    
end event


Event EDT_ROTR_PARMS.WINMSG(hWnd, Message, wParam, lParam)
    
	Begin Case

		Case (Message = ETM_INSERTROW$)
			* // Stop the insert here...
			Call Set_WinMsgVal( TRUE$, 0 )  ; * // Force PS to return 0 to Windows

		Case (Message = ETM_DELETEROW$)
			* // Stop the delete here...
			Call Set_WinMsgVal( TRUE$, 0 )  ; * // Force PS to return 0 to Windows

	end case
    Eventflow = 0
    
end event


Event EDT_REV_HIST.WINMSG(hWnd, Message, wParam, lParam)
    
	Begin Case

		Case (Message = ETM_INSERTROW$)
			* // Stop the insert here...
			Call Set_WinMsgVal( TRUE$, 0 )  ; * // Force PS to return 0 to Windows

		Case (Message = ETM_DELETEROW$)
			* // Stop the delete here...
			Call Set_WinMsgVal( TRUE$, 0 )  ; * // Force PS to return 0 to Windows

	end case
	Eventflow = 0
	
end event


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


Setup_OLE_Controls:

    Qualify     = ''
    Qualify<1>  = 1
    Qualify<4>  = 0

return


UpdateRevHist:
    
    // Gather one line revision detail
    Result              = False$
    Response            = Dialog_Box('NDW_ROTR_REV_DET', @WINDOW)
    Valid               = Response<1>
    RevisionDetail      = Response<2>
    If Valid then
        ROTRRevRec = Database_Services('ReadDataRow', 'ROTR_PARM_REV_HIST', 1)
        If Error_Services('NoError') then
            Usernames   = ROTRRevRec<ROTR_PARM_REV_HIST_MOD_USERNAME$>
            Dates       = ROTRRevRec<ROTR_PARM_REV_HIST_MOD_DATE$>
            Times       = ROTRRevRec<ROTR_PARM_REV_HIST_MOD_TIME$>
            Revisions   = ROTRRevRec<ROTR_PARM_REV_HIST_REVISION_DETAIL$>
            ModUser     = OConv( @USER4, '[XLATE_CONV,LSL_USERS*FIRST_LAST]' )
            ModDate     = Date()
            ModTime     = Time()
            ModRevision = RevisionDetail
            ROTRRevRec<ROTR_PARM_REV_HIST_MOD_USERNAME$>      = Insert(Usernames, 0, 1, 0, ModUser)
            ROTRRevRec<ROTR_PARM_REV_HIST_MOD_DATE$>          = Insert(Dates, 0, 1, 0, ModDate)
            ROTRRevRec<ROTR_PARM_REV_HIST_MOD_TIME$>          = Insert(Times, 0, 1, 0, ModTime)
            ROTRRevRec<ROTR_PARM_REV_HIST_REVISION_DETAIL$>   = Insert(Revisions, 0, 1, 0, ModRevision)            
            Database_Services('WriteDataRow', 'ROTR_PARM_REV_HIST', 1, ROTRRevRec)
            If Error_Services('NoError') then
                Result = True$
            end else
                Errmsg('Error writing ROTR_PARM_REV_HIST record.' : @SVM : 'Please notify OpenInsight administrator')
            end
        end else
            Errmsg('Error reading ROTR_PARM_REV_HIST record.' : @SVM : 'Please notify OpenInsight administrator')
        end
    end else
        Errmsg('You must enter revision details in order to save changes to ROTR parameters.')
    end
    
return


FillRevHist:
    
    RevHistRec  = Database_Services('ReadDataRow', 'ROTR_PARM_REV_HIST', 1)
    RevHistArray = ''
    RevHistArray<1> = RevHistRec<ROTR_PARM_REV_HIST_REVISION_DETAIL$>
    RevHistArray<2> = RevHistRec<ROTR_PARM_REV_HIST_MOD_USERNAME$>
    RevHistArray<3> = OConv(RevHistRec<ROTR_PARM_REV_HIST_MOD_DATE$>, 'D')
    RevHistArray<4> = OConv(RevHistRec<ROTR_PARM_REV_HIST_MOD_TIME$>, 'MTH')   
    Set_Property(@Window : '.EDT_REV_HIST', 'ARRAY', RevHistArray)
    
return
