Imports EDSL.Entities
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Configuration
Imports System.Data.Common
Public Class dbMainEnquiry
    Private Shared strMainConnString As String = "MainConnString"
    Public Function GetItem(ByVal intEnquiryID As Integer) As entMainEnquiry
        Dim objEnquiry As New entMainEnquiry
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminEnquiryGet")
                db.AddInParameter(dbCommand, "EnquiryID", DbType.Int16, intEnquiryID)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    With objEnquiry
                        .EnquiryID = Convert.ToInt32(oRecordset("EnquiryID"))
                        .EnquiryName = Convert.ToString(oRecordset("EnquiryName"))
                        .BusinessName = Convert.ToString(oRecordset("BusinessName"))
                        .Ad1 = Convert.ToString(oRecordset("Ad1"))
                        .Ad2 = Convert.ToString(oRecordset("Ad2"))
                        .Ad3 = Convert.ToString(oRecordset("Ad3"))
                        .Ad4 = Convert.ToString(oRecordset("Town"))
                        .Ad5 = Convert.ToString(oRecordset("County"))
                        .Postcode = Convert.ToString(oRecordset("Postcode"))
                        .BusinessDesc = Convert.ToString(oRecordset("BusinessDesc"))

                        .EnquiryEmail = Convert.ToString(oRecordset("EnquiryEmail"))
                        .EnquiryTelephone = Convert.ToString(oRecordset("EnquiryTelephone"))
                        .EnquiryMobile = Convert.ToString(oRecordset("EnquiryMobile"))
                        .EnquiryFaxNo = Convert.ToString(oRecordset("EnquiryFax"))
                        .EnquiryMessage = Convert.ToString(oRecordset("EnquiryMessage"))
                        '.EnquiryNotes = GetEnquiryAreasOfInterest(.EnquiryID)
                        .CreatedDate = Convert.ToString(oRecordset("CreatedDate"))
                        .EnquiryStatus = Convert.ToString(oRecordset("EnquiryStatus"))

                    End With
                End While
                oRecordset.Close()
            End Using
            Return objEnquiry
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function
    Public Function GetAll(ByVal strWHEREclause As String, ByVal strORDERBYclause As String) As List(Of entMainEnquiry)
        Dim objPage As New List(Of entMainEnquiry)

        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminEnquiryGetAll")
                db.AddInParameter(dbCommand, "WHEREclause", DbType.String, strWHEREclause)
                db.AddInParameter(dbCommand, "ORDERBYclause", DbType.String, strORDERBYclause)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    Dim objEnquiry As New entMainEnquiry
                    With objEnquiry
                        .EnquiryID = Convert.ToInt32(oRecordset("EnquiryID"))
                        .EnquiryName = Convert.ToString(oRecordset("EnquiryName"))
                        .BusinessName = Convert.ToString(oRecordset("BusinessName"))
                        '.Ad1 = Convert.ToString(oRecordset("Ad1"))
                        '.Ad2 = Convert.ToString(oRecordset("Ad2"))
                        '.Ad3 = Convert.ToString(oRecordset("Ad3"))
                        .Ad4 = Convert.ToString(oRecordset("Town"))
                        '.Ad5 = Convert.ToString(oRecordset("County"))
                        '.Postcode = Convert.ToString(oRecordset("Postcode"))
                        ' .BusinessDesc = Convert.ToString(oRecordset("BusinessDesc"))

                        .EnquiryTelephone = Convert.ToString(oRecordset("EnquiryTelephone"))
                        .EnquiryMobile = Convert.ToString(oRecordset("EnquiryMobile"))
                        .EnquiryFaxNo = Convert.ToString(oRecordset("EnquiryFax"))
                        .EnquiryEmail = Convert.ToString(oRecordset("EnquiryEmail"))
                        .EnquiryMessage = Convert.ToString(oRecordset("EnquiryMessage"))
                        '.EnquiryNotes = Convert.ToString(oRecordset("EnquiryNotes"))
                        .CreatedDate = Convert.ToString(oRecordset("CreatedDate"))
                        '.EnquiryStatus = Convert.ToString(oRecordset("EnquiryStatus"))
                        '.TextDisplay = Convert.ToString(oRecordset("TextDisplay"))
                    End With
                    objPage.Add(objEnquiry)
                End While
                oRecordset.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
        Return objPage
    End Function
    Public Function DeleteItem(ByVal intID As Integer, ByVal intUserID As Integer) As Boolean
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminEnquiryDelete")
                db.AddInParameter(dbCommand, "EnquiryID", DbType.Int16, intID)
                db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)
                db.ExecuteNonQuery(dbCommand)
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Function SaveItem(ByVal intEnquiryID As Integer, ByVal strEnquiryName As String, ByVal strCompany As String, ByVal strTown As String, ByVal strCounty As String, ByVal strPostcode As String, ByVal strEnquiryEmail As String, ByVal strEnquiryTelephone As String, ByVal strEnquiryFax As String, ByVal strEnquiryMessage As String, ByVal strEnquiryNotes As String, ByVal strEnquiryStatus As String, ByVal intUserID As Integer, ByVal strEnquiryType As String) As Integer
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Dim strSPName As String = ""
        Try
            If intEnquiryID = 0 Then
                strSPName = "MainAdminEnquiryINSERT"
            Else
                strSPName = "MainAdminEnquiryUPDATE"
            End If
            Using dbCommand As DbCommand = db.GetStoredProcCommand(strSPName)
                If intEnquiryID <> 0 Then
                    db.AddInParameter(dbCommand, "EnquiryID", DbType.Int16, intEnquiryID)
                End If

                db.AddInParameter(dbCommand, "EnquiryName", DbType.String, strEnquiryName)
                db.AddInParameter(dbCommand, "EnquiryEmail", DbType.String, strEnquiryEmail)
                db.AddInParameter(dbCommand, "EnquiryTelephone", DbType.String, strEnquiryTelephone)
                db.AddInParameter(dbCommand, "EnquiryFax", DbType.String, strEnquiryFax)
                db.AddInParameter(dbCommand, "EnquiryMessage", DbType.String, strEnquiryMessage)
                db.AddInParameter(dbCommand, "EnquiryStatus", 0)
                db.AddInParameter(dbCommand, "CompanyName", DbType.String, strCompany)
                db.AddInParameter(dbCommand, "EnquiryType", DbType.String, strEnquiryType)
                db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)

                If intEnquiryID = 0 Then
                    db.AddOutParameter(dbCommand, "NewID", DbType.Int16, 255)
                End If

                db.ExecuteNonQuery(dbCommand)
                'If intEnquiryID = 0 Then
                '    Dim strNewID As String = db.GetParameterValue(dbCommand, "NewID")
                '    'Get the enquiry preferences
                '    'Removed multi select box 23-2-2011, keep just in case
                '    'For Each item In enquiryPreferences
                '    '    Dim itemIDs As String
                '    '    Dim itemArray As Array
                '    '    itemIDs = item.ToString()
                '    '    itemArray = Split(itemIDs, "|")

                '    '    Using dbCommandEnquiry As DbCommand = db.GetStoredProcCommand("MainAdminEnquiry_Types")
                '    '        db.AddInParameter(dbCommandEnquiry, "EnquiryID", DbType.Int32, strNewID)
                '    '        db.AddInParameter(dbCommandEnquiry, "CategoryID", DbType.Int32, Convert.ToInt32(itemArray(0)))
                '    '        db.AddInParameter(dbCommandEnquiry, "ParentID", DbType.Int32, Convert.ToInt32(itemArray(1)))
                '    '        db.AddInParameter(dbCommandEnquiry, "ProductID", DbType.Int32, Convert.ToInt32(itemArray(2)))
                '    '        db.ExecuteNonQuery(dbCommandEnquiry)
                '    '    End Using
                '    'Next
                '    Return strNewID  '@@IDENTITY
                'Else
                Return intEnquiryID
                'End If

            End Using
            Return 0
        Catch ex As Exception
            Return 1
        End Try
    End Function
    Public Function GetEnquiryAreasOfInterest(ByVal enquiryID As Integer) As String
        Dim areasOfInterest As String = ""

        Dim objPage As New List(Of entMainEnquiry)

        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminGetAreasOfInterest_Enquiry")
                db.AddInParameter(dbCommand, "EnquiryID", DbType.String, enquiryID)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    Dim objEnquiry As New entMainEnquiry
                    With objEnquiry
                        Dim parentID As New Integer
                        Dim subCategory As New Integer
                        Dim productID As New Integer

                        parentID = Convert.ToInt32(oRecordset("parentID"))
                        subCategory = Convert.ToInt32(oRecordset("categoryID"))
                        productID = Convert.ToInt32(oRecordset("productID"))

                        Dim dbCat As New dbMainCategory
                        Dim entCat As New entmainCategory
                        entCat = dbCat.GetItem(parentID)
                        'Get parent" 
                        areasOfInterest += entCat.CategoryName.ToString()
                        'Get sub category
                        If subCategory > 0 Then
                            entCat = dbCat.GetItem(subCategory)
                            areasOfInterest += " > " & entCat.CategoryName
                        Else
                            areasOfInterest += " > No sub category selected"
                        End If
                        If productID > 0 Then
                            Dim dbProduct As New dbMainProduct
                            Dim entProduct As New entMainProduct
                            entProduct = dbProduct.GetItem(productID)
                            areasOfInterest += " > " & entProduct.ProductName
                        Else
                            areasOfInterest += " > No product selected"
                        End If

                        areasOfInterest += "<br >"

                    End With

                End While
                oRecordset.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
        Return areasOfInterest
    End Function
End Class
