Compile function NDW_WAFER_COUNTS_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_WAFER_COUNTS
Declare function   Reactor_Services, Database_Services, Get_Property, Error_Services, Environment_Services
Declare function   SRP_Array, Logging_Services
Declare subroutine Set_Property, Logging_Services, PlaceDialog

$Insert EVENT_SETUP
$Insert APP_INSERTS
$Insert REACTOR_EQUATES

EQU DATA_ASM_REACT_ID$    to 1
EQU DATA_ASM_SUSC_CNT$    to 2
EQU DATA_ASM_SUSC_THK$    to 3
EQU DATA_ASM_TUBE_CNT$    to 4
EQU DATA_ASM_TUBE_THK$    to 5
EQU DATA_ASM_ARM_CNT$     to 6
EQU DATA_ASM_EXHAUST_THK$ to 7

EQU DATA_HTR_REACT_ID$    to 1
EQU DATA_HTR_SUSC_CNT$    TO 2
EQU DATA_HTR_SUSC_THK$    TO 3
EQU DATA_HTR_TUBE_CNT$    TO 4
EQU DATA_HTR_TUBE_THK$    TO 5
EQU DATA_HTR_ARM_CNT$     TO 6
EQU DATA_HTR_EXHAUST_THK$ TO 7

EQU DATA_EPP_REACT_ID$         to 1
EQU DATA_EPP_SUSC_CNT$         TO 2
EQU DATA_EPP_SUSC_THK$         TO 3
EQU DATA_EPP_BELL_JAR_CNT$     TO 4
EQU DATA_EPP_BELL_JAR_THK$     TO 5
EQU DATA_EPP_LOWER_QUARTZ_CNT$ TO 6
EQU DATA_EPP_LOWER_QUARTZ_THK$ TO 7
EQU DATA_EPP_EXHAUST_THK$      TO 8

ASMDataTable = @Window : '.OLE_DATA_ASM'
HTRDataTable = @Window : '.OLE_DATA_HTR'
EPPDataTable = @Window : '.OLE_DATA_EPP'

LogPath         = Environment_Services('GetApplicationRootPath') : '\LogFiles\Reactor'
LogDate         = Oconv(Date(), 'D4/')
LogTime         = Oconv(Time(), 'MTS')
LogFileName     = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Performance Reports.csv'
Headers         = 'Logging DTM' : @FM : 'Error Message'
ColumnWidths    = 20 : @FM : 15 : @FM : 60 : @FM : 300
objLog          = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, ' ', Headers, ColumnWidths, False$, False$)
LoggingDTM      = LogDate : ' ' : LogTime   ; // Logging DTM

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)

    GoSub SetupOLEControls
    GoSub GetReactorMetrics
    
End Event


Event TABCONTROL_1.CLICK()
	
    Page = Get_Property(@Window : '.TABCONTROL_1', 'VALUE')
    Set_Property(@WINDOW,'VPOSITION', Page)
    
end event


Event PUB_REFRESH_DATA.CLICK()
	
    GoSub GetReactorMetrics
    
end event

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

