Compile function NDW_EDIT_MODE_CHANGE_HIST_EVENTS(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    Name        :   NDW_EDIT_MODE_CHANGE_HIST_EVENTS

    Description :   Commuter module for the NDW_EDIT_MODE_CHANGE_HIST form.

    Notes       :   Application errors should be logged using the Error Services module. There are a few methodological
                    assumptions built into way errors are managed which are important to understand in order to properly
                    work with Error Services:

                    - The term 'top' refers to the originating procedure of a call stack and the term 'bottom' refers to
                      the last routine (or the current routine) within a call stack. Within the OpenInsight Debugger
                      this will appear backwards since the originating procedure always appears at the bottom of the
                      list and the current routine appears at the top of the list. We are using this orientation because
                      it is common to refer to the process of calling other procedures as 'drilling down'.

                    - The reason for defining the orientation of the call stack is because Error_Services allows for
                      multiple error conditions to be appended to an original error. In most cases this will happen when
                      a procedure at the bottom of the stack generates an error condition and then returns to its
                      calling procedure. This higher level procedure can optionally add more information relevant to
                      itself. This continues as the call stack 'bubbles' its way back to the top to where the
                      originating procedure is waiting.

                    - Native OpenInsight commands that handle errors (e.g., Set_Status, Set_FSError, Set_EventStatus)
                      preserve their error state until explicitly cleared. This can hinder the normal execution of code
                      since subsequent procedures (usually SSPs) will fail if a pre-existing error condition exists.
                      Our philosophy is that error conditions should automatically be cleared before a new procedure
                      is executed to avoid this problem. However, the nature of Basic+ does not make this easy to
                      automate for any given stored procedure. Therefore, if a stored procedure wants to conform to our
                      philosophy then it should include a call into the 'Clear' service request at the top of the
                      program. Alternatively this can be done through a common insert (see SERVICE_SETUP for example.)

                    - Service modules will use the SERVICE_SETUP insert and therefore automatically clear out any
                      error conditions that were set before.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        12/07/20    djs     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_EDIT_MODE_CHANGE_HIST

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

Common /NDW_EDIT_MODE_CHANGE_HIST/ OrigHistoryList@, ReactorModeCodes@, ReactorModeDescs@, OrigFormHeight@

Equ COL$START_DTM           to 1
Equ COL$ELAP_HRS            to 2
Equ COL$MODE                to 3
Equ COL$NOTES               to 4
Equ COL$RL_ID               to 5
Equ COL$PROBLEM_CATEGORY    to 6
Equ COL$SERVICE_DESCRIPTION to 7
Equ COL$START_USER          to 8

Declare subroutine End_Window, Set_Property, Database_Services
Declare function   Reactor_Services, SRP_Array, Database_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 else
    // Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)

    ReactNo             = CreateParam
    Set_Property(@Window:'.EDL_REACTOR', 'TEXT', ReactNo)
    TypeOver            = ''
    TypeOver<MCAPTION$> = 'Admin Reactor Mode History'
    DaysToReport        = Msg(@Window, TypeOver, 'REPORT_DAYS')
    If ( (DaysToReport NE '') and Num(DaysToReport) ) then
        Set_Property(@Window:'.EDL_DAYS_TO_REPORT', 'TEXT', DaysToReport)
        FormSize        = Get_Property(@Window, 'SIZE')
        OrigFormHeight@ = FormSize<4>
        GoSub Setup_OLE_Controls
        GoSub EnableGetHistoryButton
        GoSub FillModeChgEDT
        GoSub EnableSaveButton    
        SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, '', 1)
    end else
        End_Window(@Window)
    end
    
End Event


Event PUB_CANCEL.CLICK()
    
    End_Window(@Window)
    
end event


Event PUB_SAVE.CLICK()
    
    GoSub SaveData
    
end event


Event OLE_EDT_MODE_CHG.OnComboClicked(Cell, SelPos, Value)
    
    CurrHistoryList           = Get_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST")
    Col                       = Field(Cell, ';', 1)
    Row                       = Field(Cell, ';', 2)
    CurrHistoryList<Row, Col> = Value
    Set_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST", CurrHistoryList)
    GoSub EnableSaveButton
    GoSub ColorRows
    
