Option Strict Off
Option Explicit On
Friend Class frmCadRevGrupos
	Inherits System.Windows.Forms.Form
	Private iFrmQuemChama As System.Windows.Forms.Form
	Private ifrmLocalizar As frmLocalizadorGeralNovo
	Private iblnNovo As Boolean
	Private istrTabela As String
	Private istrPriKey() As String
	Private istrForKey() As String
	Private irstTabela As ADODB.Recordset
	Private iTextBox() As clsCtrlEventos
	
	Private Sub cmdExcluir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExcluir.Click
		Call mdlControleForms.ctrl_form_ExcluirRegistros(Me, istrTabela, istrPriKey)
	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(istrTabela, Me.Text, Me)
	End Sub
	Public Sub SetaCodigo(ByVal astrTabela As String, ByRef astrPriKey As String)
		Call mdlControleForms.ctrl_form_SetaCodigo(Me, astrTabela, istrTabela, astrPriKey, irstTabela)
	End Sub
	
	Private Sub cmdNovo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNovo.Click
		Dim llngIdx As Integer
		Call LigDes(False)
		Me.txtId.Text = ""
		Me.txtIdagente.Text = ""
		Me.txtAno.Text = ""
		Me.txtTrimestre.Text = ""
		Me.OptRural.Checked = False
		Me.OptRuralIrr.Checked = False
		Me.OptRuralAquic.Checked = False
		Me.OptAguaEsgSanea.Checked = False
		Me.optGrupoA.Checked = False
		Me.optGrupoB.Checked = False
		Me.txtId.Focus()
		
	End Sub
	
	Private Sub LigDes(ByVal ablnAux As Boolean)
		
		Me.cmdExcluir.Enabled = ablnAux
		Me.cmdLocalizar.Enabled = ablnAux
		Me.cmdImprimir.Enabled = ablnAux
		
	End Sub
	
	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		
		Dim lrstAux As ADODB.Recordset
		Dim lstrSql As String
		Dim lstrCod As String
		Dim llngCod As Integer
		Dim lstrSituacao As String
		
		Dim lstrId As String
		
		lrstAux = New ADODB.Recordset
		
		
		If Me.txtIdagente.Text = "" Or Me.txtAno.Text = "" Or Me.txtTrimestre.Text = "" Then
			Call MsgBox("Precisa preencher todos os campos, verifique", MsgBoxStyle.Information, gstrTituloMensagem)
		ElseIf Me.OptRural.Checked = False And Me.OptRuralIrr.Checked = False And Me.OptRuralAquic.Checked = False And Me.OptAguaEsgSanea.Checked = False Then 
			Call MsgBox("Precisa marcar um tipo do benefício, verifique", MsgBoxStyle.Information, gstrTituloMensagem)
		ElseIf Me.optGrupoA.Checked = False And Me.optGrupoB.Checked = False Then 
			Call MsgBox("Precisa marcar um Grupo, verifique", MsgBoxStyle.Information, gstrTituloMensagem)
		Else
			Call LigDes(True)
			
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * " & vbCrLf
			lstrSql = lstrSql & " FROM `cadrev_grupos` " & vbCrLf
			lstrSql = lstrSql & " WHERE `cadrev_grupos`.`id` = '" & Me.txtId.Text & "'" & vbCrLf
			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"'
						lstrId = Trim(IIf(IsDbNull(.Fields("id").value), "", .Fields("id").value))
						lstrSituacao = "U"
					Else
						lstrSituacao = "I"
					End If
					Call .Close()
				End If
			End With
			
			If lstrSituacao = "I" Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `" & istrTabela & "`  SET " & vbCrLf
				lstrSql = lstrSql & "            `" & istrTabela & "`.`id` = '" & UCase(Me.txtId.Text) & "'," & vbCrLf
				lstrSql = lstrSql & "            `" & istrTabela & "`.`idagente` = " & Me.txtIdagente.Text & "," & vbCrLf
				lstrSql = lstrSql & "            `" & istrTabela & "`.`ano`      = " & Me.txtAno.Text & "," & vbCrLf
				lstrSql = lstrSql & "            `" & istrTabela & "`.`trimestre`= " & Me.txtTrimestre.Text & "," & vbCrLf
				If Me.OptRural.Checked = True Then
					lstrSql = lstrSql & "          `" & istrTabela & "`.`tipobeneficio` = '1- Rural'," & vbCrLf
				ElseIf Me.OptRuralIrr.Checked = True Then 
					lstrSql = lstrSql & "          `" & istrTabela & "`.`tipobeneficio` = '2- Rural com Irrigação' ," & vbCrLf
				ElseIf Me.OptRuralAquic.Checked = True Then 
					lstrSql = lstrSql & "          `" & istrTabela & "`.`tipobeneficio` = '3- Rural com Aquicultura' ," & vbCrLf
				ElseIf Me.OptAguaEsgSanea.Checked = True Then 
					lstrSql = lstrSql & "          `" & istrTabela & "`.`tipobeneficio` = '4- Agua, esgoto e saneamento' ," & vbCrLf
				End If
				If Me.optGrupoA.Checked = True Then
					lstrSql = lstrSql & "          `" & istrTabela & "`.`grupo` = 'A', " & vbCrLf
				ElseIf Me.optGrupoB.Checked = True Then 
					lstrSql = lstrSql & "          `" & istrTabela & "`.`grupo` = 'B', " & vbCrLf
				End If
				lstrSql = lstrSql & "          `" & istrTabela & "`.`datainicial` = '" & VB6.Format(dtpDt_Inicial._Value, "yyyy-mm-dd") & "'," & vbCrLf
				lstrSql = lstrSql & "          `" & istrTabela & "`.`datafinal` = '" & VB6.Format(dtpDt_Final._Value, "yyyy-mm-dd") & "'," & vbCrLf
				lstrSql = lstrSql & "          `" & istrTabela & "`.`datacancelamento` = '" & VB6.Format(dtpDt_Cancelamento._Value, "yyyy-mm-dd") & "'" & vbCrLf
				Call Executar(lstrSql)
			Else
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `" & istrTabela & "` SET "
				lstrSql = lstrSql & "       `id` = '" & UCase(Me.txtId.Text) & "'," & vbCrLf
				lstrSql = lstrSql & "       `idagente` = " & Me.txtIdagente.Text & "," & vbCrLf
				lstrSql = lstrSql & "       `ano`      = " & Me.txtAno.Text & "," & vbCrLf
				lstrSql = lstrSql & "       `trimestre`= " & Me.txtTrimestre.Text & "," & vbCrLf
				If Me.OptRural.Checked = True Then
					lstrSql = lstrSql & "     `tipobeneficio` = '1- Rural'," & vbCrLf
				ElseIf Me.OptRuralIrr.Checked = True Then 
					lstrSql = lstrSql & "     `tipobeneficio` = '2- Rural com Irrigação' ," & vbCrLf
				ElseIf Me.OptRuralAquic.Checked = True Then 
					lstrSql = lstrSql & "     `tipobeneficio` = '3- Rural com Aquicultura' ," & vbCrLf
				ElseIf Me.OptAguaEsgSanea.Checked = True Then 
					lstrSql = lstrSql & "     `tipobeneficio` = '4- Agua, esgoto e saneamento' ," & vbCrLf
				End If
				If Me.optGrupoA.Checked = True Then
					lstrSql = lstrSql & "     `grupo` = 'A', " & vbCrLf
				ElseIf Me.optGrupoB.Checked = True Then 
					lstrSql = lstrSql & "     `grupo` = 'B', " & vbCrLf
				End If
				lstrSql = lstrSql & "       `datainicial` = '" & VB6.Format(dtpDt_Inicial._Value, "yyyy-mm-dd") & "'," & vbCrLf
				lstrSql = lstrSql & "       `datafinal` = '" & VB6.Format(dtpDt_Final._Value, "yyyy-mm-dd") & "'," & vbCrLf
				lstrSql = lstrSql & "       `datacancelamento` = '" & VB6.Format(dtpDt_Cancelamento._Value, "yyyy-mm-dd") & "'" & vbCrLf
				lstrSql = lstrSql & " WHERE `id`  = '" & lstrId & "'"
				Call Executar(lstrSql)
			End If
			
			Me.txtId.Text = ""
			Me.txtIdagente.Text = ""
			Me.txtAno.Text = ""
			Me.txtTrimestre.Text = ""
			Me.OptRural.Checked = False
			Me.OptRuralIrr.Checked = False
			Me.OptRuralAquic.Checked = False
			Me.OptAguaEsgSanea.Checked = False
			Me.optGrupoA.Checked = False
			Me.optGrupoB.Checked = False
			Me.dtpDt_Inicial._Value = VB6.Format(BuscaDataBD, "dd/mm/yyyy")
			Me.dtpDt_Final._Value = VB6.Format(BuscaDataBD, "dd/mm/yyyy")
			Me.dtpDt_Cancelamento._Value = VB6.Format(BuscaDataBD, "dd/mm/yyyy")
		End If
	End Sub
	
	Private Sub frmCadRevGrupos_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim vControl As System.Windows.Forms.Control
		Dim lstrKeyPri As String
		Dim lintAux As Short
		Me.dtpDt_Inicial._Value = VB6.Format(BuscaDataBD, "dd/mm/yyyy")
		Me.dtpDt_Final._Value = VB6.Format(BuscaDataBD, "dd/mm/yyyy")
		Me.dtpDt_Cancelamento._Value = VB6.Format(BuscaDataBD, "dd/mm/yyyy")
		Call CenterFormQuemChama(Me, iFrmQuemChama)
		istrTabela = "cadrev_grupos"
		irstTabela = New ADODB.Recordset
		ifrmLocalizar = New frmLocalizadorGeralNovo
		
	End Sub
	
	Private Sub frmCadRevGrupos_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Call mdlControleForms.ctrl_form_FormKeyPress(Me, KeyAscii)
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmCadRevGrupos_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If Not iFrmQuemChama Is Nothing Then iFrmQuemChama.Enabled = True
		'UPGRADE_NOTE: Object iFrmQuemChama 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"'
		iFrmQuemChama = Nothing
		'UPGRADE_NOTE: Object ifrmLocalizar 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"'
		ifrmLocalizar = Nothing
		'UPGRADE_NOTE: Object irstTabela 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"'
		irstTabela = Nothing
	End Sub
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form)
		iFrmQuemChama = afrmQuemChama
		iFrmQuemChama.Enabled = False
	End Sub
End Class