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

End Sub

Sub Globals
	Private spnProduct As Spinner
	Private edtNumber, edtDate As EditText
	Private btnAdd, btnUpdate As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("EditStock")
	Activity.Title = "Stock " & Main.StockEditMode
	
	Init
	Select Main.StockEditMode
	Case "Add"
		InitNewItem
		btnUpdate.Visible = False
	Case "Edit"
		InitEditItem
	End Select
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
	
End Sub

Sub btnAdd_Click
	Private Query As String
	
	Query = "INSERT INTO Stock VALUES (" & edtNumber.Text & ", " & Products.RowIDList.Get(spnProduct.SelectedIndex) & ", " & DateTime.Now & ")"
	Starter.SQL1.ExecNonQuery(Query)
	btnUpdate.Visible = True
End Sub

Sub btnUpdate_Click
	Private Query As String
	
	Log(spnProduct.SelectedIndex)
	Log(Products.RowIDList.Get(spnProduct.SelectedIndex))
	Query = "UPDATE Stock SET Number = " & edtNumber.Text & ", ProductID = " & Products.RowIDList.Get(spnProduct.SelectedIndex) & ", Date = " & DateTime.DateParse(edtDate.Text)
	Query = Query & " WHERE rowid = " & Main.StockSelectedRowID
	
	Starter.SQL1.ExecNonQuery(Query)
End Sub

Sub Init
	Private ResultSet1 As ResultSet
	Private Query As String
	
	Products.RowIDList.Initialize
	
	Query = "SELECT Products.Name, Suppliers.Name, Products.rowid From Products, Suppliers"
	Query = Query & " WHERE Suppliers.rowid = Products.SupplierID"
	ResultSet1 = Starter.SQL1.ExecQuery(Query)
	
	Do While ResultSet1.NextRow
		spnProduct.Add(ResultSet1.GetString2(0) & " / " & ResultSet1.GetString2(1))
		Products.RowIDList.Add(ResultSet1.GetLong2(2))
	Loop
	ResultSet1.Close
End Sub

Sub InitNewItem
	edtNumber.Text = 1
	spnProduct.SelectedIndex = 0
	edtDate.Text = DateTime.Date(DateTime.Now)
End Sub

Sub InitEditItem
	Private ResultSet1 As ResultSet
	Private Query As String
	
	Query = "SELECT Stock.Number, Products.Name, Stock.Date, Products.rowid From Stock, Products WHERE Products.rowid = Stock.ProductID AND Stock.rowid = " & Main.StockSelectedRowID
	ResultSet1 = Starter.SQL1.ExecQuery(Query)
	Log(ResultSet1.RowCount)
	ResultSet1.NextRow
	Log(ResultSet1.GetInt2(0))
	edtNumber.Text = ResultSet1.GetInt2(0)
	Log(ResultSet1.GetLong2(1))
	spnProduct.SelectedIndex = Products.RowIDList.IndexOf(ResultSet1.GetLong2(3))
	Log(spnProduct.SelectedIndex)
	edtDate.Text = DateTime.Date(ResultSet1.GetLong2(2))
	ResultSet1.Close
End Sub