end event


Event PUB_GET_HISTORY.CLICK()
    
    GoSub FillModeChgEDT
    GoSub EnableSaveButton
    
end event


Event EDL_REACTOR.LOSTFOCUS(Flag, FocusID)
    
    GoSub EnableGetHistoryButton
    
end event


Event EDL_DAYS_TO_REPORT.LOSTFOCUS(Flag, FocusID)
    
    GoSub EnableGetHistoryButton
    
end event


Event EDL_REACTOR.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    GoSub EnableGetHistoryButton
    
end event


Event EDL_DAYS_TO_REPORT.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    GoSub EnableGetHistoryButton
    
end event


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

Setup_OLE_Controls:
    
    ModeChgCtrl = @Window:'.OLE_EDT_MODE_CHG'

    HeaderTitles  = 'Start DTM':@VM:'Elap Hrs':@VM:'Mode':@VM:'Notes':@VM:'RL ID':@VM:'Problem Category':@VM
    HeaderTitles := 'Service Description':@VM:'Start User'
    
    NumCols         = DCount(HeaderTitles, @VM)
    NumRows         = 50
    DimArray        = NumCols:@FM:NumRows
    HeaderFontArray = 'Segoe UI':@SVM:8:@SVM:700
    
    HeaderColArray    = Get_Property(ModeChgCtrl, "OLE.HeaderColumn[1]")
    HeaderColArray<2> = False$
    Set_Property(ModeChgCtrl, "OLE.HeaderColumn[1]", HeaderColArray)
    Set_Property(ModeChgCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)
    Set_Property(ModeChgCtrl, "OLE.Dimension", DimArray)
    Set_Property(ModeChgCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(ModeChgCtrl, "OLE.HeaderFont[1; All]", HeaderFontArray)
    
    Set_Property(ModeChgCtrl, "OLE.TitleList", HeaderTitles)
    Set_Property(ModeChgCtrl, "OLE.HeaderAlignment[All; 1]", 'C':@FM:'C':@FM:'L')
    Set_Property(ModeChgCtrl, "OLE.CellAlignment[":COL$START_DTM:"; All]", 'C':@FM:'L':@FM:'L')
    Set_Property(ModeChgCtrl, "OLE.CellAlignment[":COL$ELAP_HRS:"; All]", 'C':@FM:'R':@FM:'L')
    Set_Property(ModeChgCtrl, "OLE.CellAlignment[":COL$MODE:"-":COL$NOTES:"; All]", 'C':@FM:'L':@FM:'L')
    Set_Property(ModeChgCtrl, "OLE.CellAlignment[":COL$RL_ID:"; All]", 'C':@FM:'C':@FM:'L')
    Set_Property(ModeChgCtrl, "OLE.CellAlignment[":COL$PROBLEM_CATEGORY:"-":COL$START_USER:"; All]", 'C':@FM:'L':@FM:'L')
    Set_Property(ModeChgCtrl, "OLE.CellProtection[All; All]", 'SEL')
    Set_Property(ModeChgCtrl, "OLE.CellProtection[":COL$MODE:"; All]", 'None')
    Set_Property(ModeChgCtrl, "OLE.AllowDeletions", False$)
    Set_Property(ModeChgCtrl, "OLE.AllowInserts", False$)
    
    // Qualify OLE events that we want to intercept
    Qualifier    = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(ModeChgCtrl, 'QUALIFY_EVENT', 'OLE.OnComboClicked', Qualifier)

    ReactorModeList   = Xlate('SYSREPOSPOPUPS', @APPID<1>:'**':'REACTOR_MODE', PDISPLAY$, 'X')  
    Convert @VM to @FM in ReactorModeList
    Convert @SVM to @VM in ReactorModeList
    ReactorModeList   = SRP_Array('Rotate', ReactorModeList, @FM, @VM)
    ReactorModeCodes@ = ReactorModeList<1>
    ReactorModeDescs@ = ReactorModeList<2>
    ComboBoxList      = ReactorModeDescs@
    Convert @VM to @STM in ComboBoxList
    
    // Setup Tool ID and Wafer status/action combo boxes
    CellTypeArray        = ""
    CellTypeArray<1>     = "Combo"
    CellTypeArray<2, 1>  = ''
    CellTypeArray<2, 2>  = "L"
    CellTypeArray<2, 3>  = ComboBoxList
    CellTypeArray<2, 4>  = 1                         ;// Column 1 contains the values we care about
    CellTypeArray<2, 5>  = 1                         ;// Auto fill on
    CellTypeArray<2, 6>  = 0                         ;// Case sensitive on
    CellTypeArray<2, 7>  = 10                        ;// 10 visible rows max
    CellTypeArray<2, 8>  = 0                         ;// Don't fire the OnOptionClick
    CellTypeArray<2, 9>  = 1                         ;// Reduce the list to partial matches
    CellTypeArray<2, 10> = 0                         ;// Only show the drop down when the user types
    CellTypeArray<2, 11> = 0                         ;// Do not use LIST Format
    CellTypeArray<2, 12> = 1                         ;// Autofill on Tool ID name
    CellTypeArray<2, 13> = 1                         ;// Hide dropdown when user clears cell
    CellTypeArray<2, 14> = 0                         ;// Let navigation keys show the drop down
    CellTypeArray<2, 15> = 0                         ;// Show the drop down regardless of the cell's contents
    CellTypeArray<2, 16> = 0                         ;// Show the drop down during autofill
    CellTypeArray<2, 17> = 1                         ;// Remove selection when user backspaces/deletes
    CellTypeArray<2, 18> = 0                         ;// Show Popup while in read only mode
    CellTypeArray<2, 19> = 1                         ;// Show Popup When Navigating
    CellTypeArray<2, 20> = 1                         ;// Always Tab Out on Enter
    CellTypeArray<2, 21> = -1                        ;// Always show the dropdown above when close to the screen bottom
    CellTypeArray<2, 23> = 1                         ;// Limit possible values to those in dropdown
     
    Set_Property(ModeChgCtrl, "OLE.CellType[":COL$MODE:"; All]", CellTypeArray)
    
    ColSize    = Get_Property(ModeChgCtrl, "OLE.DataColumn[1]")
    ColSize<1> = 120
    ColSize<3> = True$
    ColSize<4> = False$
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$START_DTM:"]", ColSize)
    ColSize<1> = 50
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$ELAP_HRS:"]", ColSize)
    ColSize<1> = 100
    ColSize<4> = True$
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$MODE:"]", ColSize)
    ColSize<1> = 150
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$NOTES:"]", ColSize)
    ColSize<1> = 40
    ColSize<4> = False$
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$RL_ID:"]", ColSize)
    ColSize<1> = 200
    ColSize<4> = True$
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$PROBLEM_CATEGORY:"]", ColSize)
    ColSize<1> = 300
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$SERVICE_DESCRIPTION:"]", ColSize)
    ColSize<1> = 90
    ColSize<4> = False$
    Set_Property(ModeChgCtrl, "OLE.DataColumn[":COL$START_USER:"]", ColSize)
    
