Option Strict Off
Option Explicit On
Friend Class frmCadEquipamentoDet
	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 `equipamento_det` WHERE `equipamento_det`.`id` = " & CInt(Me.txtId.Text) & " AND `equipamento_det`.`idequipamento_gru` = " & CInt(Me.txtIdEquipamentoGru.Text)) Then
					Call Executar("OPTIMIZE TABLE `equipamento_det`")
					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 `equipamento_det` SET `situacao` = 'Inativo' WHERE `equipamento_det`.`id` = " & CInt(Me.txtId.Text) & " AND `equipamento_det`.`idequipamento_gru` = " & CInt(Me.txtIdEquipamentoGru.Text))
						Call SetaCodigo("equipamento_det", 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("equipamento_det", "Equipamentos", Me, "ORDER BY `idequipamento_gru` ASC, `id` ASC ")
	End Sub
	
	Private Sub cmdLocalizarEquipamentoGru_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizarEquipamentoGru.Click
		Call ifrmLocalizar.Show()
		Call ifrmLocalizar.SetParametros("equipamento_gru", "Grupos de equipamentos", 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 & "       `equipamento_det` SET "
		lstrSql = lstrSql & "       `idequipamento_gru` = " & CInt(Me.txtIdEquipamentoGru.Text) & ", " & vbCrLf
		lstrSql = lstrSql & "       `descricao`  = '" & Replace(Trim(Me.txtDescricao.Text), "'", "\'") & "', " & vbCrLf
		lstrSql = lstrSql & "       `qtde_fases`  = " & CShort(IIf(IsNumeric(Me.txtQtdeFases.Text), Me.txtQtdeFases.Text, 1)) & ", " & vbCrLf
		lstrSql = lstrSql & "       `potencia`  = " & Replace(CStr(CDbl(IIf(IsNumeric(Me.txtPotencia.Text), Me.txtPotencia.Text, 0))), ",", ".") & ", " & vbCrLf
		lstrSql = lstrSql & "       `potencia_cv`  = " & Replace(CStr(CDbl(IIf(IsNumeric(Me.txtPotenciaCV.Text), Me.txtPotenciaCV.Text, 0))), ",", ".") & ", " & vbCrLf
		lstrSql = lstrSql & "       `motor`  = " & IIf(Me.chkMotor.CheckState = System.Windows.Forms.CheckState.Unchecked, 0, 1) & ", " & vbCrLf
		lstrSql = lstrSql & "       `situacao`   = 'Ativo', " & vbCrLf
		lstrSql = lstrSql & "       `idusuarios` = " & glngUsuario_Id & " "
		If Not iblnNovo And Me.txtId.Text <> "0" Then
			lstrSql = lstrSql & " WHERE `equipamento_det`.`id` = " & CInt(Me.txtId.Text)
			lstrSql = lstrSql & "   AND `equipamento_det`.`idequipamento_gru` = " & CInt(Me.txtIdEquipamentoGru.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("equipamento_det", CInt(Me.txtId.Text), CInt(Me.txtIdEquipamentoGru.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 frmCadEquipamentoDet_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 frmCadEquipamentoDet_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 frmCadEquipamentoDet_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 = ""
		If alngId_A > 0 Then
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * " & vbCrLf
			lstrSql = lstrSql & "  FROM `" & astrTabela & "` " & vbCrLf
			lstrSql = lstrSql & " WHERE `id` = " & alngId_A
			If alngId_B > 0 Then
				lstrSql = lstrSql & "   AND `idequipamento_gru` = " & alngId_B
			End If
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						If LCase(Mid(Me.txtIdEquipamentoGru.Name, 6)) = LCase(Replace(astrTabela, "_", "")) 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.txtIdEquipamentoGru.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.txtDsEquipamentoGru.Text = Trim(IIf(IsDbNull(.Fields("descricao").value), "", .Fields("descricao").value))
						Else
							'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.txtIdEquipamentoGru.Text = VB6.Format(CInt(IIf(IsDbNull(.Fields("idequipamento_gru").value), 0, .Fields("idequipamento_gru").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.txtPotencia.Text = VB6.Format(IIf(IsDbNull(.Fields("potencia").value), "", .Fields("potencia").value), "#,##0.00")
							'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.txtPotenciaCV.Text = VB6.Format(IIf(IsDbNull(.Fields("potencia_cv").value), "", .Fields("potencia_cv").value), "#,##0.00")
							'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.txtQtdeFases.Text = Trim(IIf(IsDbNull(.Fields("qtde_fases").value), 1, .Fields("qtde_fases").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.chkMotor.CheckState = IIf(CShort(IIf(IsDbNull(.Fields("motor").value), 1, .Fields("motor").value)) = 0, System.Windows.Forms.CheckState.Unchecked, System.Windows.Forms.CheckState.Checked)
							'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 txtIdEquipamentoGru_Leave(txtIdEquipamentoGru, New System.EventArgs())
							Call Me.txtDescricao.Focus()
						End If
					Else
						Call MsgBox("Não foi possível encontrar o registro!", MsgBoxStyle.Exclamation, 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 LimpaCampos()
		Me.txtId.Text = "0"
		Me.txtDescricao.Text = ""
		Me.txtIdEquipamentoGru.Text = ""
		Me.txtDsEquipamentoGru.Text = ""
		Me.txtPotencia.Text = ""
		Me.txtPotenciaCV.Text = ""
		Me.txtQtdeFases.Text = ""
		Me.chkMotor.CheckState = System.Windows.Forms.CheckState.Unchecked
		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
	
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form)
		ifrmQuemChama = afrmQuemChama
		ifrmQuemChama.Enabled = False
	End Sub
	
	Private Sub txtDescricao_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDescricao.Leave
		'  With Me.txtDescricao
		'    .Text = Strings.UCase(.Text)
		'  End With
	End Sub
	
	Private Sub txtIdEquipamentoGru_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIdEquipamentoGru.Enter
		With Me.txtIdEquipamentoGru
			.Text = Replace(.Text, ".", "")
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtIdEquipamentoGru_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtIdEquipamentoGru.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 <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then 
			KeyAscii = 0
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtIdEquipamentoGru_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIdEquipamentoGru.Leave
		With Me.txtIdEquipamentoGru
			If IsNumeric(.Text) Then
				Call SetaCodigo("equipamento_gru", CInt(.Text))
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				.Text = VB6.Format(IIf(Len(.Text) = 0, 0, .Text), "#,##0")
			Else
				Me.txtDsEquipamentoGru.Text = ""
			End If
		End With
	End Sub
	
	Private Sub txtPotencia_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtPotencia.Enter
		With Me.txtPotencia
			.SelectionStart = 0
			.SelectionLength = CInt(Len(.Text))
		End With
	End Sub
	
	Private Sub txtPotencia_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtPotencia.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		With Me.txtPotencia
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then 
				If KeyAscii = System.Windows.Forms.Keys.Delete Then
					KeyAscii = System.Windows.Forms.Keys.Snapshot
				End If
				If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
					KeyAscii = 0
				Else
					If InStr(1, .Text, ",") > 0 Then
						KeyAscii = 0
					End If
				End If
			End If
		End With
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtPotencia_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtPotencia.Leave
		With Me.txtPotencia
			.Text = VB6.Format(CDbl(IIf(IsNumeric(.Text), .Text, 0)), "#,##0.00")
		End With
	End Sub
	
	Private Sub txtPotenciaCV_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtPotenciaCV.Enter
		With Me.txtPotenciaCV
			.SelectionStart = 0
			.SelectionLength = CInt(Len(.Text))
		End With
	End Sub
	
	Private Sub txtPotenciaCV_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtPotenciaCV.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		With Me.txtPotenciaCV
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then 
				If KeyAscii = System.Windows.Forms.Keys.Delete Then
					KeyAscii = System.Windows.Forms.Keys.Snapshot
				End If
				If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
					KeyAscii = 0
				Else
					If InStr(1, .Text, ",") > 0 Then
						KeyAscii = 0
					End If
				End If
			End If
		End With
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtPotenciaCV_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtPotenciaCV.Leave
		With Me.txtPotenciaCV
			.Text = VB6.Format(CDbl(IIf(IsNumeric(.Text), .Text, 0)), "#,##0.00")
		End With
	End Sub
	
	Private Sub txtQtdeFases_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtQtdeFases.Enter
		With Me.txtQtdeFases
			.SelectionStart = 0
			.SelectionLength = CInt(Len(.Text))
		End With
	End Sub
	
	Private Sub txtQtdeFases_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtQtdeFases.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 <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then 
			KeyAscii = 0
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtQtdeFases_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtQtdeFases.Leave
		With Me.txtQtdeFases
			.Text = VB6.Format(CDbl(IIf(IsNumeric(.Text), .Text, 0)), "#,##0")
		End With
	End Sub
End Class