Compile function NDW_TOOLS_MODE_CHG_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_TOOLS_MODE_CHG

Declare function Get_Property, Reactor_Services, Database_Services, Tool_Services, SRP_Array
Declare subroutine Set_Property, RList, Msg , Reactor_Services, End_Window, Post_Event, Tool_Services

$Insert APP_INSERTS
$Insert EVENT_SETUP
$Insert REACTOR_EQUATES
$insert logical
$Insert RLIST_EQUATES
$Insert REACTOR_MODES_EQUATES
$Insert REACT_MODE_NG_EQUATES
$Insert MSG_EQUATES
$Insert TOOL_EQUATES
$Insert TOOL_LOG_EQUATES

// 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)
    
    EventFlow = 1
    GoSub   Setup_OLE_Controls
    GoSub   PopulateTools
    GoSub   PopulateModes
    GoSub   CheckCanSubmit
    
End Event

Event PUB_SELECT_ALL.CLICK()
    ReactorTableCtrlID = @Window : '.OLE_TOOL_REPORT_TABLE'
    Send_Message(ReactorTableCtrlID, "OLE.SelectAll")
    GoSub UpdateSelectedToolsText
    GoSub CheckCanSubmit
end event

Event PUB_RESET_SELECTION.CLICK()
    ReactorTableCtrlID = @Window : '.OLE_TOOL_REPORT_TABLE'
    Send_Message(ReactorTableCtrlID, "OLE.UnselectAll")
    GoSub UpdateSelectedToolsText
    GoSub CheckCanSubmit
end event

Event CMB_TOOL_MODE.CHANGED(NewData)(NewModeData)()
    
    GoSub CheckCanSubmit
end event

Event OLE_TOOL_REPORT_TABLE.OnItemClick(Row, Button, Col, Point)
    GoSub UpdateSelectedToolsText
    GoSub CheckCanSubmit
end event

Event PUB_SUBMIT.CLICK()
    
    SelectedMode  = Get_Property(@Window : '.CMB_TOOL_MODE', "TEXT")
    Comment       = Get_Property(@Window : '.TXT_COMMENTS', 'TEXT')
    SelectedTools = Get_Property(@Window : '.OLE_TOOL_REPORT_TABLE', "OLE.SelList")
    Response         = Dialog_Box('NDW_VERIFY_USER', @WINDOW, @USER4 : @FM : 'LEAD' : @VM : 'SUPERVISOR' : @VM : 'MAINTENANCE')
    if Response AND DCOUNT(SelectedTools, @FM) GT 1 then
        Def              = ""
        Def<MCAPTION$>   = "Please wait... Changing Modes..."
        Def<MTYPE$ >     = "GC"
        Def<MEXTENT$>    = DCOUNT(SelectedTools, @FM)
        Def<MTEXTWIDTH$> = 400
        MsgUp = Msg(@WINDOW, Def)
        for each Tool in SelectedTools using @FM setting iPos
            Void = Msg(@WINDOW, MsgUp, iPos, MSGINSTUPDATE$)
            Tool_Services('ChangeToolMode', Tool<1,1>, SelectedMode, Comment, @User4, True$)
            //Reactor_Services('CreateReactModeChange', @USER4, Reactor<1,1>, SelectedMode, SelectedSubCat, Comment, True$)
        Next Tool
        Msg(@WINDOW, MsgUp) 		;* take down the gauge
        GoSub ResetForm
    end else
        
    end
end event

Event PUB_CANCEL.CLICK()
    Post_Event(@Window, 'CLOSE')
end event

Event WINDOW.CLOSE(CancelFlag)
    End_Window(@Window)
end event

//-----------------------------------------------------------------------------
// Internal Go-Subs
//-----------------------------------------------------------------------------

