Compile function NDW_PM_HISTORY_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_PM_History_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)
        05/14/2025    djs     Modified edit table properties to fit longer descriptions

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_PM_HISTORY

Declare function Get_Property, Obj_Pm_Spec, Database_Services, SRP_Array, SRP_Datetime, Utility, OConv, Datetime
Declare subroutine Set_Property, Send_Message, Msg, PlaceDialog

$Insert EVENT_SETUP
$Insert PM_EQUATES
$Insert PM_SPEC_EQUATES
$Insert APP_INSERTS
$Insert MSG_EQUATES

EQU COL$PMID     to 1
EQU COL$DESC     to 2
EQU COL$DATECOMP to 3
EQU COL$DATEDUE  to 4
EQU COL$USER     to 5

// 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)
    
    EventFlow = 1
    PMSpecID  = CreateParam
    GoSub SETUP_CONTROLS
    IF PMSpecID NE '' then
        GoSub RunReport
    end
    PlaceDialog(-2, -2)
    
End Event

Event PUB_SEARCH.CLICK()
	
    GoSub RunReport
    
end event


Event PUB_EXPORT_TO_EXCEL.CLICK()
    PmsId     = Get_Property(@Window : '.EDL_PM_SPEC', 'TEXT')
    If PmsId NE '' then
        FileRunDate = Oconv(DateTime(), 'DT')
        swap ' ' with '_' in FileRunDate
        swap ':' with '_' in FileRunDate
        FileName    = PmsId : '_' :  FileRunDate : '.csv'
        value       = "Select a directory" : @fm : "c:\temp"
        FilePath    = Utility( "CHOOSEDIR", @window, value) : '\' : FileName

        if FilePath NE '' then
            PmHistoryOutput    = ''
            PmHistoryOutput<1> = 'PM ID' : @VM : 'Description' : @VM : 'Complete Date' : @VM : 'Due Date' : @VM : 'User' 
            PMHistoryArray     = Get_Property(@Window : '.OLE_HIST_TABLE', "OLE.ARRAY")
            PMHistoryArray     = SRP_Array('Rotate',PMHistoryArray)
            for each DataRow in PmHistoryArray using @FM setting dPos
                PmHistoryOutput<-1> = DataRow
            Next DataRow
            swap @VM with ',' in PmHistoryOutput
            swap @FM with CRLF$ in PmHistoryOutput
            OSWrite PmHistoryOutput To FilePath
            FileData = ''
            OSRead FileData From FilePath then
                MsgInfo         = ''
                MsgInfo<mcol$>  = -2
                MsgInfo<MTYPE$> = 'BO'
                MsgInfo<mrow$>  = -2
                MsgInfo<mtext$> = 'File written successfully to ' : FilePath : '!'
                Msg(@Window, MsgInfo)
            End else
                MsgInfo         = ''
                MsgInfo<mcol$>  = -2
                MsgInfo<MTYPE$> = 'BO'
                MsgInfo<mrow$>  = -2
                MsgInfo<mtext$> = 'Error writing message to ' : FilePath : '!'
                Msg(@Window, MsgInfo)
            End
        end
    end
    
end event

//-----------------------------------------------------------------------------
// INTERNAL GOSUBS
//-----------------------------------------------------------------------------