return


FillModeChgEDT:
    
    ReactNo          = Get_Property(@Window:'.EDL_REACTOR', 'TEXT')
    DaysToReport     = Get_Property(@Window:'.EDL_DAYS_TO_REPORT', 'TEXT')
    HistoryList      = Reactor_Services('GetReactModeHistory', ReactNo, DaysToReport)
    Set_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST", '')
    Set_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST", HistoryList)
    OrigHistoryList@ = HistoryList
    GoSub ResizeForm
    GoSub ColorRows
    
return


EnableGetHistoryButton:
    
    ReactNo      = Get_Property(@Window:'.EDL_REACTOR', 'TEXT')
    DaysToReport = Get_Property(@Window:'.EDL_DAYS_TO_REPORT', 'TEXT')
    If ( (ReactNo NE '') and (DaysToReport NE '') ) then
        Set_Property(@Window:'.PUB_GET_HISTORY', 'ENABLED', True$)
    end else
        Set_Property(@Window:'.PUB_GET_HISTORY', 'ENABLED', False$)
    end
    
return


EnableSaveButton:
    
    CurrHistoryList = Get_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST")
    ButtonEnabled   = (CurrHistoryList NE OrigHistoryList@)
    Set_Property(@Window:'.PUB_SAVE', 'ENABLED', ButtonEnabled)
    
