B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=6.47
@EndOfDesignText@
'Static code module
Sub Process_Globals
	Private fx As JFX
	Private xui As XUI

	Public Mode As String
	Public SelectedRowID As Long
	Public RowIDList As List
	Private ResultSet1 As ResultSet

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

	Private lblToastMessage As Label
End Sub

Public Sub Show
	If frmEditProducts.IsInitialized = False Then
		frmEditProducts.Initialize("frmEditProducts", 600, 600)
		frmEditProducts.RootPane.LoadLayout("EditProduct")
		frmEditProducts.Title = "SLQiteLight4   Edit Product"
	End If
	
	InitPage
	
	Select B4XPages.MainPage.ProductEditMode
		Case "Add"
			InitNewItem
			btnUpdate.Visible = False
		Case "Edit"
			InitEditItem
	End Select
	
	frmEditProducts.Show
End Sub

Private Sub frmEditProducts_Closed
	ResultSet1.Close
	Products.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.ToastMessageShow(lblToastMessage, "Entry inserted.", False)
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

