'Static code module
Sub Process_Globals  
    Public vuetify As VuetifyApp
    Public home As VueComponent
    Public path As String
    Public name As String = "home"
    Private banano As BANano
    Private FB As BANanoDataSourceFireBase
    Private txtfirstname As VTextField
    Private txtfullname As VTextField
    Private txtlastname As VTextField
    Private txtuserid As VTextField
    Private usersCont As VContainer
    Private usersR2 As VRow
    Private usersR2C1 As VCol
    Private usersR2C2 As VCol
    Private btnSaveUser As VBtn
    Private userMsgBox As VMsgBox
    Private tblUsers As VueTable
    Private txtage As VTextField
    Private tblFName As VToolBarTitle
    Private tblUser As VToolBar
    Private ageDistribution As VChartKick
End Sub
Sub Initialize
    'establish a reference to the app
    vuetify = pgIndex.vuetify
    'initialize the component
    home.Initialize(Me, name)
    home.vuetify = vuetify
    path = home.path
    '
    'build the page html here
    banano.LoadLayout(home.Here, "home")
    'bind the element
    FB.BindState(home)
    txtfirstname.BindState(home)
    txtfullname.BindState(home)
    txtlastname.BindState(home)
    txtuserid.BindState(home)
    usersCont.BindState(home)
    usersR2.BindState(home)
    usersR2C1.BindState(home)
    usersR2C2.BindState(home)
    btnSaveUser.BindState(home)
    userMsgBox.BindState(home)
    tblUsers.BindState(home)
    txtage.BindState(home)
    tblFName.BindState(home)
    tblUser.BindState(home)
    ageDistribution.BindState(home)
   
    home.SetMounted(Me, "LoadUsers", Null)
    'watch for changes in the data
    home.SetWatch("user.firstname", True, True, Me, "updatefullname", Null)
    home.SetWatch("user.lastname", True, True, Me, "updatefullname", Null)
   
    home.SetComputed("user.fullname", Me, "updatefullname", Null)
   
    'add this route component to the app
    vuetify.AddRoute(home)
End Sub  
Sub LoadUsers            'ignoreDeadCode
    'set the datasource to nothing
    FB.RESET
    'connect to firebase
    FB.Connect
End Sub
'update the full name
Sub updatefullname() As String
    Dim fn As String = home.GetData("user.firstname")
    Dim ln As String = home.GetData("user.lastname")
    Dim ff As String = $"${fn} ${ln}"$
    home.SetData("user.fullname", ff)
End Sub
Private Sub btnSaveUser_Click (e As BANanoEvent)
    'update the loading status of the button
    btnSaveUser.UpdateLoading(home, True)
    'create or update a record depending on the mode
    FB.CREATE_OR_UPDATE
End Sub
Sub AddNew
    'set the mode to create
    FB.CREATE_MODE
    'get the refs and link them to this router
    home.refs = vuetify.GetRefs
    'set the contents of the form to the defaults
    FB.DEFAULTS
    'focus to the first name
    home.SetFocus(txtfirstname.id)
End Sub
Private Sub FB_SelectAll (Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)
    'load table contents
    tblUsers.Reload(Result)
    'load chart contents
    ageDistribution.Clear
    For Each person As Map In Result
        Dim firstname As String = person.Get("firstname")
        Dim age As String = person.Get("age")
        age = banano.parseInt(age)
        ageDistribution.AddXY(firstname, age)
    Next
    ageDistribution.Refresh
    tblUsers.UpdateLoading(False)
End Sub
Private Sub FB_Connected (Success As Boolean)
    'depending on the connection status, update the button
    If Success = False Then
        'make the button red
        vuetify.UpdateColor("btnFirebase", "red")
        vuetify.ShowSwalToastError("Could not connect to firebase, refresh your browser!", 2000)
        Return
    End If
   
    'change the button to green
    vuetify.UpdateColor("btnFirebase", "green")
    tblUsers.UpdateLoading(True)
    'execute the select all
    FB.SELECTALL
