Imports System.Data.Odbc

Public Class frmUser
    Dim Ada, tblEdit As Boolean
    Private Sub frmUser_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 frmUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Koneksi()
        Call Tampilkan()
        Call databaru()
        Call tidaksiapisi()
        Call kosongkan()
        txtpass.UseSystemPasswordChar = True
        txtRepass.UseSystemPasswordChar = True

    End Sub
    Sub databaru()
        txtNama.Text = ""
        txtpass.Text = ""
        cbolevel.Text = ""
        txtnama.Focus()
    End Sub
    Sub kosongkan()
        Call tidaksiapisi()
        txtuserId.Text = ""
        txtnama.Text = ""
        txtpass.Text = ""
        txtRepass.Text = ""
        cboLevel.Text = ""
        txtuserId.Enabled = True
        txtuserId.Focus()
    End Sub
    Sub siapisi()
        txtnama.Enabled = True
        txtpass.Enabled = True
        txtRepass.Enabled = True
        cbolevel.Enabled = True
        btnHapus.Enabled = False
        btnSimpan.Enabled = True
    End Sub
    Sub tidaksiapisi()
        txtnama.Enabled = False
        txtpass.Enabled = False
        txtRepass.Enabled = False
        cbolevel.Enabled = False
        btnSimpan.Enabled = False
        btnHapus.Enabled = False
    End Sub


    Sub Tampilkan()
        da = New OdbcDataAdapter("Select User_Id,username,level from tbuser order by user_Id", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "tbuser")
        DGV.DataSource = (ds.Tables("tbuser"))
        DGV.ReadOnly = True
        DGV.Columns(0).Width = 100
        DGV.Columns(1).Width = 170
        DGV.GridColor = Color.Blue
        DGV.DefaultCellStyle.ForeColor = Color.Blue
        DGV.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DGV.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(0).HeaderText = "User Id"
        DGV.Columns(1).HeaderText = "Nama User"
        DGV.Columns(2).HeaderText = "Level"

    End Sub

    Private Sub txtRepass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRepass.KeyPress
        If e.KeyChar = Chr(13) And txtpass.Text <> txtRepass.Text Then
            MsgBox("Maaf, Password tidak sama", , "Perhatian...!")
            txtRepass.Focus()
            Exit Sub
        Else
            btnSimpan.Enabled = True
        End If
    End Sub


    Private Sub txtuserId_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtuserId.KeyPress
        If e.KeyChar = Chr(13) Then
            cmd = New OdbcCommand("select * from tbuser where user_id='" & txtuserId.Text & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows = True Then
                txtnama.Text = rd.Item(1)
                cboLevel.Text = rd.Item(3)
                Call siapisi()
                txtuserId.Enabled = False
                btnHapus.Enabled = True
                Ada = True
            Else
                Call databaru()
                Call siapisi()
                Ada = False
            End If
            tblEdit = False
        End If
    End Sub

    Private Sub txtuserId_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtuserId.Validated
        btnSimpan.Enabled = True
    End Sub

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        If txtuserId.Text = "" Or txtnama.Text = "" Or txtpass.Text = "" Or cboLevel.Text = "" Then
            MsgBox("Data Belum Lengkap", , "Perhatian...!")
            txtuserId.Focus()
            Exit Sub
        End If
        cmd = New OdbcCommand("Select * from tbuser where user_id='" & txtuserId.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            Dim sqltambah As String = "Insert into tbuser (User_id,username,password,level) values " & _
            "('" & txtuserId.Text & "','" & txtnama.Text & "',md5('" & (txtpass.Text) & "'),'" & cboLevel.Text & "')"
            cmd = New OdbcCommand(sqltambah, Conn)

        Else
            Dim sqledit As String = "Update tbuser set " & _
            "username='" & txtnama.Text & "',password=md5('" & txtpass.Text & "') where user_id='" & txtuserId.Text & "'"
            cmd = New OdbcCommand(sqledit, Conn)

        End If
        cmd.ExecuteNonQuery()
        Call Tampilkan()
        Call kosongkan()
        btnSimpan.Enabled = False
    End Sub

    Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
        Me.Close()
    End Sub

    Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        cmd = New OdbcCommand("Select count(*) as jml from tbuser ", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.Item(0) = 1 Then
            MsgBox("Maaf..., Data User tidak dapat dihapus", MsgBoxStyle.Critical, "Perhatian...!")
            Exit Sub
        Else
            If MessageBox.Show("Yakin akan dihapus..?", "Hapus Data", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                cmd = New OdbcCommand("Delete  from tbuser where user_id='" & txtuserId.Text & "'", Conn)
                cmd.ExecuteNonQuery()

            End If
        End If
        Call Tampilkan()
        Call kosongkan()
    End Sub

    Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
        Call kosongkan()
    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) And Ada = True And tblEdit = False Then
            cmd = New OdbcCommand("Select * from tbuser where user_id='" & txtuserId.Text & "' and password=md5('" & txtpass.Text & "')", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows Then
                Call tidaksiapisi()
                btnSimpan.Enabled = False
                btnHapus.Enabled = True
            Else
                MsgBox("Maaf..., Password tidak sesuai", MsgBoxStyle.Critical, "Perhatian...!")
                txtpass.Text = ""
                txtpass.Focus()
                btnSimpan.Enabled = False
                btnHapus.Enabled = False
                Exit Sub
            End If
        End If
    End Sub

    Private Sub txtnama_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnama.TextChanged
        txtnama.Text = StripNonAlphaNum(txtnama.Text)
    End Sub

    Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged

    End Sub

    Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick

    End Sub

    Private Sub DGV_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellDoubleClick
        txtuserId.Text = DGV.Rows(e.RowIndex).Cells(0).Value
        txtuserId.Focus()
    End Sub

    Private Sub txtuserId_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuserId.TextChanged

    End Sub

    Private Sub chkTampilPass_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTampilPass.CheckedChanged
        If chkTampilPass.CheckState = CheckState.Checked Then
            txtpass.UseSystemPasswordChar = False
        Else
            txtpass.UseSystemPasswordChar = True
        End If
    End Sub

    Private Sub chkTampilPass2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTampilPass2.CheckedChanged
        If chkTampilPass2.CheckState = CheckState.Checked Then
            txtRepass.UseSystemPasswordChar = False
        Else
            txtRepass.UseSystemPasswordChar = True
        End If
    End Sub

    Private Sub cboLevel_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLevel.SelectedIndexChanged

    End Sub
End Class