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

Public Class SPHSubmittal
    Inherits System.Web.UI.Page
    Property driverId As Int64
    Property prevEmpID As Int64

    Property Complete As Boolean = False

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            driverId = Logic.Conversion.ToInteger(Request.Params("did"))
            prevEmpID = Logic.Conversion.ToInteger(Request.Params("pid"))

            If Not Page.IsPostBack Then
                If driverId > 0 And prevEmpID > 0 Then
                    _LoadForm()
                    'Signature.DefaultSigUrl = "/_assets/DefaultAppSig.aspx?appId=" & Master.app.id
                Else
                    Response.Redirect("/Error.aspx")
                End If
            Else
                '_SaveForm()
                'Response.Redirect("/SPHSubmittalComplete.aspx?pid=" & prevEmpID)
                ''_SaveDocument()
            End If


        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try

        If Complete Then
            Response.Redirect("SPHSubmittalComplete.aspx?pid=" & prevEmpID)
        End If
    End Sub

    Private Sub _LoadForm()

        Dim cmdGet = "
            SELECT 
			Driver.LastName + ', ' + Driver.FirstName + ' ' + driver.MiddleName + ' ' + driver.Suffix AS DriverName,
			Driver.SSN,
			DriverEmploymentHistory.EmployedFrom,
			DriverEmploymentHistory.EmployedTo
            ,ISNULL(DriverEmploymentHistory.ResultReceived,0) AS ResultReceived

            FROM DriverEmploymentHistory LEFT JOIN Driver ON DriverEmploymentHistory.DriverID=Driver.ID
            WHERE DriverEmploymentHistory.DriverID=@DriverId AND DriverEmploymentHistory.ID=@ID"

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

            Dim cmd As New SqlCommand(cmdGet, connection)
            cmd.Parameters.AddWithValue("DriverId", driverId)
            cmd.Parameters.AddWithValue("ID", prevEmpID)

            Dim dReader = cmd.ExecuteReader()
            If dReader.Read Then

                If dReader("ResultReceived") = False Then
                    txtName.Text = Logic.Conversion.ToString(dReader("DriverName"))
                    txtSSN.Text = "XXX-XX-" & Right(Logic.Conversion.ToString(dReader("SSN")), 4)
                    txtEmploymentDates.Text = Logic.Conversion.ToString(dReader("EmployedFrom")) & " - " & Logic.Conversion.ToString(dReader("EmployedTo"))

                    'ynTestPositive.Checked = Logic.Conversion.ToNullableBoolean(dReader("ControlledSubstancesTestPositive"))
                    'txtProfessional.Text = Logic.Conversion.ToString(dReader("ControlledSubstancesProfessional"))
                    'txtProfessionalPhone.Text = Logic.Conversion.ToString(dReader("ControlledSubstancesProfessionalPhone"))
                    'txtReturnToDutyTest.Text = Logic.Conversion.ToString(dReader("ControlledSubstancesReturnToDutyTest"))
                    'Signature.SignatureId = Logic.Conversion.ToNullableInteger(dReader("ControlledSubstancesSignatureId"))
                    Complete = False
                Else
                    Complete = True
                End If
            End If

        End Using

    End Sub

    Private Sub _SaveForm()

    End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Try

            Dim cmdSave = "UPDATE DriverEmploymentHistory SET 
                        ResultReceived=1, ResultReceivedDate=GETDATE(), ResultReceivedMethod=@ResultReceivedMethod, ResultDateCorrect=@ResultDateCorrect, ResultDateComment=@ResultDateComment, 
                        ResultDriverCommercialVehicle=@ResultDriverCommercialVehicle, ResultWasSafe=@ResultWasSafe, ResultAccidents=@ResultAccidents, ResultReasonLeaving=@ResultReasonLeaving, ResultPositiveControlledSubstance=@ResultPositiveControlledSubstance, 
                        ResultAlcohol=@ResultAlcohol, ResultRefusedTest=@ResultRefusedTest, ResultRehab=@ResultRehab, ResultViolatePolicy=@ResultViolatePolicy, ResultComments=@ResultComments, ResultName=@ResultName, ResultTitle=@ResultTitle
                        WHERE ID=@ID;INSERT INTO DriverEmploymentHistoryWebLinkLog (DriverEmploymentHistoryID,SubmitDate,SubmitBy,SubmitTitle,SubmitIP) VALUES (@ID,GETDATE(),@ResultName,@ResultTitle,@IP);"

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

                'Dim testPositive = ynTestPositive.Checked
                'Dim sigId = Signature.SignatureId

                Dim cmd As New SqlCommand(cmdSave, connection)
                With cmd.Parameters
                    .AddWithValue("ID", prevEmpID)
                    .AddWithValue("ResultReceivedMethod", "Email")
                    .AddWithValue("ResultDateCorrect", yn1.Checked)
                    .AddWithValue("ResultDateComment", txtEmploymentDatesCorrected.Text)
                    .AddWithValue("ResultDriverCommercialVehicle", yn2.Checked)
                    .AddWithValue("ResultWasSafe", yn3.Checked)
                    .AddWithValue("ResultAccidents", yn4.Checked)
                    .AddWithValue("ResultReasonLeaving", ddlReasonLeft.SelectedValue)
                    .AddWithValue("ResultPositiveControlledSubstance", yn6.Checked)
                    .AddWithValue("ResultAlcohol", yn7.Checked)
                    .AddWithValue("ResultRefusedTest", yn8.Checked)
                    .AddWithValue("ResultRehab", ddl9.SelectedValue)
                    .AddWithValue("ResultViolatePolicy", yn10.Checked)

                    .AddWithValue("ResultComments", txtComments.Text)
                    .AddWithValue("ResultName", txtByName.Text)
                    .AddWithValue("ResultTitle", txtByTitle.Text)
                    .AddWithValue("IP", Request.UserHostAddress)

                End With

                cmd.ExecuteNonQuery()

                Response.Redirect("SPHSubmittalComplete.aspx?pid=" & prevEmpID)

            End Using
        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
End Class