﻿Imports System.Web.Configuration

Partial Public Class AuditLog
  Inherits System.Web.UI.Page

  Private Shared strGVFilter As String = ""
  Private Shared dblRows As Double = 0
  Private Const idxRef = 0

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  End Sub

  Private Sub DailyLog_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
    If txtEndDate.Attributes.Count = 0 Then
      txtStartDate.Attributes.Add("onfocus", "showCalendarControl(this);")
      txtEndDate.Attributes.Add("onfocus", "showCalendarControl(this);")
    End If

    If Not Page.IsPostBack Then
      txtStartDate.Text = "01-" & Date.Today.ToString("MM-yyyy")
      txtEndDate.Text = Date.Today.ToString("dd-MM-yyyy")

      If WebConfigurationManager.AppSettings("ShowMore") = True Then
        gvLog.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
      Else
        gvLog.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
      End If

    End If

    btnSearch_Click(sender, e)

  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

    If strGVFilter.Length > 0 Then s = "WHERE " & strGVFilter
    If sqlDB.CountRows("SELECT dtStamp FROM logAudit " & s) = 0 Then
      lblMessage.Text = "<div width='100%' align='center'>- No Log Entries found -</div>"
      btnExport.Enabled = False
    End If
  End Sub

  Private Sub gvLog_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvLog.Sorting
    sdsLog.FilterExpression = strGVFilter
    dblRows = 0
  End Sub

  Private Sub gvLog_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvLog.PageIndexChanged
    sdsLog.FilterExpression = strGVFilter
    WebConfigurationManager.AppSettings("intListIndex") = gvLog.PageIndex
  End Sub

  Private Sub gvLog_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLog.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
      Try
        Dim s As String
        s = e.Row.Cells(idxRef).Text.ToString.Substring(12)
        e.Row.BackColor = Drawing.Color.FromArgb(getHexColour(s, "R"), getHexColour(s, "G"), getHexColour(s, "B"))
        e.Row.Cells(idxRef).Text = New Date(e.Row.Cells(idxRef).Text).ToString("dd MMM yyyy HH:mm:ss")
        'e.Row.Cells(intDateColumn).Text = sqlDB.SqlDate(e.Row.Cells(intDateColumn).Text, "d MMM yyyy")  ' "d MMM yyyy HH:mm")
      Catch ex As Exception

      End Try
    End If
  End Sub

  Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
    Dim strStartDate, strEndDate As String
    Dim intS, intE As String()

    lblMessage.Text = ""
    sdsLog.FilterExpression = "1=1"
    dblRows = 0

    intS = txtStartDate.Text.Split("-")
    strStartDate = New Date(intS(2), intS(1), intS(0), 0, 0, 0).Ticks
    intE = txtEndDate.Text.Split("-")
    strEndDate = New Date(intE(2), intE(1), intE(0), 23, 59, 59).Ticks

    If ddlEntity.SelectedIndex > 0 Then
      sdsLog.FilterExpression &= " AND strEntity LIKE('%" & ddlEntity.SelectedValue & "%')"
    End If

    If ddlEntityID.SelectedIndex > 0 Then
      sdsLog.FilterExpression &= " AND ID LIKE('%" & ddlEntityID.SelectedValue & "%')"
    End If

    If ddlUser.SelectedIndex > 0 Then
      sdsLog.FilterExpression &= " AND strLastUser LIKE('%" & ddlUser.SelectedValue & "%')"
    End If

    sdsLog.FilterExpression &= " AND dtStamp >= '" & strStartDate & "' AND dtStamp <= '" & strEndDate & "' "

    strGVFilter = sdsLog.FilterExpression
    WebConfigurationManager.AppSettings("intListFilter") = strGVFilter
  End Sub

  Public Shared Function getHexColour(ByVal strTime As String) As String
    Dim r, g, b As Integer

    r = CInt(strTime.Substring(0, 2) / 2) + 200
    g = CInt(strTime.Substring(2, 2) / 2) + 200
    b = CInt(strTime.Substring(4, 2) / 2) + 200

    Return "#" & Hex(r) & Hex(g) & Hex(b)
  End Function

  Public Shared Function getHexColour(ByVal strTime As String, ByVal strComponent As String) As Integer
    Dim r, g, b As Integer

    r = CInt(strTime.Substring(0, 2) / 2) + 200
    g = CInt(strTime.Substring(2, 2) / 2) + 200
    b = CInt(strTime.Substring(4, 2) / 2) + 200
    Select Case strComponent
      Case "R"
        Return r
      Case "G"
        Return g
      Case "B"
        Return b
    End Select
  End Function

  Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
    Dim s As String
    sdsLog.FilterExpression = strGVFilter
    gvExport.Visible = True

    For Each row As GridViewRow In gvExport.Rows
      Try
        s = row.Cells(idxRef).Text.ToString.Substring(12)
        row.BackColor = Drawing.Color.FromArgb(getHexColour(s, "R"), getHexColour(s, "G"), getHexColour(s, "B"))
        row.Cells(idxRef).Text = New Date(row.Cells(idxRef).Text).ToString("dd MMM yyyy HH:mm:ss")
        's = New Date(row.Cells(0).Text).Now.Ticks
      Catch ex As Exception

      End Try
    Next

    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
      Response.AddHeader("Content-Disposition", _
       "attachment;filename=AuditLog_" & 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
    gvLog.PageIndex = 0
    sdsLog.FilterExpression = strGVFilter
    If chkResize.Checked Then
      gvLog.PageSize = WebConfigurationManager.AppSettings("MAXPAGESIZE")
      WebConfigurationManager.AppSettings("ShowMore") = True
    Else
      gvLog.PageSize = WebConfigurationManager.AppSettings("PAGESIZE")
      WebConfigurationManager.AppSettings("ShowMore") = False
    End If
  End Sub
End Class