Imports System.Runtime.InteropServices

Public Class frmControlACC
#Region "Mov. Pantalla"
    <DllImport("user32.Dll", EntryPoint:="ReleaseCapture")>
    Private Shared Sub ReleaseCapture()

    End Sub

    <DllImport("user32.Dll", EntryPoint:="SendMessage")>
    Private Shared Sub SendMessage(hWnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer)

    End Sub

#End Region

    Private Sub btnCerrar_Click(sender As Object, e As EventArgs) Handles btnCerrar.Click
        Me.Dispose()
        Me.Close()
        CerrarFormulario()
    End Sub

    Private Sub PanelBarraTitulo_MouseMove(sender As Object, e As MouseEventArgs) Handles PanelBarraTitulo.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub

    Private Sub frmControlACC_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub

    Private Sub lblEmpresa_MouseMove(sender As Object, e As MouseEventArgs) Handles lblEmpresa.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub
    Private Sub frmControlACC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LlenarCombo(Users, "Usuarios", "Nombre", "Nombre")
        LimpiarCampos(Me)
    End Sub
    Sub LimpiarCampos(ByVal c1 As Control)
        'Limpia todos los controles dentro del control principal (c1)
        'aunque esten dentro de otros controles
        For Each c As Control In c1.Controls
            If TypeOf c Is TextBox Then
                c.Text = "" ' eliminar el texto 
            Else
                LimpiarCampos(c)
            End If
            If TypeOf c Is ComboBox Then
                c.Text = "" ' eliminar el texto 
            Else
                LimpiarCampos(c)
            End If
            If TypeOf c Is DateTimePicker Then
                c.Text = Date.Today ' Pone la fecha actual 
            Else
                LimpiarCampos(c)
            End If
            If (TypeOf (c) Is CheckBox) Then
                CType(c, CheckBox).Checked = False 'Quita el check
            Else
                LimpiarCampos(c)
            End If
            If (TypeOf (c) Is RadioButton) Then
                CType(c, RadioButton).Checked = False 'Quita el check
            Else
                LimpiarCampos(c)
            End If
        Next
    End Sub

    Private Sub btnSelectAll_Click(sender As Object, e As EventArgs) Handles btnSelectAll.Click
        If btnSelectAll.Text = "Marcar todos" Then
            btnSelectAll.Text = "Desmarcar todos"
            For Each ctrl As Control In Me.Controls
                If TypeOf ctrl Is CheckBox Then
                    DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked
                End If
            Next
        ElseIf btnSelectAll.Text = "Desmarcar todos" Then
            btnSelectAll.Text = "Marcar todos"
            For Each ctrl As Control In Me.Controls
                If TypeOf ctrl Is CheckBox Then
                    DirectCast(ctrl, CheckBox).CheckState = CheckState.Unchecked
                End If
            Next
        End If
    End Sub

    Private Sub Users_Validated(sender As Object, e As EventArgs) Handles Users.Validated
        If Users.Text Is Nothing Or IsDBNull(Users.Text) Or Users.Text = "" Then
        Else
            For Each ctrl As Control In Me.Controls
                If TypeOf ctrl Is CheckBox Then
                    Select Case DirectCast(ctrl, CheckBox).Text
                        Case "Usuarios del Sistema"
                            Dim opcion As String = DLookup("Opcion", "Claves", "Opcion ='Usuarios' AND Usuario ='" & Users.Text & "'")
                            If opcion = Nothing Or IsDBNull(opcion) Or opcion = "" Then
                                DirectCast(ctrl, CheckBox).CheckState = CheckState.Unchecked
                            Else
                                DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked
                            End If
                        Case "Aplicar Pagos"
                            Dim opcion As String = DLookup("Opcion", "Claves", "Opcion ='Aplicar Pagos' AND Usuario ='" & Users.Text & "'")
                            If opcion = Nothing Or IsDBNull(opcion) Or opcion = "" Then
                                DirectCast(ctrl, CheckBox).CheckState = CheckState.Unchecked
                            Else
                                DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked
                            End If
                        Case "Control de Accesos"
                            Dim opcion As String = DLookup("Opcion", "Claves", "Opcion ='Control Accesos' AND Usuario ='" & Users.Text & "'")
                            If opcion = Nothing Or IsDBNull(opcion) Or opcion = "" Then
                                DirectCast(ctrl, CheckBox).CheckState = CheckState.Unchecked
                            Else
                                DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked
                            End If
                    End Select
                End If
            Next
            Dim fechaExp As String = DLookup("Vencimiento", "Claves", "Usuario ='" & Users.Text & "'")
            If IsDBNull(fechaExp) Or fechaExp = "" Then
                FechaExpiracion.Value = Date.Today
            Else
                FechaExpiracion.Value = fechaExp
            End If
            Password.Text = DLookup("Clave", "Claves", "Usuario ='" & Users.Text & "'")
            RePassword.Text = DLookup("ReClave", "Claves", "Usuario ='" & Users.Text & "'")
        End If
    End Sub

    Private Sub AplicarCerrar_Click(sender As Object, e As EventArgs) Handles AplicarCerrar.Click
        If Users.Text Is Nothing Or IsDBNull(Users.Text) Or Users.Text = "" Then
            MsgBox("Atención!, Primero seleccione un usuario", MsgBoxStyle.Information, "Mensaje del Sistema")
            Exit Sub
        Else
            If Password.Text Is Nothing Or IsDBNull(Password.Text) Or Password.Text = "" Then
                MsgBox("Atención!, campo PASSWORD es obligatorio.", MsgBoxStyle.Information, "Mensaje del Sistema")
                Exit Sub
            End If
            If RePassword.Text Is Nothing Or IsDBNull(RePassword.Text) Or RePassword.Text = "" Then
                MsgBox("Atención!, campo CONFIRMAR PASSWORD es obligatorio.", MsgBoxStyle.Information, "Mensaje del Sistema")
                Exit Sub
            End If
            If Password.Text = RePassword.Text Then
            Else
                MsgBox("Atención!, PASSWORDS no coinciden.", MsgBoxStyle.Critical, "Mensaje del Sistema")
                Exit Sub
            End If
            QryInsUpDel("DELETE FROM Claves WHERE Usuario ='" & Users.Text & "'")

            For Each ctrl As Control In Me.Controls
                If TypeOf ctrl Is CheckBox Then
                    If DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked Then
                        Select Case DirectCast(ctrl, CheckBox).Text
                            Case "Usuarios del Sistema"
                                QryInsUpDel("INSERT INTO Claves (Opcion, Usuario, Clave, ReClave, Nivel, Turno, Vencimiento, Perfil) VALUES ('Usuarios', '" & Users.Text &
                                            "', '" & Password.Text & "', '" & RePassword.Text & "', '" & DLookup("Nivel", "Usuarios", "Nombre ='" & Users.Text & "'") &
                                            "', 'na','" & FechaExpiracion.Value.ToString("yyyy-MM-dd") & "','" & DLookup("Departamento", "Usuarios", "Nombre ='" & Users.Text & "'") & "')")

                            Case "Aplicar Pagos"
                                QryInsUpDel("INSERT INTO Claves (Opcion, Usuario, Clave, ReClave, Nivel, Turno, Vencimiento, Perfil) VALUES ('Aplicar Pagos', '" & Users.Text &
                                        "', '" & Password.Text & "', '" & RePassword.Text & "', '" & DLookup("Nivel", "Usuarios", "Nombre ='" & Users.Text & "'") &
                                        "', 'na','" & FechaExpiracion.Value.ToString("yyyy-MM-dd") & "','" & DLookup("Departamento", "Usuarios", "Nombre ='" & Users.Text & "'") & "')")

                            Case "Control de Accesos"
                                QryInsUpDel("INSERT INTO Claves (Opcion, Usuario, Clave, ReClave, Nivel, Turno, Vencimiento, Perfil) VALUES ('Control Accesos', '" & Users.Text &
                                         "', '" & Password.Text & "', '" & RePassword.Text & "', '" & DLookup("Nivel", "Usuarios", "Nombre ='" & Users.Text & "'") &
                                         "', 'na','" & FechaExpiracion.Value.ToString("yyyy-MM-dd") & "', '" & DLookup("Departamento", "Usuarios", "Nombre ='" & Users.Text & "'") & "')")

                        End Select
                    End If
                End If
            Next
            MsgBox("Atención!, PASSWORDS asignados con éxito.", vbInformation, "Mensaje del Sistema")
            LimpiarCampos(Me)
            CerrarFormulario()
            Me.Dispose()
            Me.Close()
        End If
    End Sub
End Class