Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web
Imports System.Net.Mail

Partial Class suppliers_ChangePassword
    Inherits System.Web.UI.Page
    Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
    Dim str As String
    Dim cipher_old As String
    Dim adminuserid As String
    Dim cipher_new As String

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim strIp As String = Request.UserHostAddress()

            cipher_old = Encrypt(txtOldpassword.Text)
            cipher_new = Encrypt(txtnewpassword.Text)

            'will return supplieradminuserid
            adminuserid = ReturnValue("SELECT Supplier_AdminUserId FROM Supplier_Master WHERE Supplier_Kid = '" & Session("SupplierId").ToString & "' AND Supplier_IsDeleted = '0'")
            Dim strOldPwd As String
            strOldPwd = ReturnValue("select AdminUser_Password from AdminUser where AdminUser_Kid='" + adminuserid + "'")
            'username will return
            str = ReturnValue("select AdminUser_Name from SupplierLogin where AdminUser_Password='" & cipher_old & "' and Supplier_Kid='" & Session("SupplierId").ToString() & "' and Supplier_IsDeleted='0' and AdminUser_UserTypeId='00000E6'")
            ViewState("UserName") = str


            If str <> "" Then
                If txtnewpassword.Text.Length > 5 Then

                    Dim con As New SqlConnection
                    con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
                    Dim cmd As New SqlCommand
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.CommandText = "SupplierChangePassword_Proc"                                        'Assign the Name of Stored Procedure, to Execute
                    cmd.Connection = con
                    cmd.Parameters.Add(New SqlParameter("@AdminUser_Kid", SqlDbType.NVarChar, 10)).Value = adminuserid
                    cmd.Parameters.Add(New SqlParameter("@AdminUser_Name", SqlDbType.NVarChar, 200)).Value = str
                    cmd.Parameters.Add(New SqlParameter("@AdminUser_Password", SqlDbType.NVarChar, 500)).Value = cipher_new
                    cmd.Parameters.Add(New SqlParameter("@AdminUser_UserTypeId", SqlDbType.NVarChar, 10)).Value = "00000E6"
                    cmd.Parameters.Add(New SqlParameter("@AdminUser_ClientIP", SqlDbType.NVarChar, 50)).Value = strIp
                    cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "ChangePassword"
                    cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Update"
                    con.Open()
                    cmd.ExecuteNonQuery()
                    sentmail()
                    lblMessage.Text = "Password Updated Successfully"
                    txtOldpassword.Text = ""
                    txtnewpassword.Text = ""
                    txtconfirmpassword.Text = ""
                    con.Close()
                    '''''''''''''''''' for maintain password history
                    Dim strCount As String
                    strCount = ReturnValue("select count('SupplierUpdate_FieldName') from SupplierUpdate_History where SupplierUpdate_Id='" & Session("SupplierId").ToString & "' and SupplierUpdate_FieldName='Password'")
                    Dim cmdNew As New SqlCommand
                    cmdNew.CommandType = CommandType.StoredProcedure
                    cmdNew.CommandText = "Password_History"                                        'Assign the Name of Stored Procedure, to Execute
                    cmdNew.Connection = con
                    If strCount = "0" Then
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_Id", SqlDbType.NVarChar, 10)).Value = Session("SupplierId")
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_TableName", SqlDbType.NVarChar, 100)).Value = "AdminUser"
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_FieldName", SqlDbType.NVarChar, 200)).Value = "Password"
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_OldValue", SqlDbType.NVarChar, 2000)).Value = ""
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_NewValue", SqlDbType.NVarChar, 2000)).Value = cipher_new
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_UpdateDate", SqlDbType.DateTime)).Value = System.DateTime.Now
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_UpdatedBy", SqlDbType.NVarChar, 10)).Value = Session("g_User").ToString
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_UserIP", SqlDbType.NVarChar, 50)).Value = strIp
                        cmdNew.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar, 10)).Value = "Insert"
                    ElseIf strCount = "1" Then
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_Id", SqlDbType.NVarChar, 10)).Value = Session("SupplierId")
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_OldValue", SqlDbType.NVarChar, 2000)).Value = strOldPwd
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_NewValue", SqlDbType.NVarChar, 2000)).Value = cipher_new
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_UpdateDate", SqlDbType.DateTime)).Value = System.DateTime.Now
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_UpdatedBy", SqlDbType.NVarChar, 10)).Value = Session("g_User").ToString
                        cmdNew.Parameters.Add(New SqlParameter("@SupplierUpdate_UserIP", SqlDbType.NVarChar, 50)).Value = strIp
                        cmdNew.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar, 10)).Value = "Update"
                    End If
                    con.Open()
                    cmdNew.ExecuteNonQuery()
                    con.Close()
                    ''''''''''''''''''''''''
                    'mail sent to the supplier
                    CreateMessageAlert(Me, "Your New Password is Sent to Your E-mail ID !", "strkeyval")
                Else
                    lblMessage.Text = "New Password Must have 6 or moer than 6 Characters !"
                End If
            Else
                lblMessage.Text = "Old Password is Wrong !"
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        txtOldpassword.Text = ""
        txtnewpassword.Text = ""
        txtconfirmpassword.Text = ""
    End Sub

    Protected Sub sentmail()
        Try

       
            Dim password As String
            ' password = ReturnValue("Select AdminUser_Password from AdminUser where AdminUser_Name = '" & ViewState("UserName") & "'")
            password = txtnewpassword.Text
            Dim body As String

            body = "Dear, Supplier <br> Your Username and Password is as below.<br><br>"
            body += "<b> User Name :  " & ViewState("UserName") & "</b><br>"
            body += "<b> New PassWord  :  " & password & "</b>"
            body += "<br><br><br><br><br><br><br><br><br> From, <br> Atcomaart Ltd."


            'Dim MyMailMessage As New MailMessage()

            ''From requires an instance of the MailAddress type

            'MyMailMessage.From = New MailAddress("contact@atcomaart.com", "Atcomaart Ltd.")

            ''To is a collection of MailAddress types

            'MyMailMessage.To.Add(ViewState("UserName"))

            ''MyMailMessage.CC.Add(New MailAddress("pankajmishra2006@gmail.com", "CC Name"))

            ''  MyMailMessage.Bcc.Add(New MailAddress("pankaj.mishra@edeskservices.com", "eDesk Services Ltd."))

            'MyMailMessage.Subject = "Your New Password for Atcomaart Product Listing"
            'MyMailMessage.Body = body
            'MyMailMessage.IsBodyHtml = True
            'MyMailMessage.Priority = MailPriority.High
            'Dim smtpclient As New SmtpClient("smtp.rediffmailpro.com")
            'smtpclient.Credentials = New Net.NetworkCredential("contact@atcomaart.com", "traamocta")
            'smtpclient.Send(MyMailMessage)

            MailSender.SendEmail("contact@atcomaart.com", "contact_64543", ViewState("UserName").ToString.Trim, "Your New Password for Atcomaart Product Listing", body, Web.Mail.MailFormat.Html, "")

            ''Dim msg As New System.Net.Mail.MailMessage("contact@atcomaart.com", ViewState("UserName"))
            ''msg.Subject = "Your New Password for Atcomaart Product Listing"
            ''msg.Body = body
            ''msg.IsBodyHtml = True

            ''Dim sc As New SmtpClient
            ''sc.Host = "smtp.rediffmailpro.com"
            ''sc.UseDefaultCredentials = False
            ''sc.EnableSsl = True
            ''sc.Credentials = New System.Net.NetworkCredential("smt.pillai@gmail.com", "smita123")
            ''sc.Send(msg)

        Catch ex As Exception

        End Try
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("SupplierID") Is Nothing And Session("AdminID") = Nothing Then
                Response.Redirect("listproduct.aspx?sess=expire")
                Exit Sub
            End If

            If (Not IsPostBack) Then        'If IsPostBack is false then
                If Session("SupplierID") Is Nothing Then
                    Response.Redirect("listProduct.aspx")
                    Exit Sub
                End If
                Label1.Text = ReturnValue("select  AdminUser_Name from SupplierLogin where Supplier_Kid='" & Session.Item("SupplierID").ToString & "' and Supplier_IsDeleted='0' and AdminUser_UserTypeId='00000E6'")
            End If
        Catch ex As Exception
        End Try

    End Sub
End Class
