Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class userPageListControl
    Inherits System.Web.UI.UserControl
    Dim clsMisc As New miscFunctions
    Dim strWHEREClause As String = ""
    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 Session("ramadan") = "1" Then
            repHypEdit.Visible = True
        End If

        If IsPostBack = False Then
            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 PageUserTitle ASC"
            End If
        End If
        If ViewState("WHEREClause") <> "" Then
            WHEREClause = ViewState("WHEREClause")
        End If
        If Request.QueryString("cid") <> "" Then
            WHEREClause += " AND CategoryID = " + Request.QueryString("cid")
        End If
        Call LoadData()
    End Sub
    Private Sub LoadData()
        Dim objMainEntity As List(Of entUserPage)
        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 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=""userPagelist.aspx?pageid=" & intCount & """>" & intCount & "</a>"
        Next intCount
        divPageIndicator.InnerHtml = strPageIndicator
        repRepeater.DataSource = objPds
        repRepeater.DataBind()
    End Sub
    Private Function GetAll() As List(Of entUserPage)
        strWHEREClause += " AND Active = 1"
        Dim objMain As New dbMainUserPage
        Dim objMainEntity As List(Of entUserPage) = objMain.GetAll(WHEREClause, ORDERBYClause)
        Return objMainEntity
    End Function
#Region "Control code"
    Public Sub btnSearch_Click() Handles btnSearch.Click
        Dim strWHEREClause As String = ""
        strWHEREClause += IIf(txtPageMainTitle.Text = "", "", " AND PageUserTitle LIKE '%" & txtPageMainTitle.Text & "%'")
        strWHEREClause += IIf(txtPageMainContent.Text = "", "", " AND PageUserContent LIKE '%" & txtPageMainContent.Text & "%'")

        'If radActive.SelectedValue = "1" Then strWHEREClause += " AND Active = 1"
        'If radActive.SelectedValue = "2" Then strWHEREClause += " AND Active = 0"
        WHEREClause = strWHEREClause
        ViewState.Add("WHEREClause", strWHEREClause)
        CurrentPage = 0
        Call LoadData()
    End Sub
    Public Sub btnSearchClear_Click() Handles btnSearchClear.Click
        WHEREClause = ""
        ViewState("WHEREClause") = ""
        ORDERBYClause = " ORDER BY PageUserTitle ASC "
        txtPageMainTitle.Text = ""
        txtPageMainContent.Text = ""
        'radActive.SelectedValue = "3"
        Call LoadData()
    End Sub
    Public Sub btnOrderBy_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
        ' Select case sender.id = "
        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 += "PageUserTitle "
            Case 2
                strORDERBYclause += "Active "
        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
