Imports System.Data.SqlClient
Imports System.Threading

Public Class RejectApplication
    Inherits System.Web.UI.Page

    Property contractId As Integer = 0

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

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

            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")

            Master.SetParentPage("Recruiting.aspx?cid=" & contractId)
            With New Logic.DriverApplicationSummary(appId)
                litId.Text = .id
                litName.Text = .applicantName
                litSubmitted.Text = .dateSubmitted
            End With

        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 btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("Recruiting.aspx?cid=" & contractId)
    End Sub

    Private Sub btnOkay_Click(sender As Object, e As EventArgs) Handles btnOkay.Click
        Try
            Dim app = New Logic.DriverApplicationSummary(litId.Text)
            app.Reject(rejectedBy:="[Client]")
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
        Response.Redirect("Recruiting.aspx?cid=" & contractId)
    End Sub

    Private Sub _LoadForm(appId As Integer)
        Try
            With New Logic.DriverApplicationSummary(appId)
                litId.Text = .id
                litName.Text = .applicantName
                litSubmitted.Text = .dateSubmitted
            End With
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub _SafeRedirect(destination As String)

        ' Magic...  true if other is same origin as this website
        Dim isSameOrigin = Function(other As String) As Boolean
                               Return 0 = Uri.Compare(Request.Url, New Uri(other),
                                   UriComponents.SchemeAndServer, UriFormat.SafeUnescaped, StringComparison.Ordinal)
                           End Function

        ' Redirect, if valid
        If destination.Length AndAlso isSameOrigin(destination) Then
            Response.Redirect(destination)
        End If

        ' On fail, just return to Applications
        Response.Redirect("Recruiting.aspx?cid=" + contractId)

    End Sub

End Class