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;

namespace pract
{
    public partial class Form4 : Form
    {
        private string _connString;
        public Form4(string connString)
        {
            InitializeComponent();
            _connString = connString;
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            using (var conn = new NpgsqlConnection(_connString))
            {
                conn.Open();
                var cmd = new NpgsqlCommand("SELECT id, name FROM company ORDER BY name", conn);
                var reader = cmd.ExecuteReader();

                comboBox1.Items.Clear();
                while (reader.Read())
                {
                    comboBox1.Items.Add(new
                    {
                        Id = reader.GetInt32(0),
                        Name = reader.GetString(1)
                    });
                }
                comboBox1.ValueMember = "Name";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                using (var conn = new NpgsqlConnection(_connString))
                {
                    string sql = @"SELECT * FROM get_vacancies_by_salary_range(@name, @min, @max)";
                    var cmd = new NpgsqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@name", ((dynamic)comboBox1.SelectedItem).Name);
                    cmd.Parameters.AddWithValue("@min", Convert.ToDecimal(textBox1.Text));
                    cmd.Parameters.AddWithValue("@max", Convert.ToDecimal(textBox2.Text));
                    var adapter = new NpgsqlDataAdapter(cmd);
                    adapter.Fill(dt);
                }
                dataGridView1.DataSource = dt;
                dataGridView1.Columns[0].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки: " + ex.Message);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
