Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web.Security
Imports System.Data.Sql
Partial Class Admin_AdminLogin
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("sesn") = "expire" Then
            lblError.Visible = True
            lblError.Text = "Your Session has been expired. Please login again."
            'Try
            '    'InsertAdminUserLog(Session("LogKid"), Session("LogName"), Request.UserHostAddress, 1, "Admin", "AdminLogin", "Update")
            'Catch ex As Exception

            'End Try
            Exit Sub
        End If

        If Not IsPostBack Then
            Session("SupplierID") = Nothing
            Try
                If Session("AdminID") <> Nothing Then
                    InsertAdminUserLog(Session("LogKid"), Session("LogName"), Request.UserHostAddress, 1, "Admin", "AdminLogin", "Update")
                End If
                txtName.Focus()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Try

            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(txtName.Text, "Admin")
                Dim str As String = "SELECT AdminUser_Kid FROM AdminUser WHERE (AdminUser_Name = '" & uname.Trim & "') AND (AdminUser_Password = '" & Encrypt(txtpwd.Text) & "') and (AdminUser_UserTypeId = N'000007?') AND (AdminUser_ActiveFlag='Y') AND (AdminUser_IsDeleted='0')"
                'Dim str As String = "SELECT AdminUser_Kid,Supplier_AdminUserId FROM SupplierLogin WHERE (AdminUser_Name = '" & txtName.Text.Trim & "') AND (AdminUser_Password = '" & cipherText.ToString & "') and (UserType_Kid = N'000007?')"
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                con.Open()

                Dim cmd As New SqlCommand(str, con)
                Dim dread As SqlDataReader
                dread = cmd.ExecuteReader()

                If (ds.Tables(0).Rows.Count = 0) Then

                    'Dim str As String = "SELECT AdminUser_Kid FROM AdminUser WHERE (AdminUser_Name = '" & txtName.Text.Trim & "') AND (AdminUser_Password = '" & Encrypt(txtpwd.Text) & "') and (AdminUser_UserTypeId = N'000007?')"
                    ''Dim str As String = "SELECT AdminUser_Kid,Supplier_AdminUserId FROM SupplierLogin WHERE (AdminUser_Name = '" & txtName.Text.Trim & "') AND (AdminUser_Password = '" & cipherText.ToString & "') and (UserType_Kid = N'000007?')"
                    'If con.State = ConnectionState.Open Then
                    '    con.Close()
                    'End If
                    'con.Open()
                    'Dim cmd As New SqlCommand(str, con)
                    'Dim dr As SqlDataReader
                    'dr = cmd.ExecuteReader()
                    If dread.Read Then
                        Dim key As String = RemoveLiterals(NewPrimaryKey(Me).ToString.Trim)
                        InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Admin", "AdminLogin", "Insert")
                        Session.Clear()
                        Session.LCID = 3081
                        Session("AdminID") = dread.Item("AdminUser_Kid").ToString
                        Session("g_User") = dread.Item("AdminUser_Kid").ToString
                        Session("g_Company") = "00006Z4"
                        Session("g_FinYear") = "000010N"
                        lblError.Visible = False
                        Session("LogKid") = key
                        Session("LogName") = txtName.Text
                        'Session.Timeout = 90
                        Response.Redirect("AdminHome.aspx")
                    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.ToString Then
                        lblError.Visible = True
                        lblError.Text = "You are already logged in"
                        Exit Sub
                    Else
                        ' 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
                        logout = DateAdd(DateInterval.Second, -10, DateTime.Now)
                        ExecuteQuery("Update AdminUserLog set AdminUserLog_Status =1,AdminUserLog_LogoutDateTime='" & DateAdd(DateInterval.Second, -20, DateTime.Now) & "',AdminUserLog_ClientIP='" & Request.UserHostAddress & "' where AdminUserLog_Kid='" & RemoveLiterals(ds.Tables(0).Rows(0).Item(0).ToString) & "'")
                    End If
                    'Dim str As String = "SELECT AdminUser_Kid FROM AdminUser WHERE (AdminUser_Name = '" & txtName.Text.Trim & "') AND (AdminUser_Password = '" & Encrypt(txtpwd.Text) & "') and (AdminUser_UserTypeId = N'000007?')"
                    ''Dim str As String = "SELECT AdminUser_Kid,Supplier_AdminUserId FROM SupplierLogin WHERE (AdminUser_Name = '" & txtName.Text.Trim & "') AND (AdminUser_Password = '" & cipherText.ToString & "') and (UserType_Kid = N'000007?')"
                    'If con.State = ConnectionState.Open Then
                    '    con.Close()
                    'End If
                    'con.Open()
                    'Dim cmd As New SqlCommand(str, con)
                    'Dim dr As SqlDataReader
                    'dr = cmd.ExecuteReader()
                    If dread.Read Then
                        Dim key As String = RemoveLiterals(NewPrimaryKey(Me).ToString.Trim)
                        InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Admin", "AdminLogin", "Insert")
                        Session.Clear()
                        Session.LCID = 3081
                        Session("AdminID") = dread.Item("AdminUser_Kid").ToString
                        Session("g_User") = dread.Item("AdminUser_Kid").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")
                        ' Response.Redirect("AdminHome.aspx?logName=" & ds.Tables(0).Rows(0).Item(1).ToString & "&logTime=" & ds.Tables(0).Rows(0).Item(2).ToString)
                        ' Session.Timeout = 90
                        Response.Redirect("AdminHome.aspx")
                    Else
                        lblError.Visible = True
                        lblError.Text = "Invalid UserName and Password !!"
                    End If
                End If
            End If
           
        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Sub

End Class
