Compile function NDW_VIEW_LOT_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_VIEW_LOT

Declare function Get_Property, Lot_Services, Database_Services, Lot_Event_Services
Declare subroutine Set_Property

$Insert APP_INSERTS
$Insert LOT_EQUATES
$Insert LOT_OPERATION_EQUATES
$Insert LOT_EVENT_EQUATES

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)
    GoSub   SetupFormControls
    
End Event

Event EDL_LOT_ID.LOSTFOCUS(Flag, FocusID)
    GoSub GetLot
end event

Event PUB_SEARCH_LOT.CLICK()
    GoSub GetLot
end event


//-----------------------------------------------------------------------------
// INTERNAL GO-SUBS
//-----------------------------------------------------------------------------

SetupFormControls:
    
    EditLineFont        = Get_Property(@Window : '.EDL_LOT_ID', 'FONT')
    EditLineFont<1,1,2> = -20
    EditLineFont<1,1,6> = 10
    Set_Property(@Window : '.EDL_LOT_ID', 'FONT', EditLineFont)
    Set_Property(@Window : '.LBL_LOT_ID', 'FONT', EditLineFont)
    Set_Property(@Window : '.EDL_CURR_OPERATION', 'FONT', EditLineFont)
    
return

GetLot:
    //Get Lot
    LotIdText = Get_Property(@Window : '.EDL_LOT_ID', 'TEXT')
    If LotIdText[1, 2] EQ '1T' then
        LotIdText[1,2] = ''
    end
    Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', LotIdText)
    If RowExists('LOT', LotIdText) then
        ThisLotId                = LotIdText
        ThisLotRec               = Database_Services('ReadDataRow', 'LOT', ThisLotId)
        ThisLotCurrOperationID   = Lot_Services('GetLotCurrOperationId', ThisLotId)
        ThisLotCurrOperationName = Xlate('LOT_OPERATION', ThisLotCurrOperationId, LOT_OPERATION_OPERATION_ID$, 'X')
        ThisLotCurrWaferQty      = Database_Services('ReadDataColumn', 'LOT', ThisLotId, LOT_WAFER_QTY$, True$, 0, False$)
        ThisLotMostRecentEventId = ThisLotRec<LOT_MOST_RECENT_LOT_EVENT_ID$>
        ThisLotMostRecentEventDtm = XLATE('LOT_EVENT', ThisLotMostRecentEventId, LOT_EVENT_EVENT_DATETIME$, 'X')
        
        //Lot Operation Status'
        LotOperationsInSequence  = Lot_Services('GetLotOperationSequence', ThisLotId)
        LotOperationTableData    = ''
        For each LotOperation in LotOperationsInSequence using @FM setting dPos
            LotOperationRec              = Database_Services('ReadDataRow', 'LOT_OPERATION', LotOperation)
            LotOperationTableData<1, -1> = LotOperationRec<LOT_OPERATION_OPERATION_ID$>
            LotOperationTableData<2, -1> = LotOperationRec<LOT_OPERATION_OPERATOR_IN_ID$>
            LotOperationTableData<3, -1> = Oconv(LotOperationRec<LOT_OPERATION_DATETIME_IN$>, 'DT')
            LotOperationTableData<4, -1> = LotOperationRec<LOT_OPERATION_WAFER_IN_QTY$>
            LotOperationTableData<5, -1> = LotOperationRec<LOT_OPERATION_OPERATOR_OUT_ID$>
            LotOperationTableData<6, -1> = Oconv(LotOperationRec<LOT_OPERATION_DATETIME_OUT$>, 'DT')
            LotOperationTableData<7, -1> = LotOperationRec<LOT_OPERATION_WAFER_OUT_QTY$>
        Next LotOperation
        
        //Lot Event Status'
        LotEventsInSequence = Lot_Event_Services('GetLotEventsInSequence', ThisLotId)
        LotEventTableData   = ''
        For each LotEvent in LotEventsInSequence using @FM
            LotEventRec                             = Database_Services('ReadDataRow', 'LOT_EVENT', LotEvent)
            ThisEventSequence                       = LotEventRec<LOT_EVENT_SEQUENCE$>
            LotEventTableData<1, ThisEventSequence> = OConv(LotEventRec<LOT_EVENT_EVENT_DATETIME$>, 'DT')
            LotEventTableData<2, ThisEventSequence> = LotEventRec<LOT_EVENT_LOT_EVENT_TYPE$>
            LotEventTableData<3, ThisEventSequence> = LotEventRec<LOT_EVENT_EVENT_OPERATOR_ID$>
            ThisLotEventOperationName               = Field(LotEventRec<LOT_EVENT_EVENT_OPERATION_ID$>, '*', 2)
            LotEventTableData<4, ThisEventSequence> = ThisLotEventOperationName
            LotEventTableData<5, ThisEventSequence> = LotEventRec<LOT_EVENT_EVENT_NOTE$>
        Next LotEvent
        
        //Fill form
        Set_Property(@Window : '.EDL_CURR_OPERATION', 'TEXT', ThisLotCurrOperationName)
        Set_Property(@Window : '.EDL_CURR_WAFER_QTY', 'TEXT', ThisLotCurrWaferQty)
        Set_Property(@Window : '.EDT_LOT_OPERATIONS', 'ARRAY', LotOperationTableData)
        Set_Property(@Window : '.EDT_LOT_EVENTS', 'ARRAY', LotEventTableData)
        Set_Property(@Window : '.EDL_MOST_RECENT_EVENT_DTM', 'TEXT', OConv(ThisLotMostRecentEventDtm, 'DT'))
    end else
        //Error: Lot did not exist
    end
return

ResetForm:
    Set_Property(@Window : '.EDL_CURR_OPERATION', 'TEXT', '')
    Set_Property(@Window : '.EDL_CURR_WAFER_QTY', 'TEXT', '')
    Set_Property(@Window : '.EDL_MOST_RECENT_EVENT_DTM', 'TEXT', '')
    Set_Property(@Window : '.EDT_LOT_OPERATIONS', 'ARRAY', '')
    Set_Property(@Window : '.EDT_LOT_EVENTS', 'ARRAY', '')
return

