Option Strict Off
Option Explicit On

Imports System.Collections.Generic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmVisAuditoriaTabelas
    Inherits System.Windows.Forms.Form

#Region "Propriedades"

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

        Try


            lAtualizaListBox()

        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 lsbComentarios_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lsbComentarios.SelectedIndexChanged


        Try

            Me.lsbTabelas.SelectedIndex = Me.lsbComentarios.SelectedIndex

            lClear()
            PreencheCombobox(VB6.GetItemString(Me.lsbTabelas, Me.lsbTabelas.SelectedIndex))

            lAtualizaList(VB6.GetItemString(Me.lsbTabelas, Me.lsbTabelas.SelectedIndex))

        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 cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click

        Try


            lClose()

        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 cmdLocalizar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizar.Click


        Try

            Me.lsbTabelas.SelectedIndex = Me.lsbComentarios.SelectedIndex

            lAtualizaList(VB6.GetItemString(Me.lsbTabelas, Me.lsbTabelas.SelectedIndex))


        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 frmVisAuditoriaTabelas_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
                cmdFechar_Click(cmdFechar, New System.EventArgs())
            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


#End Region

#Region "Métodos"

    Private Sub lAtualizaList(ByVal astrTabela As String)

        Dim vFiltro As String
        Dim vCampo As String
        Dim vSQL As String

        Dim vlist

        Dim dt As DataTable

        Dim objConsultaAlteracoes As New entidadeConsultaAlteracoes
        Dim repositorioConsultaAlteracoes As New repositorioConsultaAlteracoes

        Try

            vCampo = String.Empty

            vFiltro = String.Empty

            If Trim(Me.txtFiltro.Text) <> String.Empty Then
                vFiltro = txtFiltro.Text
                vCampo = VB6.GetItemString(Me.cmbCampos, Me.cmbCampos.SelectedIndex)
            End If

            vSQL = lQueryDados(vFiltro, astrTabela, vCampo)

            objConsultaAlteracoes.QUERY_NAME = vSQL

            dt = repositorioConsultaAlteracoes.Select_1(objConsultaAlteracoes)

            lvwDados.BeginUpdate()

            If lvwDados.Columns.Count <> dt.Columns.Count Then
                lvwDados.Columns.Clear()

                For Each column As DataColumn In dt.Columns
                    lvwDados.Columns.Add(column.ColumnName)
                Next
            End If

            lvwDados.Items.Clear()

            For Each row As DataRow In dt.Rows
                Dim item As ListViewItem = New ListViewItem(row(0).ToString())

                For i As Integer = 1 To dt.Columns.Count - 1
                    item.SubItems.Add(row(i).ToString())
                Next

                lvwDados.Items.Add(item)
            Next

            lvwDados.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            lvwDados.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
            lvwDados.EndUpdate()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lAtualizaListBox()

        Dim lstrTab As String
        Dim lstrCom As String

        Dim vlist

        Dim objConsultaAlteracoes As New entidadeConsultaAlteracoes
        Dim repositorioConsultaAlteracoes As New repositorioConsultaAlteracoes

        Try

            Me.lsbComentarios.Items.Clear()
            Me.lsbTabelas.Items.Clear()

            objConsultaAlteracoes.TABLE_SCHEMA = vMySqlDataBase
            objConsultaAlteracoes.TABLE_NAME = "vw%_auditoria%"

            vlist = repositorioConsultaAlteracoes.Select(objConsultaAlteracoes)

            If vlist.Count > 0 Then

                For Each objConsultaAlteracoesFor As entidadeConsultaAlteracoes In vlist

                    lstrTab = Trim(IIf(IsDBNull(objConsultaAlteracoesFor.TABLE_NAME), "", objConsultaAlteracoesFor.TABLE_NAME))
                    lstrCom = PegaComentarioTabela(lstrTab)

                    Me.lsbComentarios.Items.Add(lstrCom)
                    Me.lsbTabelas.Items.Add(lstrTab)

                Next

            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PreencheCombobox(ByVal astrTabela As String)


        Try

            Dim repositorioConsultaAlteracoes As New repositorioConsultaAlteracoes

            Dim listaConsultaAlteracoes As New List(Of entidadeConsultaAlteracoes)

            Dim objConsultaAlteracoes As New entidadeConsultaAlteracoes()

            objConsultaAlteracoes.TABLE_SCHEMA = vMySqlDataBase
            objConsultaAlteracoes.TABLE_NAME = astrTabela

            listaConsultaAlteracoes = repositorioConsultaAlteracoes.Select_3(objConsultaAlteracoes)

            cmbCampos.DataSource = listaConsultaAlteracoes
            cmbCampos.DisplayMember = "COLUMN_NAME"

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Function PegaComentarioTabela(ByVal astrTabela As String) As String

        Dim lstrTab As String
        Dim lstrRet As String

        Dim vlist

        Dim objConsultaAlteracoes As New entidadeConsultaAlteracoes
        Dim repositorioConsultaAlteracoes As New repositorioConsultaAlteracoes

        Try

            lstrRet = String.Empty
            lstrTab = Replace(Mid(astrTabela, 3), "_auditoria", "")

            objConsultaAlteracoes.TABLE_SCHEMA = vMySqlDataBase
            objConsultaAlteracoes.TABLE_NAME = lstrTab

            vlist = repositorioConsultaAlteracoes.Select_2(objConsultaAlteracoes)

            If vlist.Count > 0 Then

                For Each objConsultaAlteracoesFor As entidadeConsultaAlteracoes In vlist
                    lstrRet = Trim(IIf(IsDBNull(objConsultaAlteracoesFor.TABLE_COMMENT), "", objConsultaAlteracoesFor.TABLE_COMMENT))
                Next

            End If


            PegaComentarioTabela = lstrRet

        Catch ex As Exception
            Exit Function
        End Try

    End Function

    Private Function lQueryDados(ByVal pFiltro As String, ByVal pTabela As String, ByVal pCampo As String) As String

        Dim vSQL As String

        Dim vlist

        Dim objConsultaAlteracoes As New entidadeConsultaAlteracoes
        Dim repositorioConsultaAlteracoes As New repositorioConsultaAlteracoes

        Try

            vSQL = String.Empty
            vSQL = vSQL & "SELECT "

            objConsultaAlteracoes.TABLE_SCHEMA = vMySqlDataBase
            objConsultaAlteracoes.TABLE_NAME = pTabela

            vlist = repositorioConsultaAlteracoes.Select_3(objConsultaAlteracoes)

            For Each objConsultaAlteracoesFor As entidadeConsultaAlteracoes In vlist
                vSQL = vSQL & "`" & pTabela & "`.`" & objConsultaAlteracoesFor.COLUMN_NAME & "`, "
            Next

            vSQL = Microsoft.VisualBasic.Left(vSQL, Len(vSQL) - 2) & vbCrLf
            vSQL = vSQL & "  FROM `" & pTabela & "` " & vbCrLf
            If Len(pFiltro) > 0 Then
                vSQL = vSQL & " WHERE `" & pCampo & "` = '" & pFiltro & "' "
            End If

            Return vSQL

        Catch ex As Exception
            Exit Function
        End Try

    End Function

    Private Sub lClose()

        Try

            Close()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lClear()

        Try

            Me.cmbCampos.Items.Clear()
            Me.txtFiltro.Text = String.Empty

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

#End Region


End Class