Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data

' issue 28388
' new report of customer comments
Partial Class Reports_RatingCommentReport
    Inherits System.Web.UI.Page

    Private _db As Database

    Private Const ASCENDING As String = " ASC"
    Private Const DESCENDING As String = " DESC"

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        DB = DatabaseFactory.CreateDatabase("Charter.Website.WS.CommerceEngineConnectionString")
        ReportType = "support.GET_COMMENT_FEEDBACK"
        wucHeader1.Heading = "Rating Comments Report"
    End Sub

#Region "Database"
    Private Property DB() As Database
        Get
            Return _db
        End Get
        Set(ByVal value As Database)
            _db = value
        End Set
    End Property

    Protected Function GetDS(ByVal reportType As String) As DataSet
        Dim retCode As Data.DataSet
        If txtStartDate.Text.Length > 0 AndAlso _
            txtEndDate.Text.Length > 0 AndAlso _
            chkUseDates.Checked Then
            retCode = DB.ExecuteDataSet(reportType, CDate(txtStartDate.Text), CDate(txtEndDate.Text))
        Else
            retCode = DB.ExecuteDataSet(reportType, DBNull.Value, DBNull.Value)
        End If

        Return retCode
    End Function
#End Region

#Region "Report"
    Public Property ReportType() As String
        Get
            If ViewState("reportType") Is Nothing Then
                ViewState("reportType") = String.Empty
            End If
            Return DirectCast(ViewState("reportType"), String)
        End Get
        Set(ByVal value As String)
            ViewState("reportType") = value
        End Set
    End Property

    Protected Function GenerateReport(ByVal reportType As String) As Boolean
        Try
            GridView1.DataSource = GetDS(reportType)
            GridView1.DataBind()

            If GridView1.Rows.Count > 0 Then
                Label1.Visible = True
                GridViewSortDirection = SortDirection.Ascending
                AddSortImage(0, GridView1.HeaderRow)
            Else
                Label1.Visible = False
            End If

            Return True
        Catch ex As Exception
            Throw ex
            Return False
        End Try
    End Function

    Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
        Try
            Dim retCode As Data.DataSet
            retCode = DB.ExecuteDataSet(ReportType(), DBNull.Value, DBNull.Value)
            Dim dv As New DataView()
            dv = retCode.Tables(0).DefaultView
            dv.Sort = sortExpression & " " & direction
            If GridView1.Rows.Count > 0 Then
                GridView1.DataSource = dv
                GridView1.DataBind()
                AddSortImage(GetSortColumnIndex(sortExpression), GridView1.HeaderRow)
            Else
                Label1.Visible = False
            End If
        Catch ex As Exception
            Throw ex
        Finally
        End Try
    End Sub

    Private Function GetSortColumnIndex(ByVal sortExp As String) As Integer
        For Each field As DataControlField In GridView1.Columns
            If field.SortExpression = DirectCast(sortExp, String) Then
                Return GridView1.Columns.IndexOf(field)
            End If
        Next
        Return -1
    End Function

    Private Sub AddSortImage(ByVal columnIndex As Integer, ByVal headerRow As GridViewRow)
        Dim sortImage As New Image()
        If GridViewSortDirection = SortDirection.Ascending Then
            sortImage.ImageUrl = "~/Reports/images/uparrow.gif"
            sortImage.AlternateText = "Ascending Order"
        Else
            sortImage.ImageUrl = "~/Reports/images/downarrow.gif"
            sortImage.AlternateText = "Descending Order"
        End If
        GridView1.HeaderRow.Cells(columnIndex).Controls.Add(sortImage)
    End Sub

    Public Property GridViewSortDirection() As SortDirection
        Get
            If ViewState("sortDirection") Is Nothing Then
                ViewState("sortDirection") = SortDirection.Ascending
            End If
            Return DirectCast(ViewState("sortDirection"), SortDirection)
        End Get
        Set(ByVal value As SortDirection)
            ViewState("sortDirection") = value
        End Set
    End Property

    Protected Sub RunReport(startDate As Date)
        RunReport(startDate, DateTime.Now)
    End Sub

    Protected Sub RunReport(startDate As Date, endDate As Date)
        txtStartDate.Text = startDate
        txtEndDate.Text = endDate
        chkUseDates.Checked = True
        GenerateReport(ReportType)
    End Sub
#End Region

#Region "Event Handling"

    Protected Sub GridView1_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        Dim sortExpression As String = e.SortExpression
        If GridViewSortDirection = SortDirection.Ascending Then
            GridViewSortDirection = SortDirection.Descending
            SortGridView(sortExpression, DESCENDING)
        Else
            GridViewSortDirection = SortDirection.Ascending
            SortGridView(sortExpression, ASCENDING)
        End If

    End Sub

    Protected Sub btnDateRangeReport_Click(sender As Object, e As System.EventArgs) Handles btnDateRangeReport.Click
        chkUseDates.Checked = True

        If Not (txtStartDate.Text.Contains("/")) And (txtStartDate.Text.Length = 4) Then
            ' only start year supplied
            txtStartDate.Text = "1/1/" & txtStartDate.Text
        End If

        If Not (txtEndDate.Text.Contains("/")) And (txtEndDate.Text.Length = 4) And (Not txtEndDate.Text = String.Empty) Then
            ' only start year supplied
            txtEndDate.Text = "1/1/" & txtEndDate.Text
            If (CDate(txtStartDate.Text) > CDate(txtEndDate.Text)) Then
                txtEndDate.Text = txtStartDate.Text
            End If
        End If
        GenerateReport(ReportType)

    End Sub

    Protected Sub btnReset_Click(sender As Object, e As System.EventArgs) Handles btnReset.Click
        Response.Redirect("RatingCommentReport.aspx")
    End Sub

    Protected Sub Yearly_Click(sender As Object, e As System.EventArgs) Handles Yearly.Click
        RunReport(System.DateTime.Now().AddYears(-1))
    End Sub

    Protected Sub Monthly_Click(sender As Object, e As System.EventArgs) Handles Monthly.Click
        RunReport(System.DateTime.Now().AddMonths(-1))
    End Sub

    Protected Sub Weekly_Click(sender As Object, e As System.EventArgs) Handles Weekly.Click
        RunReport(System.DateTime.Now().AddDays(-7))
    End Sub

    Protected Sub Daily_Click(sender As Object, e As System.EventArgs) Handles Daily.Click
        RunReport(System.DateTime.Now().AddHours(-24))
    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ctrl As Control)
        ' Verifies that the control is rendered - needed for excel export
    End Sub

    Protected Sub btnExport_Click(sender As Object, e As System.EventArgs) Handles btnExport.Click
        Dim ds As DataSet = GetDS(ReportType)
        If (IsNothing(ds) OrElse (ds.Tables.Count = 0)) Then
            Return
        End If

        Dim dt As DataTable = ds.Tables(0)
        Dim attachment As String = "attachment; filename=RatingCommentReport.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        Dim tab As String = ""
        For Each dc As DataColumn In dt.Columns
            Response.Write(tab + dc.ColumnName)
            tab = vbTab
        Next

        Response.Write(vbLf)
        Dim i As Integer
        For Each dr As DataRow In dt.Rows
            tab = ""
            For i = 0 To dt.Columns.Count - 1
                Response.Write(tab & dr(i).ToString())
                tab = vbTab
            Next

            Response.Write(vbLf)
        Next
        Response.End()

    End Sub
#End Region

End Class