Compile function NDW_NEW_TEST_WAFER_PROD_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_NEW_TEST_WAFER_PROD

$Insert APP_INSERTS

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

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
    PlaceDialog(-2, -2) 
    EventFlow    = 1
    //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_ADD_DISPO.CLICK()
    //Get Property from select list
    SelectedProperty = Get_Property(@Window : '.CMB_DISPO_OPTIONS_SELECT', 'DEFPROP')
    If SelectedProperty NE '' then

        CurrentDispoList = Get_Property(@Window : '.EDB_SELECTED_DISPO', 'ARRAY')
        Locate SelectedProperty in CurrentDispoList using @VM setting iPos then
                msg(@Window, 'Disposition type already exists.')
            end else
            //Get Current list of dispos and append to the end.
            
            //Add it to first empty space
            Locate '' in CurrentDispoList using @VM setting emptyPos then
                CurrentDispoList<1,emptyPos> = SelectedProperty
            end else
                CurrentDispoList<1,-1> = SelectedProperty
            end
            Set_Property(@window : '.EDB_SELECTED_DISPO', 'ARRAY', CurrentDispoList)
            end
    end
end event

Event PUB_REMOVE_SELECTED.CLICK()
    //Get selected row
    SelectedPos    = Get_Property(@Window: '.EDB_SELECTED_DISPO', "SELPOS")<2>
    //Get Original Data
    OriginalValues = Get_Property(@window : '.EDB_SELECTED_DISPO', 'ARRAY')
    NewValues      = Delete(OriginalValues, 1, SelectedPos, 0)
    Set_Property(@window : '.EDB_SELECTED_DISPO', 'ARRAY', NewValues)
end event

Event PUB_SAVE.CLICK()
    ResultMsg   = ''
    NewProdID   = ''
    ProductName = Get_Property(@Window : '.EDT_PROD_NAME', 'TEXT')
    DispoValues = Get_Property(@window : '.EDB_SELECTED_DISPO', 'ARRAY')
    If ProductName NE '' then
        NewProdID = Test_Run_Services('CreateNewTestWaferProduct', ProductName)
        if Error_Services('NoError') then
            ResultMsg = 'Product Created'
            If DispoValues NE '' then
                for each DispoValue in DispoValues using @VM
                    If DispoValue NE '' then
                        Test_Run_Services('AddTestWaferProductDispo', NewProdID, DispoValue)
                        if Error_Services('NoError') then
                            ResultMsg := CRLF$ : 'Dispo Value ' : DispoValue : ' Added!'
                        end else
                            ResultMsg := CRLF$ : 'Error while adding ' : DispoValue : ' as a dispo value!'
                        end
                    end
                    
                Next DispoValue
            end
            Msg(@Window, ResultMsg)
            End_Dialog(@Window, 1)
        end else
            errorMsg = Error_Services('GetMessage')
            Msg(@Window, errorMsg)
        end
    end
end event



