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

Sub Process_Globals
	Public Mode As String
	Public SelectedRowID As Long
	Public RowIDList As List
End Sub

Sub Globals
	Private spnSupplier As Spinner
	Private edtName, edtPrice As EditText
	Private Cursor1 As Cursor
	Private btnAdd, btnUpdate As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("EditProduct")
	Activity.Title = "Product " & Mode
	
	Init
	Select Mode
	Case "Add"
		InitNewItem
		btnUpdate.Visible = False
	Case "Edit"
		InitEditItem
	End Select
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
	Cursor1.Close
End Sub

Sub btnAdd_Click
	Private Query As String
	
	Query = "INSERT INTO Products VALUES ('" & edtName.Text & "', " & edtPrice.Text & ", " & RowIDList.Get(spnSupplier.SelectedIndex) & ")"
	Starter.SQL1.ExecNonQuery(Query)
	btnUpdate.Visible = True
End Sub

Sub btnUpdate_Click
	Private Query As String
	
	Query = "UPDATE Products SET Name = '" & edtName.Text & "', Price = " & edtPrice.Text & ", Supplier = " & RowIDList.Get(spnSupplier.SelectedIndex)
	Query = Query & " WHERE rowid = " & SelectedRowID
	
	Starter.SQL1.ExecNonQuery(Query)
End Sub

Sub Init
	Private Row As Int
	Private Query As String
	
	Query = "SELECT Name, rowid From Suppliers"
	Cursor1 = Starter.SQL1.ExecQuery(Query)
	
	For Row = 0 To Cursor1.RowCount - 1
		Cursor1.Position = Row
		spnSupplier.Add(Cursor1.GetString2(0))
	Next
End Sub

Sub InitNewItem
	edtName.Text = ""
	edtPrice.Text = ""
	spnSupplier.SelectedIndex = 0
End Sub

Sub InitEditItem
	Private Query As String
	
	Query = "SELECT Products.Name, Products.Price, Products.Supplier From Products WHERE Products.rowid = " & SelectedRowID
	Cursor1 = Starter.SQL1.ExecQuery(Query)	
	Cursor1.Position = 0
	edtName.Text = Cursor1.GetString2(0)
	edtPrice.Text = Cursor1.GetDouble2(1)
	Log(Cursor1.GetLong2(2))
	Log(RowIDList.IndexOf(Cursor1.GetLong2(2)))
	spnSupplier.SelectedIndex = Suppliers.RowIDList.IndexOf(Cursor1.GetLong2(2))
End Sub


