Compile function NDW_TEST_RUN_EVENTS(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   NDW_Test_Run_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        06/05/24    djs     Updated commuter module to keep form open after clicking submit button. (backlog req. 849)                    

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_TEST_RUN

Declare function Get_Property, Test_Run_Services, Database_Services, Reactor_Services, Tool_Services, Test_Run_Services
Declare subroutine Set_Property, Test_Run_Services, PlaceDialog

$Insert EVENT_SETUP
$Insert RDS_EQUATES
$Insert TEST_RUN_TYPE_EQUATES
$Insert APP_INSERTS
$Insert LOGICAL

TWTableOLECtrl  = @Window : '.OLE_TW_USAGE_TABLE'

// 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)
	PlaceDialog(-2, -2)
    Set_Property(@Window, 'VISIBLE', 1)
	GoSub SetupOLEControls
	GoSub FillFormMasterData
	EventFlow = 1
	PlaceDialog(-2, -2)
	
End Event


Event EDL_RDS.CHANGED(NewData)
	
	RDSNo = NewData
	If Len(RDSNo) GE 6 then
		If RowExists('RDS', RDSNo) then
			RDSRec    = Database_Services('ReadDataRow', 'RDS', RDSNo)
			PSN       = RDSRec<RDS_PROD_SPEC_ID$>
			ReactorNo = RDSRec<RDS_REACTOR$>
			If PSN NE '' then
				Set_Property(@Window : '.EDL_PSN', 'TEXT', PSN)
				Set_Property(@Window : '.EDL_PSN', 'ENABLED', -1)
			end
			If ReactorNo NE '' then
				Set_Property(@Window : '.CMB_EQUIP_TYPE', 'VALUE', 'Reactor')
				Set_Property(@Window : '.CMB_EQUIP_TYPE', 'ENABLED', -1)
				Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', ReactorNo)
				Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'ENABLED', -1)
			end
		end
	end else
		Set_Property(@Window : '.EDL_PSN', 'TEXT', '')
		Set_Property(@Window : '.EDL_PSN', 'ENABLED', True$)
		Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', '')
		Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'ENABLED', True$)
		Set_Property(@Window : '.CMB_EQUIP_TYPE', 'ENABLED', True$)
	end
	
end event


Event CMB_EQUIP_TYPE.CHANGED(Selection)
	
	Begin Case
		Case Selection EQ 'Reactor'
			EqpOpts = Get_Property(@Window , '@REACTOR_OPTS', 'TEXT')
		Case Selection EQ 'Non-Reactor'
			EqpOpts = Get_Property(@Window , '@NON_REACTOR_OPTS', 'TEXT')
		Case 0
			EqpOpts = Get_Property(@Window , '@REACTOR_OPTS', 'TEXT')
	End Case
	Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'LIST', EqpOpts)
	
end event


Event PUB_ADD_ROW.CLICK()
	
	Index = Send_Message(TWTableOLECtrl, "OLE.InsertRecords", -1, 1)
	
end event


Event PUB_DELETE_ROW.CLICK()
	
	//Get currently selected Row
	SelectedCell   = Get_Property(TWTableOLECtrl, 'OLE.SelPos')
	If SelectedCell then
		SelectedRow = SelectedCell<2>
		rv = Send_Message(TWTableOLECtrl, "OLE.DeleteRecords", SelectedRow, 1)
	end
	
end event


