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 Электронная_Фармация.Classes;
using System.Data.SQLite;
using Электронная_Фармация;

namespace Электронная_Фармация.HelpForms
{
    public partial class HFConsignees : Form
    {
        public ElectroPharmacy  ParentForm { get; set; }
        public HFConsignees()
        {
            InitializeComponent();
        }

        void loadingDataAboutConsignees()
        {
            string connectionStringToLocalDB = AppConfig.SqliteConnectionString;

            using (SQLiteConnection conConsigneesList = new SQLiteConnection(connectionStringToLocalDB))
            {
                try
                {
                    conConsigneesList.Open();

                    string qShowMeConsigneesList = "select ConsigneesName as [Грузополучатель] from Consignees order by ConsigneesUseAsDefault desc";

                    SQLiteCommand cmdShowMeConsigneesList = new SQLiteCommand(qShowMeConsigneesList, conConsigneesList);
                    SQLiteDataAdapter daConsigneesList = new SQLiteDataAdapter(cmdShowMeConsigneesList);
                    DataTable tableConsignees = new DataTable();
                    daConsigneesList.Fill(tableConsignees);
                    dgvConsignees.DataSource = tableConsignees;
                }
                catch (Exception ex)
                {
                    UiDialogs.ShowError($"При загрузке списка получателей возникла ошибка.\n{ex}", "Ошибка", this);
                }
                finally
                {
                    conConsigneesList.Close();
                }
            }
        }

        private void btnCloseThis_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConfirmConsignee_Click(object sender, EventArgs e)
        {
            if (dgvConsignees.CurrentRow == null)
            {
                UiDialogs.ShowInfo("Выберите грузополучателя из списка.", "Грузополучатель", this);
                return;
            }

            string consigneesName = dgvConsignees.CurrentRow.Cells[0].Value.ToString();
            ParentForm?.showMePriceList(consigneesName);
            DialogResult = DialogResult.OK;
            Close();
        }

        private void HFConsignees_Load(object sender, EventArgs e)
        {
            UiThemeHelper.ApplyToControlTree(this);
            loadingDataAboutConsignees();
        }
    }
}
