Imports System.Web.Configuration
Imports Superbowl.sqlDB

Public Partial Class CompSetProductList
  Inherits System.Web.UI.Page

  Private Shared intDateColumn As Integer
  Private Shared dblRows As Double = 0
  Private Shared strGVFilter As String = ""

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    WebConfigurationManager.AppSettings("strPage") = "CompSetProductList"
  End Sub

  Private Sub CompSetProductList_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

    If Not Page.IsPostBack Then
      intDateColumn = gvCompSetProduct.Columns.Count - 2

         gvCompSetProduct.PageSize = sqlDB.Nz(WebConfigurationManager.AppSettings("PAGESIZE"), 25)
         If sqlDB.Nz(WebConfigurationManager.AppSettings("ULEVEL"), 0) <> 5 Then
            btnAdd.Enabled = False
         End If

         If sqlDB.Nz(WebConfigurationManager.AppSettings("ShowMore"), False) = True Then
            gvCompSetProduct.PageSize = sqlDB.Nz(WebConfigurationManager.AppSettings("MAXPAGESIZE"), 1000)
            chkResize.Checked = True
         End If

         gvCompSetProduct.PageIndex = sqlDB.Nz(WebConfigurationManager.AppSettings("intListIndex"), 0)
         strGVFilter = sqlDB.Nz(WebConfigurationManager.AppSettings("intListFilter"), "")

         If strGVFilter.Length > 0 Then
            Try
               sdsCompSetProduct.FilterExpression = strGVFilter
            Catch ex As Exception
            End Try
         End If

      End If

      dblRows = gvCompSetProduct.PageIndex * gvCompSetProduct.PageSize
   End Sub

   Private Sub CompSetProductList_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
      Dim s As String = ""

      lblMessage.Text = ""
      Try
         If Not strGVFilter Is Nothing AndAlso strGVFilter.Length > 0 Then s = "WHERE " & strGVFilter
      Catch ex As Exception
      End Try

    Dim strWhere As String = "WHERE 1=1"
    If s.Length > 0 Then
       strWhere &= " " & s.Replace("WHERE ", "AND ")
    End If

    Dim intCount As Integer = sqlDB.CountRows("SELECT DISTINCT strPrimaryProdCode FROM mstProductCompetitorSet " & strWhere)
      If intCount = 0 Then
         lblMessage.Text = "<div width='100%' align='center'>- No Comp Set Products found -</div>"
      End If
   End Sub

   Private Sub gvCompSetProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCompSetProduct.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
         Try
            Dim btnDelete As LinkButton = CType(e.Row.Cells(e.Row.Cells.Count - 1).Controls(0), LinkButton)
            If sqlDB.Nz(WebConfigurationManager.AppSettings("ULEVEL"), 0) = 5 Then
               'Safely get the primary product code from DataKeys
               Dim strProdCode As String = "this product"
               Try
                  If gvCompSetProduct.DataKeys.Count > 0 AndAlso e.Row.RowIndex >= 0 AndAlso e.Row.RowIndex < gvCompSetProduct.DataKeys.Count Then
                     strProdCode = gvCompSetProduct.DataKeys(e.Row.RowIndex).Value.ToString()
                  End If
               Catch ex As Exception
                  'Use default message if DataKeys access fails
               End Try
               
               btnDelete.OnClientClick = String.Format("return confirm('Are you certain you want to delete all comp set products for ""{0}"" ?');", strProdCode)
            Else
               btnDelete.Enabled = False
            End If
         Catch ex As Exception
            'Error accessing delete button - skip it
         End Try

         Try
            dblRows += 1
            e.Row.Cells(0).Text = dblRows
            If e.Row.Cells(intDateColumn).Text.Length >= 12 Then
               e.Row.Cells(intDateColumn).Text = sqlDB.SqlDate(e.Row.Cells(intDateColumn).Text, "d MMM yyyy")
            End If
         Catch ex As Exception
            e.Row.Cells(intDateColumn).Text = "n/a"
         End Try
      End If
   End Sub

   Private Sub gvCompSetProduct_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvCompSetProduct.RowDeleting
      ' Soft delete all comp set records for this primary product
      Dim strProdCode As String = e.Keys(0).ToString()
      Dim strUserName As String = Session.Item("User")
      Dim db As New sqlDB

      sqlDB.doQueryDS(db.sql_delete_ProductCompetitorSet_ByPrimaryProduct(strProdCode, strUserName))
      e.Cancel = True
      btnSearch_Click(sender, e:=New System.EventArgs())
   End Sub

   Private Sub gvCompSetProduct_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvCompSetProduct.Sorting
      If Not strGVFilter Is Nothing Then
         sdsCompSetProduct.FilterExpression = strGVFilter
      End If
      dblRows = 0
   End Sub

   Private Sub gvCompSetProduct_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvCompSetProduct.PageIndexChanged
      If Not strGVFilter Is Nothing Then
         sdsCompSetProduct.FilterExpression = strGVFilter
      End If
      WebConfigurationManager.AppSettings("intListIndex") = gvCompSetProduct.PageIndex
   End Sub

   Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
      WebConfigurationManager.AppSettings("intListIndex") = gvCompSetProduct.PageIndex
      WebConfigurationManager.AppSettings("intListFilter") = sqlDB.Nz(strGVFilter, "")
      Response.Redirect("CompSetProductDetails.aspx?PID=0")
   End Sub

   Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
      sdsCompSetProduct.FilterExpression = "1=1"
      dblRows = 0

      Dim s As String

      If txtProductName.Text.Length > 0 Then
         s = sqlDB.CleanString(txtProductName.Text.Trim)
         sdsCompSetProduct.FilterExpression &= " AND (strPrimaryProdCode LIKE '%" & s & "%' OR strPrimaryProductDesc LIKE '%" & s & "%' OR strCompSetName LIKE '%" & s & "%') "
      End If

      strGVFilter = sdsCompSetProduct.FilterExpression
      WebConfigurationManager.AppSettings("intListFilter") = strGVFilter
   End Sub

   Protected Sub chkResize_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkResize.CheckedChanged
      gvCompSetProduct.PageIndex = 0
      If Not strGVFilter Is Nothing Then
         sdsCompSetProduct.FilterExpression = strGVFilter
      End If
      If chkResize.Checked Then
         gvCompSetProduct.PageSize = sqlDB.Nz(WebConfigurationManager.AppSettings("MAXPAGESIZE"), 1000)
         WebConfigurationManager.AppSettings("ShowMore") = True
      Else
         gvCompSetProduct.PageSize = sqlDB.Nz(WebConfigurationManager.AppSettings("PAGESIZE"), 25)
         WebConfigurationManager.AppSettings("ShowMore") = False
    End If
  End Sub
End Class

