* ===============================================================
*  LREX0059  : Drilling Down using Drop downs RDMLX
* ===============================================================

Function Options(*DIRECT)
begin_com role(*EXTENDS #LRNG_FORM)

* The LRUDEMO component supplies utility methods to the LongRange Demo system and Example Formviews
Define_Com Class(#lrudemo) Name(#DemoUtilities) Scope(*Application)

Define Field(#FormNum) Reffld(#std_int) Default(1)
Define Field(#FormMax) Reffld(#std_int) Default(2)
define_com class(#prim_alph) name(#uText)

* Work fields holding the selected value for Level 1, Level 2, Level 3
define_com class(#std_text) name(#uLevel1)
define_com class(#std_text) name(#uLevel2)
define_com class(#std_text) name(#uLevel3)

* boolean work fields indicating the visibility of Level 1, Level 2, Level 3
define_com class(#prim_boln) name(#uLevel1vis)
define_com class(#prim_boln) name(#uLevel2vis)
define_com class(#prim_boln) name(#uLevel3vis)

define_com class(#prim_alph) name(#uTitle)

Define Field(#uDDName) Type(*char) Length(20)
define_com class(#std_int) name(#uDDIndex)
define_com class(#std_text) name(#uDDItemText)
define_com class(#std_text) name(#uDDItemValue)

Define Field(#uCR) Type(*string) Length(1) Desc('The Carriage Return character')


Mthroutine Name(HandleRequest) Options(*REDEFINE)

* -----------------------------------------
*  Handle currently requested action
* -----------------------------------------

* Always set the USING Element to /Form.Fields at the start
#Com_Owner.Using Element('/Form.Fields')

* Get the drop down values from the last iteration

*#Com_Owner.Get_DropDown Name(LEVEL1) Value(#uLevel1)
*#Com_Owner.Get_DropDown Name(LEVEL2) Value(#uLevel2)
*#Com_Owner.Get_DropDown Name(LEVEL3) Value(#uLevel3)

* Get the remembered value for fields indicating the visibility of Level 1, Level 2, Level 3

#Com_Owner.Get_SharedStateBool Variablename(#Com_Owner.Name + LEVEL1VIS) Value(#uLevel1vis)
#Com_Owner.Get_SharedStateBool Variablename(#Com_Owner.Name + LEVEL2VIS) Value(#uLevel2vis)
#Com_Owner.Get_SharedStateBool Variablename(#Com_Owner.Name + LEVEL3VIS) Value(#uLevel3vis)


* (nearly) Always a new form
#COM_OWNER.IsNewForm := True
#uTitle := *null

* Carriage return character
Use Builtin(HexToBin) With_Args('0D') To_Get(#uCR)


* Main Case Statement
Case Of_Field(#COM_OWNER.RequestACTION)

When (= SHOWPROG)
#COM_OWNER.IsNewForm := False
#COM_OWNER.Set_Label Name('/Form.Popover') Text('See Visual LANSA reusable part ' + #Com_Owner.Name) Color(Blue)
#Com_Owner.Set_Form Show_Popover(TRUE)

When (= LEVEL1CHANGED)
#Com_Owner.Get_DropDown Name(LEVEL1) Value(#uLevel1)

* The user selected a value from the level 1 drop down
If (#uLevel1 *NE *blanks)

#uLevel2 := *blanks
#uLevel2vis := TRUE
#uLevel3vis := FALSE

Endif
#Com_Owner.LoadLevel2
#Com_Owner.Set_Dropdown Layout_Row(2) Layout_Col(2) Onvaluechange(LEVEL2CHANGED) Value(*blanks) Name(LEVEL1) HasFocus(True)

When (= LEVEL2CHANGED)
* The user selected a value from the level 2 drop down
#Com_Owner.Get_DropDown Name(LEVEL1) Value(#uLevel2)

If (#uLevel2 *NE *blanks)

#uLevel3 := *blanks
#uLevel3vis := TRUE

Endif
#Com_Owner.LoadLevel3
#Com_Owner.Set_Dropdown Layout_Row(2) Layout_Col(2) Onvaluechange(LEVEL3CHANGED) Value(*blanks) Name(LEVEL1) HasFocus(True)

When (= LEVEL3CHANGED)
* The user selected a value from the level 3 drop down

* do nothing
#Com_Owner.Get_DropDown Name(LEVEL1) Value(#uLevel3)
#Com_Owner.Set_Dropdown Layout_Row(2) Layout_Col(2)  Value(*blanks) Name(LEVEL1) HasFocus(True)

Otherwise /* Default */
* default values

#uTitle := *blanks

#uLevel1vis := True
#uLevel2vis := False
#uLevel3vis := False

#Com_Owner.LoadLevel1
#Com_Owner.Set_Dropdown Layout_Row(2) Layout_Col(2) Onvaluechange(LEVEL1CHANGED)  Value(*blanks) Name(LEVEL1) HasFocus(True)


Endcase

* --------------------------------------------------------------
* Format out the 'static' parts of the form view to be displayed
* --------------------------------------------------------------

* The Parent Form

#COM_OWNER.Set_Form Title('Drilling Down using Drop Downs')

* Set an alternative title for small screen devices
#DemoUtilities.SmallScreenTitle Title('Dropdowns')

#COM_OWNER.Set_Form Grid_Col_N(1) Grid_Col_Width(Fit)
#COM_OWNER.Set_Form Grid_Col_N(2) Grid_Col_Width(Fill)

#COM_OWNER.Set_Form Grid_Row_N(1) Grid_Row_Height(30%)

#Com_Owner.Set_Label Layout_Row(1) Layout_Col(1) Name(TITLE) Layout_Cols(-1) Text(#uTitle)

* Remember the visibility of the drop downs

#Com_Owner.Set_SharedStateBool Variablename(#Com_Owner.Name + LEVEL1VIS) Value(#uLevel1vis)
#Com_Owner.Set_SharedStateBool Variablename(#Com_Owner.Name + LEVEL2VIS) Value(#uLevel2vis)
#Com_Owner.Set_SharedStateBool Variablename(#Com_Owner.Name + LEVEL3VIS) Value(#uLevel3vis)

Endroutine

* Load the drop down for level 1

Mthroutine Name(LoadLevel1)

#uTitle := 'Level 1 is '

If (#uLevel1vis)
#uTitle += 'loaded and ' + #uLevel1 + ' is selected'

* Load the dropdown items for LEVEL1

* Add a blank entry
#uDDIndex := 1
#Com_Owner.Set_Dropdown Item_N(#uDDIndex) Item_Text('Select...') Item_Value(*blanks) Name(LEVEL1)


Define Field(#L1DATA) Type(*char) Length(26)
#L1DATA := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

Begin_Loop Using(#uDDIndex) From(#uDDIndex + 1) To(26)

#uDDItemValue := #L1DATA.Substring( #uDDIndex 1 )
#uDDItemText := #L1DATA.Substring( #uDDIndex 1 )

#Com_Owner.Set_Dropdown Item_N(#uDDIndex) Item_Text(#uDDItemText) Item_Value(#uDDItemValue) Name(LEVEL1)

End_Loop
Else
#uTitle += 'is not visible'

Endif
Endroutine

* Load the drop down for level 2

Mthroutine Name(LoadLevel2)

#uTitle += #uCR + 'Level 2 is '

If (#uLevel2vis)
#uTitle += 'loaded and ' + #uLevel2 + ' is selected'

* Load the dropdown items for LEVEL1

#Com_Owner.Set_Null Property('LEVEL1.ITEM')

* Add a blank entry
#uDDIndex := 1
#Com_Owner.Set_Dropdown Item_N(#uDDIndex) Item_Text('A B C...') Item_Value(*blanks) Name(LEVEL1)

* get all the employees whose surname starts with the Level 1 letter
#SURNAME := #uLevel1
Select Fields(#EMPNO #SURNAME #Givename) From_File(PSLMST2) With_Key(#SURNAME) Generic(*YES)

#uDDIndex += 1

#uDDItemValue := #EMPNO
#uDDItemText := #SURNAME + ' ' + #GIVENAME

#Com_Owner.Set_Dropdown Item_N(#uDDIndex) Item_Text(#uDDItemText) Item_Value(#uDDItemValue) Name(LEVEL1)

Endselect
Else
#uTitle += 'is not visible'

Endif
Endroutine

* Load the drop down for level 3

Mthroutine Name(LoadLevel3)

#uTitle += #uCR + 'Level 3 is '

If (#uLevel3vis)
#uTitle += 'loaded and ' + #uLevel3 + ' is selected'

* Load the dropdown items for LEVEL3

#Com_Owner.Set_Null Property('LEVEL1.ITEM')

* Add a blank entry
#uDDIndex := 1
#Com_Owner.Set_Dropdown Item_N(#uDDIndex) Item_Text('Employees...') Item_Value(*blanks) Name(LEVEL1)

* get the skills for the selected employee in level 2
#EMPNO := #uLevel2
Select Fields(#SKILCODE) From_File(PSLSKL) With_Key(#EMPNO) Generic(*YES)

Fetch Fields(#SKILDESC) From_File(SKLTAB) With_Key(#SKILCODE) Keep_Last(10)

#uDDIndex += 1

#uDDItemValue := #SKILCODE
#uDDItemText := #SKILDESC

#Com_Owner.Set_Dropdown Item_N(#uDDIndex) Item_Text(#uDDItemText) Item_Value(#uDDItemValue) Name(LEVEL1)

Endselect
Else
#uTitle += 'is not visible'

Endif
Endroutine

End_Com
