Imports System.Data.SqlClient
Public Class frmLogin
    Dim TogMove As Integer
    Dim MValX As Integer
    Dim MValY As Integer
    Dim tr As Integer = 0

    Public Sub getuID()
        cm = New SqlCommand("Select * from tblUser where username like '" + username + "' and password like '" + password + "'", cn)
        dr = cm.ExecuteReader
        While dr.Read
            uID = dr.GetValue(0).ToString
        End While
        dr.Close()
    End Sub

    Public Sub getLoginDate()
        cm = New SqlCommand("select * from tblLoginTrail where uID like '" + uID + "' and username like '" + txtuser.Text + "'", cn)
        dr = cm.ExecuteReader
        dr.Read()
        If dr.HasRows Then
            loginDate = dr.GetValue(3).ToString()
        End If
        dr.Close()
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connection()
        ProgressBar1.Visible = False
        Me.Height = 237
    End Sub

    Private Sub frmLogin_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If TogMove = 1 Then
            Me.SetDesktopLocation(MousePosition.X - MValX, MousePosition.Y - MValY)
        End If
    End Sub

    Private Sub frmLogin_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        TogMove = 0
    End Sub

    Private Sub frmLogin_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        TogMove = 1
        MValX = e.X
        MValY = e.Y
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Increment(1)
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Timer1.Stop()
            frmCenter.Show()
            Me.Hide()
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            txtpass.PasswordChar = ""
        Else
            txtpass.PasswordChar = "•"
        End If
    End Sub

    Private Sub txtuser_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.Enter
        If txtuser.Text = "username" Then
            txtuser.Text = ""
            txtuser.ForeColor = Color.Black
        End If
    End Sub

    Private Sub txtuser_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.Leave
        If txtuser.Text = "" Then
            txtuser.Text = "username"
            txtuser.ForeColor = Color.Gray
        End If
    End Sub

    Private Sub txtpass_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.Enter
        If txtpass.Text = "password" Then
            txtpass.Text = ""
            txtpass.ForeColor = Color.Black
        End If
    End Sub

    Private Sub txtpass_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.Leave
        If txtpass.Text = "" Then
            txtpass.Text = "password"
            txtpass.ForeColor = Color.Gray
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If MsgBox("Are you sure you want to cancel this application?", vbYesNo + vbQuestion, "Message") = MsgBoxResult.Yes Then
            End
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If is_empty(txtuser) = True Then Exit Sub
        If is_empty(txtpass) = True Then Exit Sub
        Try

            cm = New SqlCommand("select * from tblUser where username like '" & txtuser.Text & "' and password like '" & txtpass.Text & "' and privilege like 'regular user' and status like 'online'", cn)
            dr = cm.ExecuteReader
            dr.Read()
            If dr.HasRows Then
                MessageBox.Show("This account is currently online, you forgot to logout. Please approach administrator for help. Thank you", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
                dr.Close()
                Return
            End If
            dr.Close()
            Me.Height = 244
            cm = New SqlCommand("select * from tblUser where username like '" & txtuser.Text & "' and password like '" & txtpass.Text & "'", cn)
            dr = cm.ExecuteReader
            dr.Read()
            If dr.HasRows Then

                If dr.Item(4).ToString = "regular user" Then
                    frmCenter.Button3.Enabled = False
                    frmCenter.Button4.Enabled = False
                    frmCenter.Button5.Enabled = False
                    frmCenter.Button6.Enabled = False
                    frmCenter.Button7.Enabled = False
                End If

                dr.Close()
                Module1.insetLogin(txtuser.Text, txtpass.Text)
                getuID()
                frmCenter.auditTrail(uID, "Logged in successfully")
                frmCenter.dawat(uID, loginDate)
                ProgressBar1.Visible = True
                Panel1.Enabled = False
                Timer1.Interval = 100
                Timer1.Start()
            Else
                Do While tr >= 2
                    MsgBox("sorry 3 attemps will terminate the system. Bye Bye", vbCritical)
                    Environment.Exit(0)
                Loop
                tr = tr + 1
                MsgBox("Invalid username and password. You attemp(" & tr & ")", vbCritical)
            End If
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub loginAccount()
        Try
            cm = New SqlCommand("select * from tblUser where usernamne like = '" & txtuser.Text & "' and password like = '" & txtpass.Text & "'", cn)
            dr = cm.ExecuteReader
            dr.Read()
            If dr.HasRows Then
                uID = dr.GetValue(0).ToString
            End If
            dr.Close()
            cm = New SqlCommand("select * from tblLoginTrail where uID like '" & uID & "' and loginDate like '" & loginDate & "'", cn)
            dr = cm.ExecuteReader
            dr.Read()
            If dr.HasRows Then
                logID = dr.GetValue(0).ToString()
            End If
            dr.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Panel1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        TogMove = 0
    End Sub

    Private Sub Panel1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If TogMove = 1 Then
            Me.SetDesktopLocation(MousePosition.X - MValX, MousePosition.Y - MValY)
        End If
    End Sub

    Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        TogMove = 1
        MValX = e.X
        MValY = e.Y
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        Button1.BackColor = Color.White
        Button1.ForeColor = Color.Black
    End Sub

    Private Sub Button1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        Button1.BackColor = Color.Gray
        Button1.ForeColor = Color.White
    End Sub

    Private Sub Button2_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseLeave
        Button2.BackColor = Color.White
        Button2.ForeColor = Color.Black
    End Sub

    Private Sub Button2_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseEnter
        Button2.BackColor = Color.Gray
        Button2.ForeColor = Color.White
    End Sub

    Private Sub lnkAdd_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAdd.LinkClicked
        getuID()
        frmAccesscode.accesscode(uID, "")
        frmAccesscode.ShowDialog()
    End Sub
End Class