Option Strict Off
Option Explicit On
Friend Class frmCadRamalIsolacao
	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 `ramal_isolacao` WHERE `ramal_isolacao`.`id` = " & CInt(Me.txtId.Text)) Then
					Call Executar("OPTIMIZE TABLE `ramal_isolacao`")
					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 `ramal_isolacao` SET `situacao` = 'Inativo' WHERE `ramal_isolacao`.`id` = " & CInt(Me.txtId.Text))
						Call SetaCodigo("ramal_isolacao", 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("ramal_isolacao", "Ramal - Isolação", 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 & "       `ramal_isolacao` SET "
		lstrSql = lstrSql & "       `descricao`  = '" & Replace(Trim(Me.txtDescricao.Text), "'", "\'") & "', " & vbCrLf
		lstrSql = lstrSql & "       `sigla`      = '" & Replace(Trim(Me.txtSigla.Text), "'", "\'") & "', " & vbCrLf
		lstrSql = lstrSql & "       `situacao`   = 'Ativo', " & vbCrLf
		lstrSql = lstrSql & "       `idusuarios` = " & glngUsuario_Id & " "
		If Not iblnNovo And Me.txtId.Text <> "0" Then
			lstrSql = lstrSql & " WHERE `ramal_isolacao`.`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("ramal_isolacao", 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 frmCadRamalIsolacao_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 frmCadRamalIsolacao_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 frmCadRamalIsolacao_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.txtSigla.Text = Trim(IIf(IsDbNull(.Fields("sigla").value), "C", .Fields("sigla").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.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.txtSigla.Text = ""
		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 txtSigla_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSigla.Enter
		With Me.txtSigla
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form)
		ifrmQuemChama = afrmQuemChama
		ifrmQuemChama.Enabled = False
	End Sub
End Class