Public Class FrmUsuariosCrear

    Public Sub New(frmUsuariosListar As FrmUsuariosListar)
        InitializeComponent()
        Me.Owner = frmUsuariosListar

    End Sub

    Private Sub FrmUsuariosCrear_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DtpFecFin.Value = Now
        DtpFecIni.Value = Now

        CargarEmpresas()
    End Sub

    Private Sub CargarEmpresas()
        Dim conexion As New Conexion(conexionLocal)

        Dim sql As String = $"SELECT id AS cod, nombreLegal AS nombre FROM Empresas WHERE idContrato = {idContrato}"
        Dim dt = conexion.ObtenerDataTable(sql, New List(Of SqlClient.SqlParameter))

        With CmbEmpresa
            .DataSource = dt
            .DisplayMember = "nombre"
            .ValueMember = "cod"
        End With

        CmbEmpresa.SelectedIndex = 0
    End Sub

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

        If txtPass.Text.Trim.Length < 1 Then
            txtPass.Text = "Default123"
            txtPassRepetir.Text = "Default123"
        End If

        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 UsuariosCrearArgumentos() With {
            .NumeroLegal = TxtNumLegal.Text,
            .Password = txtPass.Text,
            .Rol = 2,
            .Nombre = TxtNombres.Text.ToUpper,
            .Apellidos = TxtApellidos.Text.ToUpper,
            .IdEmpresa = CmbEmpresa.SelectedValue,
            .InicioContrato = DtpFecIni.Value,
            .FinContrato = DtpFecFin.Value,
            .MailPersonal = TxtMailPers.Text.ToUpper,
            .MailCorporativo = TxtMailCorp.Text.ToUpper,
            .MovilPersonal = TxtFonoPers.Text,
            .MovilCorporativo = TxtMailCorp.Text,
            .IdAlternativo = txtFicha.Text}

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

        If ok Then
            MessageBox.Show("Usuario creado 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 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