using MySql.Data.MySqlClient;
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 static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Excel = Microsoft.Office.Interop.Excel;


namespace KursovayaDvornikovAM
{
    public partial class ProviderForm : Form
    {
   
        public ProviderForm()
        {
            InitializeComponent();

        }


        private void ReturnButton_Click(object sender, EventArgs e)
        {
            this.Hide();
            MainForm mainForm = new MainForm();
            mainForm.Show();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void ProviderForm_Load(object sender, EventArgs e)
        {
            DataTable data = DBconnection.Select("SELECT idPostavshik,Kompania,Name,Postavka,Koli4estvoPostavok FROM `ISPr24-37_DvornikovAM_Kursovaya`.Postavshik ;");
            dataGridView1.DataSource = data;
            dataGridView1.DataSource = data;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void HideButton_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void HideButton_MouseEnter(object sender, EventArgs e)
        {
            HideButton.ForeColor = Color.Red;
        }

        private void HideButton_MouseLeave(object sender, EventArgs e)
        {
            HideButton.ForeColor = Color.Black;
        }

        private void CloseButton_MouseEnter(object sender, EventArgs e)
        {
            CloseButton.ForeColor = Color.Red;
        }

        private void CloseButton_MouseLeave(object sender, EventArgs e)
        {
            CloseButton.ForeColor = Color.Black;
        }
        Point lastPoint;
        private void ProviderForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Left += e.X - lastPoint.X;
                this.Top += e.Y - lastPoint.Y;

            }
        }

        private void ProviderForm_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);

        }

        //Добавление
        private void Dobavlenie_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == "") || (textBox4.Text == ""))
                MessageBox.Show("Заполните все поля");
            else
            {
                DBconnection.Select("insert into Postavshik (`Kompania`,`Name`,`Postavka`,`Koli4estvoPostavok`) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
                textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = "";
            }
        }
        //Редактирование
        private void Redactirovanie_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

            if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == "") || (textBox4.Text == ""))
                MessageBox.Show("Заполните все поля");
            else
            {
                DBconnection.Select("UPDATE Postavshik SET Kompania='" + textBox1.Text + "',Name='" + textBox2.Text + "',Postavka='" + textBox3.Text + "',Koli4estvoPostavok='" + textBox4.Text + "' WHERE idPostavshik='" + id + "'");
                textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = "";

            }
        }
        //Удаление
        private void Udalenie_Click(object sender, EventArgs e)
        {
            DataTable data = DBconnection.Select(@"DELETE FROM `ISPr24-37_DvornikovAM_Kursovaya`.Postavshik WHERE idPostavshik='" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "';");
            data = DBconnection.Select("SELECT idPostavshik,Kompania,Name,Postavka,Koli4estvoPostavok FROM `ISPr24-37_DvornikovAM_Kursovaya`.Postavshik;");
            dataGridView1.DataSource = data;
        }
        //Обновление
        private void Obnovlenie_Click(object sender, EventArgs e)
        {
            DataTable data = DBconnection.Select("SELECT idPostavshik,Kompania,Name,Postavka,Koli4estvoPostavok FROM `ISPr24-37_DvornikovAM_Kursovaya`.Postavshik;");
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = data;
        }

        private void Export_Click(object sender, EventArgs e)
        {
            Excel.Application exApp = new Excel.Application();

            exApp.Workbooks.Add();
            Excel.Worksheet worksheet = (Excel.Worksheet)exApp.ActiveSheet;
            int i, j;
            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                {
                    worksheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString();
                }
            }

            exApp.Visible = true;
        }

        private void Search_TextChanged(object sender, EventArgs e)
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = $"Kompania LIKE '%{Search.Text}%'";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DBconnection.Select("SELECT Kompania,Name,Postavka,Koli4estvoPostavok FROM Postavshik order by " + comboBox1.SelectedItem);
        }
    }
}
