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.Xml.Linq;
using Npgsql;

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

        public Form3(string connString, int edit)
        {
            InitializeComponent();
            _connString = connString;
            _edit = edit;

            using (var conn = new NpgsqlConnection(_connString))
            {
                conn.Open();
                string sql = "SELECT specializationid, name, lastname, surname, passportseries, passportnumber, previoussalary FROM unemployed WHERE id = @id";
                var cmd = new NpgsqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", _edit);

                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    comboBox1.SelectedValue = reader.GetInt32(0);
                    textBox1.Text = reader.GetString(1);
                    textBox2.Text = reader.GetString(2);

                    textBox3.Text = reader.IsDBNull(3) ? "" : reader.GetString(3);
                    textBox4.Text = reader.GetString(4);
                    textBox5.Text = reader.GetString(5);
                    textBox6.Text = reader.IsDBNull(6) ? "" : reader.GetDecimal(6).ToString();
                }
            }
            
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            using (var conn = new NpgsqlConnection(_connString))
            {
                conn.Open();
                var cmd = new NpgsqlCommand("SELECT id, name FROM specialization 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.DisplayMember = "Name";
                comboBox1.ValueMember = "Id";
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var conn = new NpgsqlConnection(_connString))
            {
                var cmd = new NpgsqlCommand();
                conn.Open();
                if(_edit > 0)
                {
                    string sql = @"UPDATE unemployed SET specializationID = @spec, name = @name, lastname = @lastname, 
                               surname = @surname, passportSeries = @pseries, passportNumber = @pnumber, previousSalary = @salary 
                               WHERE ID = @id";
                    cmd = new NpgsqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@id", _edit);
                }
                else
                {
                    string sql = @"INSERT INTO unemployed (specializationID, name, lastname, surname, passportSeries, passportNumber, previousSalary) 
                               VALUES (@spec, @name, @lastname, @surname, @pseries, @pnumber, @salary)";
                    cmd = new NpgsqlCommand(sql, conn);
                }
                
                cmd.Parameters.AddWithValue("@spec", ((dynamic)comboBox1.SelectedItem).Id);
                cmd.Parameters.AddWithValue("@name", textBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@lastname", textBox2.Text.Trim());
                cmd.Parameters.AddWithValue("@surname", string.IsNullOrWhiteSpace(textBox3.Text) ? DBNull.Value : (object)textBox3.Text.Trim());
                cmd.Parameters.AddWithValue("@pseries", textBox4.Text.Trim());
                cmd.Parameters.AddWithValue("@pnumber", textBox5.Text.Trim());
                if (decimal.TryParse(textBox6.Text, out decimal salary) && salary > 0)
                    cmd.Parameters.AddWithValue("@salary", salary);
                else
                    cmd.Parameters.AddWithValue("@salary", DBNull.Value);

                cmd.ExecuteNonQuery();
            }
        }
    }
}
