using MySql.Data.MySqlClient;
using System;
using System.Windows.Forms;

namespace programming_ii__lesson_2_
{
    public partial class Form3 : Form
    {
        public Form RefToForm1 { get; set; }
        public int myid;
        public Form3(int id)
        {
            InitializeComponent();
           
            myid= id;
            var myConnection = dbconnect.myConnection;
            myConnection.Open();
            string sqlQuery = null;

            sqlQuery = $"Select * from employees where id={id}";
            MySqlCommand cmd = new MySqlCommand(sqlQuery, myConnection);
            MySqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {

                textBox1.Text = rdr["name"].ToString();
                textBox2.Text = rdr["surname"].ToString();
                textBox3.Text = rdr["afm"].ToString();
                dateTimePicker1.Value = (DateTime) rdr["birth_date"];
                textBox5.Text = rdr["payroll"].ToString();
            }
            myConnection.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string surname = textBox2.Text;
            string afm = textBox3.Text;
            string birthdate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string payroll = textBox5.Text;
            

            var myConnection = dbconnect.myConnection;
            myConnection.Open();
            string query = $"update employees set name='{name}',surname='{surname}',afm='{afm}',birth_date='{birthdate}',payroll='{payroll}' where id={myid};";
            MySqlCommand cmd = new MySqlCommand(query, myConnection);
            cmd.ExecuteNonQuery();
            myConnection.Close();
            
            this.Dispose();
            this.RefToForm1.Show();
            
            

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.RefToForm1.Show();
        }
    }
}
