Compile function NDW_PM_HISTORY_EVENTS(CtrlEntId, Event, @PARAMS)
#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

$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$LATE     to 5
EQU COL$USER     to 6

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
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 : 'Late' : @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 = ''
    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)
        Late                               = Xlate('PM', PMId, 'LATE', 'X')
        If Late then Late = 'Yes' else Late = 'No'
        
        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$LATE>     = Late
        PMHistoryArray<pPos, COL$USER>     = PMRec<PM_COMP_BY$>
    Next PMId
    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", 6);*Define number of columns
    
    HeaderTitles = 'PM ID':@VM:'Desc':@VM:'Date Completed':@VM:'Sched Due':@VM:'Late':@VM:'User Completed'; *Define the column header titles
    Set_Property(EdtTableCtrl, "OLE.TitleList", HeaderTitles); *Set the column titles
    
    //Set the column Sizes
    ColSize = Get_Property(EdtTableCtrl, "OLE.DataColumn[1]")
    //Column 1
    ColSize<1> = 45
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$PMID:"]", ColSize)
    //Column 2
    ColSize<1> = 45
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$PMID:"]", ColSize)
    //Column 3
    ColSize<1> = 160
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$DATECOMP:"]", ColSize)
    //Column 4
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$DATEDUE:"]", ColSize)
    //Column 5
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$LATE:"]", ColSize)
    //Column 6
    ColSize<1> = 110
    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 rows to resizeable and to expand when selected.
    //DataRow    = Get_Property(EdtTableCtrl, "OLE.DataRow[1]")
    //DataRow<3> = True$
    //DataRow<4> = False$
    //DataRow<5> = 160
    

    //Set_Property(EdtTableCtrl, "OLE.DataRow[All]", DataRow)
    
    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


