Compile function DIALOG_SERVICE_HISTORY_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window DIALOG_SERVICE_HISTORY
Declare function Get_Property, Reactor_Log_Services, SRP_Sort_Array, Utility, Dialog_Box
Declare subroutine Set_Property, Send_Message, SRP_Run_Command, Obj_Appwindow


$INSERT MSG_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
    
    ReactNo = CreateParam
    Set_Property(@Window:'.EDT_REACT_NO', 'TEXT', ReactNo)
    
    GoSub Refresh  
End Event

Event EDT_REACT_NO.LOSTFOCUS(Flag, FocusID)
    
    GoSub   Refresh
end event
Event BTN_FILTER_ALL.CLICK()
    ListToDisplay = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
end event

Event BTN_FILTER_FILTERS.CLICK()
    FilterIDs = ''
    FilterIDs := 865 : @VM
    FilterIDs := 228 : @VM
    FilterIDs := 230 : @VM
    FilterIDs := 950 : @VM
    FilterIDs := 455 : @VM
    FilterIDs := 226 : @VM
    FilterIDs := 968 : @VM
    FilterIDs := 229 : @VM
    FilterIDs := 526 : @VM
    FilterIDs := 1085 : @VM
    FilterIDs := 269 : @VM
    FilterIDs := 1084 : @VM
    FilterIDs := 256 : @VM
    FilterIDs := 953
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in FilterIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
end event

Event BTN_FILTER_MFC.CLICK()
    MFCIDs = ''
    MFCIDs := 951 : @VM
    MFCIDs := 104 : @VM
    MFCIDs := 108 : @VM
    MFCIDs := 173 : @VM
    MFCIDs := 120 : @VM
    MFCIDs := 122 : @VM
    MFCIDs := 134 : @VM
    MFCIDs := 128 : @VM
    MFCIDs := 144 : @VM
    MFCIDs := 150 : @VM
    MFCIDs := 149 : @VM
    MFCIDs := 466 : @VM
    MFCIDs := 1142 : @VM
    MFCIDs := 1270 : @VM
    MFCIDs := 189
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in MFCIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
end event

Event BTN_FILTER_PIEZOCON.CLICK()
    
    PiezoconIDs = ''
    PiezoconIDs := 10026 : @VM
    PiezoconIDs := 1245
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in PiezoconIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
    
end event

Event BTN_FILTER_THERMOCOUPLE.CLICK()
    
    ThermocoupleIDs = ''
    ThermocoupleIDs := 1232 :   @VM
    ThermocoupleIDs := 237 :    @VM
    ThermocoupleIDs := 239 :    @VM
    ThermocoupleIDs := 238 :    @VM
    ThermocoupleIDs := 233
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in ThermocoupleIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
    
end event

Event BTN_FILTER_REGULATORS.CLICK()
    
    RegulatorIDs = ''
    RegulatorIDs := 141 : @VM
    RegulatorIDs := 131 : @VM
    RegulatorIDs := 99 : @VM
    RegulatorIDs := 98 : @VM
    RegulatorIDs := 861 : @VM
    RegulatorIDs := 948 : @VM
    RegulatorIDs := 949 : @VM
    RegulatorIDs := 125 : @VM
    RegulatorIDs := 121 : @VM
    RegulatorIDs := 1131 : @VM
    RegulatorIDs := 130 : @VM
    RegulatorIDs := 137 : @VM
    RegulatorIDs := 138 : @VM
    RegulatorIDs := 499 : @VM
    RegulatorIDs := 142 : @VM
    RegulatorIDs := 152 : @VM
    RegulatorIDs := 146 : @VM
    RegulatorIDs := 117 : @VM
    RegulatorIDs := 38 : @VM
    RegulatorIDs := 1327
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in RegulatorIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
    
end event

