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 Form1 : Form
    {
        private string _connString = "Host=80.250.189.52;Database=dbwork;Username=goreva_am;Password=qm#J6pyA%K#;";
        private bool _isAdmin = true;

        public Form1(string connString, bool isAdmin)
        {
            InitializeComponent();
            _connString = connString;
            _isAdmin = isAdmin;

            button1.Visible = isAdmin;
            button2.Visible = isAdmin;
            button3.Visible = isAdmin;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void specializationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int c;
                DataTable dt = new DataTable();
                using (var conn = new NpgsqlConnection(_connString))
                {
                    conn.Open();
                    string sql = "SELECT * FROM specialization";
                    var cmd = new NpgsqlCommand(sql, conn);
                    var adapter = new NpgsqlDataAdapter(cmd);
                    adapter.Fill(dt);

                    sql = "SELECT count(*) FROM specialization;";
                    cmd = new NpgsqlCommand(sql, conn);
                    c = Convert.ToInt32(cmd.ExecuteScalar());
                }
                dataGridView1.DataSource = dt;

                dataGridView1.Columns[0].Visible = false;

                label1.Text = c.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки: " + ex.Message);
            }
        }

        private void companyesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int c;
                DataTable dt = new DataTable();
                using (var conn = new NpgsqlConnection(_connString))
                {
                    conn.Open();
                    string sql = "SELECT * FROM company";
                    var cmd = new NpgsqlCommand(sql, conn);
                    var adapter = new NpgsqlDataAdapter(cmd);
                    adapter.Fill(dt);

                    sql = "SELECT count(*) FROM vacancy;";
                    cmd = new NpgsqlCommand(sql, conn);
                    c = Convert.ToInt32(cmd.ExecuteScalar());
                }
                dataGridView1.DataSource = dt;

                dataGridView1.Columns[0].Visible = false;

                label1.Text = c.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки: " + ex.Message);
            }
        }

        private void unemployedsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int c;
                DataTable dt = new DataTable();
                using (var conn = new NpgsqlConnection(_connString))
                {
                    conn.Open();
                    string sql = "SELECT * FROM unemployed";
                    var cmd = new NpgsqlCommand(sql, conn);
                    var adapter = new NpgsqlDataAdapter(cmd);
                    adapter.Fill(dt);

                    sql = "SELECT count(*) FROM unemployed;";
                    cmd = new NpgsqlCommand(sql, conn);
                    c = Convert.ToInt32(cmd.ExecuteScalar());
                }
                dataGridView1.DataSource = dt;

                dataGridView1.Columns[0].Visible = false;
                label1.Text = c.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки: " + ex.Message);
            }
        }

        private void vacancyesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int c;
                DataTable dt = new DataTable();
                using (var conn = new NpgsqlConnection(_connString))
                {
                    conn.Open();
                    string sql = "SELECT * FROM vacancy;";
                    var cmd = new NpgsqlCommand(sql, conn);
                    var adapter = new NpgsqlDataAdapter(cmd);
                    adapter.Fill(dt);

                    sql = "SELECT count(*) FROM vacancy;";
                    cmd = new NpgsqlCommand(sql, conn);
                    c = Convert.ToInt32(cmd.ExecuteScalar());
                }
                dataGridView1.DataSource = dt;

                dataGridView1.Columns[0].Visible = false;

                label1.Text = c.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки: " + ex.Message);
            }
        }

        private void employmentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int c;
                DataTable dt = new DataTable();
                using (var conn = new NpgsqlConnection(_connString))
                {
                    conn.Open();
                    string sql = "SELECT * FROM employment_view";
                    var cmd = new NpgsqlCommand(sql, conn);
                    var adapter = new NpgsqlDataAdapter(cmd);
                    adapter.Fill(dt);

                    sql = "SELECT count(*) FROM employment_view;";
                    cmd = new NpgsqlCommand(sql, conn);
                    c = Convert.ToInt32(cmd.ExecuteScalar());
                }
                dataGridView1.DataSource = dt;

                label1.Text = c.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки: " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value) != 0)
            {
                try
                {
                    using (var conn = new NpgsqlConnection(_connString))
                    {
                        conn.Open();
                        var cmd = new NpgsqlCommand("DELETE FROM unemployed WHERE id = @id", conn);
                        cmd.Parameters.AddWithValue("@id", Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value));
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка: " + ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3(_connString);
            form3.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value) != 0)
            {
                Form3 form3 = new Form3(_connString, Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value));
                form3.ShowDialog();
            }
        }

        private void поискВакансийToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4(_connString);
            form4.ShowDialog();
        }
    }
}
