Imports EDSL.Entities
Imports EDSL.DAL
Public Partial Class CategoryListControl
  Inherits System.Web.UI.UserControl
  dim clsMisc As New miscFunctions
  Dim strWHEREClause As String = ""
  Dim strORDERBYClause As String = ""
  Dim intTotalPageCount As Integer
  Dim intParentCategoryID 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 Request.QueryString("cid") = "" or Request.QueryString("cid") = 0 Then
    WHEREClause = strWHEREClause & " AND ParentCategoryID = 0" & ViewState("WHEREClause")
    hypParentCategory.Visible = False
    ViewState("BreadCrumbLevel") = 1
  Else
    WHEREClause = strWHEREClause & " AND ParentCategoryID = " & Request.QueryString("cid") & ViewState("WHEREClause")
    hypParentCategory.Visible = true
    ViewState("BreadCrumbLevel") = ViewState("BreadCrumbLevel") + 1
  End If

  If IsPostBack = False then
    If Request.QueryString("id") <> "" Then
    CurrentPage = Request.QueryString("id") - 1
    End If
  end if
  If ORDERBYClause = "" then
    ORDERBYClause = ViewState("ORDERBYClause")
    If ORDERBYClause = "" then
    ORDERBYclause = " ORDER BY CategoryName ASC"
    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 entmainCategory)
  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
  Else If 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 Function GetAll() As List(Of entmainCategory)
  Dim objMain As new dbmainCategory
  If WHEREClause = "" then
    If Request.QueryString("cid") = "" Then
    WHEREClause = strWHEREClause & " AND ParentCategoryID = 0 " & ViewState("WHEREClause")
    Else
    WHEREClause = strWHEREClause & " AND ParentCategoryID = " & Request.QueryString("cid")  & ViewState("WHEREClause")
    end if
  End If

  If Request.QueryString("cid") = "" Then
    txtParentCategory.Value = "0"
  Else
    Dim intParentsParentCategory As Integer = objMain.GetTheParentsID(Request.QueryString("cid"))
    Dim strLinkString As String = ""
    If intParentsParentCategory > 0 then
    strLinkString = "?cid=" & intParentsParentCategory
    End If
    hypParentCategory.NavigateUrl = "~/CategoryAdmin/List.aspx" & strLinkString
    txtParentCategory.Value = intParentsParentCategory
  End If
  repHypEdit.NavigateUrl = "~/CategoryAdmin/Edit.aspx?id=0&cid=" & Request.QueryString("cid")

  Dim objMainEntity As List(Of entmainCategory) = objMain.GetAll(WHEREclause, ORDERBYclause)
  Return objMainEntity
  End Function
#Region "Control code"
  Public Sub btnSearch_Click() Handles btnSearch.Click
  Dim strWHEREClause As String = ""
  strWHEREClause += IIf(txtSearchName.Text = "", "", " AND CategoryName LIKE '%" & txtSearchName.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 CategoryName ASC "
  txtSearchName.Text = ""
  radActive.SelectedValue = "3"
  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 += "CategoryName "
    Case 2
    strORDERBYclause += "Active "
    Case 3
    strORDERBYclause += "CountOfSubs "
  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