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 System.Data.SqlClient;
using System.Configuration;

namespace DbmsLab2
{
    public partial class Form1 : Form
    {
        public SqlConnection sqlconnection = new SqlConnection("Data Source=DANIELCUJBA\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;");
        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.ds = new System.Data.DataSet();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.dataGridView2 = new System.Windows.Forms.DataGridView();
            this.ds2 = new System.Data.DataSet();
            this.UpdateBtn = new System.Windows.Forms.Button();
            this.DeleteBtn = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.ds2)).BeginInit();
            this.SuspendLayout();
            // 
            // ds
            // 
            this.ds.DataSetName = "NewDataSet";
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(79, 12);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(745, 150);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
            // 
            // dataGridView2
            // 
            this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView2.Location = new System.Drawing.Point(79, 196);
            this.dataGridView2.Name = "dataGridView2";
            this.dataGridView2.Size = new System.Drawing.Size(745, 150);
            this.dataGridView2.TabIndex = 1;
            this.dataGridView2.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellContentClick);
            // 
            // ds2
            // 
            this.ds2.DataSetName = "NewDataSet";
            // 
            // UpdateBtn
            // 
            this.UpdateBtn.Location = new System.Drawing.Point(364, 389);
            this.UpdateBtn.Name = "UpdateBtn";
            this.UpdateBtn.Size = new System.Drawing.Size(138, 46);
            this.UpdateBtn.TabIndex = 2;
            this.UpdateBtn.Text = "Save Changes to DB";
            this.UpdateBtn.UseVisualStyleBackColor = true;
            this.UpdateBtn.Click += new System.EventHandler(this.UpdateBtn_Click);
            // 
            // DeleteBtn
            // 
            this.DeleteBtn.Location = new System.Drawing.Point(624, 389);
            this.DeleteBtn.Name = "DeleteBtn";
            this.DeleteBtn.Size = new System.Drawing.Size(108, 45);
            this.DeleteBtn.TabIndex = 3;
            this.DeleteBtn.Text = "Delete";
            this.DeleteBtn.UseVisualStyleBackColor = true;
            this.DeleteBtn.Click += new System.EventHandler(this.DeleteBtn_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(890, 473);
            this.Controls.Add(this.DeleteBtn);
            this.Controls.Add(this.UpdateBtn);
            this.Controls.Add(this.dataGridView2);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.ds2)).EndInit();
            this.ResumeLayout(false);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var command = new SqlCommand($"SELECT * FROM {ConfigurationManager.AppSettings["MasterTable"]}", sqlconnection);
            this.adapter = new SqlDataAdapter(command);



            this.ds = new DataSet(ConfigurationManager.AppSettings["MasterTable"]);
            this.adapter.Fill(ds, ConfigurationManager.AppSettings["MasterTable"]);
            dataGridView1.DataSource= this.ds.Tables[ConfigurationManager.AppSettings["MasterTable"]];
            

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
            var command = new SqlCommand($"SELECT * FROM {ConfigurationManager.AppSettings["ChildTable"]} WHERE {ConfigurationManager.AppSettings["FK"]} = {id}", sqlconnection);
            this.adapter2 = new SqlDataAdapter(command);


            this.ds2 = new DataSet(ConfigurationManager.AppSettings["ChildTable"]);
            this.adapter2.Fill(ds2, ConfigurationManager.AppSettings["ChildTable"]);
            dataGridView2.DataSource = this.ds2.Tables[ConfigurationManager.AppSettings["ChildTable"]];
        }

        private void UpdateBtn_Click(object sender, EventArgs e)
        {
            var builder = new SqlCommandBuilder(adapter2);
            adapter2.UpdateCommand = builder.GetUpdateCommand();
            try
            {
                this.adapter2.Update((DataTable)this.dataGridView2.DataSource);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                var builder = new SqlCommandBuilder(adapter2);
                adapter2.UpdateCommand = builder.GetUpdateCommand();
                dataGridView2.Rows.RemoveAt(this.SelectedChildId);
                this.adapter2.Update((DataTable)this.dataGridView2.DataSource);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            this.SelectedChildId = e.RowIndex;

        }
    }
}
