Compile function REACTOR_MODE_HISTORY_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window REACTOR_MODE_HISTORY

Declare function Get_Property, Reactor_Services, Send_Message, Environment_Services, Excel_Services, SRP_Array
Declare function Datetime, OConv
Declare subroutine Set_Property,Send_Message, Excel_Services, SRP_Run_Command

$Insert APP_INSERTS
$Insert EVENT_SETUP
$Insert REACTOR_LOG_EQUATES
$Insert REACTOR_MODES_EQUATES
$Insert REACT_MODE_NG_EQUATES
$Insert REACT_STATUS_EQUATES

// Columns in OLE Edit Table
EQU COL$START_DTM		        TO 1
EQU COL$ELAP_HRS  		        TO 2
EQU COL$MODE	                TO 3
EQU COL$NOTES                   TO 4
EQU COL$CATEGORY_DESCRIPTION	TO 5
EQU COL$START_USER		        TO 6
EQU COL$RL_ID	                TO 7
EQU COL$SERVICE_DESCRIPTION	    TO 8
EQU COL$SERVICE_NOTES           TO 9

// 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)
    
    ReactNo             = field( CreateParam, char(244), 1 )
    DaysToReport        = field( CreateParam, char(244), 2 )

    
    GoSub   Setup_OLE_Controls
    
    ReactNo = field( CreateParam, char(244), 1 )
    DaysToReport       = field( CreateParam, char(244), 2 )
    
    IF ReactNo NE '' AND DaysToReport NE '' then
        Set_Property(@Window : '.EDT_REACT_NO', 'TEXT', ReactNo)
        Set_Property(@Window : '.EDT_DAYS_TO_REPORT', 'TEXT', DaysToReport)
        GoSub DoRead
    end else
        
    end
    
    
    EventFlow = 1
    
End Event


Event PUB_RUN.CLICK()
    
    ReactNo = Get_Property(@Window : '.EDT_REACT_NO', 'TEXT')
    DaysToReport = Get_Property(@Window : '.EDT_DAYS_TO_REPORT', 'TEXT')
    
    GoSub DoRead
    
end event


Event CHK_SHOW_COLORS.CLICK()
    
    ReactNo = Get_Property(@Window : '.EDT_REACT_NO', 'TEXT')
    DaysToReport = Get_Property(@Window : '.EDT_DAYS_TO_REPORT', 'TEXT')
    
    useColors = Get_Property(@Window: '.CHK_SHOW_COLORS', 'CHECK')
    
    if useColors EQ False$ then
        GoSub   DeColorizeData
    end else
        GoSub   DoRead
    end
    //GoSub DoRead
    
end event


Event PUB_EXPORT_TO_EXCEL.CLICK()
    
    value    = "Select a directory" : @fm : "c:\temp"
	FilePath = Utility( "CHOOSEDIR", @window, value)
	
	if FilePath NE '' then
		GoSub ExportToExcel
	end
    
end event


Event EDT_LOADED_CASS.DBLCLK(CtrlKey, ShiftKey, MouseButton)
    
    RowData  = Get_Property(CtrlEntID, 'ROWDATA')
    SelRDSNo = RowData<1>
    Void     = start_window( 'RDS', @window, SelRDSNo:'*CENTER', '', '' )
    
end event

//-----------------------------------------------------------------------------
// Internal GoSubs
//-----------------------------------------------------------------------------

