Compile function NDW_5S_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_5S


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

Declare function Get_Property, Pm_Services, obj_PM_Spec, Database_Services, SRP_Array, SRP_Datetime, Msg, Datetime
Declare function Memberof
Declare subroutine Set_Property, Pm_Services, Start_Window, Obj_Appwindow, PlaceDialog

// Columns in OLE Edit Table
EQU COL$PM_ID       TO 1
EQU COL$DESC        TO 2
EQU COL$LAST_PM     TO 3
EQU COL$EARLY_START TO 4
EQU COL$SCHED_START TO 5
EQU COL$LATE_START  TO 6
EQU COL$ACTION      TO 7


// Form colors
EQU RED$FIVES    TO '5924072'
EQU GREEN$FIVES  TO '7596927'
EQU YELLOW$FIVES TO '9109501'
EQU ORANGE$FIVES TO '4168946'

// 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)
    SelectedArea = ''
    Begin Case
        Case CreateParam EQ 'CLEANROOM'
            SelectedArea = 'CLEANROOM'
        Case CreateParam EQ 'MAINTENANCE'
            SelectedArea = 'MAINTENANCE'
        Case CreateParam EQ 'SHIPPING/RECEIVING'
            SelectedArea = 'SHIPPING/RECEIVING'
        Case Otherwise$
            SelectedArea = 'CLEANROOM'
    End Case
    Set_Property(@Window : '.CMB_AREA', 'TEXT', SelectedArea)
    GoSub Setup_OLE_Controls
    GoSub SetupForm
    GoSub GetKeys
    stat = Set_Property(@WINDOW,'TIMER',10000:@FM:0)
    PlaceDialog(-2, -2)
    
End Event


Event CHK_ARCHIVED.CLICK()
    
    GoSub GetKeys
    
end event

Event CMB_AREA.CHANGED(NewData)
    GoSub GetKeys
end event


Event EDT_5S_TABLE.OnClick(Cell, Point, Button, Shift, Ctrl)
    
    EdtTableCtrl = @Window : '.EDT_5S_TABLE'
    RowClicked   = Field(Cell, ';' , 2)
    SelArray     = 1 : @FM : RowClicked
    Set_Property(@Window: '.EDT_SEL_ROW', "TEXT", RowClicked)
    Set_Property(@Window : '.PUB_VIEW_SELECTED', 'ENABLED', 1)
    
end event


Event EDT_5S_TABLE.OnButtonClick(Cell, Point, Button, Shift, Ctrl)
    
    EdtTableCtrl = @Window : '.EDT_5S_TABLE'
    TableData    = Get_Property(EdtTableCtrl, "OLE.ARRAY")
    TableData    = SRP_Array("Rotate", TableData)
    ColClicked   = Field(Cell, ';' , 1)
    Begin Case
        Case ColClicked EQ COL$ACTION
            //Clicked Complete Button
            RowNoClicked    = Field(Cell, ';' , 2)
            RowData         = TableData<RowNoClicked>
            MsgInfo         = ''
            MsgInfo<mcol$>  = -2
            MsgInfo<MTYPE$> = 'BNYC'
            MsgInfo<mrow$>  = -2
            MsgInfo<mtext$> = 'Are you sure you wish to mark this 5S task as complete?'
            Verify          = Msg(@Window, MsgInfo)
            If Verify EQ True$ then
                PMSID    = RowData<1,COL$PM_ID>
                PMSRec   = Database_Services('ReadDataRow', 'PM_SPEC', PMSID)
                CurrPMId = PMSRec<PM_SPEC_PM_KEYS$, 1>
                Pm_Services('CompleteNonToolPM', CurrPMId , @User4, Datetime())
                If Error_Services('NoError') then
                    MsgInfo         = ''
                    MsgInfo<mcol$>  = -2
                    MsgInfo<MTYPE$> = 'BO'
                    MsgInfo<mrow$>  = -2
                    MsgInfo<mtext$> = 'Task marked as complete!'
                    Msg(@Window, MsgInfo)
                    GoSub GetKeys
                end else
                    MsgInfo         = ''
                    MsgInfo<mcol$>  = -2
                    MsgInfo<MTYPE$> = 'BO'
                    MsgInfo<mrow$>  = -2
                    MsgInfo<mtext$> = Error_Services('GetMessage')
                    Msg(@Window, MsgInfo)
                    GoSub GetKeys
                end
            end else
                MsgInfo         = ''
                MsgInfo<mcol$>  = -2
                MsgInfo<MTYPE$> = 'BO'
                MsgInfo<mrow$>  = -2
                MsgInfo<mtext$> = 'Task NOT marked as complete!'
                Msg(@Window, MsgInfo)
                GoSub GetKeys
            end
        Case ColClicked EQ COL$PM_ID
                       
        Case Otherwise$
            
    End Case
    
end event


