Imports System.Data.SqlClient
Imports System.IO
Imports System.Threading

Public Class ClientMaster
    Inherits System.Web.UI.MasterPage

    Property CurrentClient As Logic.ClientAccount
    Property CurrentContractId As Integer
    Property CurrentContractName As String = "(no contract selected)"
    Property ActiveItem As String

    Public Sub SetParentPage(filename As String)
        ActiveItem = filename.Split("."c).First()
    End Sub

    Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        Try

            Dim clientId = SessionVars.ClientId(Session)
            If clientId > 0 Then
                CurrentClient = New Logic.ClientAccount(clientId)
            Else
                Dim filename = Path.GetFileName(Request.Url.LocalPath).ToLower()
                Dim req = Request.Url.ToString()
                Response.Redirect("/Client/Login.aspx?r=" & HttpUtility.UrlEncode(req))
            End If

            CurrentContractId = Logic.Conversion.ToInteger(Request.QueryString("cid"))
            If CurrentContractId = 0 And CurrentClient.ContractIds.Count > 0 Then
                Response.Redirect("/Client/Dashboard.aspx?cid=" & CurrentClient.ContractIds.First())
            ElseIf Not CurrentClient.HasContractAccess(CurrentContractId) Then
                Response.Redirect("./")
            End If

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

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

            dsContracts.ConnectionString = Logic.Database.connectionString

            ' Set up permissions

            If Not Page.IsPostBack Then
                Dim perms = CurrentClient.Permissions
                phNavRecruiting.Visible = perms.Contains("View Applications Page") Or perms.Contains("View Qualified Drivers")
                phNavDrivers.Visible = perms.Contains("View Drivers Page")
                phNavTests.Visible = perms.Contains("View Tests Page")
                phNavVehicles.Visible = perms.Contains("View Vehicles Page")
                phNavTraining.Visible = perms.Contains("View Training Page")
                phNavRoadsideInspections.Visible = perms.Contains("View Roadside Inspections Page")
                phNavAccidents.Visible = perms.Contains("View Accidents Page")
                phNavSafetyScores.Visible = perms.Contains("View Safety Scores Page")
                phNavContracts.Visible = perms.Contains("View Subcontracts")
                phNavRegistrationDocs.Visible=perms.Contains("View Carrier Registration & Permits Page")
                phNavDocumentUpload.Visible = perms.Contains("View Document Upload Page")
                phNavCompanyPolicies.Visible = perms.Contains("View Company Policies & Forms Page")
            End If

            ' Load contract info
            If CurrentContractId > 0 Then
                Using connection As New SqlConnection(Logic.Database.ConnectionString) 'My.Settings.connectionString)
                    connection.Open()
                    Dim cmd As New SqlCommand(
                        "SELECT cust.[Name], ISNULL(sc.IsEmploymentAgency,0)
                        FROM ServiceContract sc LEFT JOIN Customer cust ON cust.ID=sc.CustomerId 
                        WHERE sc.ID=@contractId", connection)
                    cmd.Parameters.AddWithValue("contractId", CurrentContractId)

                    Dim dReader = cmd.ExecuteReader()
                    dReader.Read()
                    CurrentContractName = dReader(0)
                    If dReader(1) = False Then phNavContracts.Visible = False
                End Using
            End If

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

End Class