Imports System.Data.SqlClient
Imports System.Threading

Public Class TransferApplication
    Inherits System.Web.UI.Page

    Private _transferId As Integer = 0

    Private Sub Page_Load() Handles Me.Load
        Try
            _transferId = Logic.Conversion.ToInteger(Request.Params("xferId"))
            Dim driverId = SessionVars.DriverId(Session)

            Dim targetId As Integer
            Dim currentPage As String = Nothing

            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand(
                    "SELECT xfer.currentPage, cust1.Name Name1, cust2.Name Name2, sc2.id targetId
                    FROM web_DriverMoveApplication xfer
                    LEFT JOIN web_DriverApplication app ON app.id=xfer.appId
                    LEFT JOIN ServiceContract sc1 ON sc1.ID=xfer.previousContractId
                    LEFT JOIN Customer cust1 ON cust1.ID=sc1.CustomerID
                    LEFT JOIN ServiceContract sc2 ON sc2.ID=app.clientId
                    LEFT JOIN Customer cust2 ON cust2.ID=sc2.CustomerID
                    WHERE xfer.id=@transferId AND app.driverAccountId=@accountId
                      AND app.status='transfer pending'", connection)
                cmd.Parameters.AddWithValue("transferId", _transferId)
                cmd.Parameters.AddWithValue("accountId", driverId)

                Dim dReader = cmd.ExecuteReader()
                If Not dReader.Read() Then
                    Response.Redirect("Default.aspx")
                End If
                If Not IsDBNull(dReader("currentPage")) Then
                    currentPage = dReader("currentPage")
                End If

                txtOldCompany.Text = dReader("Name1")
                txtNewCompany.Text = dReader("Name2")
                targetId = dReader("targetId")
            End Using

            analytics.LoadByContractId(targetId)

            If String.IsNullOrEmpty(currentPage) Then
                Response.Redirect(String.Format("transfer/{0}?xferId={1}", currentPage, _transferId))
            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("Default.aspx")
    End Sub

    Private Sub btnStart_Click() Handles btnStart.Click
        Try
            Using connection As New SqlConnection(Logic.Database.connectionString)
                connection.Open()
                Dim cmd As New SqlCommand(
                    "UPDATE web_DriverMoveApplication SET currentPage='Default.aspx'
                    WHERE id=@transferId", connection)
                cmd.Parameters.AddWithValue("transferId", _transferId)
                cmd.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
        Response.Redirect("transfer/Default.aspx?xferId=" & _transferId)
    End Sub

End Class