Imports System.Data.SqlClient
Imports ReportingClass

Public Class PreviousAddresses
    Inherits System.Web.UI.Page

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

            If Not Page.IsPostBack Then
                _LoadForm()
            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
                prevAddress1Street, prevAddress1City, prevAddress1State,
                prevAddress1Zip, prevAddress1Duration,
                prevAddress2Street, prevAddress2City, prevAddress2State,
                prevAddress2Zip, prevAddress2Duration,
                prevAddress3Street, prevAddress3City, prevAddress3State,
                prevAddress3Zip, prevAddress3Duration,
                applicantAddress, applicantCity, applicantState, applicantZip
            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()

            txtStreet1.Text = Logic.Conversion.ToString(dReader("prevAddress1Street"))
            txtCity1.Text = Logic.Conversion.ToString(dReader("prevAddress1City"))
            ddlState1.SelectedValue = Logic.Conversion.ToString(dReader("prevAddress1State"))
            txtZip1.Text = Logic.Conversion.ToString(dReader("prevAddress1Zip"))
            txtDuration1.Text = Logic.Conversion.ToString(dReader("prevAddress1Duration"))
            txtStreet2.Text = Logic.Conversion.ToString(dReader("prevAddress2Street"))
            txtCity2.Text = Logic.Conversion.ToString(dReader("prevAddress2City"))
            ddlState2.SelectedValue = Logic.Conversion.ToString(dReader("prevAddress2State"))
            txtZip2.Text = Logic.Conversion.ToString(dReader("prevAddress2Zip"))
            txtDuration2.Text = Logic.Conversion.ToString(dReader("prevAddress2Duration"))
            txtStreet3.Text = Logic.Conversion.ToString(dReader("prevAddress3Street"))
            txtCity3.Text = Logic.Conversion.ToString(dReader("prevAddress3City"))
            ddlState3.SelectedValue = Logic.Conversion.ToString(dReader("prevAddress3State"))
            txtZip3.Text = Logic.Conversion.ToString(dReader("prevAddress3Zip"))
            txtDuration3.Text = Logic.Conversion.ToString(dReader("prevAddress3Duration"))

            ' fill in any missing info based on previous page
            If String.IsNullOrEmpty(txtStreet1.Text) Then
                txtStreet1.Text = dReader("applicantAddress")
            End If
            If String.IsNullOrEmpty(txtCity1.Text) Then
                txtCity1.Text = dReader("applicantCity")
            End If
            If String.IsNullOrEmpty(ddlState1.SelectedValue) Then
                ddlState1.SelectedValue = dReader("applicantState")
            End If
            If String.IsNullOrEmpty(txtZip1.Text) Then
                txtZip1.Text = dReader("applicantZip")
            End If

        End Using

    End Sub

    Private Sub _SaveForm()

        Dim cmdSave = "
            UPDATE web_DriverApplication
            SET
                prevAddress1Street = @prevAddress1Street, prevAddress1City = @prevAddress1City, 
                prevAddress1State = @prevAddress1State, prevAddress1Zip = @prevAddress1Zip, 
                prevAddress1Duration = @prevAddress1Duration, 
                prevAddress2Street = @prevAddress2Street, prevAddress2City = @prevAddress2City, 
                prevAddress2State = @prevAddress2State, prevAddress2Zip = @prevAddress2Zip, 
                prevAddress2Duration = @prevAddress2Duration, 
                prevAddress3Street = @prevAddress3Street, prevAddress3City = @prevAddress3City, 
                prevAddress3State = @prevAddress3State, prevAddress3Zip = @prevAddress3Zip, 
                prevAddress3Duration = @prevAddress3Duration
            WHERE id=@appId"

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

            Dim cmd As New SqlCommand(cmdSave, connection)
            With cmd.Parameters
                .AddWithValue("appId", Master.app.id)
                .AddWithValue("prevAddress1Street", txtStreet1.Text)
                .AddWithValue("prevAddress1City", txtCity1.Text)
                .AddWithValue("prevAddress1State", ddlState1.SelectedValue)
                .AddWithValue("prevAddress1Zip", txtZip1.Text)
                .AddWithValue("prevAddress1Duration", txtDuration1.Text)
                .AddWithValue("prevAddress2Street", txtStreet2.Text)
                .AddWithValue("prevAddress2City", txtCity2.Text)
                .AddWithValue("prevAddress2State", ddlState2.SelectedValue)
                .AddWithValue("prevAddress2Zip", txtZip2.Text)
                .AddWithValue("prevAddress2Duration", txtDuration2.Text)
                .AddWithValue("prevAddress3Street", txtStreet3.Text)
                .AddWithValue("prevAddress3City", txtCity3.Text)
                .AddWithValue("prevAddress3State", ddlState3.SelectedValue)
                .AddWithValue("prevAddress3Zip", txtZip3.Text)
                .AddWithValue("prevAddress3Duration", txtDuration3.Text)
            End With

            cmd.ExecuteNonQuery()

        End Using

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

    End Sub

    Private Sub _SaveDocument()
        Dim ReportDataSet As New dsAppPreviousAddresses
        Dim ReportTableAdapter As New dsAppPreviousAddressesTableAdapters.PreviousAddressesTableAdapter

        Using Report As New crptAppPreviousAddresses
            ReportTableAdapter.Fill(ReportDataSet.PreviousAddresses, Master.app.id)
            Report.SetDataSource(ReportDataSet)
            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