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

Public Class RemoveDriver
    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 AND Terminated=0", connection)
                cmd.Parameters.AddWithValue("driverId", driverId)
                contractId = cmd.ExecuteScalar()
            End Using
            Master.CurrentClient.VerifyContractAccess(contractId)

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

            If Not Page.IsPostBack Then
                Master.SetParentPage("Drivers.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("Drivers.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 termDate = Logic.Conversion.ToDate(dtTermination.Text)

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

                Dim cmdUpdate = "
                    UPDATE Driver SET Terminated=1, TermDate=@termDate, TerminatedReason=@termReason,
                        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("termDate", termDate.Clip())
                cmd.Parameters.AddWithValue("termReason", txtReason.Text)
                cmd.ExecuteNonQuery()

            End Using

            Response.Redirect("Drivers.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("Drivers.aspx?cid=" & contractId)
            End If

        End Using
    End Sub

End Class