using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Электронная_Фармация.Classes;

namespace Электронная_Фармация.Forms
{
    public partial class FContractors : Form
    {
        public FContractors()
        {
            InitializeComponent();
        }

        private void FContractors_Load(object sender, EventArgs e)
        {
            UiThemeHelper.ApplyToControlTree(this);
            hideLabelText();
            showMeContentFromSuppliersTable();
        }

        void hideLabelText()
        {
            lblSupplierName.Text = null;
            lblSupplierTelephone.Text = null;
            lblSupplierEmail.Text = null;
            lblSupplierAddress.Text = null;
        }

        void showMeContentFromSuppliersTable()
        {
            using (SQLiteConnection conSuppliers = new SQLiteConnection(AppConfig.SqliteConnectionString))
            {
                string selectFromSuppliers = $@"select [codeSuppliers] as [Код поставщика], [SuppliersName] as [Наименование] from [Suppliers] order by [SuppliersName] asc";
                try
                {
                    conSuppliers.Open();
                    SQLiteCommand cmdShowSuppliersList = new SQLiteCommand(selectFromSuppliers, conSuppliers);
                    DataTable tableSuppliers = new DataTable();
                    SQLiteDataAdapter daSuppliersList = new SQLiteDataAdapter(cmdShowSuppliersList);
                    daSuppliersList.Fill(tableSuppliers);
                    dgvSuppliers.DataSource = tableSuppliers;
                    dgvSuppliers.DefaultCellStyle.Font = new Font("Segoe UI", 10, FontStyle.Regular);

                    


                }
                catch (Exception ex)
                {
                    ToastNotification.ShowError($"Возникла ошибка при заполнении данных о поставщиках");
                    //MessageBox.Show($"Возникла ошибка при заполнении данных о поставщиках.\nТекст ошибки:\n{ex}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    conSuppliers.Close();
                }

                if (dgvSuppliers.Rows.Count > 0)
                {
                    string supplierCode = dgvSuppliers.CurrentRow.Cells[0].Value.ToString();
                    showMeInfoAboutSupplier(supplierCode);
                }
            }
        }

        void showMeInfoAboutSupplier(string codeSupplier)
        {
            string connectionStringToLocalDB = AppConfig.SqliteConnectionString;

            using (SQLiteConnection conInfoAboutSupplier = new SQLiteConnection(connectionStringToLocalDB))
            {
                string qShowMeSupplierName = $"select SuppliersName from Suppliers where codeSuppliers = '{codeSupplier}'";
                string qShowMeSupplierTelephone = $"select SuppliersTelephone from Suppliers where codeSuppliers = '{codeSupplier}'";
                string qShowMeSupplierEmail = $"select SuppliersEmail from Suppliers where codeSuppliers = '{codeSupplier}'";
                string qShowMeSupplierAddress = $"select SuppliersAddress from Suppliers where codeSuppliers = '{codeSupplier}'";

                try
                {
                    conInfoAboutSupplier.Open();

                    SQLiteCommand cmdSupplierName = new SQLiteCommand(qShowMeSupplierName,conInfoAboutSupplier);
                    SQLiteCommand cmdSupplierTelephone = new SQLiteCommand(qShowMeSupplierTelephone, conInfoAboutSupplier);
                    SQLiteCommand cmdSupplierEmail = new SQLiteCommand(qShowMeSupplierEmail, conInfoAboutSupplier);
                    SQLiteCommand cmdSupplierAddress = new SQLiteCommand(qShowMeSupplierAddress, conInfoAboutSupplier);

                    lblSupplierName.Text = cmdSupplierName.ExecuteScalar().ToString();
                    lblSupplierTelephone.Text = cmdSupplierTelephone.ExecuteScalar().ToString();
                    lblSupplierEmail.Text = cmdSupplierEmail.ExecuteScalar().ToString();
                    lblSupplierAddress.Text = cmdSupplierAddress.ExecuteScalar().ToString();
                }
                catch(Exception ex)
                {
                    ToastNotification.ShowError($"Возникла ошибка при загрузке подробной информации о поставщике");
                    //MessageBox.Show($"Возникла ошибка при загрузке подробной информации о поставщике. Текст ошибки:\n{ex}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    conInfoAboutSupplier.Close();
                }
            }
        }

        private void txtInteractiveShowMeSupplier_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (dgvSuppliers.Rows.Count > 0)
                {
                    (dgvSuppliers.DataSource as DataTable).DefaultView.RowFilter = string.Format($"[Наименование] like '%{txtInteractiveShowMeSupplier.Text}%'");
                    if (dgvSuppliers.Rows.Count == 0)
                    {
                        showMeContentFromSuppliersTable();
                    }
                }
                else
                {
                    showMeContentFromSuppliersTable();
                }
                txtInteractiveShowMeSupplier.Text = string.Empty;
            }
        }

        private void dgvSuppliers_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvSuppliers.Rows.Count > 0)
            {
                string supplierCode = dgvSuppliers.CurrentRow.Cells[0].Value.ToString();

                showMeInfoAboutSupplier(supplierCode);
            }
        }
    }
}
