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

Public Class DriverHire
    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 & "]"
            Dim HireDate = Logic.Conversion.ToDate(dtHire.Text)

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

                Dim cmdUpdate = "
                    UPDATE Driver SET HireDate=@HireDate,LiveMVR=1,DPQHired=1,DPQHireDate=@HireDate,
                        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.Parameters.AddWithValue("HireDate", HireDate.Clip())
                cmd.ExecuteNonQuery()

                Dim cmdUpdate2 = "INSERT INTO DriverProfileMap (driverId,profileId) SELECT Driver.ID,ServiceContract.RandomDrugPoolProfileID FROM Driver LEFT JOIN ServiceContract ON Driver.ContractID=ServiceContract.ID WHERE Driver.ID=@DriverID AND ISNULL(ServiceContract.RandomDrugPoolProfileID,0)>0 AND ISNULL(Driver.CDL,0)=1"
                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