Compile function NDW_TEST_WAFER_REPORT_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_Wafer_Report_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/12/24    djs     Updated commuter module to use a temporary path instead of the user's desktop because
							FAB clients do not have write permissions to the desktop.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_TEST_WAFER_REPORT

$Insert EVENT_SETUP
$Insert APP_INSERTS
$Insert MSG_EQUATES
$Insert POPUP_EQUATES

Declare function Get_Property, Utility, Environment_Services, Error_Services, SRP_Array
Declare subroutine Set_Property, Send_Message, Placedialog, Test_Run_Services, Msg, ErrMsg, Popup

// 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)
	
    GoSub InitializeForm
    GoSub SetupOLEControls
    
End Event


Event OLE_FROM_CAL.OnSelChange(FirstDate)
	
	SelectedDate    = Get_Property(CtrlEntId, 'OLE.Selection')
	Set_Property(@Window : '.OLE_SCHEDULE', 'OLE.Date', SelectedDate)
	Set_Property(@Window : '.EDL_FROM','TEXT', OCONV(SelectedDate,'DT4/HS'))
	
End Event


Event OLE_TO_CAL.OnSelChange(FirstDate)
	
	SelectedDate    = Get_Property(CtrlEntId, 'OLE.Selection')
	Set_Property(@Window : '.OLE_SCHEDULE', 'OLE.Date', SelectedDate)
	Set_Property(@Window : '.EDL_TO','TEXT', OCONV(SelectedDate,'DT4/HS'))
	
End Event


Event PUB_DIR_BROWSE.CLICK
	
	value = "Select a directory" : @fm : "c:\temp"
	dir   = Utility( "CHOOSEDIR", @window, value)
	
	if dir NE '' then
		Set_Property(@Window : '.EDL_SAVE_TO_PATH', 'TEXT', dir: '\')	
	end
	
end event


Event PUB_EXPORT_REPORT.CLICK()
	
    SavePath = Get_Property(@Window : '.EDL_SAVE_TO_PATH', 'TEXT')
    FileName = Get_Property(@Window : '.EDL_FILE_NAME', 'TEXT')
    StartDTM = IConv(Get_Property(@Window : '.EDL_FROM','TEXT'),'DT4/HS')
    StopDTM  = IConv(Get_Property(@Window : '.EDL_TO','TEXT'),'DT4/HS')
    If SavePath NE '' then
        If SavePath[Len(SavePath), 1] NE '\' then
            SavePath = SavePath : '\'
        end
        Swap '/' with '\' in SavePath
        If FileName NE '' then
            Test_Run_Services('GenerateTWCSVReport', StartDTM, StopDTM, SavePath : FileName)
            If Error_Services('NoError') then
                Msg(@Window, 'File saved to ' : SavePath : FileName)
            end else
                Msg(@Window , Error_Services('GetMessage'))
            end
        end else
            Msg(@Window, 'You must specify a file name to save as.')
        end
    end else
        Msg(@Window, 'You must specify a location to save the file to.')
    end
    
end event


Event PUB_VIEW_REPORT.CLICK()
	
    SavePath = Get_Property(@Window : '.EDL_SAVE_TO_PATH', 'TEXT')
    FileName = Get_Property(@Window : '.EDL_FILE_NAME', 'TEXT')
    StartDTM = IConv(Get_Property(@Window : '.EDL_FROM','TEXT'),'DT4/HS')
    StopDTM  = IConv(Get_Property(@Window : '.EDL_TO','TEXT'),'DT4/HS')
    If SavePath NE '' then
        If SavePath[Len(SavePath), 1] NE '\' then
            SavePath = SavePath : '\'
        end
        Swap '/' with '\' in SavePath
        If FileName NE '' then
            Test_Run_Services('GenerateTWCSVReport', StartDTM, StopDTM, SavePath : FileName)
            If Error_Services('NoError') then
				OSRead FileData from SavePath:FileName then
					Convert CRLF$ to @FM  in FileData
					Convert ','   to @VM in FileData
					FileData    = Delete(FileData, 1, 0, 0)
					FileData    = SRP_Array('Rotate', FileData, @FM, @VM)
					FileData<2> = IConv(FileData<2>, 'DT')
					FileData    = SRP_Array('Rotate', FileData, @FM, @VM)
					FileData    = SRP_Array('SortRows', FileData, 'DR1', 'LIST', @FM, @VM, False$)
					Swap @VM with @SVM in FileData
					Swap @FM with @VM in FileData
					MsgStruct            = ''
					MsgStruct<PDISPLAY$> = FileData
					Popup(@Window, MsgStruct, 'TEST_WAFER_REPORT')
				end
            end else
                Msg(@Window , Error_Services('GetMessage'))
            end
        end else
            Msg(@Window, 'You must specify a file name to save as.')
        end
    end else
        Msg(@Window, 'You must specify a location to save the file to.')
    end
	
end event

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

InitializeForm:
	
    PlaceDialog(-2, -2) 
    DefaultPath = Environment_Services('GetTempPath')
    If DefaultPath NE '' then
        Set_Property(@Window : '.EDL_SAVE_TO_PATH', 'TEXT', DefaultPath)	
    end
    
    //Set file name to a default
    Set_Property(@Window : '.EDL_FILE_NAME', 'TEXT', 'TWUsageReport.csv')
    
return


SetupOLEControls:
	
    Ctrl = @Window : '.OLE_FROM_CAL'
    Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
    Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '9')
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', 1)

    Ctrl = @Window : '.OLE_TO_CAL'
    Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
    Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '9')
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', 1)
    
return


