Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.EnterpriseServices
Imports System.Drawing
Imports System.Web.HttpWriter
Imports System.Web.Security
Imports System.Resources.Tools
Imports System.Configuration
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.Sql
Imports System.Text

Partial Class Supplier_Login
    Inherits System.Web.UI.Page
    Shared kid, kidd As String
    Shared str, strr As String
    Shared strSupplier, StrAdmin, stradminuserid As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtpwd.Attributes.Add("OnKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + imgbtnlogin.UniqueID + "','')")
        Page.Form.DefaultButton = imgbtnlogin.UniqueID
        If Request.QueryString("sess") = "expire" Then
            lblError.Visible = True
            lblError.Text = "Your Session has been expired.<br/> Please login again."
            Try
                'InsertAdminUserLog(Session("LogKid"), Session("LogName"), Request.UserHostAddress, 1, "Supplier", "SupplierLogin", "Update")
            Catch ex As Exception
                Response.Write(ex.Message.ToString & "<br/>Please Login Again.")
            End Try

            Exit Sub
        End If

        If Page.IsPostBack = False Then
            lblError.Text = ""
            ' Session.Abandon()
            txtName.Focus()
            Session("AdminID") = Nothing
            Try
                If Session("SupplierID") <> Nothing Then
                    InsertAdminUserLog(Session("LogKid"), Session("LogName"), Request.UserHostAddress, 1, "Supplier", "SupplierLogin", "Update")
                End If
            Catch ex As Exception

            End Try




        End If
    End Sub

    Protected Sub imgbtnlogin_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles imgbtnlogin.Command
        Try
            ' Session.Timeout = 20
            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(NewPrimaryKey(Me).ToString.Trim)
                        InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Supplier", "SupplierLogin", "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
                        Session("LogName") = txtName.Text
                        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(NewPrimaryKey(Me).ToString.Trim)
                            InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Supplier", "SupplierLogin", "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
                            con.Close()
                            Session("LogKid") = key 'ds.Tables(0).Rows(0).Item(0).ToString
                            Session("LogName") = ds.Tables(0).Rows(0).Item(1).ToString
                            Session("LogTime") = Format$(Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString), "dd-MMM-yyyy hh:mm:ss tt")
                            'InsertAdminUserLog(Session("LogKid"), txtName.Text, Request.UserHostAddress, 0, "AdminLogin", "Update")

                            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
            Response.Write(ex.Message.ToString & "<br/>Please Login Again.")
        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