GetReactorMetrics:
	
    ReactorRec = ''
    ASMData    = ''
    HTRData    = ''
    EPPData    = ''
    
    //Get ASM Reactors
    ASMList  = Reactor_Services('GetReactorNumbers', 'ASM')
    ASMList := @FM : Reactor_Services('GetReactorNumbers', 'ASM+')
    ASMList  = SRP_Array('SortSimpleList', ASMList, 'AscendingNumbers', @FM)
    
    //Get HTR Reactors
    HTRList = Reactor_Services('GetReactorNumbers', 'HTR')
    
    //Get EPP Reactors
    EPPList = Reactor_Services('GetReactorNumbers', 'EPP')
    
    //Get ASM Data, display to ASM data table
    for each Reactor in ASMList using @FM setting rPos
        MetricData                        = Reactor_Services('GetWaferCntMetricsByReactorID', Reactor)
        if Error_Services('NoError') then
            ASMData<rPos, DATA_ASM_REACT_ID$>    = MetricData<1, DATA_ASM_REACT_ID$>
            ASMData<rPos, DATA_ASM_SUSC_CNT$>    = MetricData<1, DATA_ASM_SUSC_CNT$>
            ASMData<rPos, DATA_ASM_SUSC_THK$>    = MetricData<1, DATA_ASM_SUSC_THK$>
            ASMData<rPos, DATA_ASM_TUBE_CNT$>    = MetricData<1, DATA_ASM_TUBE_CNT$>
            ASMData<rPos, DATA_ASM_TUBE_THK$>    = MetricData<1, DATA_ASM_TUBE_THK$>
            ASMData<rPos, DATA_ASM_ARM_CNT$>     = MetricData<1, DATA_ASM_ARM_CNT$>
            ASMData<rPos, DATA_ASM_EXHAUST_THK$> = MetricData<1, DATA_ASM_EXHAUST_THK$>
        end else
            ErrorMessage = Error_Services('GetMessage')
            LogData      = LoggingDTM : @FM : ErrorMessage
            Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$) 
        end
    Next reactor
    Set_Property(ASMDataTable, "OLE.LIST", ASMData)
    
    //Get HTR Data, display to HTR data table
    for each Reactor in HTRList using @FM setting rPos
        MetricData                        = Reactor_Services('GetWaferCntMetricsByReactorID', Reactor)
        if Error_Services('NoError') then
            HTRData<rPos, DATA_HTR_REACT_ID$>    = MetricData <1, DATA_HTR_REACT_ID$>
            HTRData<rPos, DATA_HTR_SUSC_CNT$>    = MetricData<1, DATA_HTR_SUSC_CNT$>
            HTRData<rPos, DATA_HTR_SUSC_THK$>    = MetricData<1, DATA_HTR_SUSC_THK$>
            HTRData<rPos, DATA_HTR_TUBE_CNT$>    = MetricData<1, DATA_HTR_TUBE_CNT$>
            HTRData<rPos, DATA_HTR_TUBE_THK$>    = MetricData<1, DATA_HTR_TUBE_THK$>
            HTRData<rPos, DATA_HTR_ARM_CNT$>     = MetricData<1, DATA_HTR_ARM_CNT$>
            HTRData<rPos, DATA_HTR_EXHAUST_THK$> = MetricData<1, DATA_HTR_EXHAUST_THK$>
        end else
            ErrorMessage = Error_Services('GetMessage')
            LogData      = LoggingDTM : @FM : ErrorMessage
            Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
        end

    Next reactor
    Set_Property(HTRDataTable, "OLE.LIST", HTRData)
    
    //Get EPP Data, display to EPP data table
    for each Reactor in EPPList using @FM setting rPos
        MetricData                                = Reactor_Services('GetWaferCntMetricsByReactorID', Reactor)
        if Error_Services('NoError') then
            EPPData<rPos, DATA_EPP_REACT_ID$>         = MetricData<1, DATA_EPP_REACT_ID$>
            EPPData<rPos, DATA_EPP_SUSC_CNT$>         = MetricData<1, DATA_EPP_SUSC_CNT$>
            EPPData<rPos, DATA_EPP_SUSC_THK$>         = MetricData<1, DATA_EPP_SUSC_THK$>
            EPPData<rPos, DATA_EPP_BELL_JAR_CNT$>     = MetricData<1, DATA_EPP_BELL_JAR_CNT$>
            EPPData<rPos, DATA_EPP_BELL_JAR_THK$>     = MetricData<1, DATA_EPP_BELL_JAR_THK$>
            EPPData<rPos, DATA_EPP_LOWER_QUARTZ_CNT$> = MetricData<1, DATA_EPP_LOWER_QUARTZ_CNT$>
            EPPData<rPos, DATA_EPP_LOWER_QUARTZ_THK$> = MetricData<1, DATA_EPP_LOWER_QUARTZ_THK$>
            EPPData<rPos, DATA_EPP_EXHAUST_THK$>      = MetricData<1, DATA_EPP_EXHAUST_THK$>
        end else
            ErrorMessage = Error_Services('GetMessage')
            LogData      = LoggingDTM : @FM : ErrorMessage
            Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
        end
    Next reactor
    Set_Property(EPPDataTable, "OLE.LIST", EPPData)
    
