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

Public Class DACHConsent
    Inherits System.Web.UI.Page

    Property DLNumber As String
    Property DLState As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            _LoadForm()
            If Not Page.IsPostBack Then
                signatureConsent.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()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand("", connection)
            cmd.Parameters.AddWithValue("appId", Master.app.id)

            cmd.CommandText = "SELECT TOP 1 Number,State FROM web_DriverApplicationLicenses WHERE AppId=@appId"
            Using dReader = cmd.ExecuteReader()
                dReader.Read()
                DLNumber = dReader(0)
                DLState = dReader(1)
            End Using

            cmd.CommandText = "SELECT DachConsentSignatureId FROM web_DriverApplication WHERE id=@appId"
            Using dReader = cmd.ExecuteReader()
                If dReader.Read() Then
                    signatureConsent.SignatureId = Logic.Conversion.ToNullableInteger(dReader("DachConsentSignatureId"))
                End If
            End Using

        End Using
    End Sub

    Private Sub _SaveForm()
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()

            Dim sigConsent = signatureConsent.SignatureId

            Dim cmd As New SqlCommand(
                "UPDATE web_DriverApplication 
                SET DachConsentSignatureId=@sigConsent 
                WHERE id=@appId", connection)
            cmd.Parameters.AddWithValue("sigConsent", If(sigConsent.HasValue, sigConsent.Value, DBNull.Value))
            cmd.Parameters.AddWithValue("appId", Master.app.id)
            cmd.ExecuteNonQuery()
        End Using

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

    Private Sub _SaveDocument()
        Dim ds As New dsAppDashConsent
        Dim adapter As New dsAppDashConsentTableAdapters.dsAppDachConsentTableAdapter

        Using report As New crptAppDachConsent
            adapter.Fill(ds.dsAppDachConsent, Master.app.id)
            report.SetDataSource(ds)

            Dim picbox As PictureObject = report.ReportDefinition.ReportObjects("picSig")
            Dim formula As FormulaFieldDefinition = report.DataDefinition.FormulaFields("pathSig")
            Dim sigId = Logic.Conversion.ToInteger(ds.dsAppDachConsent.Rows(0)("DachConsentSignatureId"))
            Reporting.SetPictureToSignature(picbox, formula, sigId, 5600)

            Dim rawData = Reporting.StreamToByteArray(report)

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

                Dim cmd As New SqlCommand(
                    "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", connection)
                cmd.Parameters.AddWithValue("appId", Master.app.id)
                cmd.Parameters.AddWithValue("pageTitle", Master.CurrentTitle)
                cmd.Parameters.AddWithValue("pageFileName", Master.CurrentTitle)
                cmd.Parameters.AddWithValue("pdfData", rawData)
                cmd.Parameters.AddWithValue("pdfDataSize", rawData.Length)
                cmd.ExecuteNonQuery()

            End Using
        End Using
    End Sub

End Class