Imports System.Web.Configuration
Imports Superbowl.sqlDB
Imports System.Data.SqlClient

'20180531 - added PO usergroup ddl - maanie

Partial Class UserDetails
   Inherits System.Web.UI.Page

   Private Shared UserID As Integer = 0
   Private Shared UserName As String = ""
   Private db As New sqlDB
   Private ds As DataSet
   Private dr, drIni As DataRow

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not Page.IsPostBack Then
         WebConfigurationManager.AppSettings("strPage") = "UserDetails"
         If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
            btnSave.Enabled = False
         End If
         Try
            UserID = Request.QueryString("UID")
         Catch ex As Exception
            UserID = 0
         End Try

         If UserID <> 0 Then
            Try
               drIni = sqlDB.GetRow(db.sql_get_Users(UserID))
               txtUser.Text = Nz(drIni!strUserName, "")
               strDomainLogin.Text = Nz(drIni!strDomainLogin, "")
               strEmail.Text = Nz(drIni!strEmail, "")

               ddlStockControl.SelectedValue = Nz(drIni!refUserGroupID, 0)
               ddlSuperbowl.SelectedValue = Nz(drIni!refUserGroupSuperbowlID, 0)
               ddlForecast.SelectedValue = Nz(drIni!refUserGroupForecastID, 0)
               ddlPromotion.SelectedValue = Nz(drIni!refUserGroupPromotionID, 0)
               ' 20150323 - Added scorecard and freestock - maanie
               ddlScoreCard.SelectedValue = Nz(drIni!refUserGroupScoreCardID, 0)
               ddlFreeStockRequisition.SelectedValue = Nz(drIni!refUserGroupFreeStockID, 0)
               ddlPurchaseOrderRequisition.SelectedValue = Nz(drIni!refUserGroupPORID, 0) '20180531 - added PO usergroup ddl - maanie
               lblLastEdit.Text = Nz(drIni!strLastUser, "unknown") & " - " & SqlDate(Nz(drIni!dtStamp, ""), "d MMMM yyyy HH:mm")

               'Bind the data for the ddls

               If Nz(drIni!refManagerID, 0) <> 0 Then
                  Me.sdsManager.SelectParameters("refManagerID").DefaultValue = drIni!refManagerID
               End If

               If Nz(drIni!refManagerAlternativeID, 0) <> 0 Then
                  Me.sdsAlternativeManager.SelectParameters("refManagerAlternativeID").DefaultValue = drIni!refManagerAlternativeID
               End If

               If Nz(drIni!refManagerExecutiveID, 0) <> 0 Then
                  Me.sdsExecutiveManager.SelectParameters("refManagerExecutiveID").DefaultValue = drIni!refManagerExecutiveID
               End If

               Me.ddlManager.DataBind()

               Me.ddlAltManager.DataBind()

               Me.ddlExeManager.DataBind()

               If Not ddlManager.Items.FindByValue(Nz(drIni!refManagerID, 0)) Is Nothing Then
                  ddlManager.SelectedValue = Nz(drIni!refManagerID, 0)
               End If


               If Not ddlAltManager.Items.FindByValue(Nz(drIni!refManagerAlternativeID, 0)) Is Nothing Then
                  ddlAltManager.SelectedValue = Nz(drIni!refManagerAlternativeID, 0)
               End If

               If Not ddlExeManager.Items.FindByValue(Nz(drIni!refManagerExecutiveID, 0)) Is Nothing Then
                  ddlExeManager.SelectedValue = Nz(drIni!refManagerExecutiveID, 0)
               End If


               If drIni!blnManager = True Then
                  chkManager.Checked = True
                  ddlExeManager.Enabled = True
                  ddlManager.Enabled = False
                  ddlManager.SelectedIndex = 0
                  ddlAltManager.Enabled = False
                  ddlAltManager.SelectedIndex = 0
               Else
                  ddlExeManager.Enabled = False
                  ddlManager.Enabled = True
                  ddlAltManager.Enabled = True
               End If

               If drIni!blnManagerExecutive = True Then
                  chkExecutiveManager.Checked = True
               End If

            Catch ex As IndexOutOfRangeException                              'drIni = ds.tables(0).rows(0) doesn't exist
               lblMessage.Text = "User ID, <font color=black>" & UserID & "</font>, was not found!"
               drIni = Nothing
            End Try
         Else
            ddlExeManager.Enabled = False
         End If
      End If
   End Sub

   Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
      Dim msg As String = ""
      Dim lastUserID As Integer = 0
      Dim refManagerID As Integer = 0

      If txtUser.Text.Trim() = "" Then
         msg = "User name required!"
      End If

      If strDomainLogin.Text.Trim() = "" Then
         msg = "Domain Login required!"
      End If

      If strEmail.Text.Trim() = "" Then
         msg = "User Email required!"
      End If

      If msg.Length > 0 Then
         lblMessage.Text = msg
      Else
         lblMessage.Text = ""

         If sqlDB.CountRows(db.sql_get_Users(strDomainLogin.Text.Trim, UserID)) > 0 Then
            lblMessage.Text = "Domain Login, """ & strDomainLogin.Text & """, already exists!"
         Else
            'v1.4.0 - 20120720 - .net4 upgrade, added forecast and promo module security group saves
            '20150323 -  added Scorecard and freestock module security group saves
            '20180531 - added PO usergroup ddl - maanie
            If UserID <> 0 Then
               sqlDB.doQueryDS(db.sql_update_Users(UserID, txtUser.Text.Trim, strDomainLogin.Text.Trim, strEmail.Text.Trim, ddlStockControl.SelectedValue, ddlSuperbowl.SelectedValue, ddlForecast.SelectedValue, ddlPromotion.SelectedValue, ddlScoreCard.SelectedValue, ddlFreeStockRequisition.SelectedValue, ddlPurchaseOrderRequisition.SelectedValue, If(chkManager.Checked = False, ddlManager.SelectedValue, UserID), ddlAltManager.SelectedValue, ddlExeManager.SelectedValue, If(chkManager.Checked = False, 0, 1), If(chkExecutiveManager.Checked = False, 0, 1)))

            Else
               Try
                  sqlDB.doQueryDS(db.sql_insert_Users(txtUser.Text.Trim, strDomainLogin.Text.Trim, strEmail.Text.Trim, ddlStockControl.SelectedValue, ddlSuperbowl.SelectedValue, ddlForecast.SelectedValue, ddlPromotion.SelectedValue, ddlScoreCard.SelectedValue, ddlFreeStockRequisition.SelectedValue, ddlPurchaseOrderRequisition.SelectedValue, ddlAltManager.SelectedValue, ddlExeManager.SelectedValue, If(chkManager.Checked = False, 0, 1), If(chkExecutiveManager.Checked = False, 0, 1)))

                  '20150323 - Set refManagerID for last Inserted User - maanie
                  UserID = LastID("UserID", "mstUser")

                  If chkManager.Checked = True Then
                     refManagerID = UserID
                  Else
                     refManagerID = ddlManager.SelectedValue
                  End If

                  sqlDB.doQueryDS(db.sql_update_Users_refManagerID(UserID, refManagerID))
               Catch ex As Exception
                  lblMessage.Text = ex.Message.Substring(0, ex.Message.IndexOf(".") + 1)
                  Exit Sub
               End Try
            End If
            'Response.Redirect("UserList.aspx")
            Response.Redirect("UserDetails.aspx?UID=" & UserID)
         End If
      End If
   End Sub

   Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
      Response.Redirect("UserList.aspx")
   End Sub

   Protected Sub chkManager_CheckedChanged(sender As Object, e As EventArgs) Handles chkManager.CheckedChanged
      'Disable ddlExeManager if Manager Checkbox is not checked
      If Not chkManager.Checked Then
         ddlExeManager.Enabled = False
         ddlExeManager.SelectedIndex = 0
         ddlManager.Enabled = True
         ddlAltManager.Enabled = True
      Else
         ddlManager.Enabled = False
         ddlManager.SelectedIndex = 0
         ddlAltManager.Enabled = False
         ddlAltManager.SelectedIndex = 0
         ddlExeManager.Enabled = True
      End If
   End Sub
End Class
