Imports System
Imports System.Web.UI.WebControls
Imports ASPNET.StarterKit.TimeTracker.BusinessLogicLayer

Namespace ASPNET.StarterKit.TimeTracker.Web

    '*********************************************************************
    '
    ' ProjectList.aspx
    '
    ' The ProjectList.aspx is used to display the list of projects available.
    ' Only members of the Admin or Manager Role can access this page.
    ' Members of the Admin group will see all projects and members of the
    ' Managers group will see only the projects they are managing.
    '
    '*********************************************************************

    Public Class ProjectList
        Inherits System.Web.UI.Page
        Protected LinkButton1 As System.Web.UI.WebControls.LinkButton
        Protected WithEvents NewProjectButton As System.Web.UI.WebControls.Button
        Protected WithEvents ProjectsGrid As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Ensure that the visiting user has access to view the current page
            If TTSecurity.IsInRole(TTUser.UserRoleAdminPMgr) = False Then
                Response.Redirect("AccessDenied.aspx?Index=-1", True)
            End If

            If Not IsPostBack Then
                BindProjects()
            End If
        End Sub 'Page_Load

        '*********************************************************************
        '
        ' The BindProjects method retrieves the list of projects the current user
        ' can view based on their role and then databinds them to the ProjectsGrid
        '
        '*********************************************************************

        Private Sub BindProjects()
            Dim projectList As ProjectsCollection = Project.GetProjects(TTSecurity.GetUserID(), TTSecurity.GetUserRole())

            ' Call method to sort the data before databinding
            SortGridData(projectList, SortField, SortAscending)

            ProjectsGrid.DataSource = projectList
            ProjectsGrid.DataBind()
        End Sub 'BindProjects

        '*********************************************************************
        '
        ' The SortGridData method is a helper method called when databinding the Projects grid 
        ' to sort the columns of the grid.
        '
        '*********************************************************************

        Private Sub SortGridData(ByVal list As ProjectsCollection, ByVal sortField As String, ByVal asc As Boolean)
            Dim sortCol As ProjectsCollection.ProjectFields = ProjectsCollection.ProjectFields.InitValue

            Select Case sortField
                Case "Name"
                    sortCol = ProjectsCollection.ProjectFields.Name
                Case "Manager"
                    sortCol = ProjectsCollection.ProjectFields.ManagerUserName
                Case "Completion"
                    sortCol = ProjectsCollection.ProjectFields.CompletionDate
                Case "Duration"
                    sortCol = ProjectsCollection.ProjectFields.Duration
                Case Else
            End Select

            list.Sort(sortCol, asc)
        End Sub 'SortGridData

        '*********************************************************************
        '
        ' The NewProjectButton_Click event handler redirects to the ProjectDetails.aspx 
        ' page so the user can create a new project.
        '
        '*********************************************************************

        Private Sub NewProjectButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewProjectButton.Click
            Dim mainIndex As Integer = IIf(Request("index") Is Nothing, 0, Convert.ToInt32(Request("index")))
            Dim adminIndex As Integer = IIf(Request("adminindex") Is Nothing, 0, Convert.ToInt32(Request("adminindex")))

            Response.Redirect([String].Format("ProjectDetails.aspx?index={0}&adminindex={1}", mainIndex, adminIndex), False)
        End Sub 'NewProjectButton_Click

        '*********************************************************************
        '
        ' The ProjectsGrid_Page event handler sets the index of the currently displayed 
        ' page for the Projects grid and re-binds it.
        '
        '*********************************************************************

        Protected Sub ProjectsGrid_Page(ByVal sender As [Object], ByVal e As DataGridPageChangedEventArgs)
            ProjectsGrid.CurrentPageIndex = e.NewPageIndex

            BindProjects()
        End Sub 'ProjectsGrid_Page

        '*********************************************************************
        '
        ' The ProjectGrid_Sort event handler changes the sortfield for the Projects grid 
        ' and re-binds it.
        '
        '*********************************************************************

        Private Sub ProjectsGrid_Sort(ByVal [source] As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles ProjectsGrid.SortCommand
            SortField = e.SortExpression

            BindProjects()
        End Sub 'ProjectsGrid_Sort

        Property SortField() As String
            Get
                Dim o As Object = ViewState("SortField")
                If o Is Nothing Then
                    Return [String].Empty
                End If
                Return CStr(o)
            End Get

            Set(ByVal Value As String)
                If Value = SortField Then
                    ' same as current sort file, toggle sort direction
                    SortAscending = Not SortAscending
                End If
                ViewState("SortField") = Value
            End Set
        End Property

        '*********************************************************************
        '
        ' SortAscending property is tracked in ViewState
        '
        '*********************************************************************

        Property SortAscending() As Boolean
            Get
                Dim o As Object = ViewState("SortAscending")
                If o Is Nothing Then
                    Return True
                End If
                Return CBool(o)
            End Get

            Set(ByVal Value As Boolean)
                ViewState("SortAscending") = Value
            End Set
        End Property
    End Class 'ProjectList
End Namespace 'ASPNET.StarterKit.TimeTracker.