Imports System.Data
Imports System.Data.SqlClient
Partial Class BusinessAssociates_SSA_DiscussionThread
    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
            If Not Session("SSAID") = Nothing Then
                txtVCompanyID.Text = "f6b59677-9946-489c-8628-465872369428"
                txtSSAID.Text = Session("SSAID").ToString
                txtLeadID.Text = Request("ID").ToString
                SelectCustomerByCustomerID(txtLeadID.Text.ToString)
                FillGrid()
            End If
        End If
    End Sub

    Public Sub SelectCustomerByCustomerID(ByVal CustomerID As String)
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("Select_CRMCustomerByCustomerID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CustomerID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@CustomerID").Value = CustomerID.ToString

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "CustomerDetails")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                lblName.Text = ObjDS.Tables(0).Rows(0).Item("V_CRM_ContactPerson")
                lblCompanyName.Text = ObjDS.Tables(0).Rows(0).Item("V_CRM_CompanyName")
            End If

        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Public Sub FillGrid()
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("Select_CRMDiscussionThreadByLeadIDAndDiscussionMode_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@DiscussionMode", SqlDbType.Int))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CustomerID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@DiscussionMode").Value = -1
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim
            objAdapter.SelectCommand.Parameters("@CustomerID").Value = txtLeadID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "DiscussionThread")

            gvDiscussionThread.DataSource = ObjDS
            gvDiscussionThread.DataBind()

            ObjDS.Clear()
            ObjDS.Dispose()

            Dim J As Integer

            For J = 0 To gvDiscussionThread.Rows.Count - 1

                If gvDiscussionThread.Rows(J).Cells(3).Text = "1" Then
                    gvDiscussionThread.Rows(J).Cells(4).Text = "Email"
                ElseIf gvDiscussionThread.Rows(J).Cells(3).Text = "2" Then
                    gvDiscussionThread.Rows(J).Cells(4).Text = "Phone"
                ElseIf gvDiscussionThread.Rows(J).Cells(3).Text = "3" Then
                    gvDiscussionThread.Rows(J).Cells(4).Text = "Personal Visit"
                End If

                If gvDiscussionThread.Rows(J).Cells(6).Text = "00000000-0000-0000-0000-000000000000" Then
                    gvDiscussionThread.Rows(J).Cells(8).Text = FillAdminDetails(gvDiscussionThread.Rows(J).Cells(7).Text.ToString) & " (Admin)"
                ElseIf gvDiscussionThread.Rows(J).Cells(7).Text = "00000000-0000-0000-0000-000000000000" Then
                    gvDiscussionThread.Rows(J).Cells(8).Text = FillSSADetails(gvDiscussionThread.Rows(J).Cells(6).Text.ToString) & " (SSA)"
                End If
            Next

        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Public Function FillAdminDetails(ByVal AdminID As String) As String

        Dim Name As String

        Name = ""

        Dim Dr As DataRow
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("Select_EmployeeByAdminID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@AdminID").Value = AdminID.ToString
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.ToString

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "EmployeeDetails")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                Dr = ObjDS.Tables(0).Rows(0)
                Name = Dr("V_Emp_Name")
            End If

        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try

        Return Name

        ObjDS.Clear()
        ObjDS.Dispose()

    End Function

    Public Function FillSSADetails(ByVal SSAID As String) As String

        Dim Name As String

        Name = ""
        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='" & 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)
            Name = Dr("SSA_FirstName") & "  " & Dr("SSA_LastName")
        End If

        Return Name
        ds.Clear()
        ds.Dispose()

        con.Close()
        con.Dispose()

    End Function


    Protected Sub gvDiscussionThread_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvDiscussionThread.PageIndex = e.NewPageIndex
        FillGrid()
    End Sub

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim cmdQuotation As New Data.SqlClient.SqlCommand()
        Dim con As New Data.SqlClient.SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("Connection4Voffice")

        Try
            cmdQuotation.CommandText = "Insert_CRMDiscussionThread_Proc"
            cmdQuotation.CommandType = Data.CommandType.StoredProcedure
            cmdQuotation.Connection = con

            con.Open()

            cmdQuotation.Parameters.Add("@DiscussionID", Data.SqlDbType.VarChar).Value = System.Guid.NewGuid.ToString
            cmdQuotation.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = txtLeadID.Text.Trim
            cmdQuotation.Parameters.Add("@DiscussionMode", SqlDbType.Decimal).Value = ddlDiscussionMode.SelectedValue
            cmdQuotation.Parameters.Add("@DiscussionDateTime", SqlDbType.DateTime).Value = System.DateTime.Now
            cmdQuotation.Parameters.Add("@Discussion", SqlDbType.Text).Value = txtDiscussion.Text.Trim
            cmdQuotation.Parameters.Add("@CompanyID", SqlDbType.VarChar).Value = txtVCompanyID.Text.Trim
            cmdQuotation.Parameters.Add("@SSAID", SqlDbType.VarChar).Value = txtSSAID.Text.Trim
            cmdQuotation.Parameters.Add("@AdminID", SqlDbType.VarChar).Value = "00000000-0000-0000-0000-000000000000"
            cmdQuotation.Parameters.Add("@FormName", SqlDbType.NVarChar, 50).Value = "DiscussionThread"
            cmdQuotation.Parameters.Add("@Mode", SqlDbType.VarChar, 10).Value = "Insert"

            cmdQuotation.ExecuteNonQuery()

            con.Close()
            con.Dispose()
            showmessage("Record Added Successfully.", lblErrorData)

            FillGrid()

        Catch ex As Exception
            showmessage("Please try again later.", lblErrorData)
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub

    Protected Sub gvDiscussionThread_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDiscussionThread.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(1).Text = Format$(Convert.ToDateTime(e.Row.Cells(1).Text), "MMM-dd-yyyy")
            e.Row.Cells(2).Text = Format$(Convert.ToDateTime(e.Row.Cells(2).Text), "hh:mm:ss tt")
        End If
    End Sub
End Class
