Compile function NDW_TW_PROD_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_TEST_WAFER_PROD

Declare function Dialog_Box, Database_Services, Test_Run_Services, Get_Property, Error_Services
Declare subroutine Dialog_Box, Set_Property, Placedialog, Msg, Test_Run_Services

$Insert TEST_WAFER_TYPES_DISPO_OPTIONS_EQUATES
$Insert TEST_WAFER_PROD_EQUATES


GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
    PlaceDialog(-2, -2) 
    EventFlow    = 1
    GoSub Refresh
    //Get and set dispo options to the CMB_DISPO_OPTIONS_SELECT dropdown box
    DispoOptions = Database_Services('ReadDataRow', 'APP_INFO', 'TEST_WAFER_TYPES_DISPO_OPTIONS')
    Set_Property(@Window : '.CMB_DISPO_OPTIONS_SELECT', 'LIST', DispoOptions)
    
End Event

Event PUB_CREATE_NEW.CLICK()
    Refresh = Dialog_Box('NDW_NEW_TEST_WAFER_PROD', @Window, '')
    If Refresh then
        GoSub Refresh
    end
end event

Event CMB_TW_PRODS.CHANGED()

    SelectedProd   = PARAM1
    SelectedProdID = Field(SelectedProd, ' - ', 1)
    Swap ' ' with '' in SelectedProdID
    Set_Property(@Window : '.EDL_SEL_PROD_ID', 'TEXT', SelectedProdID)
    GoSub GetSelectedProduct
end event

Event PUB_ADD_DISPO.CLICK()
    //Get Property from select list
    SelectedProperty = Get_Property(@Window : '.CMB_DISPO_OPTIONS_SELECT', 'DEFPROP')
    SelectedProdID   = Get_Property(@Window : '.EDL_SEL_PROD_ID', 'TEXT')
    IF SelectedProdID NE '' then
        If SelectedProperty NE '' then
            //Add it to the record and refresh
            Test_Run_Services('AddTestWaferProductDispo', SelectedProdID, SelectedProperty)
            If Error_Services('NoError') then
                Msg(@Window, 'Dispo Added.')
                GoSub GetProd
            end else
                Msg(@Window, Error_Services('GetMessage'))
            end
        end 
    end else
       Msg(@Window, 'Must select a product first.') 
    end

end event

Event PUB_DELETE_DISPO.CLICK()

    CurrDispoData   = Get_Property(@Window:'.EDT_DISPO_OPTIONS', 'ARRAY')
    SelectedPos     = Get_Property(@Window: '.EDT_DISPO_OPTIONS', "SELPOS")<2>
    SelectedDispo   = CurrDispoData<1, SelectedPos>
    SelectedDispoID = Field(SelectedDispo, ' - ', 1)
    Swap ' ' with '' in SelectedDispoID
    if SelectedDispoID NE '' then
        Test_Run_Services('DeleteTestWaferProductDispo', SelectedDispoID)
        If Error_Services('NoError') then
            Msg(@Window, 'Dispo. Removed!')
            GoSub GetProd
        end else
            Msg(@Window, Error_Services('GetMessage'))
            GoSub GetProd
        end
    end
end event

Event PUB_UPDATE_ORDER.CLICK()
    SelectedProdID = Get_Property(@Window : '.EDL_SEL_PROD_ID', 'TEXT')
    SelectedOrder  = Get_Property(@Window : '.EDL_SORT_ORDER', 'TEXT')
    
    IF SelectedOrder NE '' then
        If Num(SelectedOrder) then
            Test_Run_Services('UpdateTWProdSortOrder', SelectedProdID, SelectedOrder)
            If Error_Services('NoError') then
                GoSub Refresh
            end else
                ErrorMsg = Error_Services('GetMessage')
                Msg(@Window, ErrorMsg)
            end
        end else
            Msg(@Window, 'Value for sort order must be a number!')
        end
    end else
        Msg(@Window, 'Must enter a value for sort order!')
    end
end event

//-----------------------------------------------------------------------------
// INTERNAL GOSUBS
//-----------------------------------------------------------------------------
Refresh:
    //Get a list of all the product types
    Prods       = Test_Run_Services('GetAllTWProdKeys')
    ProdOptions = ''
    for each prod in prods using @VM setting pPos
        ProdId   = Prods<1, pPos>
        ProdName = Prods<2, pPos>
        if prodId NE '' then
            ProdOptions<-1> = ProdID : ' - ' : ProdName
        end
    Next prod
    Set_Property(@Window : '.CMB_TW_PRODS', 'LIST', ProdOptions)
    //Get dispo options
    //Get and set dispo options to the CMB_DISPO_OPTIONS_SELECT dropdown box
    DispoOptions = Database_Services('ReadDataRow', 'APP_INFO', 'TEST_WAFER_TYPES_DISPO_OPTIONS')
    Set_Property(@Window : '.CMB_DISPO_OPTIONS_SELECT', 'LIST', DispoOptions)
    
    //Clear out old Values
    Set_Property(@Window : '.EDT_DISPO_OPTIONS', 'LIST', '')
    Set_Property(@Window : '.EDL_SEL_PROD_ID', 'TEXT', '')
    Set_Property(@Window : '.EDL_PROD_NAME', 'TEXT', '')
return

GetSelectedProduct:

    swap ' - ' with '*' in SelectedProd
    SelectedProdID   = Field(SelectedProd, '*', 1)
    SelectedProdName = Field(SelectedProd, '*', 2)
    GoSub GetProd
return

GetProd:
    //Clear out old Values
    //Set_Property(@Window : '.EDT_DISPO_OPTIONS', 'LIST', '')
    
    SelectedProdID  = Get_Property(@Window : '.EDL_SEL_PROD_ID', 'TEXT')
    SelectedProdRec = Database_Services('ReadDataRow', 'TEST_WAFER_PROD', SelectedProdID)
    ProdDispoIDs    = SelectedProdRec<TEST_WAFER_PROD_WAFER_DISPO_IDS$>
    CurrentDispos   = ''
    for each ProdDispoID in ProdDispoIDs using @VM setting dPos
        if ProdDispoID NE '' then
            ProdDispoDesc     = XLATE('TEST_WAFER_TYPES_DISPO_OPTIONS', ProdDispoID, TEST_WAFER_TYPES_DISPO_OPTIONS_DISPO_DESCRIPTION$, 'X')
            CurrentDispos<-1> = ProdDispoID : ' - ' : ProdDispoDesc
        end
    Next ProdDispoID
    CurrentSortOrder = SelectedProdRec<TEST_WAFER_PROD_SORT_ORDER$>
    Set_Property(@Window : '.EDL_PROD_NAME', 'TEXT', SelectedProdName)
    Set_Property(@Window : '.EDT_DISPO_OPTIONS', 'LIST', CurrentDispos)
    Set_Property(@Window : '.EDL_SEL_PROD_ID', 'TEXT', SelectedProdID)
    Set_Property(@Window : '.EDL_SORT_ORDER', 'TEXT', CurrentSortOrder)
return




