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

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Private bs1 As BillScanner
	Private pattimer As Timer
	Dim lblpatient As Label
	Dim pnlEnterPatID As Panel
	Dim txtPatNr As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
	'Do not forget to load the layout file created with the visual designer. For example:
	Activity.LoadLayout("BarcodeScanner")
	Activity.Title="Charge-Scanner"
	
	
	bs1.ScanMode = bs1.FORMAT_ONE_D
	AllgFnkt.PatID=0
	If File.Exists(File.DirInternal ,"DPM.ini") = True Then
		AllgFnkt.sURL=AllgFnkt.Get_Params("URL")
		AllgFnkt.ResettimerInterval=AllgFnkt.Get_Params("TIMER")
	Else
	End If
	lblpatient.Text="Bitte Patient auswählen"
	pattimer.Initialize("timer",AllgFnkt.ResettimerInterval)
End Sub

Sub Activity_Resume
	
	If AllgFnkt.pid<>"" Then AllgFnkt.PatID=AllgFnkt.pid

	If File.Exists(File.DirInternal ,"DPM.ini") = True Then
	    AllgFnkt.Get_Params("TIMER")
		pattimer.Enabled=False
		pattimer.Initialize("timer",AllgFnkt.ResettimerInterval)
		pattimer.Enabled=True
	End If
	If AllgFnkt.PID<>"" Then get_Pat(AllgFnkt.PID)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
	Try
		bs1.stopScanning
	Catch
	End Try
	'AllgFnkt.PatID=0
End Sub

Sub Timer_Tick
	AllgFnkt.PatID=0
	lblpatient.Text="Bitte Patient auswählen"
End Sub

#Region Job

Sub get_Pat(id As String)
	Dim job As HttpJob
	AllgFnkt.JobName="GetPat"
	job.Initialize(AllgFnkt.JobName, Me)
	
	Dim callurl As String
	callurl=AllgFnkt.sURL&"/DPMService/api/Service_View_Pat/"&id
	job.Download(callurl)
End Sub

Sub get_Pat_Name(searchstring As String)
	Dim job As HttpJob
	AllgFnkt.Searchstring=searchstring
	AllgFnkt.JobName="GetPatName"
	job.Initialize(AllgFnkt.JobName, Me)
	
	Dim callurl As String
	callurl=AllgFnkt.sURL&"/DPMService/api/Service_View_Pat/search/"&searchstring
	job.Download(callurl)
End Sub

Sub bs1_scan_value(val As String, rawbyte() As Byte, barcodetype As Int)
	If AllgFnkt.JobEnabled=False Then Return
	AllgFnkt.JobEnabled=False
	Try
	bs1.stopScanning
	Catch
    End Try
	
	Dim job As HttpJob
	AllgFnkt.JobName="PostCharche"
	job.Initialize(AllgFnkt.JobName, Me)
	
	job.PostString(AllgFnkt.sURL&"/DPMService/api/PatCharge/"&AllgFnkt.PatID&"/"&val,"")

	Dim b As Beeper
	b.Initialize(300, 500) '300 milliseconds, 500 hz
	b.Beep
	pattimer.Enabled=True
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 "GetPat"

				Dim list1 As List
				list1 = parser.NextArray
				If list1.Size<1 Then
					ToastMessageShow("Patient nicht vorhanden.",True)
					'/pnlEnterPatID.Visible=True
					AllgFnkt.PatID=0
					lblpatient.Text="Patient nicht vorhanden."
					Return
				End If
				Dim map1 As Map
				map1 = list1.Get(0)
				Log(map1)
				
				AllgFnkt.PatID = map1.Get("id")
				AllgFnkt.PatName=map1.Get("pat")
				lblpatient.Text=AllgFnkt.PatName
			Case "GetPatName"

				Dim list1 As List
				list1 = parser.NextArray
				If list1.Size<1 Then
					ToastMessageShow("Keine Daten gefunden",True)
					AllgFnkt.PatID=0
					lblpatient.Text="Patient nicht vorhanden."
					Return
				End If
				If list1.Size=1 Then
					Dim map1 As Map
					map1 = list1.Get(0)
					AllgFnkt.PatID = map1.Get("id")
					AllgFnkt.PatName=map1.Get("pat")
					lblpatient.Text=AllgFnkt.PatName
					Return
                End If
				StartActivity("ListPat")
'
'
'				Dim map1 As Map
'				map1 = list1.Get(0)
'				Log(map1)
'				
'				AllgFnkt.PatID = map1.Get("id")
'				AllgFnkt.PatName=map1.Get("pat")
'				lblpatient.Text=AllgFnkt.PatName
			Case "PostCharche"
				ToastMessageShow("Barcode erfolgreich gespeichert",False)
		End Select
	Else
		ToastMessageShow("Funktionsaufruf fehlgeschlagen.",True)
	End If
	Job.Release
End Sub
#End Region

#Region ButtonHandling
Private Sub btnSearchPat_Click
	Dim inppat As InputDialog
	If inppat.Show("Patient-Nr","Patient Nummer eingeben","OK","","",Null) = DialogResponse.CANCEL Then Return
	get_Pat(inppat.Input)
	pattimer.Enabled=False
	get_Pat(txtPatNr.Text)
	'pnlEnterPatID.Visible=False
	'btnSearchPat.RequestFocus
	pattimer.Enabled=True
End Sub
#End Region


Private Sub btnEnterPatID_Click
	Dim inppat As InputDialog
	inppat.InputType=inppat.INPUT_TYPE_NUMBERS
	If inppat.Show("","Patient Nummer eingeben","OK","","",Null) = DialogResponse.CANCEL Then Return
	get_Pat(inppat.Input)
	lblpatient.RequestFocus
End Sub

Private Sub BtnStartScan_Click
	If AllgFnkt.PatID=0 Then
		ToastMessageShow("Bitte zuerst Patient wählen",True)
		Return
	End If
	AllgFnkt.JobEnabled=True
	bs1.startScanning
	pattimer.Enabled=False
End Sub

Private Sub BtnStopScan_Click
	Try
		bs1.stopScanning
	Catch
	End Try
	Activity.Finish
End Sub

Private Sub btnListdata_Click
	If AllgFnkt.PatID=0 Then
		ToastMessageShow("Bitte zuerst Patient auswählen.",True)
		Return
	End If
	AllgFnkt.pid=AllgFnkt.PatID
	AllgFnkt.PatName=lblpatient.Text
	StartActivity("ListData")

End Sub

Private Sub btnEnterPatID_LongClick
	Dim inppat As InputDialog
	inppat.InputType=inppat.INPUT_TYPE_TEXT
	If inppat.Show("","Patient Name eingeben","OK","","",Null) = DialogResponse.CANCEL Then Return
	get_Pat_Name(inppat.Input)
	lblpatient.RequestFocus
End Sub