Imports System.Data.SqlClient
Imports System.Threading

Public Class ViewApplication
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Dim appId = Logic.Conversion.ToInteger(Request.Params("id"))

            Dim contractId = 0
            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand("SELECT ClientId FROM web_DriverApplication WHERE id=@appId", connection)
                cmd.Parameters.AddWithValue("appId", appId)
                contractId = cmd.ExecuteScalar()
            End Using
            Master.CurrentClient.VerifyContractAccess(contractId)

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

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

            dsDocs.ConnectionString = Logic.Database.connectionString
            dsDocs.SelectParameters("appId").DefaultValue = appId

        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 _LoadForm(appId As Integer)
        With New Logic.DriverApplicationSummary(appId)
            litId.Text = .id
            litSubmitted.Text = .dateSubmitted
            litName.Text = .applicantName
        End With
    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

End Class