Event PUB_SUBMIT.CLICK()
	
	NotReadyReason = ''
	//Gather all field information
	ReasonForTest  = Get_Property(@Window : '.CMB_TEST_TYPE', 'TEXT')
	RelatedRDS     = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	RelatedPSN     = Get_Property(@Window : '.EDL_PSN', 'TEXT')
	EquipmentType  = Get_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT')
	EquipmentID    = Get_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT')
	
	//Translate selection to IDs. Get Run Type ID
	SelRunTypeID = '';*Test Run Obj variable 
	RunTypeOpts  = Get_Property(@Window, '@RUN_TYPE_OPTS')
	Locate ReasonForTest in RunTypeOpts<2> using @VM setting rPos then
		SelRunTypeID = RunTypeOpts<1, rPos>
	end else
		NotReadyReason := 'Must select a valid run type reason.' : CRLF$ 
	end
	
	//Translate Equipment Type. R for Reactor, T for non-reactor
	SelEquipType = '';*Test Run Obj variable 
	Begin Case
		Case EquipmentType EQ 'Reactor'
			SelEquipType = 'R'
		Case EquipmentType EQ 'Non-Reactor'
			SelEquipType = 'T'
	End Case
	
	//Get Test wafer usage data from table
	TWProdOpts  = Get_Property(@Window, '@TW_PROD_OPTS')
	TWUsageData = Get_Property(TWTableOLECtrl, 'OLE.RecordData[All]')
	ProdUsages  = ''
	for each TWRow in TWUsageData using @FM
		ProdID   = ''
		Quantity = TWRow<1,2>
		ProdName = TWRow<1,1>
		If Quantity NE '' AND ProdName NE '' then
			if Num(Quantity) AND Quantity GT 0 then
				Locate ProdName in TWProdOpts<2> setting tPos then
					ProdID         = TWProdOpts<1, tPos>
					ProdUsages<-1> = ProdID : @VM : Quantity
				end else
					NotReadyReason := 'Invalid Product ID ' : ProdName : ' selected.' : CRLF$ 
				end
			end else
				NotReadyReason := 'Quantity for Product ID ' : ProdName : ' was not a number or was a negative number.' : CRLF$ 
			end
			
		end else
			Begin Case
				Case Quantity NE ''
					NotReadyReason := 'Prod. not selected..' : CRLF$
				Case ProdName NE ''
					NotReadyReason := 'Qty for ' : ProdName : ' is blank' : CRLF$
			End Case
		end
	Next TWRow
	
	If NotReadyReason EQ '' then
		NewTWRunKey = Test_Run_Services('CreateTestRunRecord', SelRunTypeID, SelEquipType, EquipmentID, RelatedPSN, RelatedRDS, @User4)
		If Error_Services('NoError') AND RowExists('TEST_RUN', NewTWRunKey) then
			for each Prod in ProdUsages using @FM setting pPos
				for i = 1 to Prod<1,2>
					TWPartID   = Prod<1,1>
					TRWaferKey = Test_Run_Services('CreateTestRunWaferRecord', NewTWRunKey, TWPartID, @USER4)
					If RowExists('TEST_RUN_WAFER', TRWaferKey) then
						//Do Nothing for now.
					end else
						msg(@Window, 'Error writing Test Run data! Please try again or contact FI for support.')
					end
				Next i
			Next Prod
			If Error_Services('NoError') then
				Msg(@Window, 'Test Run Created Successfully!')
				GoSub ClearFormData
			end
		end else
			msg(@Window, 'Error writing Test Run data! Please try again or contact FI for support.')
		end
	end else
		Msg(@Window, NotReadyReason)
	end
	
end event

Event PUB_CANCEL.CLICK()
	
	Post_Event(@Window, 'CLOSE')
	
end event

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ClearFormData:
	
	Set_Property(@Window : '.CMB_TEST_TYPE', 'TEXT', '')
	Set_Property(@Window : '.EDL_RDS', 'TEXT', '')
	Set_Property(@Window : '.EDL_PSN', 'TEXT', '')
	Set_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT', 'Reactor')
	Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', '')
	Set_Property(@Window : '.OLE_TW_USAGE_TABLE', 'OLE.RecordData[All]', '')
	Set_Property(@Window : '.OLE_TW_USAGE_TABLE', 'OLE.Dimension', 2:@FM:1)
	
return


