﻿Imports System.Data
Imports System.Data.SqlClient
Imports HashLibrary

Partial Class admin_login
    Inherits System.Web.UI.Page
    Public ErrorText As String = ""

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ErrorText = ""
        Session("Key") = ConfigurationManager.AppSettings("KEY").ToString
      If Not Page.IsPostBack Then
         If Not IsNothing(Request.QueryString("logout")) Then
            If Request.QueryString("logout") = 1 Then
               functions.ClearSessions()
               Response.Redirect("admin_login.aspx")
            End If
         End If

         If Not IsNothing(Session("userid")) Then
            Response.Redirect("default.aspx")
         End If

         functions.ClearSessions()

         Dim cookie As HttpCookie = Request.Cookies("UserInfo")
         If Not cookie Is Nothing AndAlso Not cookie("email") Is Nothing Then
            email2.Value = cookie("email")
            RememberMe.Checked = True
         End If
      End If
   End Sub

   Private Sub btnSignIn_ServerClick(sender As Object, e As EventArgs) Handles btnSignIn.ServerClick
      Dim authenticated As Boolean = ValidateCredentials(email2.Value, password2.Value)

      If authenticated Then
         If RememberMe.Checked Then
            Dim cookie As New HttpCookie("UserInfo")
            cookie("email") = email2.Value
            cookie.Expires = Date.Now.AddDays(35)
            Response.Cookies.Add(cookie)
         Else
            Dim cookie As New HttpCookie("UserInfo")
            cookie.Expires = Date.Now.AddDays(-1)
            Response.Cookies.Add(cookie)
         End If
         If Session("accesslevel") = 1 Then
            Response.Redirect("admin_products.aspx")
         End If

      Else
         ErrorText = "Invalid login credentials"
      End If
   End Sub

   Private Function ValidateCredentials(ByVal email As String, ByVal password As String) As Boolean
      Dim returnValue As Boolean = False
      Session("userid") = Nothing
      If email.Length > 3 AndAlso password.Length > 2 Then
         Try
            Dim dt As DataTable = GetData(email, password)
            If Not IsNothing(dt) Then
               Dim userid As Integer = CInt(dt.Rows(0)("userid"))
               If userid > 0 Then
                  If dt.Rows(0)("accesslevel") = 2 Then
                     returnValue = False 'Normal User can't login
                  Else
                     Session("userid") = userid
                     Session("accesslevel") = dt.Rows(0)("accesslevel")
                     Session("DisplayName") = dt.Rows(0)("DisplayName").ToString
                     returnValue = True
                  End If
               End If
            End If
         Catch ex As Exception
            ' Log error if needed
         End Try
      End If
      Return returnValue
   End Function

   Private Function GetData(ByVal email As String, ByVal password As String) As DataTable
      Try
         Using con As SqlConnection = New SqlConnection(functions.GetConnectionString)
            Using cmd As SqlCommand = New SqlCommand("USERS_spLOGIN", con)
               cmd.CommandType = CommandType.StoredProcedure
               cmd.Parameters.AddWithValue("@UserEmail", email.Trim)
               cmd.Parameters.AddWithValue("@Password", password.Trim)
               If con.State = ConnectionState.Closed Then con.Open()
               Dim dt As New DataTable
               Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                  da.Fill(dt)
                  If dt Is Nothing OrElse dt.Rows.Count = 0 Then
                     Return Nothing
                  Else
                     Return dt
                  End If
               End Using
            End Using
         End Using
      Catch ex As Exception
         ' Log error if needed
         Return Nothing
      End Try
   End Function
End Class