DoRead:
    Ctrl = @Window : '.EDT_HIST'
    Set_Property(Ctrl, 'OLE.Redraw', False$)
    //tableDimension = Get_Property(Ctrl, "OLE.Dimension")
    //numRows = tableDimension<2>
    rv = Send_Message(Ctrl, "OLE.Clear", 0)
    //rv = Send_Message(Ctrl, "OLE.Clear", 1)
    //Set_Property(Ctrl, "OLE.CellColors[3; All]", ColorArray)
    ReactorHistory = Reactor_Services('GetReactModeHistory', ReactNo, DaysToReport)
    Index = Send_Message(Ctrl, "OLE.INSERT", 1, ReactorHistory)
    
    //Get Curr Mode
    CurrModeRec      = Reactor_Services('GetReactCurrModeRec', ReactNo)
    CurrMode         = CurrModeRec<REACT_MODE_NG_MODE$>
    CurrModeE10State = Xlate('REACTOR_MODES', CurrMode, REACTOR_MODES_E10_STATE$, 'X')
    CurrModeColors   = Get_Property(@Window:".OLE_SUBCLASS", "OLE.CustomColors")
    modeColor        = White$
    
    //Get Loaded Cassettes
    ReactStatusRec   = XLATE('REACT_STATUS', ReactNo, '', 'X')
    LoadedRDS        = ''
    LoadedRDS<1>     = ReactStatusRec<REACT_STATUS_LOAD_RDS$>
    LoadedRDS<2>     = ReactStatusRec<REACT_STATUS_LOAD_RDS_DTM$>
    LoadedDisplay    = ''
    for each rds in LoadedRDS<1> using @VM setting vPos
        LoadedDisplay<-1>  = LoadedRDS<1,vPos> : @VM : OCONV(LoadedRDS<2,vPos>, 'DT4/H')
    Next rds
    Set_Property(@Window : '.EDT_LOADED_CASS', 'LIST', LoadedDisplay)
    
    Begin Case
        Case CurrModeE10State EQ 'Scheduled Down'
            modeColor = Orange$

        Case CurrModeE10State EQ 'Unscheduled Down'
             modeColor = Red$  
   
        Case CurrModeE10State EQ 'Productive'
             modeColor = Green$  
       
        Case 1
            modeColor = White$

    End Case
    Set_Property(@Window : '.EDT_CURR_MODE', 'TEXT', CurrMode)
    Set_Property(@window : '.EDT_CURR_MODE', "BACKCOLOR", modeColor)


    RowSettings    = Get_Property(Ctrl, "OLE.DataRow[1]")
    RowSettings<1> = 64
    //RowSettings<4> = True$
    tableDimension = Get_Property(Ctrl, "OLE.Dimension")
    numRows        = tableDimension<2>
    for i = 1 to numRows
        Set_Property(Ctrl, "OLE.DataRow[" : i : "]", RowSettings)
    Next i
    
    useColors = Get_Property(@Window: '.CHK_SHOW_COLORS', 'CHECK')
    if useColors then
      GoSub ColorizeData  
    end
    Set_Property(Ctrl, "OLE.CellMultilined[All; All]", True$)
    Set_Property(Ctrl, 'OLE.Redraw', True$)
    
    //Set_Property(Ctrl, "OLE.CellMultilined[4; 1]", 1)

    

return

ColorizeData:
    Set_Property(Ctrl, 'OLE.Redraw', False$)
    tableDimension = Get_Property(Ctrl, "OLE.Dimension")
    numRows = tableDimension<2>
    for i = 1 to numRows
        row             = ReactorHistory<i>
        e10State        = XLATE('REACTOR_MODES',row<1,COL$MODE>, REACTOR_MODES_E10_STATE$, 'X')
        ColorArray     = 'None':@FM:'White':@FM:'None':@FM:'None':@FM:'None'
        If Get_Property(@Window: '.CHK_SHOW_COLORS', 'CHECK') EQ True$ then
            IF e10State EQ 'Scheduled Down' then
                ColorFill = "Vertical(Gradient(Orange L=90, Orange L=80, 30%), Gradient(Orange L=75, Orange L=80))"
                ColorArray<2> = ColorFill
            end
            IF e10State EQ 'Unscheduled Down' then
                ColorFill = "Vertical(Gradient(Red L=90, Red L=80, 30%), Gradient(Red L=75, Red L=80))"
                ColorArray<2> = ColorFill
            end
            IF e10State EQ 'Productive' then
                ColorFill = "Vertical(Gradient(Green L=90, Green L=80, 30%), Gradient(Green L=75, Green L=80))"
                ColorArray<2> = ColorFill
            end
        end

        
        Set_Property(Ctrl, "OLE.CellColors[3; " : i : "]", ColorArray)
    Next i
    Set_Property(Ctrl, 'OLE.Redraw', True$)
return

DeColorizeData:
    Ctrl = @Window : '.EDT_HIST'
    Set_Property(Ctrl, 'OLE.Redraw', False$)
*    tableDimension = Get_Property(Ctrl, "OLE.Dimension")
*    numRows = tableDimension<2>
*    for i = 1 to numRows

        ColorArray     = 'None':@FM:'White':@FM:'None':@FM:'None':@FM:'None'

        Set_Property(Ctrl, "OLE.CellColors[3; All]", ColorArray)
        
*        Set_Property(Ctrl, "OLE.CellColors[3; " : i : "]", ColorArray)
        
*    Next i
    Set_Property(Ctrl, 'OLE.Redraw', True$)
return

