Imports System.Data.SqlClient
Imports System.Threading
Imports Logic.DateExtensions

Public Class DriverNotHire
    Inherits System.Web.UI.Page
    Property contractId As Integer
    Property driverId As Integer

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

            driverId = Logic.Conversion.ToInteger(Request.Params("id"))
            Using connection As New SqlConnection(Logic.Database.ConnectionString)
                connection.Open()
                Dim cmd As New SqlCommand("SELECT ContractId FROM Driver WHERE ID=@driverId", connection)
                cmd.Parameters.AddWithValue("driverId", driverId)
                contractId = cmd.ExecuteScalar()
            End Using
            Master.CurrentClient.VerifyContractAccess(contractId)

            Dim perms = Master.CurrentClient.Permissions
            If Not perms.Contains("DPQ Driver Hired/Not Hired") Then Throw New HttpException(403, "Forbidden")

            If Not Page.IsPostBack Then
                Master.SetParentPage("Recruiting.aspx?cid=" & contractId)
                _FillData(driverId)
            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 btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("Recruiting.aspx?cid=" & contractId)
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If Not Page.IsValid Then
            Exit Sub
        End If

        Try

            Dim username = "[Web: " & Master.CurrentClient.username & "]"

            Using connection As New SqlConnection(Logic.Database.ConnectionString)
                connection.Open()

                Dim cmdUpdate = "
                    UPDATE Driver SET Terminated=1,TermDate='9/9/9998',DPQComplete_NoEmail=1,DPQComplete=1,DPQCompleteDate=GETDATE(),DPQCompleteBy=@editedBy,DPQNotHired=1,DPQHireDate='9/9/9998',PreQual=0,
                        EditedDate=GETDATE(), EditedBy=@editedBy
                    WHERE ID=@driverId"
                Dim cmd As New SqlCommand(cmdUpdate, connection)
                cmd.Parameters.AddWithValue("driverId", driverId)
                cmd.Parameters.AddWithValue("editedBy", username)
                cmd.ExecuteNonQuery()

                Dim cmdUpdate2 = "UPDATE web_DriverApplication SET status='rejected' WHERE ID IN (SELECT WebAppID FROM Driver WHERE Id=@DriverId AND ISNULL(WebAppId,0)>0)"
                Dim cmd2 As New SqlCommand(cmdUpdate2, connection)
                cmd2.Parameters.AddWithValue("driverId", driverId)
                cmd2.ExecuteNonQuery()

            End Using

            Response.Redirect("Recruiting.aspx?cid=" & contractId)

        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 _FillData(driverId As Integer)
        Using connection As New SqlConnection(Logic.Database.ConnectionString)
            connection.Open()

            Dim cmdGet = "
                SELECT ID,
                    LastName + ', ' + FirstName + ' ' + MiddleName +
                        CASE WHEN Suffix='' THEN '' ELSE ', ' END + Suffix
                        AS FullName,
                    'xxx-xx-' + RIGHT(SSN, 4) AS SSN
                FROM Driver WHERE ID=@driverId"
            Dim cmd As New SqlCommand(cmdGet, connection)
            cmd.Parameters.AddWithValue("driverId", driverId)

            Dim dReader = cmd.ExecuteReader()
            If dReader.Read() Then
                litId.Text = dReader("ID")
                litName.Text = Logic.Conversion.ToString(dReader("FullName"))
                litSocial.Text = Logic.Conversion.ToString(dReader("SSN"))
            Else
                Response.Redirect("Recruiting.aspx?cid=" & contractId)
            End If

        End Using
    End Sub

End Class