using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Collections;

namespace EjemploMysql
{
    public partial class EliminarUsuarios : Form
    {
        MySqlConnection conector;
        List<String> pass = new List<String>();
        List<String> user = new List<String>();
        int pos = -1;
        public EliminarUsuarios()
        {
            conexionBD();
            InitializeComponent();
        }
        public void conexionBD()
        {
            conector = new MySqlConnection();
            conector.ConnectionString = "Server=localhost;Port=3306;DataBase=ejemploMysql;Uid=root;Pwd=123";
            try
            {
                conector.Open();
            }
            catch (MySqlException e)
            {
                e.ErrorCode.ToString();
            }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string text = txtPassword.Text;
            if (checkBox1.Checked)
            {
                txtPassword.UseSystemPasswordChar = false;
                txtPassword.Text = text;

            }
            else
            {
                txtPassword.UseSystemPasswordChar = true;
                txtPassword.Text = text;
            }
        }

        private void listaUsuarios_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (btnModificar.Text.Equals("Grabar"))
            {
                btnModificar.Text = "Modificar";
                txtUsuario.Enabled = false;
                txtPassword.Enabled = false;
            }
            txtUsuario.Text = listaUsuarios.SelectedItem.ToString();
            txtPassword.Text = pass[listaUsuarios.SelectedIndex].ToString();
            pos = listaUsuarios.SelectedIndex;

        }

        private void EliminarUsuarios_Load(object sender, EventArgs e)
        {
            txtPassword.UseSystemPasswordChar = true;
            mostrarUsuarios();
        }
        public void mostrarUsuarios()
        {
            listaUsuarios.Items.Clear();
            if (conector.State != ConnectionState.Open)
            {
                conector.Open();
            }
            DataTable dtDatos = new DataTable();
            MySqlDataAdapter mdaDatos = new MySqlDataAdapter("select usuario, password from usuarios", conector);
            mdaDatos.Fill(dtDatos);
            for (int i = 0; i < dtDatos.Rows.Count; i++)
            {
                DataRow dr = dtDatos.Rows[i];
                pass.Add(dr["password"].ToString());
                user.Add(dr["usuario"].ToString());
                listaUsuarios.Items.Add(dr["usuario"].ToString());
            }
            conector.Close();
        }
        private void btnModificar_Click(object sender, EventArgs e)
        {
            if(btnModificar.Text.Equals("Modificar")){
                btnModificar.Text="Grabar";
                txtUsuario.Enabled = true;
                txtPassword.Enabled = true;                
            }
            else if (btnModificar.Text.Equals("Grabar"))
            {
                btnModificar.Text = "Modificar";
                txtUsuario.Enabled = false;
                txtPassword.Enabled = false;
                if (conector.State != ConnectionState.Open)
                {
                    conector.Open();
                }
                if (txtUsuario.Text.Equals(""))
                {
                    MessageBox.Show("Favor de no dejar usuario vacio");
                }
                else
                {
                    MySqlCommand comando = conector.CreateCommand();
                    comando.CommandText = "update usuarios set usuario='"+txtUsuario.Text+"', password='"+txtPassword.Text
                        +"' where usuario='"+user[pos].ToString()+"' and password='"
                        +pass[pos].ToString()+"';";
                    comando.ExecuteNonQuery();
                    conector.Close();
                    MessageBox.Show("Registro Modificado de manera Correcta");
                    this.Close();
                }

            }
        }

        
    }
}