Event BTN_FILTER_VALVES.CLICK()
    
    FiltersIDs = ''
    FiltersIDs := 557 : @VM
    FiltersIDs := 1273 : @VM
    FiltersIDs := 82 : @VM
    FiltersIDs := 84 : @VM
    FiltersIDs := 96 : @VM
    FiltersIDs := 1311 : @VM
    FiltersIDs := 1334 : @VM
    FiltersIDs := 436 : @VM
    FiltersIDs := 100 : @VM
    FiltersIDs := 101 : @VM
    FiltersIDs := 102 : @VM
    FiltersIDs := 105 : @VM
    FiltersIDs := 917 : @VM
    FiltersIDs := 106 : @VM
    FiltersIDs := 109 : @VM
    FiltersIDs := 110 : @VM
    FiltersIDs := 112 : @VM
    FiltersIDs := 119 : @VM
    FiltersIDs := 1121 : @VM
    FiltersIDs := 1144 : @VM
    FiltersIDs := 126 : @VM
    FiltersIDs := 123 : @VM
    FiltersIDs := 127 : @VM
    FiltersIDs := 129 : @VM
    FiltersIDs := 132 : @VM
    FiltersIDs := 133 : @VM
    FiltersIDs := 912 : @VM
    FiltersIDs := 136 : @VM
    FiltersIDs := 139 : @VM
    FiltersIDs := 140 : @VM
    FiltersIDs := 1141 : @VM
    FiltersIDs := 147 : @VM
    FiltersIDs := 145 : @VM
    FiltersIDs := 148 : @VM
    FiltersIDs := 1151 : @VM
    FiltersIDs := 155 : @VM
    FiltersIDs := 154 : @VM
    FiltersIDs := 1083 : @VM
    FiltersIDs := 285 : @VM
    FiltersIDs := 1161 : @VM
    FiltersIDs := 1269 : @VM
    FiltersIDs := 1331 : @VM
    FiltersIDs := 908 : @VM
    FiltersIDs := 85 : @VM
    FiltersIDs := 118 : @VM
    FiltersIDs := 1330 : @VM
    FiltersIDs := 175 : @VM
    FiltersIDs := 1326 : @VM
    FiltersIDs := 1290 : @VM
    FiltersIDs := 1325
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in FiltersIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
end event

Event BTN_FILTER_ELEVATORS.CLICK()
    ElevatorIDs = ''
    ElevatorIDs := 1358 : @VM
    ElevatorIDs := 1361
    
    CurrentServList = Get_Property(@Window:'.EDT_ALL_DATA', 'LIST')
    ListToDisplay = ''
    for each ID in ElevatorIDs using @VM
        for i = 1 to DCOUNT(CurrentServList, @FM)
            If CurrentServList<i,1> EQ ID then
                ListToDisplay := CurrentServList<i> : @FM
            end
        Next i
    Next ID
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ListToDisplay)
end event

Event BTN_HEADER_DATE_PERFORMED.OnContextMenu(Item)
    DataArray = Get_Property(@Window:'.EDT_SERV_HIST', 'LIST'); *Get data from view
    *    for each Line in DataArray using @FM
    *        Line<1,3> = IConv(Line<1,3>, 'DE')
    *    Next Line
    for SortIndex = 1 to DCount(DataArray, @FM)
        DataArray<SortIndex, 3> = IConv(DataArray<SortIndex, 3>, 'D/')
    Next SortIndex
    DataArraySorted = ''
    //
    Begin Case
        Case Item EQ 'Ascending'
            null
            DataArraySorted = SRP_Sort_Array(DataArray, "AR3", 1)
        Case Item EQ 'Descending'
            DataArraySorted = SRP_Sort_Array(DataArray, "DR3", 1)
    End Case
    *    For each Line in DataArraySorted using @FM
    *        DataArraySorted<1,3> = OCONV(DataArraySorted<1,3>, 'D2/')
    *    Next Line
    for SortIndex = 1 to DCount(DataArraySorted, @FM)
        DataArraySorted<SortIndex, 3> = OCONV(DataArraySorted<SortIndex,3>, 'D/')
    Next SortIndex
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', DataArraySorted)
    
end event

