Compile function NDW_SCHED_EVENTS_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_Sched_Events_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)
        01/19/23    djs     Converted from MASTER_PM_LIST.

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

#pragma precomp SRP_PreCompiler
#window NDW_SCHED_EVENTS

$Insert EVENT_SETUP
$Insert MSG_EQUATES
$Insert APPCOLORS
$Insert LSL_USERS_EQU
$Insert SECURITY_RIGHTS_EQU
$Insert PM_SPEC_EQUATES
$Insert POPUP_EQUATES
$INSERT LOGICAL
$Insert TOOL_EQUATES
$Insert RTI_STYLE_EQUATES
$Insert RLIST_EQUATES
$Insert OIPRINT_EQUATES

EQU COL$PMS_ID			TO 1
EQU COL$DESC			TO 2
EQU COL$TOOL_ID			TO 3
EQU COL$TOOL_DESC		TO 4
EQU COL$TOOL_STATUS		TO 5
EQU COL$TOOL_CYCLE_CNT	TO 6
EQU COL$LAST_PM			TO 7
EQU COL$EARLY_START		TO 8
EQU COL$SCHED_START		TO 9
EQU COL$LATE_START		TO 10
EQU COL$PM_STATUS		TO 11

Declare subroutine obj_Appwindow, Set_Property, Yield, Send_Message, End_Dialog, Send_Event, ErrMsg, Set_Status
Declare subroutine Start_Window
Declare function   MemberOf, Tool_Services, Send_Message, obj_PM_Spec, SRP_Array

// 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)
    
    obj_Appwindow('Create',@WINDOW)

    IF MemberOf (@USER4, 'PM_SCHED') ELSE 
        Message = 'You do not have the proper security to enter the Master PM Schedule List...' 
        Message<MICON$> = 'H'
        Void = Msg( '', Message )
        Send_Event(@Window, 'CLOSE')
    END
    
    PMStyles				= Send_Message(@WINDOW:'.SCHED','COLSTYLE',0,'')
    PMStyles<COL$PMS_ID>	= BitOr(PMStyles<COL$PMS_ID>,DTCS_OPTIONSBUTTON$)
    
    void = Send_Message(@WINDOW:'.SCHED','COLSTYLE',0,PMStyles)
    
    ToolList = Tool_Services('GetTools')
    ToolList = SRP_Array('SortSimpleList', ToolList, 'AscendingText', @FM)
    Set_Property(@WINDOW :'.COMBO_FILTER', 'LIST', ToolList)
    Send_Message(@WINDOW:'.OLE_BTN_CLEAR_FILTER', 'QUALIFY_EVENT', 'OLE.OnClick', 1)
    
    GOSUB Refresh
    PrevCursor = Set_Property("SYSTEM", "CURSOR", "A")
    Yield()
    
End Event


Event REFRESH.CLICK()
    
    GoSub Refresh
    
end event


Event WINDOW.GOTFOCUS(PrevFocusID)
    
    GoSub Refresh
    
end event


Event WINDOW.CLOSE(CancelFlag)
    
    End_Dialog(@WINDOW,'')
    
end event


Event SCHED.DBLCLK(CtrlKey, ShiftKey, MouseButton)
    
    RowData = Get_Property(CtrlEntId,'ROWDATA')
    
    PMSId = RowData<COL$PMS_ID>

    obj_AppWindow('ViewRelated','PM_SPEC':@RM:PMSId)

end event


Event SCHED.OPTIONS()
    
    RowData = Get_Property(CtrlEntId,'ROWDATA')
    
    PMSId = RowData<COL$PMS_ID>
    
    TypeOver = ''
    
    TypeOver<PDISPLAY$> = 'WITH PMS_ID = ':QUOTE(PMSId):' BY-DSND ENTER_DTM'
    TypeOver<PSELECT$>	= '2'
    TypeOver<PTYPE$>    = 'K'
    
    PMKeys = Popup(@WINDOW,TypeOver,'PM_HISTORY')

    IF PMKeys = '' OR PMKeys = CHAR(27) THEN RETURN
    
    obj_Appwindow('ViewRelated','PM':@RM:PMKeys:@RM:@RM)
    
