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

Public Class RemoveAccident
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property accidentId As Integer

    Private Sub Page_Load() Handles Me.Load
        Try
            accidentId = Logic.Conversion.ToInteger(Request.Params("id"))

            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand("SELECT ContractID FROM Accident WHERE id=@accidentId", connection)
                cmd.Parameters.AddWithValue("accidentId", accidentId)
                contractId = cmd.ExecuteScalar()
            End Using
            Master.CurrentClient.VerifyContractAccess(contractId)

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

            If Not Page.IsPostBack Then
                Master.SetParentPage("Accidents.aspx?cid=" & contractId)
                _FillData()
            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("Accidents.aspx?cid=" & contractId)
    End Sub

    Private Sub btnDelete_Click() Handles btnDelete.Click
        If Not Page.IsValid Then Exit Sub
        Try
            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand("DELETE FROM Accident WHERE id=@accidentId", connection)
                cmd.Parameters.AddWithValue("accidentId", accidentId)
                cmd.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
        Response.Redirect("Accidents.aspx?cid=" & contractId)
    End Sub

    Private Sub _FillData()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand(
                "SELECT a.AccidentDate,a.CityState,a.NumberInjured,a.NumberFatal,a.VehicleTowed,a.HazMatRelease,a.AccidentReport,a.Cited,a.PostAccidentDocumentation,a.Cited,a.Description,
                    d.LastName+', '+d.FirstName+' '+d.MiddleName+' '+d.Suffix DriverName,
                    v1.Unit Vehicle1, v2.Unit Vehicle2, v3.Unit Vehicle3
                FROM Accident a
                LEFT JOIN Vehicle v1 ON v1.ID=a.VehicleID
                LEFT JOIN Vehicle v2 ON v2.ID=a.Vehicle2ID
                LEFT JOIN Vehicle v3 ON v3.ID=a.Vehicle3ID
                LEFT JOIN Driver d ON d.ID=a.DriverID
                WHERE a.ID=@accidentId", connection)
            cmd.Parameters.AddWithValue("accidentId", accidentId)

            Dim dReader = cmd.ExecuteReader()
            dReader.Read()
            txtDate.Text = Logic.Conversion.ToNullableDate(dReader("AccidentDate")).FormatForHtml()
            txtDriver.Text = Logic.Conversion.ToString(dReader("DriverName"))
            txtVehicle1.Text = Logic.Conversion.ToString(dReader("Vehicle1"))
            txtVehicle2.Text = Logic.Conversion.ToString(dReader("Vehicle2"))
            txtVehicle3.Text = Logic.Conversion.ToString(dReader("Vehicle3"))
            txtCityState.Text = dReader("CityState")
            txtInjured.Text = dReader("NumberInjured")
            txtFatal.Text = dReader("NumberFatal")
            chkTowed.Checked = dReader("VehicleTowed")
            chkHazmat.Checked = dReader("HazMatRelease")
            chkAccident.Checked = dReader("AccidentReport")
            chkCited.Checked = Logic.Conversion.ToBoolean(dReader("Cited"))
            chkPostAccident.Checked = Logic.Conversion.ToBoolean(dReader("PostAccidentDocumentation"))
        End Using
    End Sub

End Class