Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Class KeyGroupDetails
	Inherits System.Web.UI.Page

	Public Shared KeyAccount_GroupID As Integer = 0
	Public Shared dwKeyAccount_Group As String = ""
	Public Shared dwKeyAccount 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") = "KeyAccount_GroupDetails"
			If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
            'btnSave.Enabled = False
			End If
			Try
				KeyAccount_GroupID = Request.QueryString("KAGID")
			Catch ex As Exception
				KeyAccount_GroupID = 0
			End Try

			If KeyAccount_GroupID <> 0 Then
				Try
					drIni = sqlDB.GetRow(db.sql_get_KeyAccount_Group(KeyAccount_GroupID))
					dwKeyAccount_Group = drIni!strGroupName															  'get ori KeyAccount_Group for dw
					txtKeyAccount_Group.Text = drIni!strGroupName

					dwKeyAccount = drIni!strKeyAccount
					ddlKeyAccount.SelectedValue = Nz(drIni!refKeyAccountID, 0)												  'get ori KeyAccount for dw

					lblLastEdit.Text = Nz(drIni!strLastUser, "unknown") & " - " & SqlDate(Nz(drIni!dtStamp, ""), "d MMMM yyyy HH:mm")

				Catch ex As IndexOutOfRangeException							  'dr = ds.tables(0).rows(0) doesn't exist
					lblMessage.Text = "KeyAccount_Group ID, <font color=black>" & KeyAccount_GroupID & "</font>, was not found!"
					drIni = Nothing
				End Try
			Else
				dwKeyAccount_Group = ""
				dwKeyAccount = ""
				'txtKeyAccount_Group.ReadOnly = False
			End If
		End If
	End Sub

	Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		lblMessage.Text = ""

		If txtKeyAccount_Group.Text.Trim() = "" Then lblMessage.Text &= "KeyAccount_Group name required.<BR>"
		If Not ddlKeyAccount.SelectedIndex > 0 Then lblMessage.Text &= "KeyAccount required.<BR>"

		If sqlDB.CountRows(db.sql_get_KeyAccount_Group(txtKeyAccount_Group.Text.Trim, KeyAccount_GroupID, ddlKeyAccount.SelectedValue)) > 0 Then lblMessage.Text &= "KeyAccount_Group """ & txtKeyAccount_Group.Text.Trim & """ already exists!"

		If Not lblMessage.Text.Length > 0 Then

			If KeyAccount_GroupID <> 0 Then
            sqlDB.doQueryDS(db.sql_update_KeyAccount_Group(KeyAccount_GroupID, txtKeyAccount_Group.Text.Trim, ddlKeyAccount.SelectedValue, Session.Item("User")))
				'2-Update into DW		- ONLY ON EXISTING KAGs
				If dwKeyAccount_Group <> txtKeyAccount_Group.Text.Trim Or dwKeyAccount <> ddlKeyAccount.SelectedItem.Text Then
					sqlDB.doQueryDS(db.sql_update_KeyAccount_Group_dw(dwKeyAccount_Group, txtKeyAccount_Group.Text, dwKeyAccount, ddlKeyAccount.SelectedItem.Text))
				End If
			Else
            sqlDB.doQueryDS(db.sql_insert_KeyAccount_Group(txtKeyAccount_Group.Text.Trim, ddlKeyAccount.SelectedValue, Session.Item("User")))
			End If

			Response.Redirect("KeyGroupList.aspx")
		End If

	End Sub

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

End Class