﻿Imports System.Web.Configuration

Partial Public Class ProductDataDimensionList
   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

   End Sub

   Private Sub ProductDataDimensionList_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

      If Not Page.IsPostBack Then
         Dim arrFilter(0) As String
         Dim intStart, intEnd As Integer

         intDateColumn = gvProductDataDimension.Columns.Count - 2

         gvProductDataDimension.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
         If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
            btnAdd1.Enabled = False
            btnAdd2.Enabled = False
         End If
         Try
            If Request.QueryString("BID").Length > 0 Then
               WebConfigurationManager.AppSettings("strPage") = "ProductDataDimensionList"
               WebConfigurationManager.AppSettings("intListFilterPDD") = "1=1 AND refBrandID = " & sqlDB.CleanString(Request.QueryString("BID"))
            End If
         Catch ex As Exception
         End Try

         If WebConfigurationManager.AppSettings("strPage") = "ProductDetails" Or WebConfigurationManager.AppSettings("strPage") = "ProductList" Then

            If WebConfigurationManager.AppSettings("ShowMore") = True Then
               gvProductDataDimension.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
            End If

            gvProductDataDimension.PageIndex = WebConfigurationManager.AppSettings("intListIndex")
            strGVFilter = WebConfigurationManager.AppSettings("intListFilterPDD")

            If WebConfigurationManager.AppSettings("strPage").Contains("List") Then WebConfigurationManager.AppSettings("intListFilterPDD") = ""

            Try
               arrFilter(0) = strGVFilter.Replace(" AND ", Chr(31))
               arrFilter = arrFilter(0).Split(Chr(31))
               For i As Integer = 1 To arrFilter.Length - 1
                  If arrFilter(i).Contains("strProdCode") Then
                     intStart = arrFilter(i).LastIndexOf("'%")
                     intEnd = arrFilter(i).LastIndexOf("%'")
                     'ddlStatus.SelectedValue = arrFilter(i).Substring(arrFilter(i).IndexOf("=")).Trim
                     txtProduct.Text = arrFilter(i).Substring(intStart + 2, intEnd - intStart - 2)
                  End If
                  If arrFilter(i).Contains("refBrandID") Then ddlBrand.SelectedValue = arrFilter(i).Substring(arrFilter(i).IndexOf("=") + 1).Trim
                  If arrFilter(i).Contains("blnActive") Then ddlStatus.SelectedValue = arrFilter(i).Substring(arrFilter(i).IndexOf("=") + 1).Trim
               Next

               sdsProductDataDimension.FilterExpression = strGVFilter
               'Exit Sub
            Catch ex As Exception

            End Try

         End If

         'btnSearch_Click(sender, e)
      End If

      dblRows = gvProductDataDimension.PageIndex * gvProductDataDimension.PageSize     '< has to run for every action
   End Sub

   Private Sub ProductDataDimensionList_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
      Dim s As String = ""

      lblMessage.Text = ""
      btnExport.Enabled = True
      Try
         If strGVFilter.Length > 0 Then s = "WHERE " & strGVFilter
      Catch ex As Exception
      End Try
      If sqlDB.CountRows("SELECT strProdCode FROM mstProduct " & s) = 0 Then
         lblMessage.Text = "<div width='100%' align='center'>- No Products found -</div>"
         btnExport.Enabled = False
      End If
   End Sub

   Private Sub gvProductDataDimension_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProductDataDimension.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
         Dim btnDelete As LinkButton = CType(e.Row.Cells(e.Row.Cells.Count - 1).Controls(0), LinkButton)
         If WebConfigurationManager.AppSettings("ULEVEL") = 5 Then
            btnDelete.OnClientClick = String.Format("return confirm('Are you certain you want to delete product ""{0}"" ?');",
         DirectCast(DirectCast(e.Row.Cells(1).Controls(0), System.Web.UI.Control), System.Web.UI.WebControls.HyperLink).Text)
         Else
            btnDelete.Enabled = False
         End If

         Try
            dblRows += 1
            e.Row.Cells(0).Text = dblRows
            e.Row.Cells(intDateColumn).Text = sqlDB.SqlDate(e.Row.Cells(intDateColumn).Text, "d MMM yyyy")  ' "d MMM yyyy HH:mm")
         Catch ex As Exception
            e.Row.Cells(intDateColumn).Text = Date.Now.ToString("n/a")
         End Try
      End If
   End Sub

   Private Sub gvProductDataDimension_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProductDataDimension.RowDeleting

      sqlDB.doQueryDS("UPDATE mstProduct SET blnActive = 0 WHERE strProdCode = '" & e.Keys(0) & "' ")
      sqlDB.doQueryDS("UPDATE dwdProduct SET blnActive = 0 WHERE strProdCode = '" & e.Keys(0) & "'")
      e.Cancel = True
      btnSearch_Click(sender, e:=New System.EventArgs())
   End Sub

   Private Sub gvProductDataDimension_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvProductDataDimension.Sorting
      sdsProductDataDimension.FilterExpression = strGVFilter
      dblRows = 0
   End Sub

   Private Sub gvProductDataDimension_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProductDataDimension.PageIndexChanged
      sdsProductDataDimension.FilterExpression = strGVFilter
      WebConfigurationManager.AppSettings("intListIndex") = gvProductDataDimension.PageIndex
   End Sub

   Protected Sub btnAdd1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd1.Click, btnAdd2.Click
      WebConfigurationManager.AppSettings("intListIndex") = gvProductDataDimension.PageIndex
      WebConfigurationManager.AppSettings("intListFilterPDD") = strGVFilter
      Response.Redirect("ProductDataDimensionDetails.aspx?CID=0")
   End Sub

   Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
      sdsProductDataDimension.FilterExpression = "1=1"
      dblRows = 0

      Dim s As String

      If txtProduct.Text.Length > 0 Then
         s = sqlDB.CleanString(txtProduct.Text.Trim)
         sdsProductDataDimension.FilterExpression &= " AND (strProdCode LIKE '%" & s & "%' OR strDescription LIKE '%" & s & "%') "
      End If
      If ddlBrand.SelectedIndex > 0 Then
         sdsProductDataDimension.FilterExpression &= " AND refBrandID = " & ddlBrand.SelectedValue
      End If

      If ddlStatus.SelectedIndex > 0 Then
         sdsProductDataDimension.FilterExpression &= " AND blnActive = " & ddlStatus.SelectedValue
      End If

      strGVFilter = sdsProductDataDimension.FilterExpression
      WebConfigurationManager.AppSettings("intListFilterPDD") = strGVFilter
   End Sub

   Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
      sdsProductDataDimension.FilterExpression = strGVFilter
      gvExport.Visible = True

      Response.Clear()
      Response.Buffer = True
      Response.ContentType = "application/vnd.ms-excel"
      Response.Charset = ""
      Response.AddHeader("Content-Disposition",
       "attachment;filename=ProductDataDimensionList_" & Session.Item("User").Replace(" ", "") & "_" & Date.Today.ToString("yyyyMMdd") & ".xls") 'v1.4.5 - 20160913 - Fixed logged In user & savings (i.e use session instead) - maanie
      Me.EnableViewState = False

      Dim oStringWriter As New System.IO.StringWriter()
      Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
      Dim frm As HtmlForm = New HtmlForm()
      Me.Controls.Add(frm)
      frm.Controls.Add(gvExport)
      frm.RenderControl(oHtmlTextWriter)
      Response.Write(oStringWriter.ToString())

      gvExport.Visible = False

      Response.End()

   End Sub

   Protected Sub chkResize_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkResize.CheckedChanged
      gvProductDataDimension.PageIndex = 0
      sdsProductDataDimension.FilterExpression = strGVFilter
      If chkResize.Checked Then
         gvProductDataDimension.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
         WebConfigurationManager.AppSettings("ShowMore") = True
      Else
         gvProductDataDimension.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
         WebConfigurationManager.AppSettings("ShowMore") = False
      End If
   End Sub
End Class