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

namespace garment_factory
{
    public partial class fGetMaterial : Form
    {
        public fGetMaterial()
        {
            InitializeComponent();
        }
        public struct Tkani
        {
            public string NameTkan, ColorTkan, WidthTkan, LengthTkan, Description;
            public Image PhotoTkan;
        }

        List<Tkani> LstTkani = new List<Tkani>();
        public struct Fur
        {
            public string NameFur, Count;
            public Image PhotoFur;
        }

        List<Fur> LstFur = new List<Fur>();
        private void fGetMaterial_Load(object sender, EventArgs e)
        {
            if (Tkan1.SelectedIndex == 0) TbxColorTkan.Text = "1";
        }

        private void DelFurnit_Click(object sender, EventArgs e)
        {
            int NumFur = dgvFur.CurrentRow.Index;
            if (NumFur >= 0)
            {
                DialogResult res = MessageBox.Show("Удалить выделенную ткань?",
                    "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    LstFur.RemoveAt(NumFur);
                    dgvFur.Rows.RemoveAt(NumFur);
                }
            }
        }

        private void BtnLoadPhotoTkan_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    PbxPhotoTkan.Image = Image.FromFile(openFileDialog1.FileName);
            }
            catch { }
        }

        private void BtnClearPhotoTkan_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Очистить фотографию?", "Внимание!",
               MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.Yes)
                PbxPhotoTkan.Image = null;
        }

        private void BtnAddTkan_Click(object sender, EventArgs e)
        {
            if (Tkan1.SelectedIndex == 0) TbxColorTkan.Text = "1";
            else if (Tkan1.SelectedIndex == 1) TbxColorTkan.Text = "2";
            else if (Tkan1.SelectedIndex == 2) TbxColorTkan.Text = "3";
            else if (Tkan1.SelectedIndex == 3) TbxColorTkan.Text = "4";
            else if (Tkan1.SelectedIndex == 4) TbxColorTkan.Text = "5";
            else if (Tkan1.SelectedIndex == 5) TbxColorTkan.Text = "6";
            else if (Tkan1.SelectedIndex == 6) TbxColorTkan.Text = "7";
            else if (Tkan1.SelectedIndex == 7) TbxColorTkan.Text = "8";
            else if (Tkan1.SelectedIndex == 8) TbxColorTkan.Text = "9";
            else if (Tkan1.SelectedIndex == 9) TbxColorTkan.Text = "10";
            else if (Tkan1.SelectedIndex == 10) TbxColorTkan.Text = "10";
            else if (Tkan1.SelectedIndex == 12) TbxColorTkan.Text = "12";
            else if (Tkan1.SelectedIndex == 13) TbxColorTkan.Text = "13";
            else if (Tkan1.SelectedIndex == 14) TbxColorTkan.Text = "14";
            else TbxColorTkan.Text = "15";
            Tkani Tk1 = new Tkani();  
            Tk1.NameTkan = TbxNameTkan.Text;
            Tk1.ColorTkan = TbxColorTkan.Text;
            Tk1.WidthTkan = TbxWidthTkan.Text;
            Tk1.LengthTkan = TbxLengthTkan.Text;
            Tk1.Description = TbxDescriptionTkan.Text;
            Tk1.PhotoTkan = PbxPhotoTkan.Image;
            LstTkani.Add(Tk1); 
            DgvTkani.Rows.Add(Tk1.NameTkan, Tk1.ColorTkan, Tk1.WidthTkan, Tk1.LengthTkan, Tk1.Description);
        }

        private void BtnDelTkan_Click(object sender, EventArgs e)
        {
            int NumTkan = DgvTkani.CurrentRow.Index;
            if (NumTkan >= 0)
            {
                DialogResult res = MessageBox.Show("Удалить выделенную ткань?",
                    "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    LstTkani.RemoveAt(NumTkan);
                    DgvTkani.Rows.RemoveAt(NumTkan);
                }
            }
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            foreach (Tkani Tk1 in LstTkani)
            {
                con.Open();
                if (Tk1.PhotoTkan != null)  
                {
                    string t = string.Format(@"
insert into Tkani (Nazvanie, id_cvet, Shirina, Dlina, Primechanie, Photo)
values ('{0}', '{1}', {2}, {3}, '{4}', @phototkan)",
    Tk1.NameTkan, Tk1.ColorTkan, Tk1.WidthTkan, Tk1.LengthTkan, Tk1.Description);
                    SqlCommand query1 = new SqlCommand(t, con);

                    MemoryStream ms = new MemoryStream();  
                    Tk1.PhotoTkan.Save(ms, ImageFormat.Jpeg);
                    query1.Parameters.AddWithValue("@phototkan", ms.ToArray());
                    query1.ExecuteNonQuery();
                }
                else
                {
                    string t = string.Format(@"
insert into Tkani (Nazvanie, id_cvet, Shirina, Dlina, Primechanie)
values ('{0}', '{1}', {2}, {3}, '{4}')",
    Tk1.NameTkan, Tk1.ColorTkan, Tk1.WidthTkan, Tk1.LengthTkan, Tk1.Description);
                    SqlCommand query1 = new SqlCommand(t, con);
                    query1.ExecuteNonQuery();
                }

                con.Close();
            }
            SqlConnection con1 = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            foreach (Fur Fur1 in LstFur)
            {
                con1.Open();
                if (Fur1.PhotoFur != null)  
                {
                    string t = string.Format(@"insert into Furnitura (namefur,photo,countfur) values ('{0}', '{1}', {2})", Fur1.NameFur, Fur1.PhotoFur, int.Parse(Fur1.Count));
                    SqlCommand query1 = new SqlCommand(t, con1);
                    MemoryStream ms = new MemoryStream();  
                    Fur1.PhotoFur.Save(ms, ImageFormat.Jpeg);
                    query1.Parameters.AddWithValue("@photo", ms.ToArray());
                    query1.ExecuteNonQuery();
                }
                else
                {
                    string t = string.Format(@"insert into Furnitura (namefur,countfur) values ('{0}', '{1}')", Fur1.NameFur, int.Parse(Fur1.Count));
                    SqlCommand query1 = new SqlCommand(t, con1);
                    query1.ExecuteNonQuery();
                }

                con1.Close();
            }

            MessageBox.Show("Документ принят к учету.", "Внимание!",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        private void btnaddPhotofurnit_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
            }
            catch { }
        }

        private void btndelPhotofurnit_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Очистить фотографию?", "Внимание!",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.Yes)
                pictureBox2.Image = null;
        }

        private void AddFurnitura_Click(object sender, EventArgs e)
        {
            Fur Tk1 = new Fur();  
            Tk1.NameFur = tbxNameFur.Text;
            Tk1.Count = tbxCount.Text;
            Tk1.PhotoFur = pictureBox2.Image;
            LstFur.Add(Tk1);  
            dgvFur.Rows.Add(Tk1.NameFur, Tk1.Count);
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