Event EDT_5S_TABLE.OnDblClick(Cell, Point, Button, Shift, Ctrl)
    
    EdtTableCtrl = @Window : '.EDT_5S_TABLE'
    TableData    = Get_Property(EdtTableCtrl, "OLE.ARRAY")
    TableData    = SRP_Array("Rotate", TableData)
    ColClicked   = Field(Cell, ';' , 1)
    Begin Case
        Case ColClicked EQ COL$PM_ID
            //Clicked PM ID column, bring up PM history for this PM ID
            RowNoClicked = Field(Cell, ';' , 2)
            RowData      = TableData<RowNoClicked>
            PMSID        = RowData<1,COL$PM_ID>
            PMSRec       = Database_Services('ReadDataRow', 'PM_SPEC', PMSID)
            CurrPMId     = PMSRec<PM_SPEC_PM_KEYS$, 1>
            
            Start_Window('NDW_PM_HISTORY', @Window, PMSID)
        Case Otherwise$
            
    End Case
    
end event


Event EDT_5S_TABLE.OnLostFocus()
    
    //Set_Property(@Window : '.EDT_SEL_ROW', 'TEXT', ''); *Clear out selected row.
    //Set_Property(@Window : '.PUB_VIEW_SELECTED', 'ENABLED', 0)
    
end event


Event PUB_ADD_NEW_PM_SPEC.CLICK()
    
    Start_Window('PM_SPEC', @Window, '')
    
end event


Event PUB_VIEW_SELECTED.CLICK()
    
    EdtTableCtrl   = @Window : '.EDT_5S_TABLE'
    RowSelected    = Get_Property(@Window : '.EDT_SEL_ROW', 'TEXT')
    TableData      = Get_Property(EdtTableCtrl, "OLE.ARRAY")
    TableData      = SRP_Array("Rotate", TableData)
    SelectedPmsKey = TableData<RowSelected, 1>
    Set_Property(@Window : '.EDT_SEL_ROW', 'TEXT', ''); *Clear out selected row.
    Set_Property(@Window : '.PUB_VIEW_SELECTED', 'ENABLED', 0)
    if SelectedPmsKey NE '' then
        obj_AppWindow('ViewRelated','PM_SPEC':@RM:SelectedPmsKey)
    end
    
end event


Event WINDOW.TIMER()
    
    GoSub GetKeys
    
end event

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

GetKeys:
    ShowArchived = Get_Property(@Window : '.CHK_ARCHIVED', 'CHECK')
    SelectedArea = Get_Property(@Window : '.CMB_AREA', 'TEXT')
    
    FiveSPMKeys  = Pm_Services('Get5SPMs', ShowArchived, SelectedArea) 
    
FormatData:
    
    TableData = ''
    
    For each PMKey in FiveSPMKeys using @VM setting tPos
        //Record for the PM Specification
        PMSpecRecord  = Database_Services('ReadDataRow', 'PM_SPEC', PMKey)
        //Record for the currently scheduled PM for the specificed PM Spec.
        CurrPMKey     = PMSpecRecord<PM_SPEC_PM_KEYS$>
        CurrPMRecord  = Database_Services('ReadDataRow', 'PM', CurrPMKey)
        CurrPMDesc    = PMSpecRecord<PM_SPEC_DESC$>
        CurrPMLastPM  = OConv(PMSpecRecord<PM_SPEC_LAST_PM_COMP_DTM$>, 'DT')
        //Sched Calculation
        CurrPMSchedDT = CurrPMRecord<PM_SCHED_DT$>
        CurrPMSchedTM = CurrPMRecord<PM_SCHED_TM$>
        
        SchedStarts	= obj_PM_Spec('SchedStart',PMKey:@RM:PMSpecRecord)
        EarlyStarts	= obj_PM_Spec('EarlyStart',PMKey:@RM:PMSpecRecord)
        LateStarts	= obj_PM_Spec('LateStart',PMKey:@RM:PMSpecRecord)
        
        TableData<tPos, COL$PM_ID>       = PMKey
        TableData<tPos, COL$DESC>        = CurrPMDesc
        TableData<tPos, COL$LAST_PM>     = CurrPMLastPM
        TableData<tPos, COL$EARLY_START> = EarlyStarts
        TableData<tPos, COL$SCHED_START> = SchedStarts
        TableData<tPos, COL$LATE_START>  = LateStarts
    Next PMKey
    
PopulateTable:
    
    EdtTableCtrl = @Window : '.EDT_5S_TABLE'
    TableData    = SRP_Array("Rotate", TableData)
    Set_Property(EdtTableCtrl, "OLE.ARRAY", TableData)
    //Set last column in row to a PUB
    Set_Property(EdtTableCtrl, "OLE.CellType[7; ALL]", "Push Button")
    Set_Property(EdtTableCtrl, "OLE.CellText[7; ALL]", "Click To Complete")
    Set_Property(EdtTableCtrl, "OLE.CellImage[7; ALL]", 1)
    
    //Get All Cell Settings
    ColumnSettings      = Get_Property(EdtTableCtrl, "OLE.CellType[3; ALL]")
    ColumnSettings<2,3> = 0; *Turns off data from cell 7 of each row.
    Set_Property(EdtTableCtrl, "OLE.CellType[7; ALL]", ColumnSettings)
    
    
