Imports System.Data.SqlClient
Public Class frmManagement
    'Dim text As String
    Dim passchar As String = ""
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        PictureBox1.Visible = False
        panel3.Visible = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        btnNewSave.Text = "New"
        txtName.Enabled = False
        txtUser.Enabled = False
        txtPass.Enabled = False
        cboPrivilege.Enabled = False
        cboStatus.Enabled = False
        btnUpdate.Enabled = False
        btnDelete.Enabled = False
        btnNewSave.Enabled = True
        panel3.Visible = False
        btnEdit.Enabled = False
        PictureBox1.Visible = True
        txtName.Text = ""
        txtUser.Text = ""
        txtPass.Text = ""
    End Sub

    Private Sub frmManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connection()
        getUser()
    End Sub

    Sub getUser()
        btnEdit.Enabled = False
        ListView1.Items.Clear()
        Try
            cm = New SqlCommand("select * from tblUser", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lst = ListView1.Items.Add(dr.Item(0))
                lst.SubItems.Add(dr.Item(3)) ' account name
                lst.SubItems.Add(dr.Item(1)) ' username
                For x As Integer = 0 To dr.Item(2).ToString.Length
                    passchar += "•"
                Next
                lst.SubItems.Add(passchar) ' password
                lst.SubItems.Add(dr.Item(4)) ' privilege
                lst.SubItems.Add(dr.Item(6)) ' status
                passchar = ""
            End While
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        txtName.Enabled = True
        txtUser.Enabled = True
        txtPass.Enabled = True
        cboPrivilege.Enabled = True
        cboStatus.Enabled = True
        btnUpdate.Enabled = True
        btnDelete.Enabled = True
        btnNewSave.Enabled = False
        btnEdit.Enabled = True
        txtName.Text = ListView1.FocusedItem.SubItems(1).Text
        txtUser.Text = ListView1.FocusedItem.SubItems(2).Text
        'txtPass.Text = dr.Item(2).ToString()
        cm = New SqlCommand("select * from tblUser where uID like '" & ListView1.FocusedItem.Text & "'", cn)
        dr = cm.ExecuteReader()
        dr.Read()
        If dr.HasRows Then
            txtPass.Text = dr.Item(2).ToString()
        End If
            dr.Close()
            cboPrivilege.Text = ListView1.FocusedItem.SubItems(4).Text
            cboStatus.Text = ListView1.FocusedItem.SubItems(5).Text

    End Sub

    Private Sub cboPrivilege_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPrivilege.SelectedIndexChanged

    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        passchar = ""
        ListView1.Items.Clear()
        Try
            cm = New SqlCommand("select * from tblUser where username like '" & txtSearch.Text & "%'", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lst = ListView1.Items.Add(dr.Item(0))
                lst.SubItems.Add(dr.Item(3)) ' account name
                lst.SubItems.Add(dr.Item(1)) ' username
                text = dr.Item(2) ' password
                For x As Integer = 0 To text.Length
                    passchar += "•"
                Next
                lst.SubItems.Add(passchar) ' password
                lst.SubItems.Add(dr.Item(4)) ' privilege
                lst.SubItems.Add(dr.Item(6)) ' status
                passchar = ""
            End While
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnNewSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewSave.Click
      
        If btnNewSave.Text = "Save" Then
            If is_empty(txtName) = True Then Exit Sub
            If is_empty(txtUser) = True Then Exit Sub
            If is_empty(txtPass) = True Then Exit Sub
            If is_empty_combo(cboPrivilege) = True Then Exit Sub
            If is_empty_combo(cboStatus) = True Then Exit Sub

            cm = New SqlCommand("Select * from tblUser where username like '" + txtUser.Text + "'", cn)
            dr = cm.ExecuteReader
            dr.Read()
            If dr.HasRows Then
                MessageBox.Show("Username you've input is already used, try another one", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtUser.Clear()
                txtUser.Focus()
                dr.Close()
                Return
            End If
            dr.Close()

            cm = New SqlCommand("insert into tblUser(username,password,name,privilege,status) values('" & txtUser.Text & "','" & txtPass.Text & "','" & txtName.Text & "','" & cboPrivilege.Text & "','" & cboStatus.Text & "')", cn)
            cm.ExecuteNonQuery()
            MessageBox.Show("New user has been successfully added", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtSearch_TextChanged(sender, e)
            btnCancel_Click(sender, e)
            clear()
        Else
            btnNewSave.Text = "Save"
            txtName.Enabled = True
            txtUser.Enabled = True
            txtPass.Enabled = True
            cboPrivilege.Enabled = True
            cboStatus.Enabled = True
            btnEdit.Enabled = False
        End If
    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        PictureBox1.Visible = True
        panel3.Visible = False
    End Sub

    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        If txtPassNew.Text <> txtPassConfirm.Text Then
            MessageBox.Show("The old and new password inputted didn't matched", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        ElseIf txtPassNew.Text = txtPassConfirm.Text And txtPassNew.TextLength < 5 Then
            MessageBox.Show("Password must be atleast 5 characters", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If
        panel3.Visible = False
        PictureBox1.Visible = True
    End Sub

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

    End Sub

    Private Sub chkEditPass_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEditPass.CheckedChanged
        If chkEditPass.Checked = True Then
            txtPassOld.PasswordChar = ""
            txtPassNew.PasswordChar = ""
            txtPassConfirm.PasswordChar = ""
        Else
            txtPassOld.PasswordChar = "•"
            txtPassNew.PasswordChar = "•"
            txtPassConfirm.PasswordChar = "•"
        End If

    End Sub

    Sub clear()
        txtName.Clear()
        txtUser.Clear()
        txtPass.Clear()
        cboPrivilege.Text = String.Empty
        cboStatus.Text = String.Empty
        txtPassOld.Clear()
        txtPassConfirm.Clear()
        txtPassNew.Clear()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            cm = New SqlCommand("update tblUser set username = '" & txtUser.Text & "',password = '" & txtPass.Text & "',name = '" & txtName.Text & "',privilege = '" & cboPrivilege.Text & "',status = '" & cboStatus.Text & "' where uID like '" & ListView1.FocusedItem.Text & "'", cn)
            cm.ExecuteNonQuery()
            MessageBox.Show("Account settings has been successfully updated", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            getUser()
            btnCancel_Click(sender, e)
            clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            If MsgBox("Are you sure you want to remove this account?", vbYesNo + vbQuestion, "Message") = MsgBoxResult.Yes Then
                cm = New SqlCommand("delete from tblUser where uID like '" & ListView1.FocusedItem.Text & "'", cn)
                cm.ExecuteNonQuery()
                MessageBox.Show("Record successfully Removed", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                getUser()
                btnCancel_Click(sender, e)
                clear()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class