Imports System.Linq
Imports System.Collections.Generic

Partial Class Configurations_UserAdmin
    Inherits AdminPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        'always default detail viw to false
        'me.tblUserDetails.Visible = false
        If not Me.IsPostBack then
            PopulateUserList()
        End If
        
         
    End Sub

    Sub PopulateUserList()
        ' issue 28289
        Dim usersTA As DALTableAdapters.UsersTableAdapter = New DALTableAdapters.UsersTableAdapter
        Dim users As DAL.UsersDataTable = usersTA.GetData()
        Dim lstUsers As List(Of DAL.UsersRow)
        lstUsers = users.OrderBy(Function(x) x.UserName).ToList()
        'load users into user list
        Me.listUsers.Items.Clear()
        For Each user As DAL.UsersRow In lstUsers
            Me.listUsers.Items.Add(New ListItem(user.UserName, user.UserID))
        Next
    End Sub

    Protected Sub listUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listUsers.SelectedIndexChanged
        'if a user is selected from list, we populate the user's information
        dim userid as int32 = int32.Parse(me.listUsers.SelectedItem.Value)
        Me.ctxUserDetails.SetEditUser(userid)
        Me.tblUserDetails.Visible = True
    End Sub

    Protected Sub btnNewuser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewuser.Click
        Me.ctxUserDetails.SetCreateUser()
        Me.tblUserDetails.Visible = True
    End Sub

    Protected Sub ctxUserDetails_newUserCreated() Handles ctxUserDetails.newUserCreated
        PopulateUserList()
        listUsers.SelectedIndex = listUsers.Items.Count - 1
        Dim userid As Int32 = Int32.Parse(Me.listUsers.SelectedItem.Value)
        Me.ctxUserDetails.SetEditUser(userid)
        Me.tblUserDetails.Visible = True
    End Sub
End Class
