Imports System.Data.SqlClient
Imports System.IO

Public Class DashboardMaster
    Inherits System.Web.UI.MasterPage

    Private _parentPage As String = Nothing

#Region "Methods"

    Public Sub SetParentPage(filename As String)
        _parentPage = filename
    End Sub

#End Region

#Region "Event handlers"

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try

            Dim cid = CStr(Request.QueryString("cid"))
            If _parentPage Is Nothing Then
                _parentPage = Path.GetFileName(Request.PhysicalPath) & "?cid=" & cid
            End If

            If Not Page.IsPostBack Then
                _BuildLinks()
                nav.SetFooter("<nav><li class='complete'><a href='https://tcsusa.sharefile.com/' target='_blank'>TCS Sharefile</a></li></nav>")
            End If

            nav.SetCurrentPage(_parentPage)

        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub nav_LinkClicked(item As SideNav.MenuItem) Handles nav.LinkClicked
        Response.Redirect(item.target)
    End Sub

#End Region

#Region "Internals"

    Private Class Link
        Property Title As String
        Property Filename As String
        Sub New(title As String, filename As String)
            Me.Title = title : Me.Filename = filename
        End Sub
    End Class

    Private Sub _BuildLinks()

        ' per-contract links
        Dim links As New List(Of Link)
        Dim perms = Master.CurrentClient.Permissions
        If perms.Contains("View Applications Page") Or perms.Contains("View Qualified Drivers") Then links.Add(New Link("Driver Recruiting", "Recruiting.aspx"))
        If perms.Contains("View Drivers Page") Then links.Add(New Link("Drivers", "Drivers.aspx"))
        If perms.Contains("View Tests Page") Then links.Add(New Link("Drug & Alcohol Tests", "DrugAlcoholTests.aspx"))
        If perms.Contains("View Vehicles Page") Then links.Add(New Link("Vehicles", "Vehicles.aspx"))
        If perms.Contains("View Training Page") Then links.Add(New Link("Training", "Training.aspx"))
        If perms.Contains("View Roadside Inspections Page") Then links.Add(New Link("Roadside Inspections", "RoadsideInspections.aspx"))
        If perms.Contains("View Accidents Page") Then links.Add(New Link("Accidents", "Accidents.aspx"))
        If perms.Contains("View Safety Scores Page") Then links.Add(New Link("Safety Scores", "SafetyScores.aspx"))
        If perms.Contains("View Document Upload Page") Then links.Add(New Link("Document Uploads", "DocumentUploads.aspx"))
        Dim canViewSubcontracts = perms.Contains("View Subcontracts")
        
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()

            Dim cmd As New SqlCommand(
                "SELECT sc.id, cust.[Name], sc.IsEmploymentAgency
                FROM web_ClientContractsMap map LEFT JOIN ServiceContract sc ON sc.id=map.contractId LEFT JOIN Customer cust ON cust.id=sc.CustomerID
                WHERE map.clientId=@clientId AND sc.status='Active'
                ORDER BY ISNULL(sc.IsEmploymentAgency,0) DESC, cust.[Name], sc.id",
                connection)
            cmd.Parameters.AddWithValue("clientId", SessionVars.ClientId(Session))

            Dim dReader = cmd.ExecuteReader()
            While dReader.Read()

                Dim cid = CInt(dReader(0))
                Dim cname = CStr(dReader(1))
                Dim cemp = Logic.Conversion.ToBoolean(dReader(2))

                nav.Add(cname, Nothing, SideNavItem.ItemStatus.Header)

                If cemp And canViewSubcontracts Then
                    Dim url = "Contracts.aspx?cid=" & cid
                    Dim navitem = nav.Add("Contracts", url, SideNavItem.ItemStatus.Subitem)
                    navitem.selected = (_parentPage = url)
                    navitem.forceLink = True
                End If

                For Each link In links
                    Dim title = link.Title
                    Dim url = link.Filename & "?cid=" & cid
                    Dim navitem = nav.Add(title, url, SideNavItem.ItemStatus.Subitem)
                    navitem.selected = (_parentPage = url)
                    navitem.forceLink = True
                Next

            End While

        End Using

    End Sub

#End Region

End Class