private void btnGuardar_Click(object sender, EventArgs e)
 {
     if (_isNew)
     {
         CrearCuentaUsuario();
         NUsusario objUser = new NUsusario();
         objUser.ListadoDgv(frmListUsuario.MiFormLisUsuario.dgvUsuario);
         frmListUsuario.MiFormLisUsuario.dgvUsuario.Refresh();
     }
     else
     {
         ActualizarCuenta();
         NUsusario objUser = new NUsusario();
         objUser.ListadoDgv(frmListUsuario.MiFormLisUsuario.dgvUsuario);
         frmListUsuario.MiFormLisUsuario.dgvUsuario.Refresh();
     }
 }
        private void frmListUsuario_Load(object sender, EventArgs e)
        {
            NUsusario objUser = new NUsusario();

            objUser.ListadoDgv(dgvUsuario);
            estiloDgv();

            dgvUsuario.Columns[0].HeaderCell.Style.Alignment  = DataGridViewContentAlignment.BottomCenter;
            dgvUsuario.Columns[7].HeaderCell.Style.Alignment  = DataGridViewContentAlignment.BottomCenter;
            dgvUsuario.Columns[8].HeaderCell.Style.Alignment  = DataGridViewContentAlignment.BottomCenter;
            dgvUsuario.Columns[9].HeaderCell.Style.Alignment  = DataGridViewContentAlignment.BottomCenter;
            dgvUsuario.Columns[10].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

            dgvUsuario.Columns[0].DefaultCellStyle.Alignment  = DataGridViewContentAlignment.MiddleCenter;
            dgvUsuario.Columns[7].DefaultCellStyle.Alignment  = DataGridViewContentAlignment.MiddleCenter;
            dgvUsuario.Columns[8].DefaultCellStyle.Alignment  = DataGridViewContentAlignment.MiddleCenter;
            dgvUsuario.Columns[9].DefaultCellStyle.Alignment  = DataGridViewContentAlignment.MiddleCenter;
            dgvUsuario.Columns[10].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dgvUsuario.ColumnHeadersDefaultCellStyle.Font = new Font("Segoe UI", 11, FontStyle.Bold);
        }
        private void ActualizarCuenta()
        {
            string rpta = "";

            if (string.IsNullOrEmpty(txtUsuario.Text))
            {
                txtUsuario.Focus();
                epUsuario.SetError(txtUsuario, "Ingrese su usuario");
            }
            else if (string.IsNullOrEmpty(txtPassword.Text))
            {
                txtPassword.Focus();
                epUsuario.SetError(txtPassword, "Ingrese su password");
            }
            else
            {
                if (_idEmpleado != 0 && _idUsuario != 0)
                {
                    rpta = NUsusario.ActualizarCuenta(
                        _idUsuario,
                        txtUsuario.Text.Trim(),
                        txtPassword.Text.Trim(),
                        cboTipo.SelectedItem.ToString().Trim(),
                        _idEmpleado
                        );
                    if (rpta == "Ok")
                    {
                        MessageBox.Show(rpta + "--Se actualizó su cuenta"); Limpiar();
                    }
                    else
                    {
                        MessageBox.Show("Error ya cuenta con cuenta con tipo de usuario");
                    }
                }
                else
                {
                    MessageBox.Show("Error al actualizar");
                }
            }
        }
        private void dgvEmpleado_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string sexo = "";

            if (e.RowIndex != -1)
            {
                if (dgvEmpleado.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Crear cuenta"))
                {
                    frmUsuario frmUser    = new frmUsuario();
                    int        idempplead = Convert.ToInt32(dgvEmpleado.Rows[e.RowIndex].Cells[1].Value.ToString());
                    var        user       = NUsusario.BuscarUsuarioIdEmpleado(idempplead);
                    if (user.Rows.Count > 0)
                    {
                        if (idempplead == Convert.ToInt32(user.Rows[0]["idempleado"].ToString()))
                        {
                            MessageBox.Show("ya tiene una cuenta");
                        }
                        else
                        {
                            frmUser.Show();
                            var tabla = NEmpleado.BuscarEmpleadoId(idempplead);

                            frmUsuario.MiFormUsuario._idEmpleado     = int.Parse(tabla.Rows[0]["idempleado"].ToString());
                            frmUsuario.MiFormUsuario.txtUsuario.Text = tabla.Rows[0]["correo"].ToString();
                        }
                    }
                    else
                    {
                        frmUser.Show();
                        var tabla = NEmpleado.BuscarEmpleadoId(idempplead);

                        frmUsuario.MiFormUsuario._idEmpleado     = int.Parse(tabla.Rows[0]["idempleado"].ToString());
                        frmUsuario.MiFormUsuario.txtUsuario.Text = tabla.Rows[0]["correo"].ToString();
                    }
                }

                if (dgvEmpleado.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Editar"))
                {
                    int idEmpleado = Convert.ToInt32(dgvEmpleado.Rows[e.RowIndex].Cells[1].Value.ToString());


                    frmEmpleado frmEmpleado = new frmEmpleado();
                    frmEmpleado.Show();

                    var tabla = NEmpleado.BuscarEmpleadoId(idEmpleado);

                    frmEmpleado.MiFormEmpleado.txtNombres.Text   = tabla.Rows[0]["nombres"].ToString();
                    frmEmpleado.MiFormEmpleado.txtApellidos.Text = tabla.Rows[0]["apellidos"].ToString();
                    frmEmpleado.MiFormEmpleado.txtDni.Text       = tabla.Rows[0]["dni"].ToString();
                    sexo = tabla.Rows[0]["sexo"].ToString();
                    if (sexo == "M")
                    {
                        frmEmpleado.MiFormEmpleado.cboSexo.SelectedItem = "Masculino";
                    }
                    else
                    {
                        frmEmpleado.MiFormEmpleado.cboSexo.SelectedItem = "Femenino";
                    }
                    frmEmpleado.MiFormEmpleado.txtTelefono.Text      = tabla.Rows[0]["telefono"].ToString();
                    frmEmpleado.MiFormEmpleado.txtCorreo.Text        = tabla.Rows[0]["correo"].ToString();
                    frmEmpleado.MiFormEmpleado.txtDireccion.Text     = tabla.Rows[0]["direccion"].ToString();
                    frmEmpleado.MiFormEmpleado.cboArea.SelectedValue = tabla.Rows[0]["idarea"].ToString();
                    byte[] img = (byte[])tabla.Rows[0]["foto"];
                    var    ms  = new MemoryStream(img);
                    frmEmpleado.MiFormEmpleado.pbFoto.Image = Image.FromStream(ms);

                    frmEmpleado.MiFormEmpleado.idEmpleado      = idEmpleado;
                    frmEmpleado.MiFormEmpleado._IsNew          = false;
                    frmEmpleado.MiFormEmpleado.cboArea.Enabled = true;
                }

                if (dgvEmpleado.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Eliminar"))
                {
                    int          idempplead = Convert.ToInt32(dgvEmpleado.Rows[e.RowIndex].Cells[1].Value.ToString());
                    DialogResult rspta      = MessageBox.Show("Desea Eliminar", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (DialogResult.Yes == rspta)
                    {
                        var rpta = NEmpleado.EliminarEmpleado(idempplead);

                        if (rpta == "Ok")
                        {
                            MessageBox.Show(rpta + "---Elimado");
                            dgvEmpleado.Rows.RemoveAt(e.RowIndex);
                            NEmpleado objEmpleado = new NEmpleado();
                            objEmpleado.ListadoDgv(dgvEmpleado);
                        }
                        else
                        {
                            MessageBox.Show("error");
                        }
                    }
                }
                if (dgvEmpleado.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Ver"))
                {
                    int idEmpleado = Convert.ToInt32(dgvEmpleado.Rows[e.RowIndex].Cells[1].Value.ToString());


                    frmPerfil perfil = new frmPerfil();
                    perfil.Show();

                    var tabla = NEmpleado.BuscarEmpleadoId(idEmpleado);
                    sexo = tabla.Rows[0]["sexo"].ToString();
                    if (sexo == "M")
                    {
                        sexo = "Masculino";
                    }
                    else
                    {
                        sexo = "Femenino";
                    }
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Nombre:     " + tabla.Rows[0]["nombres"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Apellidos:  " + tabla.Rows[0]["apellidos"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Dni:        " + tabla.Rows[0]["dni"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Sexo:       " + sexo);
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Teléfono:   " + tabla.Rows[0]["telefono"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Correo:     " + tabla.Rows[0]["correo"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Dirección:  " + tabla.Rows[0]["direccion"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Area:       " + tabla.Rows[0]["area"].ToString());

                    byte[] img = (byte[])tabla.Rows[0]["foto"];
                    var    ms  = new MemoryStream(img);
                    frmPerfil.MiFormPerfil.pbFoto.Image   = Image.FromStream(ms);
                    frmPerfil.MiFormPerfil.lblTitulo.Text = "Datos Empleado";
                }
            }
        }
        private void CrearCuentaUsuario()
        {
            string rpta = "";

            if (string.IsNullOrEmpty(txtUsuario.Text))
            {
                txtUsuario.Focus();
                epUsuario.SetError(txtUsuario, "Ingrese su usuario");
            }
            else if (string.IsNullOrEmpty(txtPassword.Text))
            {
                txtPassword.Focus();
                epUsuario.SetError(txtPassword, "Ingrese su password");
            }
            else
            {
                if (_idEmpleado > 0)
                {
                    var tabla = NUsusario.ListarUsuario();

                    if (tabla.Rows.Count > 0)
                    {
                        if (tabla.Rows[0]["username"].ToString() == txtUsuario.Text)
                        {
                            txtUsuario.Focus();
                            epUsuario.SetError(txtUsuario, "Usuario no disponible ya esta siendo Utilizado por otro");
                        }
                        else
                        {
                            epUsuario.Clear();
                            rpta = NUsusario.CrearCuentaUsuario(
                                txtUsuario.Text.Trim(),
                                txtPassword.Text.Trim(),
                                cboTipo.SelectedItem.ToString().Trim(),
                                _idEmpleado
                                );
                            if (rpta == "Ok")
                            {
                                MessageBox.Show(rpta + "--Se creó su cuenta"); Limpiar();
                            }
                            else
                            {
                                MessageBox.Show("Error ya tiene cuenta con el tipo de usuario");
                            }
                        }
                    }
                    else
                    {
                        epUsuario.Clear();
                        rpta = NUsusario.CrearCuentaUsuario(
                            txtUsuario.Text.Trim(),
                            txtPassword.Text.Trim(),
                            cboTipo.SelectedItem.ToString().Trim(),
                            _idEmpleado
                            );
                        if (rpta == "Ok")
                        {
                            MessageBox.Show(rpta + "--Se creó su cuenta"); Limpiar();
                        }
                        else
                        {
                            MessageBox.Show("Error ya tiene cuenta con el tipo de usuario");
                        }
                    }
                }
            }
        }
        private void dgvUsuario_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string sexo = "";

            if (e.RowIndex != -1)
            {
                if (dgvUsuario.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Activar") || dgvUsuario.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Desactivar"))
                {
                    try
                    {
                        string estado = dgvUsuario.Rows[e.RowIndex].Cells[7].Value.ToString();
                        int    iduser = Convert.ToInt32(dgvUsuario.Rows[e.RowIndex].Cells[1].Value.ToString());

                        NUsusario objUser = new NUsusario();
                        if (NUsusario.ActualizarEstadoUsuario(estado.Equals("Desactivar") ? 0 : 1, iduser))
                        {
                            objUser.ListadoDgv(dgvUsuario);
                        }

                        else
                        {
                            MessageBox.Show("Error al controlar el estado");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        throw;
                    }
                }

                if (dgvUsuario.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Editar"))
                {
                    int idUser = Convert.ToInt32(dgvUsuario.Rows[e.RowIndex].Cells[1].Value.ToString());

                    frmUsuario frmUser = new frmUsuario();
                    frmUser.Show();
                    var tabla = NUsusario.BuscarUsuarioId(idUser);

                    frmUsuario.MiFormUsuario._isNew               = false;
                    frmUsuario.MiFormUsuario._idUsuario           = idUser;
                    frmUsuario.MiFormUsuario.txtUsuario.Text      = tabla.Rows[0]["username"].ToString();
                    frmUsuario.MiFormUsuario.txtPassword.Text     = tabla.Rows[0]["pasword"].ToString();
                    frmUsuario.MiFormUsuario.cboTipo.SelectedItem = tabla.Rows[0]["tipo"].ToString();
                    frmUsuario.MiFormUsuario._idEmpleado          = int.Parse(tabla.Rows[0]["idempleado"].ToString());

                    dgvUsuario.Refresh();
                }

                if (dgvUsuario.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Eliminar"))
                {
                    int          iduser = Convert.ToInt32(dgvUsuario.Rows[e.RowIndex].Cells[1].Value.ToString());
                    DialogResult rspta  = MessageBox.Show("Desea Eliminar", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (DialogResult.Yes == rspta)
                    {
                        var rpta = NUsusario.EliminarUsuario(iduser);

                        if (rpta == "Ok")
                        {
                            MessageBox.Show(rpta + "---Elimado");
                            dgvUsuario.Rows.RemoveAt(e.RowIndex);
                            NUsusario objUser = new NUsusario();
                            objUser.ListadoDgv(dgvUsuario);
                        }
                        else
                        {
                            MessageBox.Show("error");
                        }
                    }
                }

                if (dgvUsuario.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("Ver"))
                {
                    //DialogResult rspta = MessageBox.Show("Desea Desencripar la contraseña", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    // if (DialogResult.Yes == rspta)
                    // {
                    int idUser = Convert.ToInt32(dgvUsuario.Rows[e.RowIndex].Cells[1].Value.ToString());


                    frmPerfil perfil = new frmPerfil();
                    perfil.Show();

                    var tabla = NUsusario.BuscarUsuarioId(idUser);
                    sexo = tabla.Rows[0]["sexo"].ToString();
                    if (sexo == "M")
                    {
                        sexo = "Masculino";
                    }
                    else
                    {
                        sexo = "Femenino";
                    }
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Nombre:     " + tabla.Rows[0]["nombres"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Apellidos:  " + tabla.Rows[0]["apellidos"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Dni:        " + tabla.Rows[0]["dni"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Sexo:       " + sexo);
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Teléfono:   " + tabla.Rows[0]["telefono"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Correo:     " + tabla.Rows[0]["correo"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Dirección:  " + tabla.Rows[0]["direccion"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Usuario:    " + tabla.Rows[0]["username"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("Password:   "******"pasword"].ToString());
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("");
                    frmPerfil.MiFormPerfil.listDatos.Items.Add("TipoUser:   "******"tipo"].ToString());

                    string captura = tabla.Rows[0]["estado"].ToString();
                    frmPerfil.MiFormPerfil.lblEstadoUser.Visible = true;
                    if (captura == "1")
                    {
                        frmPerfil.MiFormPerfil.lblEstadoUser.Text      = "Activo";
                        frmPerfil.MiFormPerfil.lblEstadoUser.ForeColor = Color.Green;
                    }
                    else
                    {
                        frmPerfil.MiFormPerfil.lblEstadoUser.Text      = "Inactivo";
                        frmPerfil.MiFormPerfil.lblEstadoUser.ForeColor = Color.Red;
                    }

                    byte[] img = (byte[])tabla.Rows[0]["foto"];
                    var    ms  = new MemoryStream(img);
                    frmPerfil.MiFormPerfil.pbFoto.Image   = Image.FromStream(ms);
                    frmPerfil.MiFormPerfil.lblTitulo.Text = "Datos Usuario";

                    //}
                }
            }
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            NUsusario objUser = new NUsusario();

            objUser.ListadoDgv(dgvUsuario);
        }
        private void txtBuscar_TextChanged(object sender, EventArgs e)
        {
            NUsusario objUser = new NUsusario();

            objUser.ListarBusquedaUsuario(dgvUsuario, txtBuscar.Text.Trim());
        }