Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class UserControl_suuplier_right_banner
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LinkButton1.NavigateUrl = Page.ResolveUrl("~/forgotpass.aspx")

        If Page.IsPostBack = False Then
            ScriptManager1.SetFocus(txtName)
            Try
                If Session("SupplierID") <> Nothing Then
                    InsertAdminUserLog(Session("LogKid"), Session("LogName"), Request.UserHostAddress, 1, "Supplier", "SupplierLogin", "Update")
                End If
                '''' For Confirmation Email Id Directly in textbox...
                If Request.QueryString("emailId") <> Nothing Then
                    txtName.Text = DecodeURL(Request.QueryString("emailId").ToString)
                End If
            Catch ex As Exception

            End Try
            'Session.Abandon()
        End If
    End Sub
    Protected Sub imgbtnlogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnlogin.Click
        Try

            Dim ds_login As DataSet
            Dim con As New SqlConnection()
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim uname As String
            uname = txtName.Text
            If uname.Contains("'") Or uname.Contains("--") Or uname.Contains(";") Or uname.Contains(" ") Or uname.Contains("*") Or uname.Contains("where") Or uname.Contains("=") Then
                lblError.Visible = True
                lblError.Text = "Invalid UserName or Password."
                Exit Sub
            Else
                Dim ds As DataSet
                ds = AdminUserLog(uname, "Supplier")
                ds_login = SupLogin(uname.Trim, Encrypt(txtpwd.Text), "00000E6")
                If (ds.Tables(0).Rows.Count = 0) Then
                    If ds_login.Tables(0).Rows.Count <> 0 Then
                        Dim key As String = RemoveLiterals(NewPrimaryKeyUserControl(Me).ToString.Trim)
                        InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Supplier", "AdminLogin", "Insert")
                        Session.Clear()
                        Session.LCID = 3081
                        Session("SupplierID") = Nothing
                        Session("SupplierID") = ds_login.Tables(0).Rows(0).Item("Supplier_Kid").ToString
                        Session("g_User") = ds_login.Tables(0).Rows(0).Item("Supplier_AdminUserId").ToString
                        Session("g_Company") = "00006Z4"
                        Session("g_FinYear") = "000010N"
                        lblError.Visible = False
                        Session("LogKid") = key
                        con.Close()
                        Response.Redirect("List_Product.aspx?" & Session.SessionID)

                    Else
                        lblError.Visible = True
                        lblError.Text = "Invalid UserName and Password !!"
                    End If
                Else
                    If (ds.Tables(0).Rows(0).Item(5).ToString = 0) And ds.Tables(0).Rows(0).Item(4).ToString <> Request.UserHostAddress Then
                        lblError.Visible = True
                        lblError.Text = "You are already logged in"
                        Exit Sub
                    ElseIf (ds.Tables(0).Rows(0).Item(5).ToString = 0) And ds.Tables(0).Rows(0).Item(4).ToString = Request.UserHostAddress Then
                        ' Dim nextlogintime As String = ReturnValue("SELECT  TOP (1) AdminUserLog_LoginDateTime AS NextLogin  FROM AdminUserLog  WHERE  AdminUserLog_Status = '0' AND AdminUserLog_Kid = '" & RemoveLiterals(ds.Tables(0).Rows(0).Item(0).ToString) & "' AND AdminUserLog_UserName ='" & ds.Tables(0).Rows(0).Item(1).ToString & "' ORDER BY NextLogin desc")
                        Dim logout As Date
                        Dim timedef As Long
                        timedef = DateDiff(DateInterval.Hour, Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString), DateTime.Now)
                        If timedef >= 8 Then
                            logout = DateAdd(DateInterval.Minute, 50, Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString))
                        ElseIf timedef >= 4 Then
                            logout = DateAdd(DateInterval.Hour, 1, Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString))
                        ElseIf timedef >= 2 Then
                            logout = DateAdd(DateInterval.Minute, 50, Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString))
                        Else
                            logout = DateAdd(DateInterval.Second, -20, DateTime.Now)
                        End If
                        ExecuteQuery("Update AdminUserLog set AdminUserLog_Status =1,AdminUserLog_LogoutDateTime='" & logout & "',AdminUserLog_ClientIP='" & Request.UserHostAddress & "' where AdminUserLog_Kid='" & RemoveLiterals(ds.Tables(0).Rows(0).Item(0).ToString) & "'")
                    Else
                        If ds_login.Tables(0).Rows.Count <> 0 Then
                            Dim key As String = RemoveLiterals(NewPrimaryKeyUserControl(Me).ToString.Trim)
                            InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Supplier", "AdminLogin", "Insert")
                            Session.Clear()
                            Session.LCID = 3081
                            Session("SupplierID") = Nothing
                            Session("SupplierID") = Nothing
                            Session("SupplierID") = ds_login.Tables(0).Rows(0).Item("Supplier_Kid").ToString
                            Session("g_User") = ds_login.Tables(0).Rows(0).Item("Supplier_AdminUserId").ToString
                            Session("g_Company") = "00006Z4"
                            Session("g_FinYear") = "000010N"
                            lblError.Visible = False
                            Session("LogKid") = key 'ds.Tables(0).Rows(0).Item(0).ToString
                            Session("LogName") = ds.Tables(0).Rows(0).Item(1).ToString  'Response.Redirect("TestAdmin.aspx")
                            'Session("LogTime") = ds.Tables(0).Rows(0).Item(2).ToString
                            Session("LogTime") = Format$(Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString), "dd-MMM-yyyy hh:mm:ss tt")
                            con.Close()
                            'InsertAdminUserLog( Session("LogKid"), txtName.Text, Request.UserHostAddress, 0, "AdminLogin", "Update")
                            'Response.Redirect("List_Product.aspx?" & Session.SessionID & "&logName=" & ds.Tables(0).Rows(0).Item(1).ToString & "&logTime=" & ds.Tables(0).Rows(0).Item(2).ToString)
                            Response.Redirect("List_Product.aspx?" & Session.SessionID)

                        Else
                            lblError.Visible = True
                            lblError.Text = "Invalid UserName and Password !!"
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString(), lblError)
        End Try
    End Sub
    Public Function SupLogin(ByVal name As String, ByVal password As String, ByVal usertype As String) As DataSet
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet
        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("SupLogin_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminUser_Name", SqlDbType.NVarChar, 200))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminUser_Password", SqlDbType.NVarChar, 500))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminUser_UserTypeId", SqlDbType.NVarChar, 10))
            objAdapter.SelectCommand.Parameters("@AdminUser_Name").Value = name
            objAdapter.SelectCommand.Parameters("@AdminUser_Password").Value = password
            objAdapter.SelectCommand.Parameters("@AdminUser_UserTypeId").Value = usertype
            ObjDS = New DataSet
            objAdapter.Fill(ObjDS)
            Return ObjDS

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Function
End Class