end event


Event OLE_BTN_CLEAR_FILTER.OnClick(Point, Button, Shift, Ctrl)
    
    Set_Property(@WINDOW:'.COMBO_FILTER', 'TEXT', '')
    GoSub Refresh
    
end event


Event COMBO_FILTER.CHANGED(NewData)
    
    GoSub Refresh
    
end event


Event PUB_NEW_PM_SPEC.CLICK()
    
    Start_Window('PM_SPEC', @Window, '')
    
end event


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


Refresh:
    
    Def         = ""
    Def<MTEXT$> = "Loading scheduled preventive events..."
    Def<MTYPE$> = "U"
    MsgUp       = Msg(@window, Def)   ;* display the processing message

    ToolFilter  = Get_Property(@WINDOW :'.COMBO_FILTER', 'TEXT')
    
    // PM_SPEC and PM tables refactored for use with Silicon.
    // GaN related PM_SPEC and PM records exist for PMS_SPEC keys below 662.
    ShowArchived  = Get_Property(@Window:'.CHK_SHOW_ARCHIVED', 'CHECK')
    ShowOOS       = Get_Property(@Window:'.CHK_SHOW_OOS', 'CHECK')
    ShowScrubbers = Get_Property(@Window:'.CHK_SHOW_SCRUBBERS', 'CHECK') 	
    
    Query   = 'SELECT PM_SPEC WITH FIVE_S_FLAG NE 1 '
    If Not(ShowArchived) then Query := 'AND WITH ARCHIVED NE 1 '
	If Not(ShowScrubbers) then Query := 'AND WITH PM_CLASS NE ' : Quote('SCRUBBER')
    ErrCode = ''
    Set_Status(0)
    RList(Query, TARGET_ACTIVELIST$, '', '', '')
    If Not(Get_Status(ErrCode)) then
        Open 'PM_SPEC' to TableIn then
            Done         = False$
            SchedList    = ''
            SchedColors  = ''
            EQ_LOC       = ''
            Today        = Date() 
            SortedStarts = ''
            Loop
                ReadNext PMSId else Done = True$
            Until Done
                READ PMSpecRec FROM TableIn,PMSId THEN
                    
                    ActPMKeys	= PMSpecRec<PM_SPEC_PM_KEYS$>
                    Units		= PMSpecRec<PM_SPEC_UNITS$>
                    ToolID		= PMSpecRec<PM_SPEC_TOOL_ID$>
                    ToolStatus	= XLATE('TOOL',ToolID,'CURR_MODE_DESC','X')
                    
                    If ( ( (ToolID _EQC ToolFilter) or (ToolFilter EQ '') ) and ( (ToolStatus NE 'Out of Service') or ShowOOS ) ) then
                        
                        Conv = ''
                        IF Units = 'D' THEN	Conv = 'D'
                        IF Units = 'T' THEN Conv = 'DT'
                        IF Units = 'Q' THEN Conv = 'MD0'
                        
                        Today	= Date()
                        CurrDtm	= ICONV(OCONV(Date(),'D'):' ':OCONV(Time(),'MT'), 'DT' )
                        CurrCnt	= XLATE('TOOL',ToolID,TOOL_CYCLE_CNT$,'X')					;* Curr Cycle Count on Tool
                        Now     = ''
                        IF Units = 'D' THEN Now = Today
                        IF Units = 'T' THEN Now = CurrDtm
                        IF Units = 'Q' THEN Now = CurrCnt
                        
                        pmCnt = COUNT(ActPMKeys,@VM) + (ActPMKeys NE '')
                        
                        IF pmCnt = 0 THEN pmCnt = 1
                        
                        SchedStarts	= obj_PM_Spec('SchedStart',PMSId:@RM:PMSpecRec)
                        EarlyStarts	= obj_PM_Spec('EarlyStart',PMSId:@RM:PMSpecRec)
                        LateStarts	= obj_PM_Spec('LateStart',PMSId:@RM:PMSpecRec)
                        
                        FOR I = 1 TO pmCnt
                            SchedStart = SchedStarts<1,I>
                            EarlyStart = EarlyStarts<1,I>
                            LateStart  = LateStarts<1,I>
                            
                            IF Conv NE '' THEN
                                SchedStartIn = ICONV(SchedStart,Conv)
                                EarlyStartIn = ICONV(EarlyStart,Conv)
                                LateStartIn = ICONV(LateStart,Conv)
                            END ELSE
                                SchedStartIn = SchedStart
                                EarlyStartIn = EarlyStart
                                LateStartIn	 = LateStart
                            END
                            
                            BEGIN CASE
                                
                                CASE SchedStartIn = ''											; LineColor = WHITE$
                                CASE EarlyStartIn = '' AND Now LE SchedStartIn					; LineColor = GREEN$
                                CASE Now <	EarlyStartIn										; LineColor	= GREEN$
                                CASE Now <	SchedStartIn										; LineColor = YELLOW$
                                CASE LateStartIn = '' AND Now = SchedStartIn					; LineColor = ORANGE$
                                CASE LateStartIn = '' AND Now > SchedStartIn					; LineColor = RED$
                                CASE Now > LateStartIn											; LineColor = RED$
                                CASE Now GE SchedStartIn										; LineColor = ORANGE$													;
                                    
                            END CASE
                            
                            LastPM = OConv(Xlate('PM_SPEC', PMSId, 'LAST_PM_COMP_DTM', 'X'), 'DT4/^H')
                            
                            PMDesc 			= PMSpecRec<PM_SPEC_DESC$>
                            ToolID 			= PMSpecRec<PM_SPEC_TOOL_ID$>
                            ToolDesc 		= XLATE('TOOL',ToolID,TOOL_TOOL_DESC$,'X')
                            ToolLocation	= XLATE('TOOL',ToolID,'LOCATION','X')
                            
                            LastPM			= LastPM
                            
                            IF SchedStartIn = '' THEN
                                Pos = -1
                            END ELSE
                                
                                LOCATE SchedStartIn IN SortedStarts BY 'AR' USING @FM SETTING Pos THEN
                                    NULL
                                END ELSE
                                    NULL
                                END
                                SortedStarts = INSERT(SortedStarts,Pos,0,0,SchedStartIn)
                            END			
                            
                            SchedLine = PMSId
                            SchedLine<1,COL$DESC>			= PMDesc
                            SchedLine<1,COL$TOOL_ID>		= ToolID
                            SchedLine<1,COL$TOOL_DESC>		= ToolDesc
                            SchedLine<1,COL$TOOL_STATUS>	= ToolStatus
                            SchedLine<1,COL$TOOL_CYCLE_CNT>	= CurrCnt
                            SchedLine<1,COL$LAST_PM>		= LastPM
                            SchedLine<1,COL$EARLY_START>	= EarlyStart
                            SchedLine<1,COL$SCHED_START>	= SchedStart 
                            SchedLine<1,COL$LATE_START>		= LateStart			
                            SchedLine<1,COL$PM_STATUS>		= XLATE('PM',ActPMKeys<1,I>,'STATUS','X')	;*PMStatus				
                            SchedList	= INSERT(SchedList,Pos,0,0,SchedLine)
                            SchedColors	= INSERT(SchedColors,Pos,0,0,LineColor)			
                            
                        NEXT I
                    end
                    
                END	;* End of PM Rec read
            Repeat
            
            SchedList<-1> = STR(@VM,10)
            Set_Property(@WINDOW:'.Sched','LIST',SchedList)
            SchedCnt      = COUNT(SchedList,@FM) + (SchedList NE '')
            FOR I = 1 TO SchedCnt
                stat = Send_Message(@WINDOW:'.SCHED','COLOR_BY_POS',0,I,SchedColors<I>)
            NEXT I
        end else
            ErrorMsg = 'Unable to open "PM_SPEC" table.'
            ErrMsg(ErrorMsg)
        end
    end else
        ErrorMsg = 'Error calling RList. Error code: ':ErrCode
        ErrMsg(ErrorMsg)
    end
    Msg(@window, MsgUp)         ;* take down the processing message
    
return





