Imports System.Data.SqlClient

Public Class SafetyScores
    Inherits System.Web.UI.Page

    Property contractId As Integer

    Private Sub Page_Load() 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 Safety Scores Page") Then Throw New HttpException(404, "Forbidden")

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

            If Not Page.IsPostBack Then
                Master.SetParentPage("SafetyScores.aspx?cid=" & contractId)
                _FillUI()
            End If

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

    Private Sub _FillUI()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand(
                "SELECT Score1Name,Score2Name,Score3Name,Score4Name,Score5Name,Score6Name,Score7Name,Score8Name
                FROM SafetyScoreSetup WHERE ID=1", connection)
            Using dReader = cmd.ExecuteReader()
                dReader.Read()
                gvScores.Columns(1).HeaderText = dReader(0)
                gvScores.Columns(3).HeaderText = dReader(6)
                gvScores.Columns(5).HeaderText = dReader(1)
                gvScores.Columns(7).HeaderText = dReader(4)
                gvScores.Columns(9).HeaderText = dReader(3)
                gvScores.Columns(11).HeaderText = dReader(5)
                gvScores.Columns(13).HeaderText = dReader(2)
                gvScores.Columns(15).HeaderText = dReader(7)
            End Using
        End Using
    End Sub

End Class