Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Public Class ACProductDetails
  Inherits System.Web.UI.Page

  Public Shared ACProductID 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") = "ACProductDetails"
      If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
        btnSave.Enabled = False
      End If
      Try
        ACProductID = Request.QueryString("ACPID")
      Catch ex As Exception
        ACProductID = 0
      End Try

      If ACProductID > 0 Then
        Try
          drIni = sqlDB.GetRow("SELECT refProdCode, refACProductID FROM mapACProduct GROUP BY refProdCode, refACProductID HAVING refACProductID = '" & ACProductID & "'")
          ddlACProduct.SelectedValue = ACProductID

          ddlRGBCProduct.SelectedValue = Nz(drIni!refProdCode, "0")
          txtProdCode.Text = Nz(drIni!refProdCode, "")

          setListBox("rgbc", Nz(drIni!refProdCode, "0"))
          setListBox("ac", Nz(drIni!refACProductID, 0))

        Catch ex As IndexOutOfRangeException                'dr = ds.tables(0).rows(0) doesn't exist
          lblMessage.Text = "ACProduct ID, <font color=black>" & ACProductID & "</font>, was not found!"
          drIni = Nothing
        End Try
      Else

      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 txtACProductID.Text.Trim() = "" Then lblMessage.Text &= "ACProduct ID required.<BR>"
    If txtProdCode.Text.Trim() = "" Then txtProdCode.Text = "0"
    If ddlACProduct.SelectedIndex < 1 Then lblMessage.Text &= "AdCheck Product required.<BR>"

    If sqlDB.CountRows("SELECT * FROM mapACProduct WHERE refACProductID = " & ddlACProduct.SelectedValue & " AND refProdCode = '" & CleanString(txtProdCode.Text.Trim) & "' AND refACProductID <> " & ACProductID) > 0 Then lblMessage.Text &= "ACProduct """ & ACProductID & """ already exists!"

    If Not lblMessage.Text.Length > 0 Then

      If ACProductID <> 0 Then
        sqlDB.doQueryDS("UPDATE mapACProduct SET refACProductID = " & ddlACProduct.SelectedValue & ", refProdCode = '" & db.CleanString(txtProdCode.Text.Trim) & "' WHERE refACProductID = " & ACProductID)
        'Else
        'sqlDB.doQueryDS(db.sql_insert_ACProduct(txtACProductID.Text.Trim(), strACProductName.Text.Trim, strMarket.Text.Trim, ddlBranch.SelectedValue, ddlSegment.SelectedValue, strACProductEmail.Text.Trim, strRegionalManager.Text.Trim, strSalesManager.Text.Trim, strSalesManagerEmail.Text.Trim, CInt(chkACProduct.Checked), CInt(chkGenerateQF.Checked)))
      End If

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

  End Sub

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

  Protected Sub txtProdCode_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtProdCode.TextChanged
    setListBox("rgbc", db.CleanString(txtProdCode.Text.Trim))
  End Sub

  Protected Sub ddlRGBCProduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlRGBCProduct.SelectedIndexChanged
    If ddlRGBCProduct.SelectedIndex > 0 Then
      txtProdCode.Text = ddlRGBCProduct.SelectedValue
      setListBox("rgbc", ddlRGBCProduct.SelectedValue)
    Else
      txtProdCode.Text = ddlRGBCProduct.SelectedValue
      setListBox("rgbc", "")
    End If
  End Sub

  Protected Sub ddlACProduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlACProduct.SelectedIndexChanged
    If ddlACProduct.SelectedIndex > 0 Then
      setListBox("ac", ddlACProduct.SelectedValue)
    Else
      setListBox("ac", 0)
    End If
  End Sub

  Private Sub setListBox(ByVal strObj As String, ByVal ID As Object)
    Dim obj As ListBox
    Dim sql As String
    Select Case strObj
      Case "rgbc"
        obj = lbRGBC
        sql = "SELECT mstProduct.strProdCode, mstProduct.strLongProdDesc, mstBrand.strBrand, mstGroupingPrimary.strGroupingPrimary FROM mstProduct INNER JOIN mstBrand ON mstProduct.refBrandID = mstBrand.BrandID INNER JOIN mstGroupingPrimary ON refGroupingPrimaryID = GroupingPrimaryID WHERE mstProduct.strProdCode = '" & ID & "'"
      Case "ac"
        obj = lbAC
        sql = "SELECT TOP 1 ACProductID, strProduct, strManufacturer, strSubCategory FROM dwfACLisp WHERE ACProductID = " & ID
    End Select

    obj.Items.Clear()

    ds = db.doQueryDS(sql)
    If ds.Tables(0).Rows.Count > 0 Then
      dr = ds.Tables(0).Rows(0)
      For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
        obj.Items.Add(dr.Item(i).ToString)
      Next

    End If
  End Sub

End Class