Imports EDSL.Entities
Imports EDSL.DAL
Public Partial Class UserEditControl
  Inherits System.Web.UI.UserControl
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Request.QueryString("n") = 1 then
      hypPage2.NavigateUrl = "~/UserAdmin/edit2.aspx?id=" & Request.QueryString("id") & "&n=1"
    else
      hypPage2.NavigateUrl = "~/UserAdmin/edit2.aspx?id=" & Request.QueryString("id")
    end if

    If IsPostBack = False then
      Call PopulateddlUserType    
      If Request.QueryString("id") = 0 orelse Request.QueryString("id") = "" Then
        btnDelete.Visible = False
        divPWD.Visible = False
        txtName.Enabled = True
      else
        btnDelete.Visible = true
        txtName.Enabled = False
        call GetDetails(Request.QueryString("id"))
      End If
    End If
  End Sub
  Private sub GetDetails(byval intLoginID As Integer)
    Dim objMain As new dbmainUser
    Dim objMainEntity As entMainLoginDetails = objMain.GetItem(intLoginID)
    txtID.Value = objMainEntity.LoginID
    txtName.Text = objMainEntity.LoginName
    txtFirstName.Text = objMainEntity.Firstname
    txtSurname.Text = objMainEntity.Surname
    ddlUserType.Items.FindByValue(objMainEntity.LoginType).Selected = True
    If objMainEntity.Active = 1 then
      radActive.Checked = True
    else
      radActive.Checked = False
    End If
  End sub
  Private Sub PopulateddlUserType()
    Dim objMain As new dbmainFunctions
    Dim objDDLItems As List(Of entMainDropDown) = objMain.GetDropDownReferenceUsertypes
    For Each objDDL As entMainDropDown In objDDLItems
      Dim objListitem As New ListItem
      objListitem.Text = objddl.TextDisplay
      objListitem.Value = objddl.DropDownValue
      ddlUserType.Items.Add(objListitem)
    Next
  End Sub
  Protected Sub btnDelete_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
    Dim objMain As new dbmainUser
    Response.Redirect("Delete.aspx?id=" & txtID.Value & "&idtext=" & txtName.Text & "&r=e")
  End Sub
  Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
    Dim objMainAdmin As new dbmainUser
    Dim intResultFromSave As Integer
    Dim intID As Integer
    If txtID.Value = "" then
      intID = 0
    else 
      intID = cint(txtID.Value)
    End If

    intResultFromSave = objMainAdmin.SaveItem(intID, txtName.Text, txtFirstName.Text, txtSurname.Text, ddlUserType.SelectedValue, iif(radActive.Checked = True, 1, 0), session("passover"))
    Select Case intResultFromSave 
      Case 0
        Response.Redirect("List.aspx")
      Case 1
        divErrorPanel.InnerHtml = "Error saving user"
      Case 2
        divErrorPanel.InnerHtml = "Username already exists"
      Case Else
        Response.Redirect("List.aspx")
    End Select
  End Sub
  Protected Sub btnBack_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
    Response.Redirect("List.aspx")
  End Sub
End Class
