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

Public Class RemoveRoadsideInspection
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property inspectionId As Integer

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

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

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

            If Not Page.IsPostBack Then
                Master.SetParentPage("RoadsideInspections.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("RoadsideInspections.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 RoadsideInspection WHERE id=@inspectionid", connection)
                cmd.Parameters.AddWithValue("inspectionId", inspectionId)
                cmd.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
        Response.Redirect("RoadsideInspections.aspx?cid=" & contractId)
    End Sub

    Private Sub _FillData()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand(
                "SELECT i.InspectionDate,i.LevelOfInspection,i.Jurisdiction,i.EvidenceOfRepair,i.ViolationDiscovered,i.HaveCopy,
	                d.LastName+', '+d.FirstName+' '+d.MiddleName+' '+d.Suffix DriverName,
	                v1.Unit Vehicle1, v2.Unit Vehicle2, v3.Unit Vehicle3
                FROM RoadSideInspection i
                LEFT JOIN Vehicle v1 ON v1.ID=i.Vehicle1ID
                LEFT JOIN Vehicle v2 ON v2.ID=i.Vehicle2ID
                LEFT JOIN Vehicle v3 ON v3.ID=i.Vehicle3ID
                LEFT JOIN Driver d ON d.ID=i.DriverID
                WHERE i.ID=@inspectionId",
                connection)
            cmd.Parameters.AddWithValue("inspectionId", inspectionId)
            
            Dim dReader = cmd.ExecuteReader()
            dReader.Read()
            txtDate.Text = Logic.Conversion.ToNullableDate(dReader("InspectionDate")).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"))
            txtLevel.Text = Logic.Conversion.ToString(dReader("LevelOfInspection"))
            txtJurisdiction.Text = Logic.Conversion.ToString(dReader("Jurisdiction"))
            chkInspection.Checked = Logic.Conversion.ToBoolean(dReader("HaveCopy"))
            chkViolation.Checked = Logic.Conversion.ToBoolean(dReader("ViolationDiscovered"))
            chkCorrectiveAction.Checked = Logic.Conversion.ToBoolean(dReader("EvidenceOfRepair"))
        End Using
    End Sub

End Class