B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.8
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	
	Private Products As ProductsPage
	Private Suppliers As SuppliersPage
	
	Public Mode As String
	Public SelectedRowID As Long
	Public RowIDList As List
	Private ResultSet1 As ResultSet

	Private cbxSupplier As B4XComboBox
	Private edtName, edtPrice As B4XView
	Private btnAdd, btnUpdate As B4XView

	Private lblToastMessage As Label
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
	Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	Root.LoadLayout("EditProduct")

	Products = B4XPages.MainPage.Products
	Suppliers = B4XPages.MainPage.Suppliers
	
	B4XPages.SetTitle(Me, "SLQiteLight4   Edit Product")
	
	InitPage
	
	Select B4XPages.MainPage.ProductEditMode
		Case "Add"
			InitNewItem
			btnUpdate.Visible = False
		Case "Edit"
			InitEditItem
	End Select
End Sub

Private Sub B4XPage_Disappear
	ResultSet1.Close
'	Products.Show
End Sub

'Private Sub frmEditProducts_Closed
'	ResultSet1.Close
'	Products.Show
'End Sub

Public Sub Show
	
End Sub

Sub btnAdd_Click
	Private Query As String
	Private RecordSet1 As ResultSet
	
	Query = "SELECT Name, Price, SupplierID FROM Products "
	Query = Query & "WHERE Name = '" & edtName.Text & "' AND Price = " & edtPrice.Text & " AND SupplierID = " & Suppliers.RowIDList.Get(cbxSupplier.SelectedIndex)
	RecordSet1 = B4XPages.MainPage.SQL1.ExecQuery(Query)
	If RecordSet1.NextRow = True Then
		Dim sf As Object = xui.Msgbox2Async("Entry already exist! Do you want to add it ?", "A T T E N T I O N", "Yes", "", "No", Null)
		Wait For (sf) Msgbox_Result (Result As Int)
		If Result = xui.DialogResponse_Negative Then
			Return
		End If
	End If

	Query = "INSERT INTO Products VALUES ('" & edtName.Text & "', " & edtPrice.Text & ", " & Suppliers.RowIDList.Get(cbxSupplier.SelectedIndex) & ")"
	B4XPages.MainPage.SQL1.ExecNonQuery(Query)
	btnUpdate.Visible = True
	Utils.ToastMessageShow1(lblToastMessage, "Entry inserted.", False)
	B4XPages.MainPage.Products.Show
End Sub

Sub btnUpdate_Click
	Private Query As String
	
	Query = "UPDATE Products SET Name = '" & edtName.Text & "', Price = " & edtPrice.Text & ", SupplierID = " & Suppliers.RowIDList.Get(cbxSupplier.SelectedIndex)
	Query = Query & " WHERE rowid = " & Products.SelectedRowID
	
	B4XPages.MainPage.SQL1.ExecNonQuery(Query)
End Sub

Sub InitPage
	Private Query As String
	Private lstSuppliers As List
	
	Query = "SELECT Name, rowid From Suppliers"
	ResultSet1 = B4XPages.MainPage.SQL1.ExecQuery(Query)

	If lstSuppliers.IsInitialized = False Then
		lstSuppliers.Initialize
	End If
	lstSuppliers.Clear
	
	Do While ResultSet1.NextRow
		lstSuppliers.Add(ResultSet1.GetString2(0))
	Loop
	cbxSupplier.SetItems(lstSuppliers)
End Sub

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

Sub InitEditItem
	Private Query As String
	
	Query = "SELECT Products.Name, Products.Price, Products.SupplierID From Products, Suppliers WHERE Products.rowid = " & Products.SelectedRowID
	ResultSet1 = B4XPages.MainPage.SQL1.ExecQuery(Query)
	ResultSet1.NextRow
	edtName.Text = ResultSet1.GetString2(0)
	edtPrice.Text = ResultSet1.GetDouble2(1)
	cbxSupplier.SelectedIndex = Suppliers.RowIDList.IndexOf(ResultSet1.GetLong2(2))
End Sub

