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

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

                string sql = @"
                    SELECT DISTINCT
                        detail_id,
                        detail_name
                    FROM emelyanov_ia.details
                    ORDER BY detail_name;
                ";

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

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

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

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

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

                using var command = new NpgsqlCommand(sql, connection);
                command.Parameters.AddWithValue("@detail_id", detailId);

                using var adapter = new NpgsqlDataAdapter(command);

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

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

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            if (DetailsComboBox.SelectedValue == null)
                return;

            int detailId = Convert.ToInt32(DetailsComboBox.SelectedValue);

            LoadProductsByDetail(detailId);
        }
    }
}
