Option Strict Off
Option Explicit On
Friend Class frmCadGrupoUsuario
	Inherits System.Windows.Forms.Form
	Private istrSql As String
	Private istrNovo As String
	Private istrCodAnt As String
	
	Private Sub frmCadGrupoUsuario_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call SetaFrameDados(0)
		
		Me.cmdEditar.Enabled = False
		Me.cmdSalvar.Enabled = False
		Me.cmdExcluir.Enabled = False
		Me.txtGrupo.ReadOnly = True
		Call MontaGrid()
		Call EncheGrid()
	End Sub
	
	Private Sub frmCadGrupoUsuario_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			If Me.fmeDados1.Visible = True Then
				Call cmdCancelar_Click(cmdCancelar, New System.EventArgs())
			Else
				Call cmdSair_Click(cmdSair, New System.EventArgs())
			End If
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub MontaGrid()
		With Me.mfgDados
			.Clear()
			.Rows = 1
			.set_ColWidth(0, 0)
			.set_ColAlignment(0, 1)
			.set_TextMatrix(0, 0, "Código")
			.set_ColWidth(1, 5860)
			.set_ColAlignment(1, 1)
			.set_TextMatrix(0, 1, "Grupo")
		End With
	End Sub
	
	Private Sub EncheGrid()
		Dim lstrDescri As String
		Dim llngCodigo As Integer
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		istrSql = ""
		istrSql = istrSql & "SELECT `usuarios_grupos`.`id`, " & vbCrLf
		istrSql = istrSql & "       `usuarios_grupos`.`descricao` " & vbCrLf
		istrSql = istrSql & "  FROM `usuarios_grupos` " & vbCrLf
		istrSql = istrSql & " ORDER BY 2 ASC"
		Call Conectar(istrSql, lrstAux)
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						llngCodigo = CInt(IIf(IsDbNull(.Fields("id").value), 0, .Fields("id").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDescri = CStr(IIf(IsDbNull(.Fields("descricao").value), "", .Fields("descricao").value))
						With Me.mfgDados
							.Rows = .Rows + 1
							.set_TextMatrix(.Rows - 1, 0, llngCodigo)
							.set_TextMatrix(.Rows - 1, 1, lstrDescri)
						End With
						Call .MoveNext()
					End While
				End If
				Call .Close()
			End If
		End With
		Me.mfgDados.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightNever
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub mfgDados_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgDados.ClickEvent
		With Me.mfgDados
			If .Row > 0 And .Row < .Rows Then
				.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightAlways
				Me.txtCodigo.Text = .get_TextMatrix(.Row, 0)
				Me.txtGrupo.Text = .get_TextMatrix(.Row, 1)
			End If
		End With
		
		Me.cmdEditar.Enabled = True
		Me.cmdSalvar.Enabled = False
		Me.cmdExcluir.Enabled = True
		
		Call localizaCodigo()
	End Sub
	
	Private Sub mfgDados_DblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgDados.DblClick
		If Me.mfgDados.Rows > 1 Then Call cmdEditar_Click(cmdEditar, New System.EventArgs())
	End Sub
	
	Private Sub localizaCodigo()
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Len(Trim(Me.txtCodigo.Text)) = 0 Then
			Call MsgBox("É necessário informar um código!", MsgBoxStyle.Information, gstrTituloMensagem)
		Else
			istrSql = ""
			istrSql = istrSql & "SELECT `usuarios_grupos`.`id`, " & vbCrLf
			istrSql = istrSql & "       `usuarios_grupos`.`descricao` " & vbCrLf
			istrSql = istrSql & "  FROM `usuarios_grupos` " & vbCrLf
			istrSql = istrSql & " WHERE `usuarios_grupos`.`id` = " & Me.txtCodigo.Text
			istrSql = istrSql & " ORDER BY 2 ASC"
			Call Conectar(istrSql, lrstAux)
			With lrstAux
				If .State > 0 Then
					If .RecordCount > 0 Then
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						Me.txtCodigo.Text = CStr(CInt(IIf(IsDbNull(.Fields("id").value), 0, .Fields("id").value)))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						Me.txtGrupo.Text = CStr(IIf(IsDbNull(.Fields("descricao").value), "", .Fields("descricao").value))
						istrCodAnt = Me.txtCodigo.Text
					Else
						Call MsgBox("Código não encontrado!", MsgBoxStyle.Information, gstrTituloMensagem)
					End If
					Call .Close()
				End If
			End With
		End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub txtGrupo_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtGrupo.Enter
		With Me.txtGrupo
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtGrupo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtGrupo.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then Call cmdSalvar_Click(cmdSalvar, New System.EventArgs())
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub SetaFrameDados(ByVal ablnModo As Boolean)
		Me.fmeDados1.Enabled = ablnModo
		Me.fmeDados1.Visible = ablnModo
		Me.mfgDados.Enabled = Not ablnModo
		Me.cmdNovo.Enabled = Not ablnModo
		Me.cmdEditar.Enabled = Not ablnModo
		Me.cmdExcluir.Enabled = Not ablnModo
		Me.cmdSair.Enabled = Not ablnModo
	End Sub
	
	Private Sub cmdNovo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNovo.Click
		istrNovo = "S"
		Me.mfgDados.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightNever
		Call SetaFrameDados(True)
		Me.txtGrupo.Text = ""
		Me.cmdSalvar.Enabled = True
		Me.txtGrupo.ReadOnly = False
		Call Me.txtGrupo.Focus()
	End Sub
	
	Private Sub cmdEditar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEditar.Click
		istrNovo = "E"
		Call SetaFrameDados(True)
		Me.cmdSalvar.Enabled = True
		Me.txtGrupo.ReadOnly = False
		Call Me.txtGrupo.Focus()
	End Sub
	
	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		Dim llngCodigo As Integer
		If Len(Trim(Me.txtGrupo.Text)) = 0 Then
			Call MsgBox("O campo Grupo está vazio!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			Call Me.txtGrupo.Focus()
		Else
			istrSql = ""
			llngCodigo = CInt(IIf(IsNumeric(Me.txtCodigo.Text), Me.txtCodigo.Text, 0))
			If istrNovo = "S" Then
				istrSql = istrSql & "INSERT INTO "
			ElseIf istrNovo = "E" Then 
				istrSql = istrSql & "UPDATE "
			End If
			istrSql = istrSql & "  `usuarios_grupos` SET "
			istrSql = istrSql & "                       `id`    = " & llngCodigo & ", "
			istrSql = istrSql & "                       `descricao` = '" & UCase(Trim(Me.txtGrupo.Text)) & "' "
			If istrNovo = "E" Then
				istrSql = istrSql & " WHERE `usuarios_grupos`.`id` = " & istrCodAnt & " "
			End If
			Call Executar(istrSql)
			Me.txtGrupo.Text = ""
			istrNovo = "N"
			Me.cmdSalvar.Enabled = False
			Call frmCadGrupoUsuario_Load(Me, New System.EventArgs())
		End If
	End Sub
	
	Private Sub cmdCancelar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancelar.Click
		Call SetaFrameDados(False)
		Me.cmdNovo.Enabled = True
		Me.cmdEditar.Enabled = False
		Me.cmdSalvar.Enabled = False
		Me.cmdExcluir.Enabled = False
	End Sub
	
	Private Sub cmdExcluir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExcluir.Click
		Dim lblnAux As Boolean
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lblnAux = True
		If MsgBox("Esta operação não possui retorno." & vbCrLf & vbCrLf & "Tem certeza de que deseja excluir este registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Excluir") = MsgBoxResult.Yes Then
			istrSql = "SELECT 1 FROM `usuarios` WHERE `usuarios`.`idgrupos` = " & istrCodAnt & " "
			Call Conectar(istrSql, lrstAux)
			With lrstAux
				If .State > 0 Then
					If .RecordCount > 0 Then
						Call MsgBox("Impossível excluir este registro. Existe um ou mais usuários cadastrados para esta grupo." & vbCrLf & vbCrLf & "Remova esses usuários relacionados a este grupo e tente novamente!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
						lblnAux = False
					End If
					Call .Close()
				End If
			End With
			istrSql = "SELECT 1 FROM `usuarios_grupos` WHERE `usuarios_grupos`.`id` = " & istrCodAnt & " "
			Call Conectar(istrSql, lrstAux)
			With lrstAux
				If .State > 0 Then
					If .RecordCount > 0 Then
						Call MsgBox("Impossível excluir este registro. Existe um ou mais usuários cadastrados para esta grupo." & vbCrLf & vbCrLf & "Remova esses usuários relacionados a este grupo e tente novamente!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
						lblnAux = False
					End If
					Call .Close()
				End If
			End With
		End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
		Me.cmdSalvar.Enabled = False
		frmCadGrupoUsuario_Load(Me, New System.EventArgs())
	End Sub
	
	Private Sub cmdSair_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSair.Click
		Call Me.Close()
	End Sub
End Class