Compile function DIALOG_LOAD_SUSCEPTOR_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window DIALOG_LOAD_SUSCEPTOR

$Insert APP_INSERTS
$Insert EVENT_SETUP
$Insert REACT_RUN_EQUATES
$Insert REACTOR_EQUATES
$Insert EPI_SUSCEPTOR_EQUATES
$Insert WO_WFR_EQUATES
$Insert WO_MAT_WFR_EQUATES
$Insert RUN_STAGE_EQUATES

Declare Subroutine SRP_Show_Window, Database_Services, Set_Property, Error_Services, End_Dialog, ErrMsg, GaN_Services
Declare function Database_Services, Gan_Services, Error_Services, DateTime, GaN_Services

// 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)
    
    
    RDSNo       = Field(CreateParam, @FM, 1, 1)
    Stage       = Field(CreateParam, @FM, 2, 1)
    Reactor     = Field(CreateParam, @FM, 3, 1)
    ReactRunRec = Database_Services('ReadDataRow', 'REACT_RUN', RDSNo)
    
    GaNRunID    = ReactRunRec<REACT_RUN_GAN_RUN_ID$>
    WONo        = ReactRunRec<REACT_RUN_WO_NO$>
    WOStep      = ReactRunRec<REACT_RUN_WO_STEP$>
    
    Ctrls  = @WINDOW:'.RDS_NO':@RM			; Props  = 'DEFPROP':@RM	; Vals  = RDSNo:@RM
    Ctrls := @WINDOW:'.GAN_RUN':@RM			; Props := 'DEFPROP':@RM	; Vals := GanRunID:@RM
    Ctrls := @WINDOW:'.STAGE':@RM			; Props := 'DEFPROP':@RM	; Vals := Stage:@RM
    Ctrls := @WINDOW:'.REACTOR':@RM			; Props := 'DEFPROP':@RM	; Vals := Reactor:@RM
    Ctrls := @WINDOW:'.WO_NO':@RM			; Props := 'DEFPROP':@RM	; Vals := WONo:@RM
    Ctrls := @WINDOW:'.WO_STEP'				; Props := 'DEFPROP'		; Vals := WOStep

    Set_Property(Ctrls,Props,Vals)
    
    PrerunInfo    = Gan_Services('GetRunInfo', GanRunID)
    SuscSerial    = PreRunInfo<2,1>
    Pockets       = PrerunInfo<3>
    Scribes       = PrerunInfo<4>
    SatIDs        = PrerunInfo<5>
    Usages        = ''
    InWfrIDs      = ''
    NumWfrsNeeded = 0
    NumDummyWfrs  = 0
    
    Set_Property(Window:'.SUSC_SERIAL', 'TEXT', SuscSerial)
    
    For each Scribe in Scribes using @VM setting vPos
        WaferID   = GanRunID[1,7]:'W'
        DummyID   = GanRunID[1,7]:'D'
        EmptyID   = GaNRunID[1,7]:'E'
        EmptyFlag = (Indexc(Scribe, 'EMPTY', 1) > 0)
        DummyFlag = (Indexc(Scribe, 'DUMMY', 1) > 0)
        
        Begin Case
            Case ( (Scribe[1,8] EQ DummyID) or (DummyFlag EQ True$) )
                Usages<1, vPos> = 'DUMMY'
                NumDummyWfrs += 1
                
            Case ( (Scribe[1,8] EQ EmptyID) or (EmptyFlag EQ True$) )
                Usages<1, vPos> = 'EMPTY'
                
            Case Scribe[1,8] EQ WaferID
                Usages<1, vPos> = 'PROD'
                NumWfrsNeeded += 1
                       
            Case Otherwise$
                Usages<1, vPos> = 'PROD'
                NumWfrsNeeded += 1
        End Case 
        
    Next Scribe
    
    AvailInWfrs = GaN_Services('GetPreEpiWfrs', WONo, NumWfrsNeeded)
    If Error_Services('NoError') then
        AvailInWfrIndex = 1
        For each Usage in Usages using @VM setting vPos
            If Usage EQ 'PROD' then
                InWfrIDs<1, vPos> = AvailInWfrs<1, AvailInWfrIndex>
                AvailInWfrIndex += 1
            end
        Next Usage        
        SuscConfigID = Xlate('REACTOR', Reactor, REACTOR_SUSC_CONFIG_ID$, 'X')
        If SuscConfigID NE '' then
            SuscRec = Database_Services('ReadDataRow', 'EPI_SUSCEPTOR', SuscConfigID)
            CarrSlots  = ''
            CarrWfrIDs = ''
            pCnt = DCount(Pockets, @VM)
            For PocketIndex = 1 to pCnt                   
                Locate PocketIndex in SuscRec<EPI_SUSCEPTOR_POCKET_NO$> using @VM setting Pos then
                    CarrSlot = SuscRec<EPI_SUSCEPTOR_CARR_SLOT$,Pos>
                    CarrSlots<1,PocketIndex>  = CarrSlot
                    CarrWfrIDs<1,PocketIndex> = RDSNo:'.':CarrSlot
                end               
            Next PocketIndex                         
            ReactorLoadList    = ''
            ReactorLoadList<1> = Usages
            ReactorLoadList<2> = Pockets
            ReactorLoadList<3> = Scribes
            ReactorLoadList<4> = SatIDs
            ReactorLoadList<5> = InWfrIDs
            ReactorLoadList<6> = CarrSlots
            ReactorLoadList<7> = CarrWfrIDs      
            Set_Property(@Window:'.REACTOR_TABLE', 'ARRAY', ReactorLoadList)
            Set_Property(@Window, '@NUM_DUMMY_WFRS', NumDummyWfrs)
            
            // Verify wafer list is complete, otherwise disable the load confirmation button.
            NumWfrs = 0
            For WfrIndex = 1 to 8
                If InWfrIDs<0, WfrIndex> NE '' then NumWfrs += 1
            Next WfrIndex
            If NumWfrs LT 5 then
                ErrorMessage = 'There was an error retrieving run information. Unable to load at this time.'
                ErrMsg(ErrorMessage)
                Set_Property(@Window:'.CONFIRM_BUTTON', 'ENABLED', -1)
            end
            FormSize = ''
            SRP_Show_Window(@Window, 'REACT_RUN', 'C', 'C', 1, '', False$, False$, FormSize)
        end else           
            ErrorMessage = 'SUSC_CONFIG_ID value has not been set up on reactor ':Reactor:'. Unable to load.'
            ErrMsg(ErrorMessage)
            End_Dialog(@Window, False$)
        end       
    end else
        ErrorMessage = Error_Services('GetMessage')
        ErrMsg(ErrorMessage)
        End_Dialog(@Window, False$)
    end
    
