Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class EnquiryListControl
    Inherits System.Web.UI.UserControl
    Dim clsMisc As New miscFunctionsFormatString
    Dim strWHEREClause As String = " AND EnquiryType LIKE 'S'"
    Dim strORDERBYClause As String = ""
    Dim intTotalPageCount As Integer
    Const m_DISPLAYEDROWS = 20
    Const m_PAGESIZE As Integer = 5
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            'Call PopulateddlStatus()
            If Request.QueryString("pageid") <> "" Then
                CurrentPage = Request.QueryString("pageid") - 1
            End If
        End If
        If ORDERBYClause = "" Then
            ORDERBYClause = ViewState("ORDERBYClause")
            If ORDERBYClause = "" Then
                ORDERBYClause = " ORDER BY CreatedDate DESC"
            End If
        End If
        If ViewState("WHEREClause") <> "" Then
            WHEREClause = ViewState("WHEREClause")
        End If
        Call LoadData()
    End Sub
    Private Sub LoadData()
        Dim objMainEntity As List(Of entMainEnquiry)
        objMainEntity = GetAll()

        Dim objPds As New PagedDataSource
        objPds.DataSource = objMainEntity
        objPds.AllowPaging = True
        objPds.PageSize = m_DISPLAYEDROWS
        objPds.CurrentPageIndex = CurrentPage

        btnFirst.Enabled = IIf(objPds.IsFirstPage, False, True)
        btnPrev.Enabled = Not (objPds.IsFirstPage)
        btnNext.Enabled = Not (objPds.IsLastPage)
        btnLast.Enabled = IIf(objPds.IsLastPage, False, True)

        TotalPageCount = objPds.PageCount
        lblCurrentPage.Text = CurrentPage + 1 & " of " & TotalPageCount.ToString()

        Dim intLow As Integer = objPds.CurrentPageIndex - 1
        If objPds.CurrentPageIndex - 1 < 1 Then intLow = 1
        If intLow > TotalPageCount - m_PAGESIZE Then intLow = TotalPageCount - m_PAGESIZE + 1
        If intLow < 1 Then intLow = 1

        Dim intHigh As Integer = objPds.CurrentPageIndex + 3
        ' If intHigh > TotalPageCount then intHigh = TotalPageCount
        If objPds.CurrentPageIndex + 3 >= objPds.PageCount Then
            intHigh = objPds.PageCount
        ElseIf intHigh < m_PAGESIZE Then
            intHigh = m_PAGESIZE
        End If

        Dim strPageIndicator As String = ""
        For intCount = intLow To intHigh
            Dim strSelected = ""
            If intCount - 1 = objPds.CurrentPageIndex Then
                strSelected = "selected"
            End If
            strPageIndicator += "<a class=""alink " & strSelected & """ href=""list.aspx?pageid=" & intCount & """>" & intCount & "</a>"
        Next intCount
        divPageIndicator.InnerHtml = strPageIndicator
        repRepeater.DataSource = objPds
        repRepeater.DataBind()
    End Sub
    'Private Sub PopulateddlStatus()
    '    Dim objMain As New dbMainFunctions
    '    Dim objDDLItems As List(Of entMainDropDown) = objMain.GetDropDownReference(4)

    '    'Call ddlStatus.Items.Clear()
    '    Dim objListitemTop As New ListItem
    '    objListitemTop.Text = ""
    '    objListitemTop.Value = ""
    '    ddlStatus.Items.Add(objListitemTop)

    '    For Each objDDL As entMainDropDown In objDDLItems
    '        Dim objListitem As New ListItem
    '        objListitem.Text = objDDL.TextDisplay
    '        objListitem.Value = objDDL.Value
    '        ddlStatus.Items.Add(objListitem)
    '    Next
    'End Sub
    Private Function GetAll() As List(Of entMainEnquiry)
        Dim objMain As New dbMainEnquiry
        Dim objMainEntity As List(Of entMainEnquiry) = objMain.GetAll(WHEREClause, ORDERBYClause)
        Return objMainEntity
    End Function
