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

namespace KursovayaDvornikovAM
{

    public partial class ProductsForm : Form
    {

        public ProductsForm()
        {
            InitializeComponent();
        }


        private void ProductsForm_Load(object sender, EventArgs e)
        {
            DataTable data = DBconnection.Select("SELECT idTovar,Nazvanie,Kategoria,Cena,Opisanie FROM `ISPr24-37_DvornikovAM_Kursovaya`.Tovar ;");
            dataGridView1.DataSource = data;
            dataGridView1.DataSource = data;
            dataGridView1.AllowUserToAddRows = false;
            
        }

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

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

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

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

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

        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;
        }
        Point lastPoint;
        private void ProductsForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Left += e.X - lastPoint.X;
                this.Top += e.Y - lastPoint.Y;

            }
        }

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

        }




        //редактирование
        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 Tovar SET Nazvanie='" + textBox1.Text + "',Kategoria='" + textBox2.Text + "',Cena='" + textBox3.Text + "',Opisanie='" + textBox4.Text + "' WHERE idTovar='" + id + "'");
                ProductsForm_Click(sender, e);
                textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = "";

            }

        }


        //Добавление убрать склад и айди
        private void Dobavlenie_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == "") || (textBox4.Text == ""))
                MessageBox.Show("Заполните все поля");
            else
            {
                DBconnection.Select("insert into Tovar (`Nazvanie`,`Kategoria`,`Cena`,`Opisanie`) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
                ProductsForm_Click(sender, e);
                textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = "";

            }
        }

        //удаление 
        private void Udalenie_Click(object sender, EventArgs e)
        {
            ProductsForm_Click(sender, e);
            DataTable data = DBconnection.Select(@"DELETE FROM `ISPr24-37_DvornikovAM_Kursovaya`.Tovar WHERE idTovar='" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "';");
            data = DBconnection.Select("SELECT idTovar,Nazvanie,Kategoria,Cena,Opisanie FROM `ISPr24-37_DvornikovAM_Kursovaya`.Tovar;");
            dataGridView1.DataSource = data;

        }

        //Обновление
        private void Obnovlenie_Click(object sender, EventArgs e)
        {
            DataTable data = DBconnection.Select("SELECT idTovar,Nazvanie,Kategoria,Cena,Opisanie FROM `ISPr24-37_DvornikovAM_Kursovaya`.Tovar;");
            ProductsForm_Click(sender, e);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = data;


        }

        private void ProductsForm_Click(object sender, EventArgs e)
        {
            textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = "";
        }

        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 comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DBconnection.Select("SELECT Nazvanie, Kategoria, Cena, Opisanie FROM Tovar order by " + comboBox1.SelectedItem);
        }

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

        //private void textBox5_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        if (string.IsNullOrEmpty(textBox5.Text))
        //        {
        //            customersBindingSource.Filter = string.Empty;
        //        }
        //        else
        //        {
        //            customersBindingSource.Filter = string.Format("0='{1}'", cmbColumn.Text, textBox5.Text;
        //        }
        //    }
        //}

    }
}
