Imports System
Imports System.Data
Imports System.Data.SqlClient
'Imports System.Net.Mail
Imports System.Web.Mail


Partial Class product_alert
    Inherits System.Web.UI.Page
    Dim catid As String
    Dim rootcatid As String
    Dim parid As String
    Dim dt As DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        g_User = "U1"
        g_Company = "00006Z4"

        If Not IsPostBack Then
            txtName.Text = ""
            txtEmail.Text = ""
            lblMsg.Text = ""
            txtName.Focus()
            btnClose.Attributes.Add("onclick", "javascript:window.close();")
            tblmsg.Visible = False
            tblalert.Visible = True
        End If


        If Request.QueryString("product-code") <> Nothing Then

            Session("ProductAlertCode") = DecodeURL(RemoveLiterals(Request.QueryString("product-code")))
       
            fillcategory()
        End If
    End Sub

    Protected Sub fillcategory()
        Try

            dt = ReturnDataTable("Select Product_Name,Product_CategoryId From Product_Master where Product_Kid = '" & RemoveLiterals(Session("ProductAlertCode").ToString.Trim) & "' and Product_IsDeleted ='0'")
            lblproduct.Text = dt.Rows(0).Item("Product_Name")
            catid = dt.Rows(0).Item("Product_CategoryId")
            lblcategory.Text = ReturnValue("SELECT Category_Name FROM Category_Master WHERE  Category_Kid = '" & RemoveLiterals(dt.Rows(0).Item("Product_CategoryId")) & "' AND Category_IsDeleted='0'")

            parid = ReturnValue("Select Category_ParentId from Category_Master where Category_Kid='" & RemoveLiterals(dt.Rows(0).Item("Product_CategoryId")) & "'")

            rootcatid = ReturnValue("SELECT Category_Master.Category_ParentId  FROM   Category_Master INNER JOIN  Category_Master AS Category_Master_1 ON Category_Master.Category_ParentId = Category_Master_1.Category_Kid WHERE  (Category_Master.Category_IsDeleted = '0') AND (Category_Master.Category_Kid = '" & RemoveLiterals(parid.ToString) & "')")
        Catch ex As Exception

        End Try
    End Sub
   

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try

            If txtEmail.Text = "" Then
                lblMsg.Text = "Please Enter E-mail ID."
                Exit Sub
            End If

            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                       'Open the Connection

            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "AlertReceive"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con

            cmd.Parameters.Add(New SqlParameter("@Receive_Kid", SqlDbType.NVarChar, 20)).Value = NewPrimaryKey(Me)
            cmd.Parameters.Add(New SqlParameter("@Receive_Email", SqlDbType.NVarChar, 200)).Value = txtEmail.Text
            cmd.Parameters.Add(New SqlParameter("@Receive_CatCode", SqlDbType.NVarChar, 50)).Value = rootcatid.ToString
            cmd.Parameters.Add(New SqlParameter("@Receive_Sub2CatCode", SqlDbType.NVarChar, 50)).Value = catid.ToString
            cmd.Parameters.Add(New SqlParameter("@Receive_date", SqlDbType.DateTime)).Value = Date.Today
            cmd.Parameters.Add(New SqlParameter("@Receive_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(g_Company)
            cmd.Parameters.Add(New SqlParameter("@Receive_UserId", SqlDbType.NVarChar, 20)).Value = Trim(g_User)
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Receive_Alert"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Insert"

            cmd.ExecuteNonQuery()
            lblMsg.Text = "Record Saved Successfully..."
            Sendmail()
            lblemail.Text = txtName.Text
            txtEmail.Text = ""
            txtName.Text = ""
            tblmsg.Visible = True
            tblalert.Visible = False
        Catch ex As Exception

        End Try
    End Sub
    Public Sub Sendmail()
        Try
            Dim StrMail As String = ""
            StrMail = "<div style='font-family:Arial;FONT-SIZE: 10pt;'>"
            StrMail += "Dear ,<b> " & txtName.Text & "</b><br/>"
            StrMail += "Thanks for using the services of AtcoMaart Product Alert <br/> "
            StrMail += "<b>Receive Alerts for the Product & Category :</b><br/> "
            StrMail += "<table width='600' border='1' cellspacing='0' cellpadding='1' align='left' style='BORDER-RIGHT: dimgray 1px solid; position:fixed ; BORDER-TOP: dimgray 1px solid; FONT-SIZE: 10pt; BORDER-LEFT: dimgray 1px solid;  BORDER-BOTTOM: dimgray 1px solid; FONT-FAMILY: Arial;BACKGROUND-COLOR: #f8f8f8'>"
            StrMail += "<tr> <td><br/><b> Receive Alert for Products: </b></td>"
            StrMail += "<td>" & lblproduct.Text & "</td>"
            StrMail += "</tr>"

            StrMail += "<tr> <td><br/><b> Receive Alert for Category: </b></td>"
            StrMail += "<td>" & lblcategory.Text & "</td>"
            StrMail += "</tr>"

            StrMail += "</table><br/><br/><br/><br/><br/><br/><br/><br/>Have a nice day.<br/>"
            StrMail += "Thanks & Regards,<br/>ATCOMAART TEAM<br/><br/><a href='www.atcomaart.com'> (www.atcomaart.com)</a>"
            StrMail += "</Div>"

            'Dim mailmsg As New MailMessage()
            'mailmsg.From = New MailAddress("contact.atcomaart@atco.in", "AtcoMaart Ltd.")
            'mailmsg.To.Add(New MailAddress(txtEmail.Text, txtEmail.Text))
            'mailmsg.IsBodyHtml = True
            'mailmsg.Priority = MailPriority.High
            'mailmsg.Body = StrMail
            'mailmsg.Subject = "Product Alert Services From AtcoMaart"
            'Dim smtp As New SmtpClient("smtp.rediffmailpro.com")
            'smtp.Credentials = New Net.NetworkCredential("contact.atcomaart@atco.in", "contact@55017_atco")
            'smtp.Send(mailmsg)
            MailSender.SendEmail(txtEmail.Text, "Product Alert Services From AtcoMaart", StrMail, MailFormat.Html, "")

        Catch ex As Exception

        End Try
    End Sub

   
End Class
