using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Npgsql;

namespace WpfApp1.Views
{
    /// <summary>
    /// Логика взаимодействия для DictionariesView.xaml
    /// </summary>
    public partial class DictionariesView : UserControl
    {
        private const string ConnectionString = "Host=edu-pg.itiscaf.ru;Port=5432;Database=dbwork;Username=emelyanov_ia;Password=4&U4p#u#1M0";
        public DictionariesView()
        {
            InitializeComponent();

            LoadDetails();
            LoadMethods();
        }

        private void LoadDetails()
        {
            try
            {
                using var connection = new NpgsqlConnection(ConnectionString);
                connection.Open();

                string sql = @"
                    SELECT
                        ""Код детали"",
                        ""Деталь"",
                        ""Изделие"",
                        ""Метод обработки"",
                        ""№ операции"",
                        ""Точность обработки"",
                        ""Количество"",
                        ""Норма времени"",
                        ""Общее время""
                    FROM emelyanov_ia.v_details_dictionary
                    ORDER BY ""Код детали"";
                ";

                using var adapter = new NpgsqlDataAdapter(sql, connection);

                DataTable table = new DataTable();
                adapter.Fill(table);

                DetailsDataGrid.ItemsSource = table.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "Ошибка загрузки справочника деталей:\n" + ex.Message,
                    "Ошибка",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void LoadMethods()
        {
            try
            {
                using var connection = new NpgsqlConnection(ConnectionString);
                connection.Open();

                string sql = @"
                    SELECT
                        method_id,
                        method_name,
                        achievable_accuracy
                    FROM emelyanov_ia.processing_methods
                    ORDER BY method_id;
                ";

                using var adapter = new NpgsqlDataAdapter(sql, connection);

                DataTable table = new DataTable();
                adapter.Fill(table);

                MethodsDataGrid.ItemsSource = table.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "Ошибка загрузки справочника методов обработки:\n" + ex.Message,
                    "Ошибка",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }
    }
}