ExportToExcel:
    
    //TODO Replace FilePath
    *FilePath     = "C:\Users\MESOuellette\Desktop\"
    
    TemplatePath = Environment_Services('GetApplicationRootPath') : '\Reports\ReactMode\Reactor_Mode.xlsx' 
    objDocument  = Excel_Services('OpenDocument',TemplatePath)
    If Error_Services('HasError') then
        error = Error_Services('GetCode')
        return
    end
    Ctrl        = @Window : '.EDT_HIST'
    displayData = Get_Property(Ctrl, "OLE.ARRAY")
    displayData = SRP_Array("Rotate", displayData)
    CellRow     = 2
    
    for each row in displayData using @FM
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'A', CellRow, row<1,COL$START_DTM>)
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'B', CellRow, row<1,COL$ELAP_HRS>)
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'C', CellRow, row<1,COL$MODE>)
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'D', CellRow, row<1,COL$NOTES>)
        //Services Required
        ServiceCats   = row<1,COL$CATEGORY_DESCRIPTION>
        for ServiceCat = 1 to DCount(ServiceCats, @SVM)
            ServiceCats<1,1,ServiceCat> = ' - ' : ServiceCats<1,1,ServiceCat>
        Next ServiceCat
        swap @svm with CRLF$ in ServiceCats
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'E', CellRow, ServiceCats)
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'F', CellRow, row<1,COL$START_USER>)
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'G', CellRow, row<1,COL$RL_ID>)
        //Services Performed
        ServiceDescs = row<1,COL$SERVICE_DESCRIPTION>
        for ServiceDesc = 1 to DCount(ServiceDescs, @SVM)
            ServiceDescs<1,1,ServiceDesc> = ' - ' : ServiceDescs<1,1,ServiceDesc>
        Next ServiceDesc
        swap @svm with CRLF$ in ServiceDescs
        
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'H', CellRow, ServiceDescs)
        Excel_Services('SetCellValue', objDocument, 'ReactorModes', 'I', CellRow, row<1,COL$SERVICE_NOTES >)
        CellRow += 1
    Next row
    FileSaveName = 'REACT_MODE_LOG_': OConv(DateTime(), 'DT2^HS ') : '.xlsx'
    //Excel_Services('SaveDocument',objDocument,FilePath : 'REACT_MODE_LOG.xlsx')
    Excel_Services('SaveDocument',objDocument,FilePath : '\' : FileSaveName)
    If Error_Services('HasError') then
        test = Error_Services('GetMessage')
    end
    Excel_Services('CloseDocument', objDocument)
    IF FilePath NE '' then
        SRP_Run_Command('start excel "':FilePath : '\' : FileSaveName:'"', 'VAR')
    end
return

Setup_OLE_Controls:
  
    Ctrl = @Window : '.EDT_HIST'
    
    Set_Property(Ctrl, 'OLE.Redraw', False$)
    //Set up columns
    Set_Property(Ctrl, "OLE.Dimension", 9);*Define number of columns
    
    HeaderTitles = 'Start DTM':@VM:'Elap. Hrs.':@VM:'Mode':@VM:'Mode Change Reason':@VM:'Category Description':@VM:'Start User' |
    :@VM:'RL ID':@VM:'Service Description':@VM:'Reactor Log Comments'; *Define the column header titles
    Set_Property(Ctrl, "OLE.TitleList", HeaderTitles); *Set the column titles
    
    //Set the column Sizes
    ColSize = Get_Property(Ctrl, "OLE.DataColumn[1]")
    
    //Column 1
    ColSize<1> = 120
    Set_Property(Ctrl, "OLE.DataColumn[":COL$START_DTM:"]", ColSize)
    
    //Column 2
    ColSize<1> = 60
    //ColSize<4> = True$
    Set_Property(Ctrl, "OLE.DataColumn[":COL$ELAP_HRS:"]", ColSize)
    //Set_Property(Ctrl, "OLE.DataColumn[":COL$ELAP_HRS:"]", ColSize)
    
    //Column 3
    ColSize<1> = 250
    Set_Property(Ctrl, "OLE.DataColumn[":COL$MODE:"]", ColSize)
    
    //Column 4
    ColSize<1> = 200
    Set_Property(Ctrl, "OLE.DataColumn[":COL$NOTES:"]", ColSize)
    
    //Column 5
    ColSize<1> = 150
    Set_Property(Ctrl, "OLE.DataColumn[":COL$CATEGORY_DESCRIPTION:"]", ColSize)
    
    //Column 6
    ColSize<1> = 100
    Set_Property(Ctrl, "OLE.DataColumn[":COL$START_USER:"]", ColSize)
    
    //Column 7
    ColSize<1> = 40
    Set_Property(Ctrl, "OLE.DataColumn[":COL$RL_ID:"]", ColSize)
    
    //Column 8
    ColSize<1> = 225
    Set_Property(Ctrl, "OLE.DataColumn[":COL$SERVICE_DESCRIPTION:"]", ColSize)
    
    //Column 9
    ColSize<1> = 200
    ColSize<4> = True$
    Set_Property(Ctrl, "OLE.DataColumn[":COL$SERVICE_NOTES:"]", ColSize)
    
    Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
    Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '15')
    Set_Property(Ctrl, "OLE.CellProtection[All; All]", 'RO')
    
    // Set rows to resizeable and to expand when selected.
    DataRow    = Get_Property(Ctrl, "OLE.DataRow[1]")
    DataRow<3> = True$
    DataRow<4> = False$
    DataRow<5> = 160
    Set_Property(Ctrl, "OLE.DataRow[All]", DataRow)
    
    Set_Property(Ctrl, 'OLE.Redraw', True$)

return
