Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmCadPermissaoMenu
	Inherits System.Windows.Forms.Form
	
	Private istrSql As String
	Private iintIdx As Short
	
	'UPGRADE_WARNING: Event cmbGrupo.SelectedIndexChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub cmbGrupo_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbGrupo.SelectedIndexChanged
		Call LeAcesso()
	End Sub
	
	Private Sub cmdCancelar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancelar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdRegistrar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRegistrar.Click
		Call SalvarAcesso()
	End Sub
	
	Private Sub frmCadPermissaoMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lobjControl As Object
		Dim lstrDescri As String
		Dim lstrFilho As String
		Dim lstrNoPai As String
		Dim lintPContr As Short
		Dim lintPAtual As Short
		Dim lintCTraco As Short
		lintPAtual = 0
		lintPContr = 0
		lintCTraco = 0
		lstrNoPai = "mnuPrincipal"
		With Me.tevMenus.Nodes
			Call .Add(lstrNoPai, "Opções de Menus")
			'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			.Item(.Count).Expand()
			For	Each lobjControl In FrmAtPrincipal.Controls
				If TypeOf lobjControl Is System.Windows.Forms.ToolStripMenuItem Then
					'UPGRADE_WARNING: Couldn't resolve default property of object lobjControl.Caption. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrDescri = Str_Retira(lobjControl.Caption, "&")
					'UPGRADE_WARNING: Couldn't resolve default property of object lobjControl.Name. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrFilho = lobjControl.Name
					If lstrDescri <> "" And lstrDescri <> "-" Then
						lintPContr = InStr(1, lstrFilho, "_")
						lintPAtual = lintPContr
						While lintPContr > 1
							lintCTraco = lintCTraco + 1
							lintPContr = InStr(lintPContr + 1, lstrFilho, "_")
							If lintPContr > 0 Then
								lintPAtual = lintPContr
							End If
						End While
						If lintCTraco = 0 Then
							lstrNoPai = "mnuPrincipal"
						Else
							lstrNoPai = Mid(lstrFilho, 1, lintPAtual - 1)
						End If
						'UPGRADE_WARNING: Add method behavior has changed Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="DBD08912-7C17-401D-9BE9-BA85E7772B99"'
						Call .Find(lstrNoPai, True)(0).Nodes.Add(lstrFilho, lstrDescri)
						'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Item(.Count).Expand()
						lintCTraco = 0
					End If
				End If
			Next lobjControl
		End With
		Call CarregaTabelaComboBox((Me.cmbGrupo), "`usuarios_grupos`", "`descricao`", "`id`", "`descricao`",  , 1)
	End Sub
	
	Private Sub SalvarAcesso()
		Dim lmd5Aux As clsMd5
		Dim lstrIdM As String
		Dim lstrVlr As String
		lmd5Aux = New clsMd5
		Me.lblInfo.Text = "Gravando permissões nos menus dos grupos de usuários"
		Call Me.lblInfo.Refresh()
		istrSql = ""
		istrSql = istrSql & "DELETE FROM `usuarios_grupos_permissao_menu` " & vbCrLf
		istrSql = istrSql & "      WHERE `usuarios_grupos_permissao_menu`.`idgrupos` = " & VB6.GetItemData(Me.cmbGrupo, Me.cmbGrupo.SelectedIndex)
		Call myConC.BeginTrans()
		Call Executar(istrSql)
		Call myConC.CommitTrans()
		With Me.tevMenus.Nodes
			If .Count > 0 Then
				istrSql = ""
				istrSql = istrSql & "INSERT INTO `usuarios_grupos_permissao_menu` (`idgrupos`, `idmenus`, `valor`) VALUES "
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Count)
				For iintIdx = 1 To .Count
					'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					lstrVlr = IIf(.Item(iintIdx).Checked = True, "SIM", "NAO")
					'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					lstrIdM = .Item(iintIdx).Name
					istrSql = istrSql & vbCrLf & "(" & VB6.GetItemData(Me.cmbGrupo, Me.cmbGrupo.SelectedIndex) & ",'" & LCase(lmd5Aux.DigestStrToHexStr(lstrIdM)) & "', '" & LCase(lmd5Aux.DigestStrToHexStr(lstrIdM & "_" & lstrVlr)) & "'), "
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				Next iintIdx
				Call myConC.BeginTrans()
				Call Executar(VB.Left(istrSql, Len(istrSql) - 2))
				Call myConC.CommitTrans()
			End If
		End With
		Me.lblInfo.Text = " Finalizando a gravação de permissão nos menus do grupo de usuário! "
		Call Me.lblInfo.Refresh()
		'UPGRADE_NOTE: Object lmd5Aux 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"'
		lmd5Aux = Nothing
	End Sub
	
	Private Sub LeAcesso()
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Me.cmbGrupo.SelectedIndex >= 0 Then
			Call CarregaGridUsuGruPerMnu()
			Me.tevMenus.Enabled = False
			With Me.tevMenus.Nodes
				If .Count > 0 Then
					' Desmarcando os itens do menu
					For iintIdx = 1 To .Count
						'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Item(iintIdx).Checked = False
					Next iintIdx
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Count)
					Me.lblInfo.Text = " Buscando permissões dos grupos"
					Call Me.lblInfo.Refresh()
					'Checkando os itens do menu
					For iintIdx = 1 To .Count
						'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Item(iintIdx).Checked = GetValorMenu(.Item(iintIdx).Name)
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						System.Windows.Forms.Application.DoEvents()
					Next iintIdx
				End If
			End With
			Me.tevMenus.Enabled = True
			Me.lblInfo.Text = ""
			Call Me.lblInfo.Refresh()
		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 Function GetValorMenu(ByVal astrMenu As String) As Boolean
		Dim lmd5Aux As clsMd5
		Dim lstrVlr As String
		Dim lstrMnu As String
		Dim llngRow As Integer
		lmd5Aux = New clsMd5
		
		With Me.mfgUsuariosGruposPermissaoMenu
			For llngRow = 1 To .Rows - 1
				lstrMnu = LCase(lmd5Aux.DigestStrToHexStr(astrMenu))
				If .get_TextMatrix(llngRow, 0) = lstrMnu Then
					lstrVlr = LCase(lmd5Aux.DigestStrToHexStr(astrMenu & "_SIM"))
					GetValorMenu = (.get_TextMatrix(llngRow, 1) = lstrVlr)
					llngRow = .Rows - 1
				Else
					GetValorMenu = False
				End If
			Next llngRow
		End With
		'UPGRADE_NOTE: Object lmd5Aux 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"'
		lmd5Aux = Nothing
	End Function
	
	Private Sub CarregaGridUsuGruPerMnu()
		Dim lstrIdM As String
		Dim lstrVlr As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		With Me.mfgUsuariosGruposPermissaoMenu
			.Redraw = False
			.Rows = 2
			.FixedRows = 1
			.FixedCols = 0
			Call .Clear()
			.set_TextMatrix(0, 0, "Menu")
			.set_TextMatrix(0, 1, "Valor")
			.set_ColWidth(0, 3000)
			.set_ColWidth(1, 3000)
			.set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
			.set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
		End With
		istrSql = ""
		istrSql = istrSql & "SELECT `usuarios_grupos_permissao_menu`.`idmenus`, "
		istrSql = istrSql & "       `usuarios_grupos_permissao_menu`.`valor`"
		istrSql = istrSql & "  FROM `usuarios_grupos_permissao_menu` "
		istrSql = istrSql & " WHERE `usuarios_grupos_permissao_menu`.`idgrupos` = " & VB6.GetItemData(Me.cmbGrupo, Me.cmbGrupo.SelectedIndex)
		Call Conectar(istrSql, lrstAux)
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					Me.mfgUsuariosGruposPermissaoMenu.Rows = 1
					While Not .EOF
						'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"'
						lstrIdM = IIf(IsDbNull(.Fields("idmenus").value), "", .Fields("idmenus").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"'
						lstrVlr = IIf(IsDbNull(.Fields("valor").value), "", .Fields("valor").value)
						
						With Me.mfgUsuariosGruposPermissaoMenu
							.Rows = .Rows + 1
							.set_TextMatrix(.Rows - 1, 0, lstrIdM)
							.set_TextMatrix(.Rows - 1, 1, lstrVlr)
						End With
						Call .MoveNext()
					End While
				End If
				Call .Close()
			End If
		End With
		Me.mfgUsuariosGruposPermissaoMenu.Redraw = True
		'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 tevMenus_AfterCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.TreeViewEventArgs) Handles tevMenus.AfterCheck
		Dim Node As System.Windows.Forms.TreeNode = eventArgs.Node
		Dim lintIdx As Short
		With Me.tevMenus
			.SelectedNode = Node
			'Marca os filhos quado o pai for selecionado
			If .SelectedNode.GetNodeCount(False) Then
				'UPGRADE_ISSUE: MSComctlLib.Node property tevMenus.SelectedItem.Child.LastSibling was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
				'UPGRADE_ISSUE: MSComctlLib.Node property tevMenus.SelectedItem.Child.FirstSibling was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
				For iintIdx = .SelectedNode.FirstNode.FirstSibling.Index To .SelectedNode.FirstNode.LastSibling.Index
					'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Nodes.Item(iintIdx).Checked = .SelectedNode.Checked
					'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					If .Nodes.Item(iintIdx).GetNodeCount(False) Then
						'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						'UPGRADE_ISSUE: MSComctlLib.Node property tevMenus.Nodes.Item.Child.LastSibling was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
						'UPGRADE_ISSUE: MSComctlLib.Node property tevMenus.Nodes.Item.Child.FirstSibling was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
						For lintIdx = .Nodes.Item(iintIdx).FirstNode.FirstSibling.Index To .Nodes.Item(iintIdx).FirstNode.LastSibling.Index
							'UPGRADE_WARNING: Lower bound of collection Me.tevMenus.Nodes has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							.Nodes.Item(lintIdx).Checked = .Nodes.Item(iintIdx).Checked
						Next lintIdx
					End If
				Next iintIdx
			End If
			'Quando um dos filhos for marcado, entao marca os pais
			If Node.Checked Then
				If Not Node.Parent Is Nothing Then
					Node.Parent.Checked = True
					If Not Node.Parent.Parent Is Nothing Then
						Node.Parent.Parent.Checked = True
					End If
				End If
			End If
		End With
	End Sub
End Class