Imports EDSL.Entities
Imports EDSL.DAL
Public Partial Class NewsListControl
    Inherits System.Web.UI.UserControl
    Dim intLimit As Integer
    Dim intTotalPageCount As Integer
    Const m_DISPLAYEDROWS = 7
    Const m_PAGESIZE As Integer = 7

    Dim clsMisc As New miscFunctionsFormatString

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        btnFirst.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnLast.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnNext.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnPrev.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnFirstBottom.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnLastBottom.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnNextBottom.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")
        btnPrevBottom.PostBackUrl = "~/news/lastestnews.html?n=" & Request.QueryString("n") & "&ns=" & Request.QueryString("ns")


        If IsPostBack = False Then
            If Request.QueryString("pageid") <> "" Then
                CurrentPage = Request.QueryString("pageid") - 1
            End If
            Call LoadData()

        End If


    End Sub
    Private Sub LoadData()
        Dim objMainNews As List(Of entMainNews)
        Dim newsType As New Integer

        If Request.QueryString("n") <> "" Then
            newsType = Request.QueryString("n")
        End If
        Dim objCategory As New dbMainCategory
        Dim category As New entmainCategory
        category = objCategory.GetItem(newsType)
        'Find the breadcrumb control
        Dim control As UserControl = CType(Me.Parent.FindControl("breadcrumb"), Breadcrumb)
        Dim pageInfo As HyperLink = control.FindControl("lnkPage")
        'Find the page title
        Dim lbl As Label = Me.Parent.FindControl("labelPageTitle")


        If newsType > 0 Then
            'Find page control for meta description
            DirectCast(Me.Page, News).SetMetaData("", category.CategoryName.ToString() & " News")

            'Find div on master page
            Dim masterPage As main = DirectCast(Me.Page.Master, main)
            masterPage.BodyTag.Attributes.Add("class", category.DropDownValue.ToString())

            pageInfo.Text = category.CategoryName.ToString() & " News"

            lbl.Text = category.CategoryName.ToString() & " News"

        Else
            'Find page control for meta description
            DirectCast(Me.Page, News).SetMetaData("", "General News")
            pageInfo.Text = "General News"
            lbl.Text = "General News"

        End If

        
        Dim subCat As New Integer
        subCat = Request.QueryString("ns")
        If subCat > 0 Then
            objMainNews = GetByTypeID(subCat)
        Else
            objMainNews = GetByTypeID(newsType)
        End If




        If objMainNews.Count > 0 Then

            Dim objPds As New PagedDataSource
            objPds.DataSource = objMainNews
            objPds.AllowPaging = True
            objPds.PageSize = m_DISPLAYEDROWS
            objPds.CurrentPageIndex = CurrentPage
            rptNews.DataSource = objPds
            rptNews.DataBind()

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

            TotalPageCount = objPds.PageCount
            lblCurrentPage.Text = CurrentPage + 1 & " of " & TotalPageCount.ToString()
            lblCurrentPageBottom.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 = "yellow"
                End If
                strPageIndicator += "<a href=""lastestnews.html?n=" & newsType & "&pageid=" & intCount & "&ns=" & Request.QueryString("ns") & """ class=""" & strSelected & """>" & intCount & "</a>"
            Next intCount
            divPageIndicator.InnerHtml = strPageIndicator
            divPageIndicatorBottom.InnerHtml = strPageIndicator
        Else
            Dim strOutput As String = "<div id=""divCatalogueRow"" class=""divCatalogueRow""" & ">Sorry no news articles were found</div>"
            'divctlContent.InnerHtml = strOutput
        End If

    End Sub
  Private Function GetAll() As List(Of entMainNews)
    Dim objMain As New dbMainNews
        Dim objMainEntity As List(Of entMainNews) = objMain.GetAll(" AND Active = 1 ", "", LimitNewslist)
    Return objMainEntity
  End Function
  Private Function ConvertDateToLongDisplayFormat(byval strInputDate As string) as string
    If strInputDate = "" Then
      Return ""
    End If
    Dim datInput As Date = CDate(strInputDate)
    Dim strOutput As String = ""
    strOutput &= datInput.DayOfWeek.ToString & " "
    strOutput &= datInput.ToLongDateString
    Return strOutput
  End Function
    Private Function GetByTypeID(ByVal newsTypeID As Integer) As List(Of entMainNews)
        Dim objMain As New dbMainNews
        Dim objMainEntity As List(Of entMainNews) = objMain.GetItemsByType(newsTypeID)
        Return objMainEntity
    End Function

    Private Function CheckStringValue(ByVal strInput As String) As String
        Dim strOutput As String = ""
        If Not strInput Is Nothing AndAlso strInput <> "" Then
            strOutput = strInput & "<br />"
        End If
        Return strOutput
    End Function

    Public Function CleanUrlRewriter(ByVal strInput As String) As String
        Dim strOutput As String = ""
        Dim func As New miscFunctions
        strOutput = func.CleanUrlString(strInput)

        Return strOutput
    End Function
    Public Property LimitNewslist() As Integer
        Get
            Return intLimit
        End Get
        Set(ByVal value As Integer)
            intLimit = value
        End Set
    End Property
#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 Me.ViewState("_TotalPageCount")
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("_TotalPageCount") = value
        End Set
    End Property
#End Region
#Region "Nav control code"
    Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click, btnFirstBottom.Click
        CurrentPage = 0
        Call LoadData()
    End Sub
    Private Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click, btnPrevBottom.Click
        CurrentPage -= 1
        Call LoadData()
    End Sub
    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click, btnNextBottom.Click
        CurrentPage += 1
        Call LoadData()
    End Sub
    Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click, btnLastBottom.Click

        CurrentPage = TotalPageCount - 1
        Call LoadData()
    End Sub
#End Region
End Class
