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

Public Class RemoveVehicle
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property vehicleId As Integer

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            
            vehicleId = Logic.Conversion.ToInteger(Request.Params("id"))
            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand("SELECT ContractId FROM Vehicle WHERE ID=@vehicleId AND NotInService=0", connection)
                cmd.Parameters.AddWithValue("vehicleId", vehicleId)
                contractId = cmd.ExecuteScalar()
            End Using
            Master.CurrentClient.VerifyContractAccess(contractId)

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

            If Not Page.IsPostBack Then
                Master.SetParentPage("Vehicles.aspx?cid=" & contractId)
                _FillData(vehicleId)
            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("Vehicles.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(dtOutOfService.Text)

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

                Dim cmdUpdate = "
                    UPDATE Vehicle SET NotInService=1, NotInServiceDate=@termDate,
                        EditedDate=GETDATE(), EditedBy=@editedBy
                    WHERE ID=@vehicleId"
                Dim cmd As New SqlCommand(cmdUpdate, connection)
                cmd.Parameters.AddWithValue("vehicleId", vehicleId)
                cmd.Parameters.AddWithValue("editedBy", username)
                cmd.Parameters.AddWithValue("termDate", termDate.Clip())
                cmd.ExecuteNonQuery()

            End Using

            Response.Redirect("Vehicles.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(vehicleId As Integer)
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()

            Dim cmdGet = "
                SELECT ID,
                    Make + '/' + Model + '/' + UnitYear AS VehicleName,
                    VIN
                FROM Vehicle WHERE ID=@vehicleId"
            Dim cmd As New SqlCommand(cmdGet, connection)
            cmd.Parameters.AddWithValue("vehicleId", vehicleId)

            Dim dReader = cmd.ExecuteReader()
            If dReader.Read() Then
                litId.Text = dReader("ID")
                litName.Text = Logic.Conversion.ToString(dReader("VehicleName"))
                litVIN.Text = Logic.Conversion.ToString(dReader("VIN"))
            Else
                Response.Redirect("Vehicles.aspx?cid=" & contractId)
            End If

        End Using
    End Sub

End Class