Imports System.Data.SqlClient

Public Class Recruiting
    Inherits System.Web.UI.Page

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try

            Dim contractId = Logic.Conversion.ToInteger(Request.Params("cid"))
            Master.CurrentClient.VerifyContractAccess(contractId)

            Dim perms = Master.CurrentClient.Permissions
            Dim showApplications = perms.Contains("View Applications Page")
            Dim showDrivers = perms.Contains("View Qualified Drivers")
            If Not (showApplications Or showDrivers) Then Throw New HttpException(403, "Forbidden")

            phApplicationTabs1.Visible = showApplications
            phApplicationTabs2.Visible = showApplications
            phApplicationLinks1.Visible = showApplications
            phApplicationLinks2.Visible = showApplications
            phQualifiedLink.Visible = showDrivers
            phQualifiedTab.Visible = showDrivers

            txtAppLink.Text = My.Settings.baseUrl & "Driver/StartApplication.aspx?clientId=" & contractId

            dsPending.ConnectionString = Logic.Database.connectionString
            dsPending.SelectParameters("contractId").DefaultValue = contractId
            dsReview.ConnectionString = Logic.Database.connectionString
            dsReview.SelectParameters("contractId").DefaultValue = contractId
            dsPast.ConnectionString = Logic.Database.connectionString
            dsPast.SelectParameters("contractId").DefaultValue = contractId
            
            dsDrivers.ConnectionString = Logic.Database.ConnectionString
            dsDrivers.SelectParameters("contractId").DefaultValue = contractId

            If Not perms.Contains("DPQ Driver Hired/Not Hired") Then
                Dim colCount = gvReview.Columns.Count
                gvReview.Columns(colCount - 1).Visible = False
            End If


            If Not Page.IsPostBack Then
                Using connection As New SqlConnection(Logic.Database.connectionString)
                    connection.Open()
                    Dim cmd As New SqlCommand("", connection)

                    cmd.CommandText = 
                        "SELECT 0 ID,'(all)' Name
                        UNION ALL SELECT sc.ID, c.Name+' - '+CONVERT(VARCHAR,sc.ID) name
                        FROM ContractAgencyMap map LEFT JOIN ServiceContract sc ON sc.ID=map.agencyId LEFT JOIN Customer c ON c.ID=sc.CustomerID
                        WHERE sc.Status='Active' AND map.contractId=@contractId
                        ORDER BY name"
                    cmd.Parameters.AddWithValue("contractId", contractId)
                    Using dReader = cmd.ExecuteReader()
                        While dReader.Read()
                            ddlAgency.Items.Add(New ListItem(dReader(1), dReader(0)))
                        End While
                    End Using

                    cmd.CommandText = 
                        "SELECT comp.emailDriverAppLinkBody [template], sc.id, cust.Name
                        FROM ServiceContract sc LEFT JOIN Customer cust ON cust.ID=sc.CustomerID LEFT JOIN Company comp ON 1=1
                        WHERE sc.ID=@contractId"
                    Using dReader = cmd.ExecuteReader()
                        dReader.Read()
                        litContractId.Text = dReader("id")
                        litContractId2.Text = dReader("id")
                        litCustomerName.Text = Strings.Replace(Strings.Replace(Strings.Replace(dReader("name"), "'", "&apos"), """", "&quot"), "&", "&amp")

                        txtPreview.InnerText = dReader("template")
                    End Using
                End Using
                Master.SetParentPage("Recruiting.aspx?cid=" & contractId)
            End If

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

    Private Sub repHistory_ItemDataBound(sender As Object, e As EventArgs) Handles repPending.ItemDataBound
        repPending.Visible = repPending.Items.Count > 0
    End Sub

    Private Sub gvReview_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvReview.RowDataBound
        'If e.Row.RowType = DataControlRowType.DataRow Then
        '    Dim data As DataRowView = e.Row.DataItem
        '    Dim url = String.Format("ViewApplication.aspx?id={0}&cid={1}", data("id"), data("clientId"))
        '    e.Row.Attributes.Add("data-href", url)
        'End If
    End Sub

    Private Sub gvPast_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvPast.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            Dim url = String.Format("ViewApplication.aspx?id={0}&cid={1}", data("id"), data("clientId"))
            e.Row.Attributes.Add("data-href", url)
        End If
    End Sub

    Private Sub gvDrivers_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDrivers.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            Dim url = String.Format("ReviewDriver.aspx?cid={0}&id={1}", data("ContractId"), data("Id"))
            e.Row.Attributes.Add("data-href", url)
        End If
    End Sub

End Class