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;

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

        private void btnExecuteSqlScript_Click(object sender, EventArgs e)
        {
            string connectionStringToLocalDB = AppConfig.SqliteConnectionString;

            string sqlScript = RTXT_Sql.Text;


            using (SQLiteConnection debugCon = new SQLiteConnection(connectionStringToLocalDB))
            {
                try
                {
                    debugCon.Open();
                    SQLiteCommand cmdDebug = new SQLiteCommand(sqlScript, debugCon);
                    cmdDebug.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"При выполнении запроса возникла ошибка. Текст ошибки:\n{ex.ToString()}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    debugCon.Close();
                }
            }
        }

        private void btnCleanSqlScript_Click(object sender, EventArgs e)
        {
            RTXT_Sql.Text = "";
        }

        private void btnExecuteSelectSqlScript_Click(object sender, EventArgs e)
        {
            string connectionStringToLocalDB = AppConfig.SqliteConnectionString;

            string sqlScript = RTXT_Sql.Text;

            using (SQLiteConnection debugSelectCon = new SQLiteConnection(connectionStringToLocalDB))
            {
                try
                {
                    debugSelectCon.Open();
                    SQLiteCommand cmdSelectSqlCommand = new SQLiteCommand(sqlScript, debugSelectCon);
                    SQLiteDataAdapter daSelectDataAdapter = new SQLiteDataAdapter(cmdSelectSqlCommand);
                    DataTable tableSelect = new DataTable();
                    daSelectDataAdapter.Fill(tableSelect);
                    dgvSelectCommand.DataSource = tableSelect;
                }
                catch(Exception ex)
                {
                    MessageBox.Show($"Возникла ошибка при заполнении таблицы данными.\nТекст ошибки:\n{ex.ToString()}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    debugSelectCon.Close();
                }
            }
        }
    }
}