FillFormMasterData:
	
	//Get Test run type options. Populate to CMB_TEST_TYPE
	TestRunTypeList = Test_Run_Services('GetAllTestRunTypes')
	Set_Property(@Window, '@RUN_TYPE_OPTS', TestRunTypeList)
	TestRunTypeOpts = ''
	for each TestRunID in TestRunTypeList<1> using @VM setting tPos
		TestRunName         = TestRunTypeList<2, tPos>
		TestRunTypeOpts<-1> = TestRunName
	Next TestRunID
	Set_Property(@Window : '.CMB_TEST_TYPE', 'LIST', TestRunTypeOpts)
	
	//Get Tool and Reactor Identifiers and buffer them in user defined window properties @REACTOR_OPTS and @NON_REACTOR_OPTS
	//Reactors
	ReactorOpts    = Reactor_Services('GetReactorNumbers')
	Set_Property(@Window : '.EDT_REACTORS', 'TEXT', ReactorOpts)
	Set_Property(@Window, '@REACTOR_OPTS', ReactorOpts)
	
	//Non Reactors
	NonReactorOpts = Tool_Services('GetAllTools')
	swap @VM with @FM in NonReactorOpts
	Set_Property(@Window : '.EDT_NON_REACTORS', 'TEXT', NonReactorOpts)
	Set_Property(@Window, '@NON_REACTOR_OPTS', NonReactorOpts)
	//Then set the inital combo list to be filled with reactors because that is the default.
	Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'LIST', ReactorOpts)
	//Get TW Products and store them in user defined window property @TW_PROD_OPTS
	Prods    = Test_Run_Services('GetAllTWProdKeys', 1)
	Set_Property(@Window, '@TW_PROD_OPTS', Prods)
	
	//Add Product options to the OLE_TW_USAGE_TABLE
	ProdOpts        = Prods<2>
	swap @VM with @STM in ProdOpts
	TWProdOpts<1>   = 'COB'
	Set_Property(TWTableOLECtrl, "OLE.CellType[1; ALL]", ProdOpts)
	TypeData        = ''
	TypeData<1>     = "Combo"
	TypeData<2, 1>  = 'Prod Name'
	TypeData<2, 2>  = "L"
	TypeData<2, 3>  = ProdOpts
	TypeData<2, 4>  = 1                         ;// column 2 contains the values we care about
	TypeData<2, 5>  = 1                          ;// auto fill on
	TypeData<2, 6>  = 0                          ;// case sensitive off
	TypeData<2, 7>  = 10                         ;// 10 visible rows max
	TypeData<2, 8>  = 0                          ;// Don't fire the OnOptionClick
	TypeData<2, 9>  = 1                          ;// Reduce the list to partial matches
	TypeData<2, 10> = 0                         ;// Only show the drop down when the user types
	TypeData<2, 11> = 0                         ;// Do not use LIST Format
	TypeData<2, 12> = 1                         ;// Autofill on first names
	TypeData<2, 13> = 1                         ;// Hide dropdown when user clears cell
	TypeData<2, 14> = 0                         ;// Let navigation keys show the drop down
	TypeData<2, 15> = 0                         ;// Show the drop down regardless of the cell's contents
	TypeData<2, 16> = 0                         ;// Show the drop down during autofill
	TypeData<2, 17> = 1                         ;// Remove selection when user backspaces/deletes
	TypeData<2, 18> = 0                         ;// Show Popup while in read only mode
	TypeData<2, 19> = 1                         ;// Show Popup When Navigating
	TypeData<2, 20> = 1                         ;// Always Tab Out on Enter
	TypeData<2, 21> = -1                        ;// Always show the dropdown above when close to the screen bottom
	
	Set_Property(TWTableOLECtrl, "OLE.CellType[1; All]", TypeData)
	
return


SetupOLEControls:
	
	Set_Property(TWTableOLECtrl, "OLE.AllowInserts", 0)
	Set_Property(TWTableOLECtrl, "OLE.BlankRowManagement", 1:@FM:0)
	Set_Property(TWTableOLECtrl, "OLE.Dimension", 2);*Define number of columns
	//Set Column width of tw prod column
	ColSizeArray    = ''
	ColSizeArray<1> = 150
	Set_Property(TWTableOLECtrl, "OLE.DataColumn[All]", ColSizeArray)
	
	//Set Column Header Titles
	HeaderTitles = 'Test Wafer Prod':@VM:'Quantity'; *Define the column header titles
	Set_Property(TWTableOLECtrl, "OLE.TitleList", HeaderTitles); *Set the column titles
	
	//Sets column header to be invisible
	Set_Property(TWTableOLECtrl, "OLE.HeaderColumn[1]", '':@FM:False$)
	//Set first field as a dropdown
	TWProdOpts      = Get_Property(TWTableOLECtrl, "OLE.CellType[1; ALL]")
	
	//Cell Default coloring
	CellColors    = ""
	CellColors<4> = 'LightCyan'
	Set_Property(TWTableOLECtrl, "OLE.CellSelColors[All; All]", CellColors)
	
	//Selection Properties
	SelArray    = ''
	SelArray<8> = True$
	Set_Property(TWTableOLECtrl, "OLE.SelectionStyle", SelArray)
	//Maintains SelPos on lostfocus
	Set_Property(TWTableOLECtrl, 'OLE.Redraw', False$)
	Set_Property(TWTableOLECtrl, "OLE.ResetSelPos", False$)
	
	//Qualify OLE Events
	Qualifier = ''
	Qualifier<1> = 1
	Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
	Send_Message(TWTableOLECtrl,  'QUALIFY_EVENT', 'OLE.OnLostFocus', Qualifier)
	
return



