Imports System.Web.Configuration
Partial Public Class UsageLog
	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 UsageLog_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

		If Not Page.IsPostBack Then
			intDateColumn = gvUsage.Columns.Count - 1
			If txtEndDate.Attributes.Count = 0 Then
				txtStartDate.Attributes.Add("onfocus", "showCalendarControl(this);")
				txtEndDate.Attributes.Add("onfocus", "showCalendarControl(this);")
			End If

			If WebConfigurationManager.AppSettings("ShowMore") = True Then
				gvUsage.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
			Else
				gvUsage.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
			End If

			txtStartDate.Text = "01-" & Date.Today.ToString("MM-yyyy")
			txtEndDate.Text = Date.Today.ToString("dd-MM-yyyy")
		End If

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

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

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

	Private Sub gvUsage_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvUsage.RowDataBound

		If e.Row.RowType = DataControlRowType.DataRow Then
			Try
				dblRows += 1
				e.Row.Cells(0).Text = dblRows
				e.Row.Cells(intDateColumn).Text = sqlDB.SqlDate(e.Row.Cells(intDateColumn).Text, "d MMM yyyy HH:mm")	' "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

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

		If chkIgnore.Checked Then
		Else
			sdsUsage.FilterExpression &= " AND dtStamp >= '" & sqlDB.dbDate(txtStartDate.Text) & "0000' AND dtStamp <= '" & sqlDB.dbDate(txtEndDate.Text) & "9999' "
		End If

		If ddlUser.SelectedIndex > 0 Then
			sdsUsage.FilterExpression &= " AND UserID = " & ddlUser.SelectedValue
		End If

		If ddlSystem.SelectedIndex > 0 Then
			sdsUsage.FilterExpression &= " AND strSystem = '" & ddlSystem.SelectedValue & "' "
		End If

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

	End Sub

	Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
		Dim s As String = ""

		If chkIgnore.Checked Then
			s = Date.Today.ToString("yyyyMMdd")
		Else
			s = sqlDB.dbDate(txtStartDate.Text.Trim) & "_" & sqlDB.dbDate(txtEndDate.Text.Trim)
		End If

		sdsUsage.FilterExpression = strGVFilter
		gvExport.Visible = True

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