Imports System.Data.SqlClient
Imports System.Threading
Imports Logic.NullableDateExtensions

Public Class ReviewDriver
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property agencyId As Integer
    Property driverId As Integer

    Protected Sub Page_Load() Handles Me.Load
        contractId = Logic.Conversion.ToInteger(Request.Params("cid"))
        driverId = Logic.Conversion.ToInteger(Request.Params("id"))
        agencyId = Logic.Conversion.ToInteger(hidAgencyId.Value)
        Try
            Master.CurrentClient.VerifyContractAccess(contractId)

            Dim perms = Master.CurrentClient.Permissions
            If Not perms.Contains("View Qualified Drivers") Then Throw New HttpException(403, "Forbidden")

            dsDocs.ConnectionString = Logic.Database.connectionString

            If Not Page.IsPostBack Then
                Master.SetParentPage("Recruiting.aspx?cid=" & contractId)
                _LoadData()
            End If

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

    Private Sub btnGet_Click() Handles btnGet.Click
        Response.Redirect(String.Format(
            "GetDriver.aspx?contractId={0}&agencyId={1}&driverId={2}",
            contractId, agencyId, driverId))
    End Sub

    Private Sub gvDocs_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDocs.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            e.Row.Attributes.Add("data-href", data("pageUrl"))
            e.Row.Attributes.Add("data-target", "_blank")
        End If
    End Sub

    Private Sub _LoadData()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand(
                "SELECT ContractID,WebAppId,FirstName,MiddleName,LastName,Suffix,SSN,DOB
                FROM Driver WHERE ID=@driverId
                AND ContractID IN (SELECT agencyId FROM ContractAgencyMap WHERE contractId=@contractId)", connection)
            cmd.Parameters.AddWithValue("driverId", driverId)
            cmd.Parameters.AddWithValue("contractId", contractId)

            Dim dReader = cmd.ExecuteReader()
            If Not dReader.Read() Then Throw New HttpException(403, "Forbidden")

            hidAgencyId.Value = dReader("ContractID")
            Dim appId = Logic.Conversion.ToInteger(dReader("WebAppId"))
            txtFirstName.Text = dReader("FirstName")
            txtMiddleName.Text = dReader("MiddleName")
            txtLastName.Text = dReader("LastName")
            txtNameSuffix.Text = dReader("Suffix")
            txtSocial.Value = dReader("SSN")
            dtBirth.Text = Logic.Conversion.ToNullableDate(dReader("DOB")).FormatForHtml()
            
            agencyId = dReader("ContractID")
            
            dsDocs.SelectParameters("appId").DefaultValue = appId
            If appId = 0 Then dsDocs.SelectCommand =
                "SELECT 0 id,'' pageFileName, '' pageTitle, '' pageUrl
                WHERE 0=1"
        End Using
    End Sub

End Class