using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Швейная_фабрика
{
    public partial class FormManager : Form
    {
        public FormManager()
        {
            InitializeComponent();
        }

        DataRowView CurrentUser;
        SqlConnection con = new SqlConnection("Data Source=213.155.192.79,3002;Initial Catalog=sewing_factory;Persist Security Info=True;User ID=u21baranov;Password=au5p");
        void RefreshZakazi()
        {
            DgvZakazi.Rows.Clear();
            string Query = $@"SELECT  Izdelie.name_izd, Tkan.Title, Furnitura.namefur, Orders.countfur, Orders.CountIzd
FROM     Orders INNER JOIN
                  Furnitura ON Orders.id_fur = Furnitura.id_fur INNER JOIN
                  Izdelie ON Orders.id_izd = Izdelie.id_izd INNER JOIN
                  Tkan ON Orders.id_tkan = Tkan.id_Tkan INNER JOIN
                  Users ON Orders.id_men = Users.id_User
where Users.id_User = {lblID.Text}";
            con.Open();
            SqlCommand cmd = new SqlCommand(Query, con);
            SqlDataReader Rez = cmd.ExecuteReader();
            while (Rez.Read())
            {
                DgvZakazi.Rows.Add(Rez["name_izd"], Rez["Title"], Rez["namefur"], Rez["countfur"], Rez["CountIzd"]);
            }
            con.Close();
        }

        private void FormManager_Load(object sender, EventArgs e)
        {
            // TODO: данная строка кода позволяет загрузить данные в таблицу "сlothesFactoryLGDataSet.Users". При необходимости она может быть перемещена или удалена.
            this.usersTableAdapter.Fill(this.сlothesFactoryLGDataSet.Users);
            // TODO: данная строка кода позволяет загрузить данные в таблицу "сlothesFactoryLGDataSet.Izdelie". При необходимости она может быть перемещена или удалена.
            this.izdelieTableAdapter.Fill(this.сlothesFactoryLGDataSet.Izdelie);
            RefreshZakazi();
            lblID.Hide();
        }

        private void BtnProfil_Click(object sender, EventArgs e)
        {
            FormProfil frm = new FormProfil();
            frm.BsUsers.Filter = this.BsUsers.Filter;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.usersTableAdapter.Fill(this.сlothesFactoryLGDataSet.Users);
            }
            RefreshZakazi();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            FormAddIzdelie FrmAddEdit = new FormAddIzdelie();
            FrmAddEdit.LblFunc.Text = "Добавление изделия";
            if (FrmAddEdit.ShowDialog() == DialogResult.OK)
            {
                this.izdelieTableAdapter.Fill(this.сlothesFactoryLGDataSet.Izdelie);
                MessageBox.Show("Изделие добавлено.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            RefreshZakazi();
        }

        private void BtnRed_Click(object sender, EventArgs e)
        {
            FormAddIzdelie FrmAddEdit = new FormAddIzdelie();
            FrmAddEdit.LblFunc.Text = "Редактирование изделия";
            FrmAddEdit.BSizdelie.Filter = $"id_izd = {DgvIzdeliy.CurrentRow.Cells[0].Value}";
            if (FrmAddEdit.ShowDialog() == DialogResult.OK)
            {
                this.izdelieTableAdapter.Fill(this.сlothesFactoryLGDataSet.Izdelie);
                MessageBox.Show("Изделие обновлено.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            RefreshZakazi();
        }

        private void BtnExportAll_Click(object sender, EventArgs e)
        {
            try
            {
                if (RbxWORD.Checked)
                {
                    if (SaveFDWord.ShowDialog() == DialogResult.OK)
                    {
                        StreamWriter f = new StreamWriter(SaveFDWord.FileName);
                        string t = $"{LblFIO.Text}";
                        f.WriteLine(t);
                        for (int i = 0; i < DgvZakazi.RowCount; i++)
                        {
                            t = "\r\n";
                            t += "Izdelie: " + DgvZakazi.Rows[i].Cells[0].Value + "\r\n";
                            t += "Tkan: " + DgvZakazi.Rows[i].Cells[1].Value + "\r\n";
                            t += "Furnitura: " + DgvZakazi.Rows[i].Cells[2].Value + "\r\n";
                            t += "CountFurnit: " + DgvZakazi.Rows[i].Cells[3].Value + "\r\n";
                            t += "CountIzdel: " + DgvZakazi.Rows[i].Cells[4].Value + "\r\n";
                            f.WriteLine(t);
                        }
                        f.Close();
                    }
                }
                else
                {
                    if (SaveFDExcel.ShowDialog() == DialogResult.OK)
                    {
                        StreamWriter f = new StreamWriter(SaveFDExcel.FileName);
                        string t = $"{LblFIO.Text}";
                        f.WriteLine(t);
                        t = "Izdelie;Tkan;Furnitura;CountFurnit;CountIzdel";
                        f.WriteLine(t);

                        for (int i = 0; i < DgvZakazi.RowCount; i++)
                        {
                            t = "";
                            t += DgvZakazi.Rows[i].Cells[0].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[1].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[2].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[3].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[4].Value + ";";
                            f.WriteLine(t);
                        }
                        f.Close();
                    }
                }
                MessageBox.Show("Файл сохранен.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("Ошибка в сохранении файла.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnExportSelected_Click(object sender, EventArgs e)
        {
            try
            {
                if (RbxWORD.Checked)
                {
                    if (SaveFDWord.ShowDialog() == DialogResult.OK)
                    {
                        StreamWriter f = new StreamWriter(SaveFDWord.FileName);
                        string t = $"{LblFIO.Text}";
                        f.WriteLine(t);
                        for (int i = 0; i < DgvZakazi.SelectedRows.Count; i++)
                        {
                            t = "\r\n";
                            t += "Izdelie: " + DgvZakazi.Rows[i].Cells[0].Value + "\r\n";
                            t += "Tkan: " + DgvZakazi.Rows[i].Cells[1].Value + "\r\n";
                            t += "Furnitura: " + DgvZakazi.Rows[i].Cells[2].Value + "\r\n";
                            t += "CountFurnit: " + DgvZakazi.Rows[i].Cells[3].Value + "\r\n";
                            t += "CountIzdel: " + DgvZakazi.Rows[i].Cells[4].Value + "\r\n";
                            f.WriteLine(t);
                        }
                        f.Close();
                    }
                }
                else
                {
                    if (SaveFDExcel.ShowDialog() == DialogResult.OK)
                    {
                        StreamWriter f = new StreamWriter(SaveFDExcel.FileName);
                        string t = $"{LblFIO.Text}";
                        f.WriteLine(t);
                        t = "Izdelie;Tkan;Furnitura;CountFurnit;CountIzdel";
                        f.WriteLine(t);

                        for (int i = 0; i < DgvZakazi.SelectedRows.Count; i++)
                        {
                            t = "";
                            t += DgvZakazi.Rows[i].Cells[0].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[1].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[2].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[3].Value + ";";
                            t += DgvZakazi.Rows[i].Cells[4].Value + ";";
                            f.WriteLine(t);
                        }
                        f.Close();
                    }
                }
                MessageBox.Show("Файл сохранен.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("Ошибка в сохранении файла.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