#Region "Control code"
    Public Sub btnSearch_Click() Handles btnSearch.Click
        Dim strWHEREClause As String = ""
        strWHEREClause += " AND EnquiryType LIKE 'S'"
        strWHEREClause += IIf(txtSearchName.Text = "", "", " AND EnquiryName LIKE '%" & txtSearchName.Text & "%'")
        'strWHEREClause += IIf(ddlStatus.SelectedIndex = 0, "", " AND EnquiryStatus LIKE '%" & ddlStatus.SelectedValue & "%'")

        If txtSearchDateFrom.Text <> "" Then
            strWHEREClause += " AND CreatedDate >= '" & clsMisc.ConvertDateToDBFormat(txtSearchDateFrom.Text) & " 00:00:00'"
        End If
        If txtSearchDateTo.Text <> "" Then
            strWHEREClause += " AND CreatedDate <= '" & clsMisc.ConvertDateToDBFormat(txtSearchDateTo.Text) & " 23:59:59'"
        End If

        WHEREClause = strWHEREClause
        ViewState.Add("WHEREClause", strWHEREClause)
        CurrentPage = 0
        Call LoadData()
    End Sub
    Public Sub btnSearchClear_Click() Handles btnSearchClear.Click
        strWHEREClause += " AND EnquiryType LIKE 'S'"
        ViewState("WHEREClause") = " AND EnquiryType LIKE 'S'"
        ORDERBYClause = " ORDER BY EnquiryName ASC "
        txtSearchName.Text = ""

        txtSearchDateFrom.Text = ""
        txtSearchDateTo.Text = ""
        Call LoadData()
    End Sub
    Public Sub btnOrderBy_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim argumentList() As String = e.CommandArgument.ToString.Split(",")
        Dim strButton As String = argumentList(0)
        Dim booAsc As Boolean = IIf(argumentList(1) = "a", True, False)
        Dim strORDERBYclause As String = "ORDER BY "
        Select Case strButton
            Case 1
                strORDERBYclause += "EnquiryName "
            Case 2
                strORDERBYclause += "EnquiryEmail "
            Case 3
                strORDERBYclause += "EnquiryTelephone "
            Case 4
                strORDERBYclause += "CreatedDate "
            Case 5
                strORDERBYclause += "EnquiryStatus "
        End Select

        If booAsc = True Then
            strORDERBYclause += "ASC"
        Else
            strORDERBYclause += "DESC"
        End If
        ORDERBYClause = strORDERBYclause
        ViewState.Add("ORDERBYClause", strORDERBYclause)
        CurrentPage = 0
        Call LoadData()
    End Sub
#End Region
#Region "Nav control code"
    Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        CurrentPage = 0
        Call LoadData()
    End Sub
    Private Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        CurrentPage -= 1
        Call LoadData()
    End Sub
    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        CurrentPage += 1
        Call LoadData()
    End Sub
    Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click
        CurrentPage = TotalPageCount - 1
        Call LoadData()
    End Sub
#End Region
#Region "Internal properties"
    Private Property CurrentPage() As Integer
        Get
            Return CInt(Me.ViewState("_CurrentPage"))
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("_CurrentPage") = value
        End Set
    End Property
    Private Property TotalPageCount() As Integer
        Get
            Return intTotalPageCount
        End Get
        Set(ByVal value As Integer)
            intTotalPageCount = value
        End Set
    End Property
    Private Property WHEREClause() As String
        Get
            Return strWHEREClause
        End Get
        Set(ByVal value As String)
            strWHEREClause = value
        End Set
    End Property
    Private Property ORDERBYClause() As String
        Get
            Return strORDERBYClause
        End Get
        Set(ByVal value As String)
            strORDERBYClause = value
        End Set
    End Property
#End Region
End Class
