Imports System.Runtime.InteropServices
Imports System.Data.SqlClient

Public Class FrmLogin
#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.Close()
    End Sub

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

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

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

    Private Sub PictureBox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub
    Private Sub txtUsuario_Click(sender As Object, e As EventArgs) Handles txtUsuario.Click
        txtUsuario.Text = ""
        txtUsuario.Focus()
    End Sub

    Private Sub txtPassword_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPassword.KeyDown
        If e.KeyCode = Keys.Enter Then
            Acceso()
        End If
    End Sub

    Private Sub txtPassword_Click(sender As Object, e As EventArgs) Handles txtPassword.Click
        txtPassword.Text = ""
        txtPassword.Focus()
    End Sub

    Sub Acceso()
        Dim cn As New SqlConnection(GlobalVariables.cstring)
        Dim sql As String = "select * from Usuarios where usuario='" & Me.txtUsuario.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS and  password='" & Me.txtPassword.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS"
        'COLLATE SQL_Latin1_General_CP1_CS_AS esto despues de cada comparacion valida que sean exactamente mayusculas y minusculas.
        Try
            Dim cmd As New SqlCommand(sql, cn)
            cn.Open()
            Dim objDataReader As SqlDataReader = cmd.ExecuteReader()
            If objDataReader.HasRows Then
                objDataReader.Read()
                GlobalVariables.VarUsuario = objDataReader("usuario")
                GlobalVariables.VarNivel = objDataReader("nivel")
                GlobalVariables.VarEmail = objDataReader("Email")

                GlobalVariables.VarSession = True
                frmMain.lblUsuario.Text = GlobalVariables.VarUsuario
                frmMain.lblNivel.Text = GlobalVariables.VarNivel
                frmMain.lblMail.Text = GlobalVariables.VarEmail


                frmMain.Show()
                Me.Hide()
            Else
                MsgBox("Usuario o Contraseña Inválidos.", MsgBoxStyle.Information, "Mensaje del Sistema")
            End If
            objDataReader.Close()
            cmd.Dispose()
            cn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Acceso()
    End Sub

    Private Sub PictureBox2_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox2.DoubleClick
        GlobalVariables.varConfig = True
        frmAccesoConfig.ShowDialog()
    End Sub

    Private Sub FrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim cstring As String = Desencriptar(My.Settings.ConnString)
        Dim varConecction As String() = cstring.Split(";")

        Dim varServer As String = Replace(varConecction(0), "Data Source=", "")
        Dim varServer2 As String = Replace(varServer, "\SQLEXPRESS", "")

        GlobalVariables.varServidorBD = varConecction(0)
        Servidor.Text = varConecction(0)
    End Sub


End Class
