Imports System.Data.SqlClient

Public Class Documents
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property itemContext As String
    Property driverId As Int64
    Property appId As Int64

    Protected Sub Page_Load() Handles Me.Load
        Try
            appId = Logic.Conversion.ToInteger(Request.Params("aid"))
            itemContext = Logic.Conversion.ToString(Request.Params("ctx"))
            driverId = Logic.Conversion.ToInteger(Request.Params("did"))
            contractId = Logic.Conversion.ToInteger(Request.Params("cid"))

            Master.CurrentClient.VerifyContractAccess(contractId)
            If Not _HasPerms(itemContext) Then Throw New HttpException(403, "Forbidden")

            If Not Page.IsPostBack Then
                _FillData(itemContext, appId)
            End If

            dsDocs.ConnectionString = Logic.Database.ConnectionString
            dsDocs.SelectParameters("appId").DefaultValue = appId

        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
        Select Case itemContext
            Case "DriverPQ"
                Response.Redirect("Recruiting.aspx?cid=" & contractId)
        End Select
    End Sub


    Private Sub _FillData(itemContext As String, itemId As Integer)
        Using connection As New SqlConnection(Logic.Database.ConnectionString)
            connection.Open()
            Dim cmd As New SqlCommand("", connection)
            cmd.Parameters.Add("appID", SqlDbType.BigInt).Value = itemId
            Select Case itemContext
                Case "DriverPQ"
                    Master.SetParentPage("Recruiting.aspx?cid=" & contractId)
                    litContext.Text = "Driver Recruiting"
                    cmd.CommandText = "SELECT applicantNameLast + ', ' + applicantNameFirst + CASE WHEN NOT NULLIF(applicantNameMiddle, '') IS NULL THEN ' ' + applicantNameMiddle ELSE '' END + CASE WHEN NOT NULLIF(applicantNameSuffix, '') IS NULL THEN ', ' + applicantNameSuffix ELSE '' END AS applicantName FROM web_DriverApplication WHERE ID=@appId"

            End Select
            litItemName.Text = HttpUtility.HtmlEncode(cmd.ExecuteScalar())
        End Using
    End Sub

    Private Function _HasPerms(context As String) As Boolean
        Return True
    End Function



    Private Sub gvDocs_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDocs.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            e.Row.Attributes.Add("data-href", data("pageUrl"))
            e.Row.Attributes.Add("data-target", "_blank")
        End If
    End Sub

End Class