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

Public Class RemoveServiceItem
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property vehicleId As Integer
    Property itemId As Integer

    Private Sub Page_Load() Handles Me.Load
        Try

            itemId = Logic.Conversion.ToInteger(Request.Params("id"))

            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand("", connection)

                cmd.CommandText = "SELECT vehicleId FROM VehicleServiceItems WHERE id=@itemId"
                cmd.Parameters.AddWithValue("itemId", itemId)
                vehicleId = Logic.Conversion.ToInteger(cmd.ExecuteScalar())

                If vehicleId > 0 Then
                    cmd.CommandText = "SELECT contractId FROM Vehicle WHERE ID=@vehicleId"
                    cmd.Parameters.AddWithValue("vehicleId", vehicleId)
                    contractId = Logic.Conversion.ToInteger(cmd.ExecuteScalar())
                Else
                    cmd.CommandText = "SELECT contractId FROM VehicleServiceItems WHERE id=@itemId"
                    contractId = Logic.Conversion.ToInteger(cmd.ExecuteScalar())
                End If
            End Using

            Master.CurrentClient.VerifyContractAccess(contractId)

            Dim perms = Master.CurrentClient.Permissions
            If Not perms.Contains("Edit Vehicle Service Items") Then Throw New HttpException(403, "Forbidden")

            If Not Page.IsPostBack Then
                Master.SetParentPage("Vehicles.aspx")
                _FillData(itemId)
            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() Handles btnCancel.Click
        Response.Redirect("Vehicles.aspx?cid=" & contractId)
    End Sub

    Private Sub btnDelete_Click() Handles btnDelete.Click
        If Not Page.IsValid Then
            Exit Sub
        End If

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

                Dim cmd As New SqlCommand("DELETE FROM VehicleServiceItems WHERE id=@itemId", connection)
                cmd.Parameters.AddWithValue("itemId", itemId)
                cmd.ExecuteScalar()
            End Using
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try

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

    End Sub

    Private Sub _FillData(itemId As Integer)
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()

            Dim cmd As New SqlCommand(
                "SELECT items.id, items.vehicleId, items.label, items.serviceDate, items.vehicleId, v.unit, v.vin
                FROM VehicleServiceItems items LEFT JOIN Vehicle v ON items.vehicleId=v.ID
                WHERE items.id=@itemId", connection)
            cmd.Parameters.AddWithValue("itemId", itemId)

            Dim dReader = cmd.ExecuteReader()
            dReader.Read()
            txtId.Text = dReader("id")
            txtServiceType.Text = dReader("label")
            dtService.Text = Logic.Conversion.ToDate(dReader("serviceDate")).FormatForHtml5()
            txtVehicle.Text = "unit:" & dReader("unit") & " - vin:" & dReader("vin") & " - id:" & dReader("vin")
            If Logic.Conversion.ToInteger(dReader("vehicleId")) = 0 Then txtVehicle.Text = "(All)"

        End Using
    End Sub

End Class