Compile function NDW_EPP_MET_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_EPP_Met_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)
        03/11/22    djs     Created initial commuter module.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler
#window NDW_EPP_MET_REPORT

$Insert APP_INSERTS
$Insert EVENT_SETUP
$Insert MSG_EQUATES

Declare subroutine SRP_Show_Window, Report_Services
Declare function   Epi_Part_Services, Environment_Services, Error_Services

// 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

Return EventFlow else 1

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Event WINDOW.CREATE(CreateParam)
    
    GoSub SetupOLEControls
    FormSize = ''
    SRP_Show_Window(@Window, '', 'C', 'C', True$, '', '', '', FormSize, True$)
    
End Event


Event OLE_SUBCLASS.OnOptionClick(CtrlId, Sel)
    
    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    Begin Case
        Case CtrlId EQ @Window:'.EDL_DATE_FROM'
            SelDate = Dialog_Box('POPUP_YEAR', @Window, '')
            DateTo  = Get_Property(@Window:'.EDL_DATE_TO', 'TEXT')
            DateTo  = IConv(DateTo, 'D')
            If ( (DateTo NE '') and (SelDate GT DateTo) ) then
                Set_Property(@Window : '.EDL_DATE_FROM', 'FOCUS', True$)
                Set_Property(@Window : '.EDL_DATE_FROM', 'SELECTION', 1 : @FM : 999)
                Message = '"Date From" cannot be after "Date To"'
                Form_Services('DisplayControlMessage', Message, 'Invalid date selected', @Window : '.EDL_DATE_FROM', 'VALIDATION', 'RGB(229,20,0)')
            end else
                Set_Property(@Window:'.EDL_DATE_FROM', 'TEXT', OConv(SelDate, 'D4/'))
            end
        Case CtrlId EQ @Window:'.EDL_DATE_TO'
            SelDate  = Dialog_Box('POPUP_YEAR', @Window, '')
            DateFrom = Get_Property(@Window:'.EDL_DATE_FROM', 'TEXT')
            DateFrom = IConv(DateFrom, 'D')
            If ( (DateFrom NE '') and (SelDate LT DateFrom) ) then
                Set_Property(@Window : '.EDL_DATE_TO', 'FOCUS', True$)
                Set_Property(@Window : '.EDL_DATE_TO', 'SELECTION', 1 : @FM : 999)
                Message = '"Date To" cannot be before "Date From"'
                Form_Services('DisplayControlMessage', Message, 'Invalid date selected', @Window : '.EDL_DATE_TO', 'VALIDATION', 'RGB(229,20,0)')
            end else
                Set_Property(@Window:'.EDL_DATE_TO', 'TEXT', OConv(SelDate, 'D4/'))
            end
        Case CtrlId EQ @Window:'.EDL_EXPORT_DIR'
            ReportsPath = Environment_Services('GetReportsRootPath')
            CFOpt       = ""
            CFOpt<1>    = 1                     ;* Save as dialog
            CFOpt<2>    = "Comma Separated Values (*.csv)/"
            CFOpt<3>    = ""
            CFOpt<4>    = "EPPMetReport.csv"    ;* default filename
            CFOpt<5>    = ""
            CFOpt<6>    = ReportsPath:'\'       ; * initial directry
            ExportDir   = Utility("CHOOSEFILE", @window, CFOpt)
            Set_Property(@Window:'.EDL_EXPORT_DIR', 'TEXT', ExportDir)
            
    End Case
    GoSub EnableRunReportButton
    
end event


Event OLE_SUBCLASS.OnComboClick(CtrlId, Sel, Value)
    
    GoSub EnableRunReportButton
    
end event


Event EDL_DATE_FROM.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    
end event


Event EDL_DATE_FROM.CHANGED(NewData)
    
    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    GoSub EnableRunReportButton
    
end event


Event EDL_DATE_TO.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    
end event


Event EDL_DATE_TO.CHANGED(NewData)
    
    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    GoSub EnableRunReportButton
    
end event


Event EDL_PART_NO.CHANGED(NewData)
    
    GoSub EnableRunReportButton
    
end event


Event EDL_EXPORT_DIR.CHANGED(NewData)
    
    GoSub EnableRunReportButton
    
end event


Event PUB_RUN_REPORT.CLICK()
    
    DateFrom  = Get_Property(@Window:'.EDL_DATE_FROM', 'TEXT')
    DateTo    = Get_Property(@Window:'.EDL_DATE_TO', 'TEXT')
    EpiPartNo = Get_Property(@Window:'.EDL_PART_NO', 'TEXT')
    ExportDir = Get_Property(@Window:'.EDL_EXPORT_DIR', 'TEXT')
    Report_Services('GenerateEPPMetReport', DateFrom, DateTo, EpiPartNo, ExportDir)
    If Error_Services('NoError') then
        MsgStruct         = ''
        MsgStruct<MICON$> = '*' ; // Info icon
        Msg(@Window, MsgStruct, 'OK', '', 'Success':@FM:'Report successfully saved to ':ExportDir:'.')
    end else
        MsgStruct         = ''
        MsgStruct<MICON$> = 'H' ; // Error icon
        Msg(@Window, MsgStruct, 'OK', '', 'Error':@FM:'Error generating report. Error message: ':Error_Services('GetMessage'))
    end
    