return


ResizeForm:
    
    SystemSize      = Get_Property('SYSTEM', 'SIZE')
    MaxClientHeight = SystemSize<4>    
    ModeChgCtrl     = @Window:'.OLE_EDT_MODE_CHG'
    CtrlSize        = Get_Property(ModeChgCtrl, 'SIZE')
    OrigCtrlHeight  = CtrlSize<4>
    DataRowArray    = Get_Property(ModeChgCtrl, "OLE.DataRow[1]")
    RowHeight       = DataRowArray<1>
    DimArray        = Get_Property(ModeChgCtrl, "OLE.Dimension")
    NumRows         = DimArray<2>
    NewCtrlHeight   = OrigCtrlHeight + (RowHeight * (NumRows) )   
    SizeDiff        = NewCtrlHeight - OrigCtrlHeight
    NewFormHeight   = OrigFormHeight@ + SizeDiff
    If NewFormHeight GT MaxClientHeight then
        MaxFormHeight = MaxClientHeight * 0.8
        NewFormHeight = MaxFormHeight
    end
    FormSize    = Get_Property(@Window, 'SIZE')
    FormSize<4> = NewFormHeight
    FormSize<5> = -1 ; // Keep form invisible until we are ready to show it
    Set_Property(@Window, 'SIZE', FormSize)
    Send_Message(ModeChgCtrl, "OLE.Refresh")
    
return


ColorRows:
    
    DefaultColorArray = 'None':@FM:'None':@FM:'None':@FM:'None':@FM:'None'
    ChangedColorArray = 'None':@FM:'Yellow':@FM:'None':@FM:'Yellow':@FM:'None'
    CurrHistoryList   = Get_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST")
    For each Row in CurrHistoryList using @FM setting RowIndex
        OrigRow = OrigHistoryList@<RowIndex>
        If Row NE OrigRow then
            Set_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.CellColors[All;":RowIndex:"]", ChangedColorArray)
        end else
            Set_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.CellColors[All;":RowIndex:"]", DefaultColorArray)
        end
    Next Row
    
return


SaveData:

    ReactNo         = Get_Property(@Window:'.EDL_REACTOR' , 'TEXT')
    RowsToSave      = ''
    CurrHistoryList = Get_Property(@Window:'.OLE_EDT_MODE_CHG', "OLE.LIST")
    For each Row in CurrHistoryList using @FM setting RowIndex
    OrigRow         = OrigHistoryList@<RowIndex>
    If Row NE OrigRow then RowsToSave<-1> = RowIndex
    Next Row
    For each RowIndex in RowsToSave using @FM
        Row                       = CurrHistoryList<RowIndex>
        KeyDTM                    = Row<0, 1>
        KeyDTM                    = IConv(KeyDTM, 'DT')
        ModeKey                   = ReactNo:'*':KeyDTM
        ModeRec                   = Database_Services('ReadDataRow', 'REACT_MODE', ModeKey)
        NewModeDesc               = Row<0, COL$MODE>
        Locate NewModeDesc in ReactorModeDescs@ using @VM setting ModePos then
            NewModeCode = ReactorModeCodes@<0, ModePos>
            ModeRec<REACT_MODE_MODE$> = NewModeCode
        end       
        Database_Services('WriteDataRow', 'REACT_MODE', ModeKey, ModeRec, True$, False$, True$)
    Next RowIndex
    OrigHistoryList@ = CurrHistoryList
    GoSub ColorRows
    GoSub EnableSaveButton
    
return