End Event


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


Event CANCEL_BUTTON.CLICK()
    
    End_Dialog(@Window, False$)
    
end event


Event CONFIRM_BUTTON.CLICK()
    
    CurrDTM          = DateTime()
    StageDesc        = Get_Property(@Window:'.STAGE', 'TEXT')
    StageIndex       = GaN_Services('GetStageIndex', '', StageDesc)
    StageID          = GaN_Services('GetStageID', StageDesc)
    SuscSerial       = Get_Property(@Window:'.SUSC_SERIAL', 'TEXT')
    ReactorLoadArray = Get_Property(@Window:'.REACTOR_TABLE', 'ARRAY')
    RDSNo            = Get_Property(@Window:'.RDS_NO', 'TEXT')
    GaNRunID         = Get_Property(@Window:'.GAN_RUN', 'TEXT')
    Reactor          = Get_Property(@Window:'.REACTOR', 'TEXT')
    WONo             = Get_Property(@Window:'.WO_NO', 'TEXT')
    ToolID           = 'R':Reactor
    Usages           = ReactorLoadArray<1>
    Pockets          = ReactorLoadArray<2>
    Scribes          = ReactorLoadArray<3>
    SatIDs           = ReactorLoadArray<4>
    InWfrIDs         = ReactorLoadArray<5>
    CarrSlots        = ReactorLoadArray<6>
    CarrWfrIDs       = ReactorLoadArray<7>
    
    // Update REACT_RUN record
    ReactRunRec                             = Database_Services('ReadDataRow', 'REACT_RUN', RDSNo)
    ReactRunRec<REACT_RUN_IN_SLOT_ID$>      = InWfrIDs
    ReactRunRec<REACT_RUN_IN_WFR_ID$>       = InWfrIDs
    ReactRunRec<REACT_RUN_LOAD_SIG$>        = @User4
    ReactRunRec<REACT_RUN_LOAD_SIG_DTM$>    = CurrDTM
    ReactRunRec<REACT_RUN_IN_WFR_CHAR$>     = Usages
    ReactRunRec<REACT_RUN_CARR_SLOT_NO$>    = CarrSlots
    ReactRunRec<REACT_RUN_PKT_SAT_ID$>      = SatIDs
    ReactRunRec<REACT_RUN_GAN_SUSC_SERIAL$> = SuscSerial

    // Bind current GaN Param Keys from the PSN to the REACT_RUN record
    PSNo = Xlate('REACT_RUN', RDSNo, 'PS_NO', 'X')
    
    GaNParamKeys = Xlate('PROD_SPEC', PSNo, 'GAN_PARAM_KEYS', 'X')
    If GaNParamKeys NE '' then
        ReactRunRec<REACT_RUN_GAN_PARAM_KEYS$> = GaNParamKeys
        // Store list of critical stages
        CritStages = ''
        For each GaNParamKey in GaNParamKeys using @VM
            CritStage = Field(GaNParamKey, '*', 2, 1)
            CritStages<0, -1> = CritStage
        Next GaNParamKey
        ReactRunRec<REACT_RUN_GAN_CRITICAL_STAGES$> = CritStages
    end
    
    GaNStageRoute = Xlate('PROD_SPEC', PSNo, 'PRS_STAGE_ROUTE', 'X')
    If GaNStageRoute NE '' then
        ReactRunRec<REACT_RUN_GAN_STAGE_ROUTE$> = GaNStageRoute
    end else
        ReactRunRec<REACT_RUN_GAN_STAGE_ROUTE$> = Database_Services('ReadDataRow', 'APP_INFO', 'DEFAULT_GAN_STAGE_ROUTE')
    end
    Database_Services('WriteDataRow', 'REACT_RUN', RDSNo, ReactRunRec, True$, False$, True$)  

    // Update WO_WFR record(s) with scribe, event, location, DTM, etc.
    For each InWfrID in InWfrIDs using @VM setting vPos
        // Ensure the wafer is PROD by looking for a value
        If InWfrID NE '' then
            WOWfrRec = Database_Services('ReadDataRow', 'WO_WFR', InWfrID)
            LocDTMs = WOWfrRec<WO_WFR_LOC_DTM$>
            NextIndex = DCount(LocDTMs, @VM) + 1
            WOWfrRec<WO_WFR_LOC_DTM$, NextIndex>   = CurrDTM
            WOWfrRec<WO_WFR_LOC_BY$, NextIndex>    = @User4
            WOWfrRec<WO_WFR_LOC_EVENT$, NextIndex> = 'LOAD'
            WOWfrRec<WO_WFR_SLOT_ID$>              = InWfrID
            WOWfrRec<WO_WFR_RUN_LOC$, NextIndex>   = RDSNo:'.':vPos:'.'
            WOWfrRec<WO_WFR_CARR_SLOT$, NextIndex> = CarrWfrIDs<0, vPos>
            WOWfrRec<WO_WFR_GAN_RUN_ID$>           = GaNRunID             
            WOWfrRec<WO_WFR_TOOL_ID$, NextIndex>   = ToolID
            WOWfrRec<WO_WFR_INV_LOC$, NextIndex>   = ''
            WOWfrRec<WO_WFR_SCRIBE$>               = Scribes<0, vPos>
            WOWfrRec<WO_WFR_POCKET$>               = Pockets<0, vPos>
            Database_Services('WriteDataRow', 'WO_WFR', InWfrID, WOWfrRec, True$, False$, True$)
            // Move wafer(s) to appropriate TOOL_WFR queue
            GaN_Services('MoveWfrToQueue', InWfrID, ToolID)
            // Mark inbound material consumed (i.e. move WfrID to PrevWfrID within WO_MAT_WFR (WorkOrder*InboundCassette) record)
            InboundCass          = Field(InWfrID, '*', 2, 1)
            WOMatWfrKey          = WONo:'*':InboundCass
            WOMatWfrRec          = Database_Services('ReadDataRow', 'WO_MAT_WFR', WOMatWfrKey)
            ReadyQ               = WOMatWfrRec<WO_MAT_WFR_IN_WFR_ID$>
            ConsumedQ            = WOMatWfrRec<WO_MAT_WFR_IN_PREV_WFR_ID$>
            SlotNo               = Field(InWfrID, '*', 3, 1)
            ReadyQ<0, SlotNo>    = ''
            ConsumedQ<0, SlotNo> = InWfrID
            WOMatWfrRec<WO_MAT_WFR_IN_WFR_ID$>      = ReadyQ
            WOMatWfrRec<WO_MAT_WFR_IN_PREV_WFR_ID$> = ConsumedQ
            Database_Services('WriteDataRow', 'WO_MAT_WFR', WOMatWfrKey, WOMatWfrRec, True$, False$, True$)
        end            
    Next InWfrID       
    // Remove run number from current LOCATION queue
    WOMatKey = Xlate('REACT_RUN', RDSNo, 'WO_MAT_KEY', 'X')
    GaN_Services('RemoveCassFromWIP', WOMatKey)
    // Close dialog. Return True$ for success, otherwise present error message
    If Error_Services('NoError') then
        // Update RUN_STAGE record
        RunStageKey = RDSNo:'*':StageID
        RunStageRec = Database_Services('ReadDataRow', 'RUN_STAGE', RunStageKey)
        InvAct = 'LOAD'
        InvActs = RunStageRec<RUN_STAGE_SPEC_INV_ACTION$>
        Locate InvAct in InvActs using @VM setting vPos then
            RunStageRec<RUN_STAGE_TOOL_ID$>            = ToolID
            RunStageRec<RUN_STAGE_SIA_COMP_BY$, vPos>  = @User4
            RunStageRec<RUN_STAGE_SIA_COMP_DTM$, vPos> = CurrDTM
            RunStageRec<RUN_STAGE_STATUS$>             = 'START'    
            Database_Services('WriteDataRow', 'RUN_STAGE', RunStageKey, RunStageRec, True$, False$, True$)
            Result                                     = True$
        end               
        End_Dialog(@Window, Result)
    end else
        ErrorMessage = Error_Services('GetMessage')
        ErrMsg(ErrorMessage)
    end
    
end event