RunReport:
    
    PMSpecId       = Get_Property(@Window : '.EDL_PM_SPEC', 'TEXT')
    PMSpecRecord   = Database_Services('ReadDataRow', 'PM_SPEC', PMSpecId)
    PMDescription  = XLATE('PM_SPEC', PMSpecId, PM_SPEC_DESC$, 'X')
    PMHistory      = obj_pm_spec('GetHistory', PMSpecId);*Returns list of PM Keys delimited by @VM
    PMHistoryArray = ''
	Def            = ""
	Def<MCAPTION$> = "Loading PM History..."
	Def<MTYPE$   > = "GC"
	Def<MEXTENT$ > = DCount(PMHistory, @VM)
	MsgUp          = Msg(@window, Def)
    For each PMId in PMHistory using @VM setting pPos
        PMRec                              = Database_Services('ReadDataRow', 'PM', PMId)
        SchedDtm                           = SRP_Datetime('Combine', PMRec<PM_SCHED_DT$>, PMRec<PM_SCHED_TM$>)
        DueByDTM                           = obj_PM_Spec('LateStart',PMId:@RM:PMSpecRecord)
        PMHistoryArray<pPos, COL$PMID>     = PMId
        PMHistoryArray<pPos, COL$DESC>     = PMDescription
        PMHistoryArray<pPos, COL$DATECOMP> = OCONV(PMRec<PM_COMP_DTM$>, 'DT')
        PMHistoryArray<pPos, COL$DATEDUE>  = OCONV(SchedDtm, 'DT')
        PMHistoryArray<pPos, COL$USER>     = OConv(PMRec<PM_COMP_BY$>, '[XLATE_CONV,LSL_USERS*FIRST_LAST]')
	While Msg(@window, MsgUp, pPos, MSGINSTUPDATE$) ; * update the gauge and check if cancel was pressed
    Next PMId
    Msg(@window, MsgUp)         ;* take down the gauge
    PMHistoryArray = SRP_Array('Rotate',PMHistoryArray)
    PMHistoryArray = SRP_Array('SortRows', PMHistoryArray, 'D1', 'ARRAY', '', '')
    Set_Property(@Window : '.OLE_HIST_TABLE', "OLE.ARRAY", PMHistoryArray)
    
return


ColorCode:
	
    PMHistoryArray = Get_Property(@Window : '.OLE_HIST_TABLE', "OLE.ARRAY")
    PMHistoryArray = SRP_Array('Rotate',PMHistoryArray)
    
return


SETUP_CONTROLS:
	
    Set_Property(@Window : '.EDL_PM_SPEC', 'TEXT', PMSpecID)
    GoSub Setup_OLE_Controls
    
return


Setup_OLE_Controls:
	
    EdtTableCtrl = @Window : '.OLE_HIST_TABLE'
    Set_Property(EdtTableCtrl, 'OLE.Redraw', False$)
    //Set up columns
    Set_Property(EdtTableCtrl, "OLE.Dimension", 5);*Define number of columns
    
    HeaderTitles = 'PM ID':@VM:'Desc':@VM:'Date Completed':@VM:'Sched Due':@VM:'User Completed'; *Define the column header titles
    Set_Property(EdtTableCtrl, "OLE.TitleList", HeaderTitles); *Set the column titles
    
    //Set the column Sizes
    Set_Property(EdtTableCtrl, "OLE.HeaderColumn[1]", '':@FM:False$)
    ColSize    = Get_Property(EdtTableCtrl, "OLE.DataColumn[1]")
    //Column 1
    ColSize<1> = 45
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$PMID:"]", ColSize)
    //Column 2
    ColSize<1> = 250
    ColSize<4> = True$
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$DESC:"]", ColSize)
    ColSize<4> = False$
    //Column 3
    ColSize<1> = 120
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$DATECOMP:"]", ColSize)
    //Column 4
    ColSize<1> = 135
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$DATEDUE:"]", ColSize)
    //Column 5
    ColSize<1> = 185
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$USER:"]", ColSize)
    
    Set_Property(EdtTableCtrl, 'OLE.Border', 'XP Flat')
    Set_Property(EdtTableCtrl, 'OLE.Font', 'Segoe UI' : @SVM : '15')
    Set_Property(EdtTableCtrl, "OLE.CellProtection[All; All]", 'RO')
    Set_Property(EdtTableCtrl, 'OLE.Redraw', True$)
    
    // Qualify OLE events that we want to intercept
    Qualifier = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(EdtTableCtrl,  'QUALIFY_EVENT', 'OLE.OnButtonClick', Qualifier)
    Send_Message(EdtTableCtrl,  'QUALIFY_EVENT', 'OLE.OnDblClick', Qualifier)
    
return


