Imports System.Web.Configuration

Partial Public Class KeyAccount_GroupList
	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
		If Not Page.IsPostBack Then dblRows = 0

		lblMessage.Text = ""
	End Sub

	Private Sub Page_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 = gvKeyAccount_Group.Columns.Count - 2

			gvKeyAccount_Group.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
			If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
				btnAdd1.Enabled = False
				btnAdd2.Enabled = False
      End If

      Try
        If Request.QueryString("KAID").Length > 0 Then
          WebConfigurationManager.AppSettings("strPage") = "CustomerList"
          WebConfigurationManager.AppSettings("intListFilter") = "1=1 AND refKeyAccountID = " & sqlDB.CleanString(Request.QueryString("KAID"))
        End If
      Catch ex As Exception

      End Try

      If WebConfigurationManager.AppSettings("strPage") = "KeyAccount_GroupDetails" Or WebConfigurationManager.AppSettings("strPage") = "CustomerList" Then

        If WebConfigurationManager.AppSettings("ShowMore") = True Then
          gvKeyAccount_Group.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
        End If

        gvKeyAccount_Group.PageIndex = WebConfigurationManager.AppSettings("intListIndex")
        strGVFilter = WebConfigurationManager.AppSettings("intListFilter")
        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("strGroupName") Then
              intStart = arrFilter(i).LastIndexOf("'%")
              intEnd = arrFilter(i).LastIndexOf("%'")
              'ddlStatus.SelectedValue = arrFilter(i).Substring(arrFilter(i).IndexOf("=")).Trim
              txtKeyAccount_Group.Text = arrFilter(i).Substring(intStart + 2, intEnd - intStart - 2)
            End If
            If arrFilter(i).Contains("refKeyAccountID") Then ddlKeyAccount.SelectedValue = arrFilter(i).Substring(arrFilter(i).IndexOf("=") + 1).Trim
          Next

          sdsKeyAccount_Group.FilterExpression = strGVFilter
          'Exit Sub
        Catch ex As Exception

        End Try

      End If

      'btnSearch_Click(sender, e)
		End If

		dblRows = gvKeyAccount_Group.PageIndex * gvKeyAccount_Group.PageSize		 '< has to run for every action				
	End Sub

	Private Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
		Dim s As String = ""

		btnExport.Enabled = True

    Try
      If strGVFilter.Length > 0 Then s = "WHERE " & strGVFilter
      If sqlDB.CountRows("SELECT KeyAccount_GroupID FROM mstKeyAccount_Group " & s) = 0 Then
        lblMessage.Text = "<div width='100%' align='center'>- No Key Account Groups found -</div>"
        btnExport.Enabled = False
      End If
    Catch ex As Exception
    End Try
	End Sub

	Private Sub gvKeyAccount_Group_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvKeyAccount_Group.Sorting
		sdsKeyAccount_Group.FilterExpression = strGVFilter
		dblRows = 0
	End Sub

	Private Sub gvKeyAccount_Group_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvKeyAccount_Group.PageIndexChanged
		sdsKeyAccount_Group.FilterExpression = strGVFilter
		WebConfigurationManager.AppSettings("intListIndex") = gvKeyAccount_Group.PageIndex
	End Sub

	Private Sub gvKeyAccount_Group_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvKeyAccount_Group.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 KeyAccount_Group ""{0}"" ?');", _
				 DirectCast(DirectCast(e.Row.Cells(1).Controls(0), System.Web.UI.Control), System.Web.UI.WebControls.HyperLink).Text)
				'might need to change e.Row.Cells(1).Controls(0) if using inGridEdit
			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 gvKeyAccount_Group_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvKeyAccount_Group.RowDeleting
		Dim intRows As Integer = 0
		lblMessage.Text = ""
		intRows += sqlDB.CountRows("SELECT strCustomerNo FROM dwdCustomer WHERE strGroupName = '" & e.Keys(1) & "' ")
		intRows += sqlDB.CountRows("SELECT refKeyAccount_GroupID FROM mstCustomer WHERE refKeyAccount_GroupID = " & e.Keys(0))

		If intRows > 0 Then
			lblMessage.Text = "<div width='100%' align='right'>Unable to delete KeyAccount_Group '" & e.Keys(1) & "': KeyAccount_Group is in use</div>"
			e.Cancel = True
		Else
			e.Cancel = False
			btnSearch_Click(sender, e:=New System.EventArgs())
		End If
	End Sub

	Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
		sdsKeyAccount_Group.FilterExpression = "1=1"
		dblRows = 0

		Dim s As String

		If txtKeyAccount_Group.Text.Length > 0 Then
			s = sqlDB.CleanString(txtKeyAccount_Group.Text.Trim)
			sdsKeyAccount_Group.FilterExpression &= " AND strGroupName LIKE '%" & s & "%'"
		End If
		If ddlKeyAccount.SelectedIndex > 0 Then
			sdsKeyAccount_Group.FilterExpression &= " AND refKeyAccountID = " & ddlKeyAccount.SelectedValue
		End If

		strGVFilter = sdsKeyAccount_Group.FilterExpression
		WebConfigurationManager.AppSettings("intListFilter") = strGVFilter

	End Sub

	Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd1.Click, btnAdd2.Click
		Response.Redirect("KeyGroupDetails.aspx?KAGID=0")
	End Sub

	Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
		sdsKeyAccount_Group.FilterExpression = strGVFilter
		gvExport.Visible = True

		Response.Clear()
		Response.Buffer = True
		Response.ContentType = "application/vnd.ms-excel"
		Response.Charset = ""
      Response.AddHeader("Content-Disposition", _
       "attachment;filename=KeyAccountGroupList_" & 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
		gvKeyAccount_Group.PageIndex = 0
		sdsKeyAccount_Group.FilterExpression = strGVFilter
		If chkResize.Checked Then
			gvKeyAccount_Group.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
			WebConfigurationManager.AppSettings("ShowMore") = True
		Else
			gvKeyAccount_Group.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
			WebConfigurationManager.AppSettings("ShowMore") = False
		End If
	End Sub

End Class