Imports System.Data.SqlClient
Imports Logic.DateExtensions

Public Class DrugAlcoholTests
    Inherits System.Web.UI.Page

    Property contractId As Integer

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

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

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

            Dim showSchedule = perms.Contains("Request Drug Test") AndAlso _HasI3Data()
            btnSchedule.Visible = showSchedule
            gvAwaitingTests.Columns(3).Visible = showSchedule

            dsAwaitingTests.ConnectionString = Logic.Database.connectionString
            dsAwaitingTests.SelectParameters("contractId").DefaultValue = contractId

            dsAwaitingResults.ConnectionString = Logic.Database.connectionString
            dsAwaitingResults.SelectParameters("contractId").DefaultValue = contractId

            dsCompleted.ConnectionString = Logic.Database.connectionString
            dsCompleted.SelectParameters("contractId").DefaultValue = contractId

            dsNotTested.ConnectionString = Logic.Database.connectionString
            dsNotTested.SelectParameters("contractId").DefaultValue = contractId

            If Not Page.IsPostBack() Then
                txtPendingStart.Text = New Date(Today.Year, 1, 1).FormatForHtml5()
                txtPendingTo.Text = New Date(Today.Year, 12, 31).FormatForHtml5()
                Master.SetParentPage("DrugAlcoholTests.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 btnSchedule_Click() Handles btnSchedule.Click
        Response.Redirect("RequestTest.aspx?cid=" & contractId)
    End Sub

    Private Sub gvCompleted_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvCompleted.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            '            Dim url = String.Format("Documents.aspx?&cid={0}&ctx=DrugAlcoholTest&id={1}", data("contractId"), data("id"))
            Dim url = String.Format("DocumentsNew.aspx?&cid={0}&ctx=DrugAlcoholTest&id={1}", data("contractId"), data("id"))
            e.Row.Attributes.Add("data-href", url)

            'DocumentsNew.aspx?ctx=DrugAlcoholTest&id={0}&cid={1}
        End If
    End Sub

    Private Function _HasI3Data() As Boolean
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand("SELECT NULLIF(i3ScreenOrgId,'') FROM ServiceContract WHERE id=@contractId", connection)
            cmd.Parameters.AddWithValue("contractId", contractId)
            Return TypeOf cmd.ExecuteScalar() IsNot DBNull
        End Using
    End Function

End Class