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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            var command = new SqlCommand("SELECT * FROM Guest", connection);
            this.adapter1 = new SqlDataAdapter(command);
            this.ds1 = new DataSet("Guest");
            this.adapter1.Fill(ds1, "Guest");
            this.dgvGuest.DataSource = ds1.Tables["Guest"];
        }

        private void dgvGuest_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = dgvGuest.Rows[e.RowIndex].Cells[0].Value;
            var command = new SqlCommand($"SELECT * FROM Reservation WHERE guestId = ${id}", connection);
            this.adapter2 = new SqlDataAdapter(command);
            this.ds2 = new DataSet("Reservation");
            this.adapter2.Fill(ds2, "Reservation");
            this.dgvReservations.DataSource = ds2.Tables["Reservation"];
        }

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