Imports System.Data.SqlClient

Public Class FrmUsuariosModificar
    Dim idUsuario As String = ""

    Public Sub New(frmUsuariosListar As FrmUsuariosListar, idUsuario As String)
        InitializeComponent()
        Me.Owner = frmUsuariosListar
        Me.idUsuario = idUsuario
    End Sub

    Private Sub FrmUsuariosModificar_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cargaDatosUsurio(Me.idUsuario)
        TxtNumLegal.Select()
    End Sub

    Private Sub BtnGuardar_Click(sender As Object, e As EventArgs) Handles BtnGuardar.Click
        Dim conexion As New Conexion(conexionLocal)
        Dim manejador As New ClsUsuariosModificar(conexion)
        Dim mensajeError As String = ""
        Dim ok As Boolean = False

        If txtPass.Text <> txtPassRepetir.Text Then
            MessageBox.Show("Contraseñas no coinciden. Verificar")
            txtPass.Select()
            Exit Sub
        End If

        If txtFicha.Text.Trim.Length < 1 Or Not IsNumeric(txtFicha.Text.Trim) Then
            MessageBox.Show("Ficha vacía ó no numerica. Verificar")
            txtFicha.Select()
            Exit Sub
        End If

        Try
            Dim argumentoEntrada As New UsuariosModificarArgumentos() With {
            .Id = TxtIdFicha.Text,
            .NumeroLegal = TxtNumLegal.Text,
            .Password = txtPass.Text,
            .Rol = 2,
            .Nombre = TxtNombres.Text.ToUpper,
            .Apellidos = TxtApellidos.Text.ToUpper,
            .IdEmpresa = 1,
            .InicioContrato = DtpFecIni.Value,
            .FinContrato = DtpFecFin.Value,
            .MailPersonal = TxtMailPers.Text.ToUpper,
            .MailCorporativo = TxtMailCorp.Text.ToUpper,
            .MovilPersonal = TxtFonoPers.Text,
            .MovilCorporativo = TxtMailCorp.Text,
            .IdAlternativo = txtFicha.Text}

            ok = manejador.Modificar(argumentoEntrada, mensajeError)
        Catch ex As Exception
            mensajeError += vbNewLine + ex.Message
        Finally
            conexion.Dispose()
        End Try

        If ok Then
            MessageBox.Show("Usuario Modificado correctamente.")
            CType(Me.Owner, FrmUsuariosListar).Filtrar()
            Me.Close()
        Else
            MsgBox(mensajeError)
            TxtNumLegal.Focus()
            TxtNumLegal.SelectAll()
        End If
    End Sub

    Private Sub KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtApellidos.KeyPress,
                                                                          TxtFonoCorp.KeyPress,
                                                                          TxtFonoPers.KeyPress,
                                                                          TxtMailCorp.KeyPress,
                                                                          TxtMailPers.KeyPress,
                                                                          TxtNombres.KeyPress,
                                                                          TxtNumLegal.KeyPress,
                                                                          DtpFecFin.KeyPress,
                                                                          DtpFecIni.KeyPress,
                                                                          txtPass.KeyPress,
                                                                          txtPassRepetir.KeyPress,
                                                                          txtFicha.KeyPress

        If e.KeyChar = Chr(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
            Exit Sub
        End If
    End Sub


    Private Sub cargaDatosUsurio(ByVal idUsuario As String)
        Dim sql As String
        Dim sqlDT As New DataTable
        Dim sqlDA As SqlDataAdapter
        Dim con As New SqlConnection(conexionLocal)



        Try

            con.Open()
            sql = " SELECT * FROM usuarios "
            sql += " WHERE id =  " & CInt(idUsuario)
            sqlDA = New SqlDataAdapter(Sql, con)
            sqlDA.Fill(sqlDT)
            If sqlDT.Rows.Count > 0 Then
                TxtIdFicha.Text = idUsuario
                TxtNumLegal.Text = sqlDT.Rows(0).Item("numeroLegal").ToString
                DtpFecIni.Value = sqlDT.Rows(0).Item("inicioContrato")
                DtpFecFin.Value = sqlDT.Rows(0).Item("finContrato")
                TxtNombres.Text = sqlDT.Rows(0).Item("nombre").ToString
                TxtApellidos.Text = sqlDT.Rows(0).Item("apellidos").ToString
                TxtMailPers.Text = sqlDT.Rows(0).Item("mailPersonal").ToString
                TxtMailCorp.Text = sqlDT.Rows(0).Item("mailCorporativo").ToString
                TxtFonoPers.Text = sqlDT.Rows(0).Item("movilPersonal").ToString
                TxtFonoCorp.Text = sqlDT.Rows(0).Item("movilCorporativo").ToString
                txtFicha.Text = sqlDT.Rows(0).Item("idAlternativo").ToString
                txtPass.Text = sqlDT.Rows(0).Item("pass").ToString
                txtPassRepetir.Text = sqlDT.Rows(0).Item("pass").ToString
            End If
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            con.Close()
        Finally
            con.Close()
        End Try



    End Sub

    Private Sub TxtMailPers_Leave(sender As Object, e As EventArgs) Handles TxtMailPers.Leave
        If TxtMailPers.Text <> "" Then
            If Not validar_Mail(LCase(TxtMailPers.Text)) Then
                MessageBox.Show("Formato de correo incorrecto, favor corregir. formato: nombre@dominio.com")
                TxtMailPers.Focus()
            End If
        End If
    End Sub

    Private Sub TxtMailCorp_Leave(sender As Object, e As EventArgs) Handles TxtMailCorp.Leave
        If TxtMailCorp.Text <> "" Then
            If Not validar_Mail(LCase(TxtMailCorp.Text)) Then
                MessageBox.Show("Formato de correo incorrecto, favor corregir. formato: nombre@dominio.com")
                TxtMailCorp.Focus()
            End If
        End If
    End Sub
End Class