Setup_OLE_Controls:
 
    ToolTableCtrlID = @Window : '.OLE_TOOL_REPORT_TABLE'
    Set_Property(ToolTableCtrlID, "OLE.AlwaysShowSelection", True$)
    rv              = Send_Message(ToolTableCtrlID, "QUALIFY_EVENT", "OnItemClick", 1)
    rv              = Send_Message(ToolTableCtrlID, "QUALIFY_EVENT", "OnSelChange", 1)

    ColArray       = ''
    //Col 1(Reactor No)
    ColArray<1,1>  = 'Reactor';//Col Title - Reqd
    ColArray<1,2>  = 'T';//Col Title - required
    ColArray<1,3>  = 100;//Col Width - required
    ColArray<1,4>  = False$;//Autosize
    ColArray<1,7>  = True$;//Sortable
    ColArray<1,10> = 'Center';//Alignment
    //Col 2(Reactor Type)
    ColArray<2,1>  = 'Type';//Col Title - Reqd
    ColArray<2,2>  = 'T';//Col Title - required
    ColArray<2,3>  = 35;//Col Width - required
    ColArray<2,4>  = False$;//Autosize
    ColArray<2,7>  = True$;//Sortable
    ColArray<2,10> = 'Center';//Alignment
    //Col 3(Current Mode)
    ColArray<3,1>  = 'Curr Mode';//Col Title - Reqd
    ColArray<3,2>  = 'T';//Col Title - required
    ColArray<3,3>  = 200;//Col Width - required
    ColArray<3,4>  = False$;//Autosize
    ColArray<3,7>  = True$;//Sortable
    ColArray<3,10> = 'Center';//Alignment
    
    //Set Table Columns
    Set_Property(ToolTableCtrlID, "OLE.ColumnList", ColArray)
    Set_Property(ToolTableCtrlID, "OLE.MultiSelect", 2)
return

PopulateTools:
    
    AllTools    = Tool_Services('GetAllTools')
    ActiveTools = ''
    for each toolId in AllTools using @VM setting tPos
        arrIndex   = 1
        ToolRec    = Database_Services('ReadDataRow', 'TOOL', toolId)
        ToolStatus = ToolRec<TOOL_OUT_OF_SERVICE$>
        IF ToolStatus NE True$ then
            ToolType        = ToolRec<TOOL_TOOL_TYPE$>
            ToolCurrModeKey = ToolRec<TOOL_CURR_MODE_KEY$>
            ToolCurrModeRec = Database_Services('ReadDataRow', 'TOOL_LOG', ToolCurrModeKey)
            ToolCurrMode    = ToolCurrModeRec<TOOL_LOG_TOOL_MODE$>
            ActiveTools<-1> = toolId : @VM : ToolType : @VM : ToolCurrMode
        end
    Next toolId
    Set_Property(@Window:".OLE_TOOL_REPORT_TABLE", "OLE.RecordList", ActiveTools)
    Send_Message(@Window:".OLE_TOOL_REPORT_TABLE", "OLE.UnselectAll")


return

PopulateModes:
    
    Modes = Xlate('SYSREPOSPOPUPS', 'LSL2**TOOL_MODE', 8, 'X')
    Modes = SRP_Array('Rotate', Modes, @VM, @SVM)
    Modes = Modes<0,1>
    swap @SVM with @FM in Modes
    
    Set_Property(@Window : '.CMB_TOOL_MODE', 'LIST', Modes)
    
return

CheckCanSubmit:
    
    SelectedTools = Get_Property(@Window : '.OLE_TOOL_REPORT_TABLE', "OLE.SelList")
    NewMode       = Get_Property(@Window : '.CMB_TOOL_MODE', 'TEXT')
    //Step1: Check that there are more than one tools selected
    if DCount(SelectedTools, @FM) > 1 then
        //Step2: Check that a mode is selected
        if NewMode NE '' then
            //Step3: Check that a mode category is selected
                Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 1)
        end else
            Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 0)
        end
    end else
        Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 0)
    end
return

ResetForm:
    
    //Unselect all reactors
    Send_Message(@Window:".OLE_TOOL_REPORT_TABLE", "OLE.UnselectAll")
    //Unselect Mode
    Set_Property(@Window : '.CMB_TOOL_MODE', 'TEXT', '')
    //Clear Comments
    Set_Property(@Window : '.TXT_COMMENTS', 'TEXT', '')
    //Reset Selected Reactors Text
    GoSub UpdateSelectedToolsText
    //Reset Submit Button
    Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', False$)
    //Refresh the select box
    Set_Property(ReactorTableCtrlID, "OLE.RecordList", '')
    GoSub PopulateTools
return

UpdateSelectedToolsText:
    ToolTableCtrlID   = @Window : '.OLE_TOOL_REPORT_TABLE'
    SelectedToolsText = 'Selected Tools: '
    SelectedTools     = Get_Property(ToolTableCtrlID, "OLE.SelList")
    for each Tool in SelectedTools using @FM setting iPos
        SelectedToolsText := Tool<1,1> : ', '
    Next Tool
    Set_Property(@Window : '.TXT_SELECTED_TOOLS', 'TEXT', SelectedToolsText)
return


