Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports ReportingClass

Public Class AgreementControlledSubstanceTest
    Inherits System.Web.UI.Page

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

            If Not Page.IsPostBack Then
                _LoadForm()
                '   signatureConsent.DefaultSigUrl = "/_assets/DefaultAppSig.aspx?appId=" & Master.app.id
                '   signaturePolicyReceived.DefaultSigUrl = "/_assets/DefaultAppSig.aspx?appId=" & Master.app.id
            Else
                _SaveForm()
                _SaveDocument()
            End If

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

    Private Sub _LoadForm()

        Dim cmdGet = "
            SELECT Agreement_ControlledSubstanceTest1,Agreement_ControlledSubstanceTest2
            FROM web_DriverApplication WHERE id=@appId"

        Using connection As New SqlConnection(Logic.Database.ConnectionString)
            connection.Open()

            Dim cmd As New SqlCommand(cmdGet, connection)
            cmd.Parameters.AddWithValue("appId", Master.app.id)

            Dim dReader = cmd.ExecuteReader()
            dReader.Read()

            ynAgree.Checked = Logic.Conversion.ToNullableBoolean(dReader("Agreement_ControlledSubstanceTest1"))
            ynAgree2.Checked = Logic.Conversion.ToNullableBoolean(dReader("Agreement_ControlledSubstanceTest2"))

            'signatureConsent.SignatureId = Logic.Conversion.ToNullableInteger(dReader("TestingConsentSignatureId"))
            'signaturePolicyReceived.SignatureId = Logic.Conversion.ToNullableInteger(dReader("TestingPolicyReceivedSignatureId"))

        End Using

    End Sub

    Private Sub _SaveForm()

        Dim cmdSave = "
            UPDATE web_DriverApplication
            SET Agreement_ControlledSubstanceTest1=@Agreement_ControlledSubstanceTest1, Agreement_ControlledSubstanceTest2=@Agreement_ControlledSubstanceTest2
            WHERE id=@appId"

        Using connection As New SqlConnection(Logic.Database.ConnectionString)
            connection.Open()

            'Dim sigConsent = signatureConsent.SignatureId
            'Dim sigReceived = signaturePolicyReceived.SignatureId
            Dim Agree = ynAgree.Checked
            Dim Agree2 = ynAgree2.Checked


            Dim cmd As New SqlCommand(cmdSave, connection)
            cmd.Parameters.AddWithValue("appId", Master.app.id)
            cmd.Parameters.AddWithValue("Agreement_ControlledSubstanceTest1", If(Agree.HasValue, Agree.Value, DBNull.Value))
            cmd.Parameters.AddWithValue("Agreement_ControlledSubstanceTest2", If(Agree2.HasValue, Agree2.Value, DBNull.Value))

            cmd.ExecuteNonQuery()

        End Using

        Page.Validate()
        If Page.IsValid Then
            Master.EnableNextPage()
        End If

    End Sub

    Private Sub _SaveDocument()
        Dim ReportDataSet As New dsAppControlledSubstancesTestConsent
        Dim ReportTableAdapter As New dsAppControlledSubstancesTestConsentTableAdapters.ControlledSubstancesTestConsentTableAdapter

        Using Report As New crptAppControlledSubstancesTestConsent
            ReportTableAdapter.Fill(ReportDataSet.ControlledSubstancesTestConsent, Master.app.id)
            Report.SetDataSource(ReportDataSet)

            Dim PictureBox As PictureObject = Report.ReportDefinition.ReportObjects("picSig1")
            Dim LinkedFormula As FormulaFieldDefinition = Report.DataDefinition.FormulaFields("pathSig1")
            Dim SignatureId = Logic.Conversion.ToInteger(ReportDataSet.ControlledSubstancesTestConsent.Rows(0)("TestingConsentSignatureId"))
            Reporting.SetPictureToSignature(PictureBox, LinkedFormula, SignatureId, 5600)

            PictureBox = Report.ReportDefinition.ReportObjects("picSig2")
            LinkedFormula = Report.DataDefinition.FormulaFields("pathSig2")
            SignatureId = Logic.Conversion.ToInteger(ReportDataSet.ControlledSubstancesTestConsent.Rows(0)("TestingPolicyReceivedSignatureId"))
            Reporting.SetPictureToSignature(PictureBox, LinkedFormula, SignatureId, 5600)

            Dim rawData = Reporting.StreamToByteArray(Report)

            Using connection As New SqlConnection(Logic.Database.ConnectionString)
                connection.Open()

                Dim cmdSave = "
                    UPDATE web_DriverApplicationPdf SET pageTitle=@pageTitle, pdfData=@pdfData, pdfDataSize=@pdfDataSize
                    WHERE appId=@appId AND pageFileName=@pageFileName;
                    IF @@ROWCOUNT = 0 BEGIN
                        INSERT INTO web_DriverApplicationPdf (appId, pageTitle, pageFileName, pdfData, pdfDataSize)
                        VALUES (@appId, @pageTitle, @pageFileName, @pdfData, @pdfDataSize);
                    END"
                Dim cmd As New SqlCommand(cmdSave, connection)
                cmd.Parameters.AddWithValue("appId", Master.app.id)
                cmd.Parameters.AddWithValue("pageTitle", Master.CurrentTitle)
                cmd.Parameters.AddWithValue("pageFileName", Master.CurrentPage)
                cmd.Parameters.AddWithValue("pdfData", rawData)
                cmd.Parameters.AddWithValue("pdfDataSize", rawData.Length)
                cmd.ExecuteNonQuery()

            End Using

        End Using
    End Sub

End Class