Imports System.Data.Odbc
Public Class frmLogin

    Private Sub frmLogin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Koneksi()
    End Sub

    Sub cekUser()
        cmd = New OdbcCommand("Select * from tbuser where user_id='" & txtUser.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            MsgBox("Maaf..., User Id Salah", MsgBoxStyle.Critical, "Perhatian...!")
            txtUser.Text = ""
            txtUser.Focus()
        End If
    End Sub
    Sub cekPassword()
        Dim cLevel As String
        'On Error Resume Next
        cmd = New OdbcCommand("Select * from tbuser where user_Id='" & txtUser.Text & "' and password=md5('" & txtPass.Text & "')", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            Me.Visible = False
            cLevel = CStr(rd.Item("level"))
            If cLevel = "Admin" Then
                frmMenuUtama.mnMaster.Enabled = True
                frmMenuUtama.mnProses.Enabled = True
                frmMenuUtama.mnUtility.Enabled = True
                frmMenuUtama.btnKriteria.Enabled = True
                frmMenuUtama.btnSubKriteria.Enabled = True
                frmMenuUtama.btnImportir.Enabled = True
                frmMenuUtama.btnBerpasangan.Enabled = True
                frmMenuUtama.btnSubBerpasangan.Enabled = True
                frmMenuUtama.btnPenilaian.Enabled = True
                frmMenuUtama.btnProsesAHP.Enabled = True

            ElseIf cLevel = "user" Then
                frmMenuUtama.mnMaster.Enabled = False
                frmMenuUtama.mnProses.Enabled = False
                frmMenuUtama.mnUtility.Enabled = False
                frmMenuUtama.btnKriteria.Enabled = False
                frmMenuUtama.btnSubKriteria.Enabled = False
                frmMenuUtama.btnImportir.Enabled = False
                frmMenuUtama.btnBerpasangan.Enabled = False
                frmMenuUtama.btnSubBerpasangan.Enabled = False
                frmMenuUtama.btnPenilaian.Enabled = False
                frmMenuUtama.btnProsesAHP.Enabled = False
                frmMenuUtama.mnLapKriteria.Enabled = True
                frmMenuUtama.btnLapKriteria.Enabled = True
            Else
                frmMenuUtama.mnMaster.Enabled = False
                frmMenuUtama.mnProses.Enabled = False
                frmMenuUtama.mnUtility.Enabled = False
                frmMenuUtama.btnKriteria.Enabled = False
                frmMenuUtama.btnSubKriteria.Enabled = False
                frmMenuUtama.btnImportir.Enabled = False
                frmMenuUtama.btnBerpasangan.Enabled = False
                frmMenuUtama.btnSubBerpasangan.Enabled = False
                frmMenuUtama.btnPenilaian.Enabled = False
                frmMenuUtama.btnProsesAHP.Enabled = False
                frmMenuUtama.mnLapKriteria.Enabled = False
                frmMenuUtama.mnLapKriteria.Enabled = False
                frmMenuUtama.btnLapKriteria.Enabled = False
            End If
            frmMenuUtama.Show()

        Else
            MsgBox("Maaf..., Password Salah", MsgBoxStyle.Critical, "Perhatian...!")
            txtPass.Text = ""
            txtPass.Focus()
            Exit Sub
        End If
    End Sub
    Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
        Me.Close()
    End Sub

    Private Sub txtPass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPass.KeyPress
        If e.KeyChar = Chr(13) Then
            ' Call cekPassword()
            Me.btnOk_Click(sender, e)
        End If
    End Sub


    Private Sub txtUser_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUser.KeyPress
        If e.KeyChar = Chr(13) Then
            Call cekUser()
        End If
        If Char.IsLetterOrDigit(e.KeyChar) = False Then
            If e.KeyChar = CChar(ChrW(Keys.Back)) Or _
               e.KeyChar = CChar(ChrW(Keys.Space)) Then
                e.Handled = False
            Else
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub txtUser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUser.TextChanged
        txtUser.Text = StripNonAlphaNum(txtUser.Text)
    End Sub
    
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Call cekUser()
        Call cekPassword()
    End Sub

    
    
End Class