using Npgsql;
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 Npgsql;
using System.Web;

namespace db_lab
{
    public partial class DataForm : Form
    {
        NpgsqlConnection connection;

        public DataForm(string connection)
        {
            this.connection = new NpgsqlConnection(connection);
            InitializeComponent();
        }

        private void load_data(string view_or_table)
        {
            try
            {
                connection.Open();
                string sql = $"SELECT * FROM {view_or_table}";
                NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, this.connection);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset, "data");
                dataGridView1.DataSource = dataset.Tables["data"];
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error! Log: {ex.Message}");
                connection.Close();
            }
        }

        private void selectAllBtn_Click(object sender, EventArgs e)
        {
            var tableOrView = tableOrViewInput.Text.Trim();
            load_data(tableOrView);
        }

        private void selectBuildingBtn_Click(object sender, EventArgs e)
        {
            load_data("Building");
        }

        private void selectDepartmentBtn_Click(object sender, EventArgs e)
        {
            load_data("Department");
        }

        private void selectAllRoomsBtn_Click(object sender, EventArgs e)
        {
            load_data("Rooms");
        }

        private void selectAllEquipmentBtn_Click(object sender, EventArgs e)
        {
            load_data("Equipment");
        }

        private void selectAllEquipmentArrivalDeparture_Click(object sender, EventArgs e)
        {
            load_data("equipment_arrival_departure");
        }
    }
}