end event


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


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

SetupOLEControls:
    
    Qualify    = ''
    Qualify<1> = 1
    Qualify<4> = 0
    
    Send_Message(@Window:".OLE_SUBCLASS", 'QUALIFY_EVENT', 'OLE.OnOptionClick', Qualify)
    Send_Message(@Window:".OLE_SUBCLASS", 'QUALIFY_EVENT', 'OLE.OnComboClick', Qualify)
    
    Ctrls = 'EDL_DATE_TO,EDL_DATE_FROM,EDL_PART_NO,EDL_EXPORT_DIR'
    For each Ctrl in Ctrls using ','
        CtrlId = @Window:'.':Ctrl
        Handle = Get_Property(CtrlId, 'HANDLE')
        Send_Message(@Window:'.OLE_SUBCLASS', 'OLE.Subclass', Handle, CtrlId)
        Convert "." to ";" in CtrlId
        Begin Case
            Case ( (Ctrl EQ 'EDL_DATE_TO') or (Ctrl EQ 'EDL_DATE_FROM') )
                
                Set_Property(@Window:".OLE_SUBCLASS", "OLE.OptionButton[":CtrlId:"]", 1)
                ImageArray    = ''
                ImageArray<1> = '.\BMPS\calendar.png'
                ImageArray<2> = 'Auto'
                Set_Property(@Window:".OLE_SUBCLASS", "OLE.OptionImage[":CtrlId:"]", ImageArray)
                
            Case Ctrl EQ 'EDL_PART_NO'
                
                EPPEpiParts          = Epi_Part_Services('GetEpiParts', 'EpiPro')
                Swap @FM with @STM in EPPEpiParts
                ComboDropDown        = ""
                ComboDropDown<1>     = 1
                ComboDropDown<2, 1>  = 'EpiPartNo'
                ComboDropDown<2, 2>  = "L":@STM:60
                ComboDropDown<2, 3>  = EPPEpiParts
                ComboDropDown<2, 4>  = 1                         ;// column  contains the values we care about
                ComboDropDown<2, 5>  = 1                         ;// auto fill on
                ComboDropDown<2, 6>  = 0                         ;// case sensitive off
                ComboDropDown<2, 7>  = 10                        ;// 10 visible rows max
                ComboDropDown<2, 8>  = 0                         ;// Don't fire the OnOptionClick
                ComboDropDown<2, 9>  = 1                         ;// Reduce the list to partial matches
                ComboDropDown<2, 10> = 0                        ;// Only show the drop down when the user types
                ComboDropDown<2, 11> = 0                        ;// Do not use LIST Format
                ComboDropDown<2, 12> = 1                        ;// Autofill on first names
                ComboDropDown<2, 13> = 1                        ;// Hide dropdown when user clears cell
                ComboDropDown<2, 14> = 0                        ;// Let navigation keys show the drop down
                ComboDropDown<2, 15> = 0                        ;// Show the drop down regardless of the contents
                ComboDropDown<2, 16> = 0                        ;// Show the drop down during autofill
                ComboDropDown<2, 17> = 1                        ;// Remove selection when user backspaces/deletes
                ComboDropDown<2, 18> = 0                        ;// Show Popup while in read only mode
                ComboDropDown<2, 19> = 1                        ;// Show Popup When Navigating
                ComboDropDown<2, 20> = 1                        ;// Always Tab Out on Enter
                ComboDropDown<2, 21> = -1                       ;// Always show the dropdown above when close to the screen bottom
                Set_Property(@Window:".OLE_SUBCLASS", "OLE.Combo[":CtrlId:"]", ComboDropDown)
                
            Case Ctrl EQ 'EDL_EXPORT_DIR'
                
                Set_Property(@Window:".OLE_SUBCLASS", "OLE.OptionButton[":CtrlId:"]", 1)
                ImageArray    = ''
                ImageArray<1> = '.\SRP Editor\SRPEditorImages\open.png'
                ImageArray<2> = 'Auto'
                Set_Property(@Window:".OLE_SUBCLASS", "OLE.OptionImage[":CtrlId:"]", ImageArray)
                
        End Case
    Next Ctrl
   
return


EnableRunReportButton:
    
    DateFrom  = Get_Property(@Window:'.EDL_DATE_FROM', 'TEXT')
    EpiPartNo = Get_Property(@Window:'.EDL_PART_NO', 'TEXT')
    ExportDir = Get_Property(@Window:'.EDL_EXPORT_DIR', 'TEXT')
    Enabled   = ( (DateFrom NE '') and (EpiPartNo NE '') and (ExportDir NE '') )
    Set_Property(@Window:'.PUB_RUN_REPORT', 'ENABLED', Enabled)
    
return


