Option Strict Off
Option Explicit On
Friend Class frmCadAlimentador
	Inherits System.Windows.Forms.Form
	Private ifrmQuemChama As System.Windows.Forms.Form
	Private ifrmLocalizar As frmLocalizadorGeral
	Private iblnNovo As Boolean
	
	Private Sub cmdExcluir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExcluir.Click
		If Trim(Me.txtId.Text) <> "0" Then
			If MsgBox("Deseja realmente excluir este registro!", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes Then
				If Executar("DELETE FROM `alimentador` WHERE `alimentador`.`id` = " & CInt(Me.txtId.Text)) Then
					Call Executar("OPTIMIZE TABLE `alimentador`")
					Call LimpaCampos()
					Call MsgBox("Registro eliminado!", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					If MsgBox("Deseja desativar este registro!", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes Then
						Call Executar("UPDATE `alimentador` SET `situacao` = 'Inativo' WHERE `alimentador`.`id` = " & CInt(Me.txtId.Text))
						Call SetaCodigo("alimentador", CInt(Me.txtId.Text))
					End If
				End If
			End If
		End If
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdLocalizar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizar.Click
		Call ifrmLocalizar.Show()
		Call ifrmLocalizar.SetParametros("alimentador", "Alimentador", Me)
	End Sub
	
	Private Sub cmdNovo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNovo.Click
		iblnNovo = True
		Call LimpaCampos()
	End Sub
	
	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = ""
		If Me.txtId.Text = "0" Or iblnNovo Then
			lstrSql = lstrSql & "INSERT INTO " & vbCrLf
		Else
			lstrSql = lstrSql & "UPDATE " & vbCrLf
		End If
		lstrSql = lstrSql & "       `alimentador` SET "
		lstrSql = lstrSql & "       `descricao`  = '" & Replace(Trim(Me.txtDescricao.Text), "'", "\'") & "', " & vbCrLf
		lstrSql = lstrSql & "       `valor`      = " & CInt(Replace(Me.txtValor.Text, ".", "")) & ", " & vbCrLf
		lstrSql = lstrSql & "       `situacao`   = 'Ativo', " & vbCrLf
		lstrSql = lstrSql & "       `idusuarios` = " & glngUsuario_Id & " "
		If Not iblnNovo And Me.txtId.Text <> "0" Then
			lstrSql = lstrSql & " WHERE `alimentador`.`id` = " & CInt(Me.txtId.Text)
		End If
		If Executar(lstrSql) Then
			If Me.txtId.Text = "0" Or iblnNovo Then
				Call Conectar("SELECT LAST_INSERT_ID() ", lrstAux)
				With lrstAux
					If .State = ADODB.ObjectStateEnum.adStateOpen 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.txtId.Text = CStr(CInt(IIf(IsDbNull(.Fields(0).value), 0, .Fields(0).value)))
						End If
						Call .Close()
					End If
				End With
			End If
			Call SetaCodigo("alimentador", CInt(Me.txtId.Text))
			Call MsgBox("As informações foram salvas com sucesso.", MsgBoxStyle.Information, gstrTituloMensagem)
		Else
			Call MsgBox("Problema na gravação, favor verificar!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
		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 frmCadAlimentador_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		ifrmLocalizar = New frmLocalizadorGeral
		Call LimpaCampos()
	End Sub
	
	Private Sub frmCadAlimentador_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
			Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		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 frmCadAlimentador_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If ifrmQuemChama Is Nothing Then
			'
		Else
			ifrmQuemChama.Enabled = True
		End If
	End Sub
	
	Public Sub SetaCodigo(ByVal astrTabela As String, ByVal alngId_A As Integer, Optional ByVal alngId_B As Integer = 0, Optional ByVal alngId_C As Integer = 0, Optional ByVal alngId_D As Integer = 0)
		Dim lintDig As Short
		Dim lintAux As Short
		Dim lstrAux As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If ValidaView(astrTabela) Then
			astrTabela = Mid(astrTabela, 3)
		End If
		lstrAux = ""
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * " & vbCrLf
		lstrSql = lstrSql & "  FROM `" & astrTabela & "` " & vbCrLf
		lstrSql = lstrSql & " WHERE `id` = " & alngId_A
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen 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.txtId.Text = VB6.Format(CInt(IIf(IsDbNull(.Fields("id").value), 0, .Fields("id").value)), "#,##0")
					'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.txtDescricao.Text = Trim(IIf(IsDbNull(.Fields("descricao").value), "", .Fields("descricao").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.txtValor.Text = VB6.Format(Trim(IIf(IsDbNull(.Fields("valor").value), "0", .Fields("valor").value)), "#,##0")
					'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.lblSituacao.Text = "Situação: " & Trim(IIf(IsDbNull(.Fields("situacao").value), "Ativo", .Fields("situacao").value)) & "  -  último acesso: " & BuscaNomeUsuario(CInt(IIf(IsDbNull(.Fields("idusuarios").value), 0, .Fields("idusuarios").value)))
					iblnNovo = False
					Call Me.txtDescricao.Focus()
				Else
					Call MsgBox("Não foi possível encontrar o registro!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				End If
				Call .Close()
			End If
		End With
		'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 LimpaCampos()
		Me.txtId.Text = "0"
		Me.txtDescricao.Text = ""
		Me.txtValor.Text = "0"
		Me.lblSituacao.Text = ""
	End Sub
	
	Private Sub txtDescricao_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDescricao.Enter
		With Me.txtDescricao
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtDescricao_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDescricao.Leave
		With Me.txtDescricao
			.Text = UCase(.Text)
		End With
	End Sub
	
	Private Sub txtValor_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValor.Enter
		With Me.txtValor
			.Text = Replace(.Text, ".", "")
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtValor_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValor.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		ElseIf KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then 
			KeyAscii = 0
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtValor_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValor.Leave
		With Me.txtValor
			.Text = FormatNumber(IIf(Len(.Text) = 0, 0, .Text), 0)
		End With
	End Sub
	
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form)
		ifrmQuemChama = afrmQuemChama
		ifrmQuemChama.Enabled = False
	End Sub
End Class