Event EDT_SERV_HIST.DBLCLK(CtrlKey, ShiftKey, MouseButton)
    
    DataArray = Get_Property(@Window:'.EDT_SERV_HIST', 'LIST')
    SelectedPos = Get_Property(@Window:'.EDT_SERV_HIST', "SELPOS")
    SelectedCol = SelectedPos<1>
    SelectedRow = SelectedPos<2>
    Begin Case
        Case SelectedCol EQ 1
            //User Selected the service id.
            
            
            SelectedServID = DataArray<SelectedRow, 1>
            ReactNo = Get_Property(@Window:'.EDT_REACT_NO', 'TEXT')
            
            IF ReactNo NE '' AND SelectedServID NE '' then
                Def = ""
                Def<MTEXT$> = "Gathering REACTOR_LOG records..."
                Def<MTYPE$> = "U"
                MsgUp = Msg(@window, Def)
                ServiceList = Reactor_Log_Services('GetLogsByReactorAndServID', ReactNo, SelectedServID)
                Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', ServiceList)
                Msg(@window, MsgUp) 
            end
            
        Case SelectedCol EQ 4
            //User Selected the reactor log id
            SelectedRLID = DataArray<SelectedRow, 4>
            IF SelectedRLID NE '' then
               Void = Dialog_Box('REACTOR_LOG', @Window, SelectedRLID:'*CENTER') 
            end
            
        Case SelectedCol EQ 5
            //User Selected the reactor item id
            SelectedRiID = DataArray<SelectedRow, 5>
            If SelectedRiID NE '' then
               obj_AppWindow('ViewRelated','REACT_ITEM':@RM:SelectedRiID) 
            end
            
            //Void = Dialog_Box('REACT_ITEM', @Window, SelectedRiID:'*CENTER')
            
    End Case
    
    
    
    
end event
Event PUB_DIR_BROWSE.CLICK
    value = "Select a directory" : @fm : "c:\temp"
    dir   = Utility( "CHOOSEDIR", @window, value)
    
    if dir NE '' then
        Set_Property(@Window : '.FILE_PATH', 'DEFPROP', dir: '\')	
    end
    
end event

EVENT PUB_EXPORT.CLICK()
    
    equ crlf$ to \0D0A\
    equ tab$ to char(09)
    equ comma$ to char(44)
    equ space$ to char(32)
    equ record$ to char(00)
    equ scolon$ to char(59)
    ReactNo = Get_Property(@Window : '.EDT_REACT_NO', 'TEXT')
    FilePath = Get_Property(@Window : '.FILE_PATH', 'TEXT')
    FileName = 'Reactor_Service_History_' : ReactNo : '_' : Date() : '.csv'
    Data = 'Service ID' : @VM : 'Service Description' : @VM : 'Date Last Performed' : @FM
    Data := Get_Property(@Window : '.EDT_SERV_HIST', 'LIST')
    swap @fm with crlf$ in Data
    swap @vm with ',' in Data
    
    SavePath = FilePath : FileName
    OSWrite data To SavePath
    
    IF FilePath NE '' then
        SRP_Run_Command('start excel "':SavePath:'"', 'VAR')
    end
    
end event

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

Refresh:
    
    ReactNo = Get_Property(@Window:'.EDT_REACT_NO', 'TEXT')
    IF ReactNo NE '' then
        Def = ""
        Def<MTEXT$> = "Gathering REACTOR_LOG records..."
        Def<MTYPE$> = "U"
        MsgUp = Msg(@window, Def)
        AllServIDs = Reactor_Log_Services('GetAllServIDs')
        ReactServsPerformed = Reactor_Log_Services('GetLatestReactorLogsByReactor', ReactNo)
        for each ReactServ in ReactServsPerformed using @FM
            For i = 1 to DCount(AllServIDs, @FM)
                If ReactServ<1,1> EQ AllServIDs<i,1> then
                    AllServIDs<i, 3> = ReactServ<1,4>
                    AllServIDs<i, 4> = ReactServ<1,2>
                    AllServIDs<i, 5> = ReactServ<1,5>
                end
            Next i
            
            *            Locate ReactServ<1,1> in AllServIDs using @FM setting AddPos then
            *                AllServIDs<AddPos, 3> = ReactServ<1,4>
            *            end
        Next ReactServ
        Msg(@window, MsgUp) 
    end
    Set_Property(@Window:'.EDT_SERV_HIST', 'LIST', AllServIDs)
    Set_Property(@Window:'.EDT_ALL_DATA', 'LIST', AllServIDs); //Using this control to store all data for filtering
    
    
return

Setup_OLE_Controls:    
    Ctrl = @Window : '.BTN_HEADER_DATE_PERFORMED'
    Menu = "Ascending":@FM:"Descending"
    Set_Property(Ctrl, "OLE.ContextMenu", Menu)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnContextMenu', 1)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnContextMenuClick', 1)
    
return

