Imports System.Data.SqlClient
Imports ReportingClass

Public Class Submit2
    Inherits System.Web.UI.Page

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            phDach.Visible = Master.app.includeDach

            Using connection As New SqlConnection(Logic.Database.ConnectionString)
                connection.Open()
                Using transaction = connection.BeginTransaction()
                    _UpdateApplication(connection, transaction)
                    _SaveDocument(connection, transaction)
                    transaction.Commit()
                End Using
            End Using
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub _UpdateApplication(connection As SqlConnection, transaction As SqlTransaction)

        ' If we have a support services request, just switch it back to "in review"
        Dim cmd As New SqlCommand(
            "SELECT COUNT(id) FROM web_DriverApplicationPdf
            WHERE appId=@appId AND pageTitle='Support Services Request'",
            connection, transaction)
        cmd.Parameters.AddWithValue("appId", Master.app.id)
        If CInt(cmd.ExecuteScalar()) > 0 Then
            Master.app.status = "in review"
        End If

        ' Submit if incomplete app; otherwise just save it
        If Master.app.status = "incomplete" Then
            Master.app.Submit("[Driver]", connection, transaction)
        Else
            Master.app.Update("[Driver]", connection, transaction, resubmit:=True)
        End If

    End Sub

    Private Sub _SaveDocument(connection As SqlConnection, transaction As SqlTransaction)
        Dim ReportDataSet As New dsAppGeneralQuestions
        Dim ReportTableAdapter As New TransactionalAppGeneralQuestionsTableAdapter
        ReportTableAdapter.SetConnection(connection, transaction)
        ReportTableAdapter.Fill(ReportDataSet.AppGeneralQuestionsDataTable, Master.app.id)

        Using Report As New crptAppGeneralQuestions
            Report.SetDataSource(ReportDataSet)
            Dim rawData = Reporting.StreamToByteArray(Report)

            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, transaction)
            cmd.Parameters.AddWithValue("appId", Master.app.id)
            cmd.Parameters.AddWithValue("pageTitle", "General Questions")
            cmd.Parameters.AddWithValue("pageFileName", "generalquestions.aspx")
            cmd.Parameters.AddWithValue("pdfData", rawData)
            cmd.Parameters.AddWithValue("pdfDataSize", rawData.Length)
            cmd.ExecuteNonQuery()
        End Using
    End Sub

End Class