﻿Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Class ChannelDetails
	Inherits System.Web.UI.Page

	Public Shared ChannelID As Integer = 0
	Private db As New sqlDB
	Private ds As DataSet
  Private dr, drIni As DataRow
  Private dwCP, dwCS, dwSP, dwSS As String


	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		If Not Page.IsPostBack Then
      WebConfigurationManager.AppSettings("strPage") = "ChannelSegmentDetails"
			If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
				btnSave.Enabled = False
			End If
			Try
				ChannelID = Request.QueryString("CID")
			Catch ex As Exception
				ChannelID = 0
			End Try

			If ChannelID <> 0 Then
				Try
          drIni = sqlDB.GetRow(db.sql_get_ChannelSegment(ChannelID))
          txtChannelPrimary.Text = drIni!strChannelPrimary
          txtChannelSecondary.Text = drIni!strChannel
          txtSegmentPrimary.Text = drIni!strSegmentPrimary
          txtSegmentSecondary.Text = drIni!strSegment

          dwCP = drIni!strChannelPrimary
          dwCS = drIni!strChannel
          dwSP = drIni!strSegmentPrimary
          dwSS = drIni!strSegment

					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 = "Channel Segment ID, <font color=black>" & ChannelID & "</font>, was not found!"
					drIni = Nothing
				End Try
      Else
        dwCP = ""
        dwCS = ""
        dwSP = ""
        dwSS = ""
        'txtChannel.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 Not lblMessage.Text.Length > 0 Then

			If ChannelID <> 0 Then
            sqlDB.doQueryDS(db.sql_update_ChannelSegment(ChannelID, txtChannelPrimary.Text.Trim, txtChannelSecondary.Text.Trim, txtSegmentPrimary.Text.Trim, txtSegmentSecondary.Text.Trim, Session.Item("User")))
				'2-Update into DW		

        sqlDB.doQueryDS(db.sql_update_ChannelSegment_dw(dwCP, dwCS, dwSP, dwSS))

      Else
            sqlDB.doQueryDS(db.sql_insert_ChannelSegment(txtChannelPrimary.Text.Trim, txtChannelSecondary.Text.Trim, txtSegmentPrimary.Text.Trim, txtSegmentSecondary.Text.Trim, Session.Item("User")))
      End If

      Response.Redirect("ChannelList.aspx")
    End If

	End Sub

	Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
		Response.Redirect("ChannelList.aspx")
	End Sub

End Class