End Sub
Private Sub userMsgBox_ok_click (e As BANanoEvent)
    'get the process
    Dim sprocess As String = userMsgBox.Process(home)
    Select Case sprocess
    Case "delete"
        'update loading of the button
        tblUsers.UpdateLoading(True)
        'we are deleting the user, set the mode
        FB.DELETE_MODE
        'hide the dialog
        userMsgBox.UpdateVisible(home, False)
        'delete the record from the db, it has been saved
        FB.DELETE      
    End Select
End Sub
Private Sub userMsgBox_cancel_click (e As BANanoEvent)
    'hide the dialog
    userMsgBox.UpdateVisible(home, False)
End Sub
Private Sub tblUsers_Add_Click (e As BANanoEvent)
    AddNew
End Sub
Private Sub tblUsers_Edit (item As Map)
    'show loading on table
    tblUsers.UpdateLoading(True)
    'read from the database in case the record has changed
    FB.UPDATE_MODE
    'read the record
    FB.READ1(item)
End Sub
Private Sub tblUsers_Delete (item As Map)
    'get the singular
    Dim ssingular As String = FB.Singular
    'build the message
    Dim msg As String = $"Está seguro de que desea eliminar este registro?"$
    'set the record to delete
    FB.SetRecord(item)
   
    Dim sDisplayvalue As String = FB.DisplayValue
    'ask the user to confirm deleting
    userMsgBox.Confirm(home, "delete", $"Thibitisha Futa: ${sDisplayvalue} ${ssingular.tolowercase}"$, msg, "Ita", "Um")
End Sub
Private Sub tblUsers_Refresh_Click (e As BANanoEvent)
    'upload loading of the table
    tblUsers.UpdateLoading(True)
    'select everything from fb
    FB.SELECTALL  
End Sub
Private Sub tblUsers_ClearSort_Click (e As BANanoEvent)
    tblUsers.ClearSort
End Sub
Private Sub tblUsers_Back_Click (e As BANanoEvent)
    vuetify.NavigateTo("/")
End Sub
Private Sub FB_Delete (Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)
    'turn off loading
    tblUsers.UpdateLoading(False)
    '
    If affectedRows = 0 Then
        'no delete was made
        vuetify.ShowSwalToastInfo("The person could not be deleted!", 2000)
    Else if affectedRows = 1 Then          
        vuetify.ShowSwalToastSuccess("Person was deleted successfully!", 1000)
        'reload all categories
        FB.SELECTALL
    End If
End Sub
Private Sub FB_Read (Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)
    'hide table loading
    tblUsers.UpdateLoading(False)
    'Do we have a record
    If affectedRows = 1 Then
        'the form data has been updated automatically
    Else
        vuetify.ShowSwalToastError("Person could not be read!", 2000)
        'prepare adding a new record instead
        AddNew
    End If
End Sub
Private Sub FB_Create (Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)
    'turn off loading for button
    btnSaveUser.UpdateLoading(home, False)
    'show notification
    If affectedRows >= 1 Then
        vuetify.ShowSwalToastSuccess("Person created successfully!", 1000)
        'ready the screen for another entry
        AddNew
        'load everything in the database / append to the table this record
        tblUsers.UpdateLoading(True)
        FB.SELECTALL
    Else          
        vuetify.ShowSwalToastError("Person could not be created!", 2000)
    End If
End Sub
Private Sub FB_Update (Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)
    'hide table loading
    btnSaveUser.UpdateLoading(home, False)
    'Do we have a record
    If affectedRows = 1 Then
        'the form data has been updated automatically
        vuetify.ShowSwalToastError("Person updated successfully!", 1000)
        'reload all categories
        FB.SELECTALL
    Else
        vuetify.ShowSwalToastError("Person could not be updated!", 2000)
        'prepare adding a new record instead
        AddNew
    End If
End Sub
Private Sub FB_OnAuthStateChanged (User As Map)
    Log("FB_OnAuthStateChanged")
    Log(User)
End Sub