Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Partial Class BusinessAssociates_SSA_Document
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            FillSSADetails()
        End If
    End Sub

    Public Sub FillSSADetails()

        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim con As New Data.SqlClient.SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

        Dim Str As String
        Str = "select * From SSA_Master Where SSA_KID='" & Session("SSAID").ToString & "'"
        If con.State = Data.ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        cmd = New SqlCommand(Str, con)
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet
        Dim Dr As DataRow
        adp.SelectCommand = cmd
        adp.Fill(ds)

        If ds.Tables(0).Rows.Count > 0 Then
            Dr = ds.Tables(0).Rows(0)

            txtSSAID.Text = Session("SSAID").ToString
            hlDocuments1.Text = Dr("SSA_Document1")
            hlDocuments1.NavigateUrl = System.Configuration.ConfigurationManager.AppSettings("AtcoPageBase") & Dr("SSA_Document1")
            hlDocuments2.Text = Dr("SSA_Document2")
            hlDocuments2.NavigateUrl = System.Configuration.ConfigurationManager.AppSettings("AtcoPageBase") & Dr("SSA_Document2")
            hlDocuments3.Text = Dr("SSA_Document3")
            hlDocuments3.NavigateUrl = System.Configuration.ConfigurationManager.AppSettings("AtcoPageBase") & Dr("SSA_Document3")

        End If

        con.Close()
        con.Dispose()

    End Sub

    Private Function UploadDocument(ByVal FileUpload As FileUpload) As String
        Dim strFileName As String
        Dim Name As String
        strFileName = Mid(FileUpload.FileName.ToString, FileUpload.FileName.ToString.LastIndexOf("\") + 2, Len(FileUpload.FileName.ToString))

        Dim MyPath As String
        If strFileName.Trim <> "" Then
            Name = System.IO.Path.GetFileName(strFileName)
            MyPath = Server.MapPath("~/BusinessAssociates/Documents/") + Name
            If IO.File.Exists(MyPath) Then
                showmessage(Name & " is already Exists. Save it in Different Name", lblErrorData1)
                Return ""
                Exit Function
            End If
            FileUpload.SaveAs(MyPath)
            Return Name
        End If
    End Function

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim DocumentStr1, DocumentStr2, DocumentStr3 As String

        DocumentStr1 = ""
        DocumentStr2 = ""
        DocumentStr3 = ""

        If (Document1.HasFile) Then
            If Path.GetExtension(Document1.FileName).ToLower = ".pdf" Then
                DocumentStr1 = UploadDocument(Document1)
                If DocumentStr1.Trim = "" Then
                    Exit Sub
                End If
            Else
                CreateMessageAlert(Me, "Please Select .pdf File Only!", "StrKeyVal")             'Display the Message
                Document1.Focus()
                Exit Sub
            End If
        Else
            DocumentStr1 = hlDocuments1.Text
        End If

        If (Document2.HasFile) Then
            If Path.GetExtension(Document2.FileName).ToLower = ".pdf" Then
                DocumentStr2 = UploadDocument(Document2)
                If DocumentStr2.Trim = "" Then
                    Exit Sub
                End If
            Else
                CreateMessageAlert(Me, "Please Select .pdf File Only!", "StrKeyVal")             'Display the Message
                Document2.Focus()
                Exit Sub
            End If
        Else
            DocumentStr2 = hlDocuments2.Text
        End If

        If (Document3.HasFile) Then
            If Path.GetExtension(Document3.FileName).ToLower = ".pdf" Then
                DocumentStr3 = UploadDocument(Document3)
                If DocumentStr3.Trim = "" Then
                    Exit Sub
                End If
            Else
                CreateMessageAlert(Me, "Please Select .pdf File Only!", "StrKeyVal")             'Display the Message
                Document3.Focus()
                Exit Sub
            End If
        Else
            DocumentStr3 = hlDocuments3.Text
        End If

        Try
            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim con As New Data.SqlClient.SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

            cmd.CommandText = "Update_SSAProfile_Proc"
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.Connection = con

            con.Open()
            cmd.Parameters.Add("@SSAID", Data.SqlDbType.VarChar).Value = txtSSAID.Text.Trim
            cmd.Parameters.Add("@FirstName", Data.SqlDbType.NVarChar, 100).Value = ""
            cmd.Parameters.Add("@LastName", Data.SqlDbType.NVarChar, 100).Value = ""
            cmd.Parameters.Add("@DateOFBirth", Data.SqlDbType.DateTime).Value = "01/01/1999"
            cmd.Parameters.Add("@Address", Data.SqlDbType.NVarChar, 100).Value = ""
            cmd.Parameters.Add("@EmailAddress", Data.SqlDbType.NVarChar, 100).Value = ""
            cmd.Parameters.Add("@Document1", Data.SqlDbType.NVarChar, 100).Value = DocumentStr1.Trim
            cmd.Parameters.Add("@Document2", Data.SqlDbType.NVarChar, 100).Value = DocumentStr2.Trim
            cmd.Parameters.Add("@Document3", Data.SqlDbType.NVarChar, 100).Value = DocumentStr3.Trim
            cmd.Parameters.Add("@Password", Data.SqlDbType.NVarChar, 100).Value = ""
            cmd.Parameters.Add("@FormName", Data.SqlDbType.NVarChar, 100).Value = Me.Title.ToString
            cmd.Parameters.Add("@Mode", Data.SqlDbType.NVarChar, 100).Value = "UpdateDocument"

            cmd.ExecuteNonQuery()


            showmessage("Your Documents has been updated.", lblErrorData1)

            FillSSADetails()

        Catch ex As Exception
            showmessage("Please try again later.", lblErrorData1)
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub
End Class
