B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=11
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
	Dim jobname As String
	Dim SV As ScrollView
	Dim BtnClose As Button
	Dim Header As Panel
	Dim Table As Panel
	Dim NumberOfColumns, RowHeight, ColumnWidth As Int
	Dim HeaderColor, TableColor, FontColor, HeaderFontColor As Int
	Dim FontSize As Float
	Type RowCol (Row As Int, Col As Int)
	Dim Alignment As Int
	Dim SelectedRow As Int
	Dim SelectedRowColor As Int
	
	'Table settings
	HeaderColor = Colors.Gray
	NumberOfColumns = 3
	RowHeight = 30dip
	TableColor = Colors.White
	FontColor = Colors.Black
	HeaderFontColor = Colors.White
	FontSize = 11
	Alignment = Gravity.CENTER 'change to Gravity.LEFT or Gravity.RIGHT for other alignments.
	SelectedRowColor = Colors.Blue
	Dim list1 As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
	init_view
End Sub

Sub init_view
	
	SV.Initialize(0)
	Activity.LoadLayout("LData")
	
	Table = SV.Panel
	Table.Color = TableColor
	Activity.AddView(SV, 5%x, 10%y, 90%x, 80%y)
	Activity.Title="Chargen"
	ColumnWidth = SV.Width / NumberOfColumns
	SelectedRow = -1
	'add header
	SetHeader(Array As String("ID", "Charge", "Datum"))
	'add rows
	Get_Data
	Activity.Title="Daten " & AllgFnkt.PatName
End Sub

Sub Get_Data
	Dim job As HttpJob
	jobname="GetData"
	job.Initialize(jobname, Me)

	job.Download(AllgFnkt.sURL&"/DPMService/api/PatCharge/"&AllgFnkt.PatID)
End Sub

Sub JobDone(Job As HttpJob)
	ProgressDialogHide
	If Job.Success Then
		Dim res As String
		res = Job.GetString
		Dim parser As JSONParser
		parser.Initialize(res)
		
		Log("Response from server: " & res)
		
		Select Job.JobName
			Case "DelData"
				list1.Clear
				Table.RemoveAllViews
				init_view
				'Get_Data
	
			Case "GetData"
			
				list1 = parser.NextArray
				Dim i As Int
				For i=0 To list1.Size-1
					Dim map1 As Map
					map1=list1.Get(i)
					Dim s1 As String
					Dim s2 As String
					Dim s3 As String
					s1=map1.Get("id")
					s2=map1.Get("charge")
					s3=map1.Get("datum")
					AddRow(Array As String(s1,s2,s3))
				Next
				If list1.Size<1 Then
					ToastMessageShow("Keine Daten vorhanden.",True)
					Return
				End If
		End Select
	End If
End Sub



Sub Cell_LongClick
	Dim rc As RowCol
	Dim l As Label
	Dim x As String
	x=""
	l = Sender
	rc = l.Tag
	SelectRow(rc.Row)
'	Activity.Title = "Cell clicked: (" & rc.Row & ", " & rc.Col &")"
	For i=0 To list1.Size-1
		If i=rc.Row Then
			Dim map1 As Map
			map1=list1.Get(i)
			x=map1.Get("id")
	
			'ToastMessageShow(map1.Get("id"),False)
		End If
	Next
	If x<>"" Then
		Msgbox2Async("Eintrag Nr. "&x&" wirkllich löschen?", "Charge-Eintrag löschen", "Ja", "Nein", "", Null, False)
		Wait For MsgBox_Result (Result As Int)
		If Result = DialogResponse.POSITIVE Then
			Dim job As HttpJob
			jobname="DelData"
			job.Initialize(jobname, Me)
			job.Delete(AllgFnkt.sURL&"/DPMService/api/PatCharge/"&x)
		End If
	End If
End Sub

Sub Cell_Click
	Dim rc As RowCol
	Dim l As Label
	l = Sender
	rc = l.Tag
	SelectRow(rc.Row)
'	Activity.Title = "Cell clicked: (" & rc.Row & ", " & rc.Col &")"
	For i=0 To list1.Size-1
		If i=rc.Row Then
			Dim map1 As Map
			map1=list1.Get(i)
			'ToastMessageShow(map1.Get("id"),False)
		End If
	Next
End Sub
Sub Header_Click
	Dim l As Label
	Dim col As Int
	l = Sender
	col = l.Tag
'	Activity.Title = "Header clicked: " & col
End Sub
Sub SelectRow(Row As Int)
	'remove the color of previously selected row
	If SelectedRow > -1 Then
		For col = 0 To NumberOfColumns - 1
			GetView(SelectedRow, col).Color = Colors.Transparent
		Next
	End If
	SelectedRow = Row
	For col = 0 To NumberOfColumns - 1
		GetView(Row, col).Color = SelectedRowColor
	Next
End Sub
Sub GetView(Row As Int, Col As Int) As Label
	Dim l As Label
	l = Table.GetView(Row * NumberOfColumns + Col)
	Return l
End Sub
Sub AddRow(Values() As String)
	If Values.Length <> NumberOfColumns Then
		Log("Wrong number of values.")
		Return
	End If
	Dim lastRow As Int
	lastRow = NumberOfRows
	For i = 0 To NumberOfColumns - 1
		Dim l As Label
		l.Initialize("cell")
		l.Text = Values(i)
		l.Gravity = Alignment
		l.TextSize = FontSize
		l.TextColor = FontColor
		Dim rc As RowCol
		rc.Initialize
		rc.Col = i
		rc.Row = lastRow
		l.Tag = rc
		Table.AddView(l, ColumnWidth * i, RowHeight * lastRow, ColumnWidth, RowHeight)
	Next
	Table.Height = NumberOfRows * RowHeight
End Sub
Sub SetHeader(Values() As String)
	If Header.IsInitialized Then Return 'should only be called once
	Header.Initialize("")
	For i = 0 To NumberOfColumns - 1
		Dim l As Label
		l.Initialize("header")
		l.Text = Values(i)
		l.Gravity = Gravity.CENTER
		l.TextSize = FontSize
		l.Color = HeaderColor
		l.TextColor = HeaderFontColor
		
		
		l.Tag = i
		Header.AddView(l, ColumnWidth * i, 0, ColumnWidth, RowHeight)
	Next
	Activity.AddView(Header, SV.Left, SV.Top - RowHeight, SV.Width, RowHeight)
End Sub
Sub NumberOfRows As Int
	Return Table.NumberOfViews / NumberOfColumns
End Sub

Sub SetCell(Row As Int, Col As Int, Value As String)
	GetView(Row, Col).Text = Value
End Sub
Sub GetCell(Row As Int, Col As Int) As String
	Return GetView(Row, Col).Text
End Sub
Sub ClearAll
	For i = Table.NumberOfViews -1 To 0 Step -1
		Table.RemoveViewAt(i)
	Next
	Table.Height = 0
	SelectedRow = -1
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Private Sub bClose_Click
	Activity.Finish
	'StartActivity("Main")
End Sub