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

Namespace ASPNET.StarterKit.TimeTracker.Web

    '*********************************************************************
    '
    '	ResourceReport.aspx
    '
    '	This page is used by Administrator or Project Manager to monitor how
    '	consultants utilize their time.  Project managers can only view time 
    '	entries related to the projects they manage.
    '
    '*********************************************************************

    Public Class ResourceReportForm
        Inherits System.Web.UI.Page
        Protected UserList As System.Web.UI.WebControls.DataList
        Protected BackLink1 As System.Web.UI.WebControls.HyperLink
        Protected BackLink2 As System.Web.UI.WebControls.HyperLink
        Protected NoData As System.Web.UI.WebControls.Label
        Protected _startDate As DateTime = DateTime.MinValue
        Protected _endDate As DateTime = DateTime.MinValue
        Private _userIDs As String

#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 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

            ' Get user's input
            _userIDs = IIf(Request.QueryString("IDs") Is Nothing, "0", Request.QueryString("IDs"))

            Dim pageIndex As String = IIf(Request.QueryString("index") Is Nothing, "0", Request.QueryString("index"))

            _startDate = Convert.ToDateTime(IIf(Request.QueryString("Start") Is Nothing, DateTime.MinValue.ToShortDateString(), Request.QueryString("Start")))
            _endDate = Convert.ToDateTime(IIf(Request.QueryString("End") Is Nothing, DateTime.MinValue.ToShortDateString(), Request.QueryString("End")))

            BackLink1.NavigateUrl = "Reports.aspx?index=" + pageIndex
            BackLink2.NavigateUrl = "Reports.aspx?index=" + pageIndex

            BindList(_userIDs, _startDate, _endDate)
        End Sub 'Page_Load

        '*********************************************************************
        '
        '	BindList method retrieves all user summaries based on the User IDs passed in. 
        '	The appropriate user's information is retrieved only when the requesting user 
        '	has the appropriate authorization (i.e. Project Manager can only view his 
        '	team members information).  
        '
        '	The resulting collection is used to bind UserList web control.  This method also 
        '	display a friendly description when there is no data retrieved from the database.
        '
        '	Notice that User IDs are passed in as a comma delimited string.
        '
        '*********************************************************************

        Private Sub BindList(ByVal userIDs As String, ByVal start As DateTime, ByVal [end] As DateTime)
            Dim userData As ResourceReportUserCollection = ResourceReportUser.GetUserSummary(TTSecurity.GetUserID(), userIDs, start, [end])
            UserList.DataSource = userData
            UserList.DataBind()

            ' If there is no data, then display no data notification label.
            If userData.Count = 0 Then
                UserList.Visible = False
                NoData.Visible = True
            End If
        End Sub 'BindList

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

        Private Sub SortGridData(ByVal list As TimeEntriesCollection, ByVal sortField As String, ByVal asc As Boolean)
            Dim sortCol As TimeEntriesCollection.TimeEntryFields = TimeEntriesCollection.TimeEntryFields.InitValue
            Select Case sortField
                Case "EntryDate"
                    sortCol = TimeEntriesCollection.TimeEntryFields.Day
                Case "ProjectName"
                    sortCol = TimeEntriesCollection.TimeEntryFields.Project
                Case "CategoryName"
                    sortCol = TimeEntriesCollection.TimeEntryFields.Category
                Case "Duration"
                    sortCol = TimeEntriesCollection.TimeEntryFields.Hours
                Case "Description"
                    sortCol = TimeEntriesCollection.TimeEntryFields.Description
            End Select
            list.Sort(sortCol, asc)
        End Sub 'SortGridData

        '*********************************************************************
        '
        ' ListTimeEntry retrieves sorted time entries for each user, and the result is
        ' is used for returning TimeEntryGrid DataSource
        '
        '*********************************************************************

        Protected Function ListTimeEntry(ByVal mgrID As Integer, ByVal userID As Integer, ByVal start As DateTime, ByVal [end] As DateTime) As TimeEntriesCollection
            Dim entryList As TimeEntriesCollection = BusinessLogicLayer.TimeEntry.GetEntries(mgrID, userID, start, [end])

            'Always check for sorting when loading grid
            If entryList.Count > 0 Then
                SortGridData(entryList, SortField, SortAscending)
            Else
                entryList = Nothing
            End If
            Return entryList
        End Function 'ListTimeEntry

        '*********************************************************************
        '
        '	This event fires when any header of the grid is clicked, saves the sort expression,
        '	and rebinds the Time Entry data grid with the correct sorting.
        '
        '*********************************************************************

        Protected Sub TimeEntryGrid_Sort(ByVal [source] As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)
            SortField = e.SortExpression
            BindList(_userIDs, _startDate, _endDate)
        End Sub 'TimeEntryGrid_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 'ResourceReportForm
End Namespace 'ASPNET.StarterKit.TimeTracker.Web


