Imports System.Data.SqlClient
Imports System.Threading

Public Class MoveDriver
    Inherits System.Web.UI.Page

    Property driverId As Integer
    Property contractId As Integer

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

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

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

            If Not Page.IsPostBack Then
                Master.SetParentPage("Drivers.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 btnMove_Click() Handles btnMove.Click
        If Not Page.IsValid Then Exit Sub
        Try
            Logic.Driver.Move(
                driverId, ddlToContract.SelectedValue,
                "[Web: " & Master.CurrentClient.username & "]")
            Response.Redirect("Drivers.aspx?cid=" & contractId)
        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 _FillData()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand("", connection)
            cmd.Parameters.AddWithValue("driverId", driverId)
            cmd.Parameters.AddWithValue("contractId", contractId)
            cmd.Parameters.AddWithValue("accountId", Master.CurrentClient.id)

            cmd.CommandText =
                "SELECT sc.ID,c.Name
                FROM ServiceContract sc LEFT JOIN Customer c ON c.ID=sc.CustomerID
                WHERE c.OwnerContractId=@contractId AND sc.Status='Active'
                  AND sc.ID IN (Select contractId FROM web_ClientContractsMap WHERE clientId=@accountId)
                ORDER BY c.Name,sc.ID"
            Using dReader = cmd.ExecuteReader()
                ddlToContract.Items.Clear()
                ddlToContract.Items.Add(New ListItem("", 0))
                While dReader.Read()
                    If dReader(0) = contractId Then Continue While
                    ddlToContract.Items.Add(New ListItem(dReader(1), dReader(0)))
                End While
            End Using

            cmd.CommandText = 
                "SELECT d.LastName+', '+d.FirstName+' '+' '+d.MiddleName+' '+d.Suffix driverName, cust.Name contractName
                FROM Driver d LEFT JOIN ServiceContract sc ON sc.ID=d.ContractID LEFT JOIN Customer cust ON cust.ID=sc.CustomerID
                WHERE d.ID=@driverId"
            Using dReader = cmd.ExecuteReader()
                If Not dReader.Read() Then Throw New HttpException(404, "Not Found")
                txtDriverName.Text = dReader("driverName")
                txtFromContract.Text = dReader("contractName")
            End Using

        End Using
    End Sub

End Class