Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Partial Class product_it_inquiry
    Inherits System.Web.UI.Page
    Shared Category(10) As String
    Shared i As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Response.Write(Session("ProductCode"))
            Session("ProductCode") = Request.QueryString("StID").ToString
            txtCompanyName.Focus()
            i = 0
            Dim catid As String
            catid = ReturnValue("Select Product_CategoryID from Product_Master where Product_Kid= '00016QL'and Product_IsDeleted='0'")
            ' get categoryid according productid
            FillCategoryName(catid)
            getlbl()
            btnCancel.Attributes.Add("onclick", "javascript:window.close();")
            Fill_Combo("SELECT distinct Unit_Kid, Unit_Name, Unit_Flag FROM Unit_Master WHERE Unit_Flag = N'Qty' and unit_IsDeleted='0' order by unit_name", ddlQtyType)

            Image1.Attributes.Add("onmouseOver", "javascript:over(this)")
            btnClose.Attributes.Add("onclick", "javascript:window.close();")

            txtCountryCode.Attributes.Add("onkeypress", "return CheckNumericValue(event);")
            txtCityCode.Attributes.Add("onkeypress", "return CheckNumericValue(event);")
            txtPhoneNo.Attributes.Add("onkeypress", "return CheckNumericValue(event);")
            txtCountryCodeM.Attributes.Add("onkeypress", "return CheckNumericValue(event);")
            txtMobileNo.Attributes.Add("onkeypress", "return CheckNumericValue(event);")
            txtQtyRequired.Attributes.Add("onkeypress", "return CheckNumericValue(event);")

            txtCityCode.Attributes.Add("onkeydown", "return testmobile(document.getElementById('txtCountryCode'));")
            txtMobileNo.Attributes.Add("onkeydown", "return testmobile(document.getElementById('txtCountryCodeM'));")
            txtPhoneNo.Attributes.Add("onkeydown", "return testphone(document.getElementById('txtCountryCode'),document.getElementById('txtCityCode'));")

            txtCountryCode.Text = "91"
            txtCountryCodeM.Text = "91"
            tbl1.Visible = False
            'Image1.Attributes.Add("onmouseOut", "javascript:out(this)")
        End If
        lblMsg.Visible = False
    End Sub
    Public Sub getlbl()
        lblProductName.Text = ReturnValue("Select Product_Name From Product_Master where Product_Kid = '00016QL' and Product_IsDeleted ='0'")
        lblProductCode.Text = ReturnValue("Select Product_Code From Product_Master where Product_Kid = '00016QL' and Product_IsDeleted ='0'")
        lblModelNo.Text = ReturnValue("Select Product_ModelNo From Product_Master where Product_Kid = '00016QL' and Product_IsDeleted ='0'")

    End Sub
    Public Sub FillCategoryName(ByVal Catid As String)
        ' Fill category herarchy in label
        Dim dt As DataTable
        dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0'")
        Dim ParentId As String
        ParentId = dt.Rows(0).Item(3).ToString()
        If ParentId = "Root Category" Then
            Dim j As Integer
            Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
            For j = 0 To i
                lblCat.Text += Category(i)
                i = i - 1
            Next
            Exit Sub
        Else
            Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
            i = i + 1
            FillCategoryName(dt.Rows(0).Item(3).ToString())
        End If
        dt.Clear()
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            'If CheckValid() = True Then
            ' lblError.Visible = False

            If txtQtyRequired.Text <> "" Then
                If ddlQtyType.SelectedIndex = 0 Then
                    showmessage("Please Select Unit.", lblMsg)
                    ScriptManager1.SetFocus(ddlQtyType)
                    Exit Sub
                End If
            End If

            If txtRequiredDate.Text <> "" Then
                Try
                    Dim dt As DateTime
                    dt = Format$(Convert.ToDateTime(txtRequiredDate.Text.ToString), "")

                    If dt <= DateTime.Now Then
                        showmessage("Please Select Proper Future Date.", lblMsg)
                        ScriptManager1.SetFocus(txtRequiredDate.Text)
                        Exit Sub
                    End If

                Catch ex As Exception
                    showmessage("Please Enter Correct Date.", lblMsg)
                    ScriptManager1.SetFocus(txtRequiredDate.Text)
                    Exit Sub
                End Try

            End If

            If txtPhoneNo.Text = "" And txtMobileNo.Text = "" And txtEmailID.Text = "" Then
                showmessage("Plese Give Atleast One Contact Details. Like Phone No, Mobile No Or EmailID", lblMsg)
                Exit Sub
            End If


            If chkcall.Checked = True Then
                If txtPhoneNo.Text = "" And txtMobileNo.Text = "" Then
                    showmessage("Plese Give Atleast One Contact No. For Contact.", lblMsg)
                    Exit Sub
                End If
            End If

            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim cmd As New SqlCommand
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "Product_Specific_Inquiry_Proc" ' storeprocedure
            cmd.Connection = con

            cmd.Parameters.Add(New SqlParameter("@PSE_Kid", SqlDbType.NVarChar, 10)).Value = NewPrimaryKey(Me)
            cmd.Parameters.Add(New SqlParameter("@PSE_Code", SqlDbType.NVarChar, 25)).Value = GenerateCode()
            cmd.Parameters.Add(New SqlParameter("@PSE_ProductID", SqlDbType.NVarChar, 10)).Value = "00016QL"
            cmd.Parameters.Add(New SqlParameter("@PSE_CompanyName", SqlDbType.NVarChar, 200)).Value = txtCompanyName.Text
            cmd.Parameters.Add(New SqlParameter("@PSE_ContactPersonName", SqlDbType.NVarChar, 200)).Value = Trim(txtContactPersonName.Text)

            cmd.Parameters.Add(New SqlParameter("@PSE_PhoneCountryCode", SqlDbType.NVarChar, 10)).Value = Trim(txtCountryCode.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_PhoneCityCode", SqlDbType.NVarChar, 10)).Value = Trim(txtCityCode.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_PhoneNo", SqlDbType.NVarChar, 20)).Value = Trim(txtPhoneNo.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_MobileCountryCode", SqlDbType.NVarChar, 10)).Value = Trim(txtCountryCodeM.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_MobileNo", SqlDbType.NVarChar, 20)).Value = Trim(txtMobileNo.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_EmailId", SqlDbType.NVarChar, 50)).Value = Trim(txtEmailID.Text)

            cmd.Parameters.Add(New SqlParameter("@PSE_QtyRequired", SqlDbType.NVarChar, 10)).Value = Trim(txtQtyRequired.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_QtyRequiredUnit", SqlDbType.NVarChar, 10)).Value = Trim(ddlQtyType.SelectedItem.Value.ToString)
            cmd.Parameters.Add(New SqlParameter("@PSE_RequiredByDate", SqlDbType.NVarChar, 50)).Value = Trim(txtRequiredDate.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_RequiredPlace", SqlDbType.NVarChar, 200)).Value = Trim(txtRequiredPlace.Text)
            cmd.Parameters.Add(New SqlParameter("@PSE_SpecificRequirement", SqlDbType.NVarChar, 200)).Value = Trim(txtSpecificRequirement.Text)

            If chkcall.Checked = True Then
                cmd.Parameters.Add(New SqlParameter("@PSE_FlagCall", SqlDbType.NVarChar, 1)).Value = "Y"
            Else
                cmd.Parameters.Add(New SqlParameter("@PSE_FlagCall", SqlDbType.NVarChar, 1)).Value = "N"
            End If
            cmd.Parameters.Add(New SqlParameter("@PSE_Status", SqlDbType.NVarChar, 50)).Value = "Pending"

            cmd.Parameters.Add(New SqlParameter("@PSE_FinancialYearId", SqlDbType.NVarChar, 10)).Value = ""
            cmd.Parameters.Add(New SqlParameter("@PSE_CompanyId", SqlDbType.NVarChar, 10)).Value = ""
            cmd.Parameters.Add(New SqlParameter("@PSE_UserId", SqlDbType.NVarChar, 10)).Value = ""
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "Product Specidic Inquiry"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar, 10)).Value = "Insert" ' mode
            If con.State = ConnectionState.Open Then ' check connection state
                con.Close()
            End If
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close() ' close connection
            con.Dispose()
            sendmail()
            'tbl.Visible = False
            'tbl1.Visible = True
        Catch ex As Exception
            showmessage(ex.Message, lblMsg)                                'Display the Error Message
        End Try
    End Sub
    Public Sub sendmail()
        Try


            'Dim msg As New MailMessage
            'If txtEmailID.Text = "" Then
            '    msg.From = "contact@atcomaart.com"

            'Else
            '    msg.From = "contact@atcomaart.com"
            'End If

            '' msg.To = "pankajmishra2006@gmail.com"
            'msg.To = "contact@atcomaart.com"
            Dim lt As New Label
            lt.Text = "<h1> Inquiry From " & txtEmailID.Text & " </h1> </br>"
            lt.Text += lblCat.Text
            lt.Text += "<br/>Product Code :    "
            lt.Text += "<b>" & lblProductCode.Text & "</b>"

            lt.Text += "<br/>Product Name :    "
            lt.Text += "<b>" & lblProductName.Text & "</b>"

            lt.Text += "<br/>Product ModelNo. :    "
            lt.Text += "<b>" & lblModelNo.Text & "</b>"

            lt.Text += "<br/>Company Name :    "
            lt.Text += "<b>" & txtCompanyName.Text & "</b>"

            lt.Text += "<br/>Company Contact Person Name :    "
            lt.Text += "<b>" & txtContactPersonName.Text & "</b>"

            If txtCityCode.Text = "" And txtPhoneNo.Text = "" Then
            Else
                lt.Text += "<br/>Phone No :    "
                lt.Text += "<b>" & txtCountryCode.Text & "-" & txtCityCode.Text & "-" & txtPhoneNo.Text & "</b>"
            End If

            If txtMobileNo.Text <> "" Then
                lt.Text += "<br/>Mobile No :    "
                lt.Text += "<b>" & txtCountryCodeM.Text & "-" & txtMobileNo.Text & "</b>"
            End If

            If txtEmailID.Text = "" Then
                lt.Text += "<br/>Email ID :    "
                lt.Text += "<b>" & txtEmailID.Text & "</b>"
            End If

            If txtQtyRequired.Text <> "" Then
                lt.Text += "<br/>Quantity Required :    "
                lt.Text += "<b>" & txtQtyRequired.Text & "   " & ddlQtyType.SelectedItem.Text & "</b>"
            End If

            If txtRequiredDate.Text <> "" Then
                lt.Text += "<br/>Required By Date :    "
                lt.Text += "<b>" & txtRequiredDate.Text & "</b>"
            End If

            If txtRequiredPlace.Text <> "" Then
                lt.Text += "<br/>Place At Which Required :    "
                lt.Text += "<b>" & txtRequiredPlace.Text & "</b>"
            End If

            If txtSpecificRequirement.Text <> "" Then
                lt.Text += "<br/>Specific Requirement :    "
                lt.Text += "<b>" & txtSpecificRequirement.Text & "</b>"

            End If

            If chkcall.Checked = True Then
                lt.Text += "<br/>Call Me :    "
                lt.Text += "<b> Please Call Me. </b>"
            Else
                lt.Text += "<br/>Call Me :    "
                lt.Text += "<b> NO </b>"
            End If
            Dim mail As New System.Net.Mail.MailMessage("contact@atcomaart.com", "pankajmishra2006@gmail.com", "Product Specific Inquiry.", lt.Text)
            mail.IsBodyHtml = True
            mail.Priority = Net.Mail.MailPriority.High
            Dim smtpclient As New System.Net.Mail.SmtpClient("smtp.rediffmailpro.com")
            smtpclient.Credentials = New System.Net.NetworkCredential("contact@atcomaart.com", "passw123")
            smtpclient.Send(mail)
            'msg.BodyFormat = MailFormat.Html
            'msg.Body = lt.Text
            'msg.Subject = "Product Specific Inquiry."

            ''SmtpMail.SmtpServer = ""

            'SmtpMail.SmtpServer = "smtp.rediffmailpro.com"
            '' Dim i As System.Web.Mail


            'SmtpMail.Send(msg)


            'Dim message As New System.Net.Mail.MailMessage



            'mail.From = "jjjitiya@gmail.com"
            'mail.To = "pankajmishra2006@gmail.com"
            'message.Body = "TTest ...."
            'message.Subject = "Subject"
            ''SmtpMail.SmtpServer = "kalpesh.Atcomaart.local"
            ''SmtpMail.Send(mail)
            'Dim smtp As New System.Net.Mail.SmtpClient
            'smtp.Host = "smtp.rediffmailpro.com"
            'smtp.Send(message)
            tbl.Visible = False
            tbl1.Visible = True
        Catch ex As Exception
            showmessage(ex.Message, lblMsg)
        End Try
    End Sub

    Private Function GenerateCode() As String
        ' generate kid and code
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            'txtBranchNetworkKid.Text = Trim(NewPrimaryKey(Me))
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "PSI" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "Select count(*) from Product_Specific_Inquiry where PSE_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = (Val(i) + 1).ToString
                    i = Format(Val(i), "000")
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Function
End Class
