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

Public Class RemoveTraining
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property trainingId As Integer

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

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

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

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

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

    Private Sub _FillData()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand(
                "SELECT t.TrainingDate,t.Expires,t.ExpireDate,t.TrainingQuiz,t.TrainingQuizDate,
                    t.DriverName,t.SpecificTopic, tp.Name TopicName,
                    d.LastName+', '+d.FirstName+' '+d.MiddleName+' '+d.Suffix DriverCombinedName
                FROM Training t
                LEFT JOIN TrainingTopic tp ON tp.ID=t.TopicID
                LEFT JOIN Driver d ON d.ID=t.DriverID
                WHERE t.ID=@trainingId",
                connection)
            cmd.Parameters.AddWithValue("trainingId", trainingId)

            Dim dReader = cmd.ExecuteReader()
            dReader.Read()
            litDriver.Text = Logic.Conversion.ToString(dReader("DriverCombinedName"))
            litDriverName.Text = Logic.Conversion.ToString(dReader("DriverName"))
            litTopic.Text = Logic.Conversion.ToString(dReader("TopicName"))
            litSpecificTopic.Text = Logic.Conversion.ToString(dReader("SpecificTopic"))
            litDate.Text = Logic.Conversion.ToNullableDate(dReader("TrainingDate")).FormatForHtml()
            litExpires.Text = Logic.Conversion.ToNullableDate(dReader("ExpireDate")).FormatForHtml()
            If Logic.Conversion.ToBoolean(dReader("Expires")) = False Then litExpires.Text = ""
            litQuiz.Text = Logic.Conversion.ToNullableDate(dReader("TrainingQuizDate")).FormatForHtml()
            If Logic.Conversion.ToBoolean(dReader("TrainingQuiz")) = False Then litQuiz.Text = ""
        End Using
    End Sub

End Class