ColorCodeRows:
        
    //Here we color code all the rows based on whether they're not due, due, or overdue.
    TableData      = Get_Property(EdtTableCtrl, "OLE.ARRAY")
    TableData      = SRP_Array('Rotate', TableData)
    CurrDTM        = SRP_Datetime('Now')
    for each DataRow in TableData using @FM setting dPos
        EarlyStart = ''
        SchedStart = ''
        LateStart  = ''
        PMSpecID   = DataRow<1,COL$PM_ID>
        Units      = XLATE('PM_SPEC', PMSpecID, PM_SPEC_UNITS$, 'X')
        Begin Case
            Case Units EQ 'D'
                EarlyStart = ICONV(DataRow<1, COL$EARLY_START>, 'D')
                SchedStart = ICONV(DataRow<1, COL$SCHED_START>, 'D')
                LateStart  = ICONV(DataRow<1, COL$LATE_START>, 'D')
            Case Units EQ 'T'
                EarlyStart = ICONV(DataRow<1, COL$EARLY_START>, 'DT')
                SchedStart = ICONV(DataRow<1, COL$SCHED_START>, 'DT')
                LateStart  = ICONV(DataRow<1, COL$LATE_START>, 'DT')
        End Case
        Begin Case
            Case CurrDTM LT EarlyStart
                //Not Due Yet
                LineColor = GREEN$FIVES
            Case CurrDTM GT EarlyStart AND CurrDTM LT SchedStart
                //Within the early start window
                LineColor = YELLOW$FIVES
            Case CurrDTM GT SchedStart AND CurrDTM LT LateStart
                //Due
                LineColor = ORANGE$FIVES
            Case CurrDTM GT LateStart
                //Past Due
                LineColor = RED$FIVES
            Case 1
                LineColor = WHITE$
        End Case
        RowSettings    = Get_Property(EdtTableCtrl, "OLE.CellColors[1; ":dPos:"]")
        RowSettings<2> = LineColor
        Set_Property(EdtTableCtrl, "OLE.CellColors[ALL; ":dPos:"]", RowSettings)
    Next DataRow
    
return


SetupForm:
    
    If NOT(Memberof(@User4, 'SUPERVISOR')) then
        Set_Property(@Window : '.PUB_ADD_NEW_PM_SPEC', 'VISIBLE', 0)
        Set_Property(@Window : '.PUB_VIEW_SELECTED', 'VISIBLE', 0)
    end
    
    Set_Property(@Window : '.PUB_VIEW_SELECTED', 'ENABLED', 0)
    
return


Setup_OLE_Controls:
    
    EdtTableCtrl   = @Window : '.EDT_5S_TABLE'
    //Row Selection Settings
    RowSelArray    = Get_Property(EdtTableCtrl, "OLE.SelectionStyle")
    RowSelArray<7> = 'Header'
    RowSelArray<8> = True$;*Always show row selection
    Set_Property(EdtTableCtrl, "OLE.SelectionStyle", RowSelArray)
    Set_Property(EdtTableCtrl, 'OLE.Redraw', False$)
    //Set up columns
    Set_Property(EdtTableCtrl, "OLE.Dimension", 7);*Define number of columns
    
    HeaderTitles = 'PM ID':@VM:'Desc':@VM:'Last PM':@VM:'Early Start':@VM:'Sched Start':@VM:'Late Start':@VM:'Action'; *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$PM_ID:"]", ColSize)
    
    //Column 2
    ColSize<1> = 160
    //ColSize<4> = True$
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$DESC:"]", ColSize)
    //Set_Property(Ctrl, "OLE.DataColumn[":COL$ELAP_HRS:"]", ColSize)
    
    //Column 3
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$LAST_PM:"]", ColSize)
    
    //Column 4
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$EARLY_START:"]", ColSize)
    
    //Column 5
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$SCHED_START:"]", ColSize)
    
    //Column 6
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$LATE_START:"]", ColSize)
    
    //Column 7
    ColSize<1> = 110
    Set_Property(EdtTableCtrl, "OLE.DataColumn[":COL$ACTION:"]", 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.OnClick', Qualifier)
    Send_Message(EdtTableCtrl,  'QUALIFY_EVENT', 'OLE.OnButtonClick', Qualifier)
    Send_Message(EdtTableCtrl,  'QUALIFY_EVENT', 'OLE.OnDblClick', Qualifier)
    Send_Message(EdtTableCtrl,  'QUALIFY_EVENT', 'OLE.OnLostFocus', Qualifier)
    
return



