Option Strict Off
Option Explicit On

Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Friend Class frmPreencherEnderecos
    Inherits System.Windows.Forms.Form

    Private vblnTodasUf As Boolean
    Private vstrEndUf As String
    Private vstrEndCid As String
    Private vstrEndDis As String
    Private vstrEndBai As String
    Private vstrEndRua As String
    Private vstrEndCep As String
    Private vstrEndCod As String
    Private vUF, vLogra, vBairro, vDistrito, vMunic



#Region "Propriedades"

    Private Sub FrmPreencherEnde_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Try



        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdConfirmar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdConfirmar.Click


        Try

            If Trim(vstrEndCod) <> "" Then
                vfrmOld.SetaCamposEndereco(StrConv(vstrEndUf, VbStrConv.Uppercase), StrConv(vstrEndCid, VbStrConv.Uppercase), StrConv(vstrEndDis, VbStrConv.Uppercase), StrConv(vstrEndBai, VbStrConv.Uppercase), StrConv(vstrEndRua, VbStrConv.Uppercase), StrConv(vstrEndCep, VbStrConv.Uppercase), vstrEndCod)
            End If

            Me.Close()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdCancelar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancelar.Click

        Try

            vfrmNew = Nothing
            Me.Close()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub FrmPreencherEnde_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try

            vfrmOld.Enabled = True

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub FrmPreencherEnde_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Escape Then
                Me.Close()
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub TrvData_NodeClick(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TrvData.NodeMouseClick

        Dim Node As System.Windows.Forms.TreeNode = eventArgs.Node
        Dim lstrManda As String
        Dim codNode

        Try

            Select Case Node.Tag ' Ve qual tipo de Nó (Registro ou Tabela)

                Case "Estado" 'Carrega os registros da tabela
                    CarregaUf(Node)
                Case "RegUF" 'Carrega o estado
                    'lstrManda = Mid(Trim(Node.Text), 1, 2)
                    'lstrManda = Replace(Mid(Trim(Node.Text), 1, 2), ",", "")
                    'vUF = lstrManda
                    codNode = Split(Trim(Node.Text), ",", -1)
                    vUF = Trim(codNode(2))
                    CarregaCidade(Node, vUF)
                Case "RegCid" 'Carrega a cidade
                    'lstrManda = Mid(Trim(Node.Text), 1, 6)
                    'lstrManda = Replace(Mid(Trim(Node.Text), 1, 2), ",", "")
                    'vMunic = lstrManda
                    codNode = Split(Trim(Node.Text), ",", -1)
                    vMunic = Trim(codNode(0))
                    CarregaDistrito(Node, vMunic)
                Case "RegDistrito" ' Carrega o distrito
                    'lstrManda = Mid(Trim(Node.Text), 1, 8)
                    'lstrManda = Replace(Mid(Trim(Node.Text), 1, 2), ",", "")
                    'vDistrito = lstrManda
                    codNode = Split(Trim(Node.Text), ",", -1)
                    vDistrito = codNode(0)
                    CarregaBairro(Node, vDistrito)
                Case "RegBair" ' Carrega o bairro
                    'lstrManda = Mid(Trim(Node.Text), 1, 11)
                    'lstrManda = Replace(Mid(Trim(Node.Text), 1, 2), ",", "")
                    'vBairro = lstrManda
                    codNode = Split(Trim(Node.Text), ",", -1)
                    vBairro = codNode(0)
                    CarregaLogra(Node, vBairro)
                Case "RegLogra" ' Carrega o logradouro
                    'lstrManda = Mid(Trim(Node.Text), 1, 15)
                    'lstrManda = Replace(Mid(Trim(Node.Text), 1, 3), ",", "")
                    lstrManda = Trim(Node.Text)
                    codNode = Split(lstrManda, ",", -1)
                    vLogra = codNode(0)
                    CarregaAllGlobal(vLogra)

            End Select

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub


#End Region

#Region "Métodos"

    Public Sub SetaPreencherEnde(ByVal afrmQuemChama As System.Windows.Forms.Form, Optional ByRef ablnTodasUf As Boolean = True, Optional ByVal astrCodigo As String = "")

        Try

            vfrmOld = afrmQuemChama
            vfrmOld.Enabled = False
            vblnTodasUf = ablnTodasUf
            vstrEndCod = astrCodigo
            Lista()

        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Private Sub Lista()

        Dim lndeTabela As System.Windows.Forms.TreeNode

        Try

            With Me.TrvData
                .Nodes.Clear() 'Limpa o TreeView
                .ImageList = Me.imlAuxiliar
                lndeTabela = .Nodes.Add("", "Estado", "fechado") 'Associa a imagem
                lndeTabela.Tag = "Estado"
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CarregaUf(ByVal andeTabela As System.Windows.Forms.TreeNode)

        Dim lndeNoReg As System.Windows.Forms.TreeNode
        Dim lstrAuxiliar As String

        Dim vlist
        Dim objUf As New entidadeUf
        Dim repositorioUf As New repositorioUf

        Try

            'Se os registros da tabela foram carregados não faz nada
            If andeTabela.GetNodeCount(False) < 1 Then

                If vblnTodasUf = False Then
                    objUf.UF = gstrEmpresa_UF
                Else
                    objUf.UF = String.Empty
                End If

                vlist = repositorioUf.SelectOthersTables_1(objUf)

                lstrAuxiliar = ""

                For Each objUfFor As entidadeUf In vlist

                    'Constroi a string do registro
                    lstrAuxiliar = ""
                    lstrAuxiliar = lstrAuxiliar & ", " & objUfFor.COD & ", " & objUfFor.UF & ", " & objUfFor.DESCR
                    lstrAuxiliar = Mid(lstrAuxiliar, 2)

                    'Inclui o registro no nó
                    lndeNoReg = TrvData.Nodes.Find(andeTabela.Name, True)(0).Nodes.Add("", lstrAuxiliar, "semiaberto")
                    lndeNoReg.Tag = "RegUF"
                    lndeNoReg.EnsureVisible()

                Next

            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CarregaCidade(ByVal andeTabela As System.Windows.Forms.TreeNode, ByVal astrCodigo As String)

        Dim lndeNoReg As System.Windows.Forms.TreeNode
        Dim lstrAuxiliar As String

        Dim vlist
        Dim objMunic As New entidadeMunic
        Dim repositorioMunic As New repositorioMunic

        Try
            'Se os registros da tabela foram carregados não faz nada
            If andeTabela.GetNodeCount(False) < 1 Then

                'objMunic.COD = astrCodigo
                objMunic.NOME = astrCodigo

                'vlist = repositorioMunic.SelectOthersTables_1(objMunic)
                'vlist = repositorioMunic.SelectOthersTables_3(objMunic)
                vlist = repositorioMunic.SelectOthersTables_5(objMunic)

                lstrAuxiliar = ""

                For Each objMunicFor As entidadeMunic In vlist

                    'Constroi a string do registro
                    lstrAuxiliar = ""
                    lstrAuxiliar = lstrAuxiliar & ", " & objMunicFor.COD & ", " & objMunicFor.NOME
                    lstrAuxiliar = Mid(lstrAuxiliar, 2)

                    'Inclui o registro no Nó
                    lndeNoReg = TrvData.Nodes.Find(andeTabela.Name, True)(0).Nodes.Add("", lstrAuxiliar, "semiaberto")
                    lndeNoReg.Tag = "RegCid"
                    lndeNoReg.EnsureVisible()

                Next

            End If


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub



    Private Sub CarregaDistrito(ByVal andeTabela As System.Windows.Forms.TreeNode, ByVal astrCodigo As String)

        Dim lndeNoReg As System.Windows.Forms.TreeNode
        Dim lstrAuxiliar As String

        Dim vlist
        Dim objDistrito As New entidadeDistrito
        Dim repositorioDistrito As New repositorioDistrito

        Try
            'Se os registros da tabela foram carregados não faz nada
            If andeTabela.GetNodeCount(False) < 1 Then

                'objDistrito.COD = astrCodigo
                objDistrito.CODCID = astrCodigo

                'vlist = repositorioDistrito.SelectOthersTables_1(objDistrito)
                vlist = repositorioDistrito.SelectOthersTables_3(objDistrito)

                lstrAuxiliar = ""

                For Each objDistritoFor As entidadeDistrito In vlist

                    'Constroi a string do registro
                    lstrAuxiliar = ""
                    lstrAuxiliar = lstrAuxiliar & ", " & objDistritoFor.COD & ", " & objDistritoFor.NOME
                    lstrAuxiliar = Mid(lstrAuxiliar, 2)

                    'Inclui o registro no Nó
                    lndeNoReg = TrvData.Nodes.Find(andeTabela.Name, True)(0).Nodes.Add("", lstrAuxiliar, "semiaberto")
                    lndeNoReg.Tag = "RegDistrito"
                    lndeNoReg.EnsureVisible()

                Next

            End If


        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Private Sub CarregaBairro(ByVal andeTabela As System.Windows.Forms.TreeNode, ByVal astrCodigo As String)

        Dim lndeNoReg As System.Windows.Forms.TreeNode
        Dim lstrAuxiliar As String

        Dim vlist
        Dim objBairro As New entidadeBairro
        Dim repositorioBairro As New repositorioBairro

        Try
            'Se os registros da tabela foram carregados não faz nada
            If andeTabela.GetNodeCount(False) < 1 Then

                objBairro.CODDIST = astrCodigo

                'vlist = repositorioBairro.SelectOthersTables_1(objBairro)
                vlist = repositorioBairro.SelectOthersTables_3(objBairro)

                lstrAuxiliar = ""

                For Each objBairroFor As entidadeBairro In vlist

                    'Constroi a string do registro
                    lstrAuxiliar = ""
                    lstrAuxiliar = lstrAuxiliar & ", " & objBairroFor.COD & ", " & objBairroFor.NOME
                    lstrAuxiliar = Mid(lstrAuxiliar, 2)

                    'Inclui o registro no Nó
                    lndeNoReg = TrvData.Nodes.Find(andeTabela.Name, True)(0).Nodes.Add("", lstrAuxiliar, "semiaberto")
                    lndeNoReg.Tag = "RegBair"
                    lndeNoReg.EnsureVisible()

                Next

            End If


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CarregaLogra(ByVal andeTabela As System.Windows.Forms.TreeNode, ByVal astrCodigo As String)

        Dim lndeNoReg As System.Windows.Forms.TreeNode
        Dim lstrAuxiliar As String

        Dim vlist
        Dim objLogradouro As New entidadeLogradouro
        Dim repositorioLogradouro As New repositorioLogradouro

        Try
            'Se os registros da tabela foram carregados não faz nada
            If andeTabela.GetNodeCount(False) < 1 Then

                'objLogradouro.COD = astrCodigo
                objLogradouro.CODBAI = astrCodigo

                'vlist = repositorioLogradouro.SelectOthersTables_1(objLogradouro)
                vlist = repositorioLogradouro.SelectOthersTables_3(objLogradouro)

                lstrAuxiliar = ""

                For Each objLogradouroFor As entidadeLogradouro In vlist

                    'Constroi a string do registro
                    lstrAuxiliar = ""
                    lstrAuxiliar = lstrAuxiliar & ", " & objLogradouroFor.COD & ", " & objLogradouroFor.NOME & ", " & objLogradouroFor.CEP
                    lstrAuxiliar = Mid(lstrAuxiliar, 2)

                    'Inclui o registro no Nó
                    lndeNoReg = TrvData.Nodes.Find(andeTabela.Name, True)(0).Nodes.Add("", lstrAuxiliar, "semiaberto")
                    lndeNoReg.Tag = "RegLogra"
                    lndeNoReg.EnsureVisible()

                Next

            End If


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CarregaAllGlobal(ByVal astrCodigo As String)

        Dim dt As DataTable
        Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
        Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
        'Dim objUf As New entidadeUf
        'Dim objLogradouro As New entidadeLogradouro
        'Dim objBairro As New entidadeBairro
        'Dim objDistrito As New entidadeDistrito
        'Dim objMunic As New entidadeMunic
        'Dim repositorioUf As New repositorioUf

        Try

            'objUf.COD = astrCodigo

            'dt = repositorioUf.SelectOthersTables_3(objUf)
            'dt = repositorioUf.SelectOthersTables_2(objUf)

            Dim lstrsql As String
            lstrsql = "SELECT "
            lstrsql = lstrsql & " U.UF   AS NMUF , "
            lstrsql = lstrsql & " M.NOME AS NMCID,  "
            lstrsql = lstrsql & " D.NOME AS NMDIS,  "
            lstrsql = lstrsql & " B.NOME AS NMBAI, "
            lstrsql = lstrsql & " L.NOME AS NMRUA, "
            lstrsql = lstrsql & " L.CEP  AS NMCEP "
            lstrsql = lstrsql & " FROM UF       AS U, "
            lstrsql = lstrsql & " MUNIC    AS M, "
            lstrsql = lstrsql & " DISTRITO AS D,  "
            lstrsql = lstrsql & " BAIRRO   AS B, "
            lstrsql = lstrsql & " LOGRA    AS L  "
            lstrsql = lstrsql & " WHERE 1 = 1 "
            lstrsql = lstrsql & " AND L.COD = " & vLogra
            lstrsql = lstrsql & " AND B.COD = " & vBairro
            lstrsql = lstrsql & " AND D.COD = " & vDistrito
            lstrsql = lstrsql & " AND M.COD = " & vMunic
            lstrsql = lstrsql & " AND U.DESCR = '" & vUF & "'"
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            For Each row As DataRow In dt.Rows

                txtCodEnd.Text = astrCodigo
                vstrEndCod = astrCodigo

                txtLogradouro.Text = Convert.ToString(row("NMRUA"))
                vstrEndRua = Convert.ToString(row("NMRUA"))

                txtCep.Text = Convert.ToString(row("NMCEP"))
                vstrEndCep = Convert.ToString(row("NMCEP"))

                txtBairro.Text = Convert.ToString(row("NMBAI"))
                vstrEndBai = Convert.ToString(row("NMBAI"))

                txtDistrito.Text = Convert.ToString(row("NMDIS"))
                vstrEndDis = Convert.ToString(row("NMDIS"))

                txtCidade.Text = Convert.ToString(row("NMCID"))
                vstrEndCid = Convert.ToString(row("NMCID"))

                txtEstado.Text = Convert.ToString(row("NMUF"))
                vstrEndUf = Convert.ToString(row("NMUF"))

            Next row

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

#End Region

End Class