Option Strict Off
Option Explicit On
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports System.IO
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmHistorico
	Inherits System.Windows.Forms.Form
	Private ilngUnidId As Integer
	Private ilngConsId As Integer
	Private istrGrupo As String
	Private dt, dt2, dt3, dt4 As DataTable
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancosDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancosDados As New repositorioAtualizaBancoDados
	'Private ifrmImpHis As FrmRelatorio

	Public Sub SetaDados(ByVal astrGrupo As String, ByVal alngConsId As Integer, ByVal alngUnidId As Integer)
		Try
			istrGrupo = astrGrupo
			ilngUnidId = alngUnidId
			ilngConsId = alngConsId
			Me.Text = Me.Text & "Grupo " & istrGrupo
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetaDados" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub cmdEditSv_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEditSv.Click
		Dim lstrSql As String
		Try

			If Me.cmdEditSv.Text = "Editar" Then
				Me.cmdEditSv.Text = "Salvar"
				Me.txtHistorico.ReadOnly = False
			Else
				lstrSql = ""
				'lstrSql = lstrSql & "UPDATE `unid" & LCase(istrGrupo) & "` "
				lstrSql = lstrSql & "UPDATE `uc_faturamento` "
				lstrSql = lstrSql & "   SET historico = '" & Me.txtHistorico.Text & "' "
				lstrSql = lstrSql & " WHERE id = " & ilngUnidId & " "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancosDados.Execute(objExecAtualizaBancoDados)

				Me.cmdEditSv.Text = "Editar"
				Me.txtHistorico.ReadOnly = True

			End If

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdEditSv_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	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 cmdImp_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImp.Click
		Call MontaHistorico()
	End Sub
	
	Private Sub cmdProc_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProc.Click
		Dim posA As Short
		Dim posB As Short
		Try
			If CDbl(Me.txtHistorico.Tag) > 0 Then posA = CShort(Me.txtHistorico.Tag)

			If Me.txtProc.Text <> "" Then
				posA = InStr(CShort(Me.txtHistorico.Tag), Me.txtHistorico.Text, Me.txtProc.Text)

				If posA > 0 Then
					posB = Len(Me.txtProc.Text)
					Me.txtHistorico.SelectionStart = posA - 1
					Me.txtHistorico.SelectionLength = posB
					If Me.txtHistorico.Enabled = True Then Me.txtHistorico.Focus()
				Else
					MsgBox("'" & txtProc.Text & "' não encontrado!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				End If

				txtHistorico.Tag = posA + 1
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProc_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub frmHistorico_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmHistorico_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub frmHistorico_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			Dim lstrSql As String

			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nome`, "
			lstrSql = lstrSql & "       `historico` "
			lstrSql = lstrSql & "  FROM `uc_faturamento`" '& ilngUnidId & " "
			'lstrSql = lstrSql & "  FROM `unid" & LCase(istrGrupo) & "` "
			lstrSql = lstrSql & " WHERE id = " & ilngUnidId & " "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancosDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					Me.txtNomeCons.Text = Trim(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))
					Me.txtHistorico.Text = Trim(IIf(IsDBNull(row.Item("historico")), "", row.Item("historico")))
				Next
			End If

			'ifrmImpHis = New FrmRelatorio
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmHistorico_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try


	End Sub

	Private Sub MontaHistorico()
		Dim sNomeRelatorio As String
		Try
			sNomeRelatorio = "rptHistoricoArrec"

			Call lPrint(sNomeRelatorio)
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaHistorico" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Public Function CabecaRel() As Object
		'With ifrmImpHis
		'	Call Cabecalho_Relatorio(.VS, "Historico do assinante", Me.txtNomeCons.Text, CStr(4), False, "")
		'	Call LineFeed(.VS, 1)
		'End With
	End Function
	Private Sub lPrint(ByVal vNomeRPT As String)

		Dim snomeassinante As String
		Dim stextoassinante As String

		Try
			Dim rpt As New ReportDocument()

			If vNomeRPT = "rptHistoricoArrec" Then

				snomeassinante = Trim(txtNomeCons.Text)
				stextoassinante = Trim(txtHistorico.Text)

				If snomeassinante = "" And stextoassinante = "" Then
					MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
					Exit Sub
				Else

					rpt = New rptHistoricoArrec

					'rpt.SetDataSource(ds)
					rpt.SetParameterValue("nomeassinante", snomeassinante)
					rpt.SetParameterValue("textohistorico", stextoassinante)

					frmViewRelatorios.rpt = rpt

					frmViewRelatorios.CRViewer.Zoom((100))
					frmViewRelatorios.Text = "Relatório Recebimento Manual"
					frmViewRelatorios.ShowDialog()

				End If

			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lPrint" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub
End Class