﻿Imports System.Data
Imports System.Data.SqlClient
Imports HashLibrary

Partial Class rep_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("rep_login.aspx")
                End If
            End If

            If Not IsNothing(Session("REP_ID")) Then
                Response.Redirect("default.aspx")
            End If

            functions.ClearSessions()

            Dim cookie As HttpCookie = Request.Cookies("RepInfo")
            If Not cookie Is Nothing AndAlso Not cookie("repemail") Is Nothing Then
                email2.Value = cookie("repemail")
                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("RepInfo")
                cookie("repemail") = email2.Value
                cookie.Expires = Date.Now.AddDays(35)
                Response.Cookies.Add(cookie)
            Else
                Dim cookie As New HttpCookie("RepInfo")
                cookie.Expires = Date.Now.AddDays(-1)
                Response.Cookies.Add(cookie)
            End If
            Response.Redirect("rep_users.aspx")
        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("REP_ID") = Nothing
        If email.Length > 3 AndAlso password.Length > 2 Then
            'Dim conn As SqlConnection = Nothing

            Try
                Dim dt As DataTable = GetData(email, password)
                If Not IsNothing(dt) Then
                    Dim userid As Integer = CInt(dt.Rows(0)("REP_ID"))
                    If userid > 0 Then
                        Session("REP_ID") = userid
                        Session("REP_FULLNAME") = dt.Rows(0)("REP_FULLNAME").ToString
                        returnValue = True
                    End If
                End If

            Catch ex As Exception

            Finally
                'If conn IsNot Nothing Then conn.Close()
            End Try
        Else
        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("REPS_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 Then
                            Return Nothing
                        Else
                            If dt.Rows.Count = 0 Then
                                Return Nothing
                            Else
                                Return dt
                            End If
                        End If
                    End Using
                End Using
            End Using
        Catch ex As Exception
            Dim s As String
            s = ex.Message
        End Try
    End Function

End Class