return


SetupOLEControls:
    
    // Set Row Headers to invisible
    Set_Property(ASMDataTable, "OLE.HeaderColumn[1]", '':@FM:False$)
    Set_Property(HTRDataTable, "OLE.HeaderColumn[1]", '':@FM:False$)
    Set_Property(EPPDataTable, "OLE.HeaderColumn[1]", '':@FM:False$)
    
    // Set Number of columns for each table
    Set_Property(ASMDataTable, "OLE.Dimension", 7);*6 columns for ASM Data Table
    Set_Property(HTRDataTable, "OLE.Dimension", 7);*6 columns for HTR Data Table
    Set_Property(EPPDataTable, "OLE.Dimension", 8);*7 columns for EPP Data Table
    
    // Set size of all columns
    ColSizeArray    = ''
    ColSizeArray<1> = 115
    Set_Property(ASMDataTable, "OLE.DataColumn[All]", ColSizeArray)
    Set_Property(HTRDataTable, "OLE.DataColumn[All]", ColSizeArray)
    Set_Property(EPPDataTable, "OLE.DataColumn[All]", ColSizeArray)
    
    // Shrink size of column 1
    ColSizeArray<1> = 60
    Set_Property(ASMDataTable, "OLE.DataColumn[1]", ColSizeArray)
    Set_Property(HTRDataTable, "OLE.DataColumn[1]", ColSizeArray)
    Set_Property(EPPDataTable, "OLE.DataColumn[1]", ColSizeArray)
    
    // Set size of all columns
    ColSizeArray    = ''
    ColSizeArray<1> = 125
    Set_Property(EPPDataTable, "OLE.DataColumn[6]", ColSizeArray)
    
    // Set autosize last column to true to fill out table width
    ColSizeArray<1> = 125
    ColSizeArray<4> = True$
    Set_Property(ASMDataTable, "OLE.DataColumn[7]", ColSizeArray)
    Set_Property(HTRDataTable, "OLE.DataColumn[7]", ColSizeArray)
    Set_Property(EPPDataTable, "OLE.DataColumn[8]", ColSizeArray)    
    
    //Set ASM Table Col. Headers
    HeaderTitles = 'Reactor#':@VM:'Susc. Wfr. Cnt.':@VM:'Susc. Thk':@VM:'Tube Wfr. Cnt.':@VM:'Tube Wfr. Thk':@VM:'Arm Wfr. Cnt.':@VM:'Exhaust Chamber Thk.'
    Set_Property(ASMDataTable, "OLE.TitleList", HeaderTitles)
    
    //Set HTR Table Col. Headers
    HeaderTitles = 'Reactor#':@VM:'Susc. Run Cnt.':@VM:'Susc. Thk':@VM:'Tube Run Cnt.':@VM:'Tube Thk':@VM:'Arm Wfr. Cnt.':@VM:'Exhaust Chamber Thk.'
    Set_Property(HTRDataTable, "OLE.TitleList", HeaderTitles)
    
    //Set EPP Table Col. Headers
    HeaderTitles = 'Reactor#':@VM:'Susc. Run Cnt.':@VM:'Susc. Thk':@VM:'Bell Jar Run Cnt.':@VM:'Bell Jar Thk':@VM:'Lower Quartz Run Cnt.':@VM:'Lower Quartz thk.':@VM:'Exhaust Chamber Thk.'
    Set_Property(EPPDataTable, "OLE.TitleList", HeaderTitles)
    
return

