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

namespace garment_factory
{
    public partial class fKladovschik : Form
    {
        public fKladovschik()
        {
            InitializeComponent();
        }

        static public string idtkan, nametkan, widthtkan, lengthtkan, furid, furname;
        public string IdZakazchik = "";
        string IdTkan = "", IdFur = "";
        public struct Tkani
        {
            public string IdTkan, NameTkan, ColorTkan, Width, Length, Description;
            public Image PhotoTkan;
        }

        List<Tkani> LstTkani = new List<Tkani>();

        void GetTkaniFromDB()
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            con.Open();
            SqlCommand Query = new SqlCommand("select * from Tkani t join Cveta c on t.id_cvet=c.id_cvet where Dlina > 0", con);
            SqlDataReader Data = Query.ExecuteReader();
            while (Data.Read())
            {
                Tkani Tk = new Tkani();
                Tk.IdTkan = Data["id_Tkani"].ToString();
                Tk.NameTkan = Data["Nazvanie"].ToString();
                Tk.ColorTkan = Data["Name_cvet"].ToString();
                Tk.Width = Data["Shirina"].ToString();
                Tk.Length = Data["Dlina"].ToString();
                Tk.Description = Data["Primechanie"].ToString();
                try
                {
                    byte[] PhotoByte = (byte[])Data["Photo"];
                    ImageConverter ImConverter = new ImageConverter();
                    Tk.PhotoTkan = (Bitmap)ImConverter.ConvertFrom(PhotoByte);
                }
                catch
                {
                    Tk.PhotoTkan = Properties.Resources.picture;
                }
                LstTkani.Add(Tk);
            }
            con.Close();
        }

        void FillLVTkani()
        {
            LVTkani.Items.Clear();
            ImageListTkani.Images.Clear();
            foreach (Tkani Tk in LstTkani)
            {
                ListViewItem LV = new ListViewItem(Tk.NameTkan);
                ImageListTkani.Images.Add(Tk.PhotoTkan);
                LV.ImageIndex = ImageListTkani.Images.Count - 1;
                LVTkani.Items.Add(LV);
            }
        }

        private void LVTkani_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LVTkani.SelectedItems.Count > 0)  // есть выделенные элементы
            {
                int num = LVTkani.SelectedIndices[0];  // номер выделенного элемента
                NametkanTextBox.Text = LstTkani[num].NameTkan;
                ColorTextBox.Text = LstTkani[num].ColorTkan;
                WidthTextBox.Text = LstTkani[num].Width;
                LengthTextBox.Text = LstTkani[num].Length;
                DescriptionTextBox.Text = LstTkani[num].Description;
            }
            else  // нет выделенных элементов
            {
                NametkanTextBox.Text = "";
                ColorTextBox.Text = "";
                WidthTextBox.Text = "";
                LengthTextBox.Text = "";
                DescriptionTextBox.Text = "";
            }
        }

        private void BtnProfile_Click(object sender, EventArgs e)
        {
            fProfil frm = new fProfil();
            frm.BsUsers.Filter = this.usersBindingSource.Filter;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.usersTableAdapter.Fill(this.fabrika2024DataSet.Users);
            }
        }

        private void BtnGetMaterial_Click(object sender, EventArgs e)
        {
            fGetMaterial frm = new fGetMaterial();
            frm.ShowDialog();
            GetTkaniFromDB();
            FillLVTkani();
            GetFurnituraFromDB();
            FillLVFurnitura();
        }

        private void BtnSpisMaterial_Click(object sender, EventArgs e)
        {
            fSpisMaterial frm = new fSpisMaterial();
            frm.ShowDialog();
            GetTkaniFromDB();
            FillLVTkani();
            GetFurnituraFromDB();
            FillLVFurnitura();
        }

        private void LVFurnitura_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LVFurnitura.SelectedItems.Count > 0)  // есть выделенные элементы
            {
                int num = LVFurnitura.SelectedIndices[0];  // номер выделенного элемента
                TbxCountFurSklad.Text = LstFurnitura[num].CountFur;
                TbxNameFur.Text = LstFurnitura[num].NameFur;

            }
            else  // нет выделенных элементов
            {

                TbxCountFurSklad.Text = "";
                TbxNameFur.Text = "";
            }
        }

        public struct Furnitura
        {
            public string IdFur, NameFur, CountFur;
            public Image PhotoFur;
        }

        List<Furnitura> LstFurnitura = new List<Furnitura>();

        void GetFurnituraFromDB()
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            con.Open();
            SqlCommand Query = new SqlCommand("select * from Furnitura where countfur > 0", con);
            SqlDataReader Data = Query.ExecuteReader();
            while (Data.Read())
            {
                Furnitura Fr = new Furnitura();
                Fr.IdFur = Data["idfur"].ToString();
                Fr.NameFur = Data["namefur"].ToString();
                Fr.CountFur = Data["countfur"].ToString();
                try
                {
                    byte[] PhotoByte = (byte[])Data["photo"];
                    ImageConverter ImConverter = new ImageConverter();
                    Fr.PhotoFur = (Bitmap)ImConverter.ConvertFrom(PhotoByte);
                }
                catch
                {
                    Fr.PhotoFur = Properties.Resources.picture;
                }
                LstFurnitura.Add(Fr);
            }
            con.Close();
        }

        void FillLVFurnitura()
        {
            LVFurnitura.Items.Clear();
            ImageListFur.Images.Clear();
            foreach (Furnitura Fr in LstFurnitura)
            {
                ListViewItem LV = new ListViewItem(Fr.NameFur);
                ImageListFur.Images.Add(Fr.PhotoFur);
                LV.ImageIndex = ImageListFur.Images.Count - 1;
                LVFurnitura.Items.Add(LV);
            }
        }

        private void fKladovschik_Load(object sender, EventArgs e)
        {
            // TODO: данная строка кода позволяет загрузить данные в таблицу "fabrika2024DataSet.Users". При необходимости она может быть перемещена или удалена.
            this.usersTableAdapter.Fill(this.fabrika2024DataSet.Users);
            string id = id_userLabel1.Text;
            SqlConnection con = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            con.Open();
            string t1 = string.Format($@"select * from Users where id_user = {id}");
            SqlCommand query = new SqlCommand(t1, con);
            // выполнить запрос
            SqlDataReader rdr = query.ExecuteReader();
            while (rdr.Read())
            {
                FIO.Text = rdr["Surname"].ToString() + " " + rdr["Name"].ToString() + " " + rdr["Otch"].ToString();
            }
            con.Close();

            FillListZakaz();

            GetTkaniFromDB();
            FillLVTkani();
            GetFurnituraFromDB();
            FillLVFurnitura();
        }
        void FillListZakaz()
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            DgvZakaz.Rows.Clear();
            string Query = $@"select Izdelie.Name_izd, 
	    Orders.CountIzdel , 
	    Tkani.Nazvanie , 
	    Furnitura.namefur , 
	    Orders.CountFurnit , 
	    UsersMan.Surname as mana, UsersZak.Surname as Orders
    from Orders, Users as UsersMan, Users as UsersZak, 
	    Tkani, Furnitura, Izdelie
    where 
	    UsersMan.Id_user = Orders.id_manager 
	    and UsersZak.Id_user = Orders.id_order 
	    and Tkani.Id_Tkani = Orders.id_tkani 
	    and Furnitura.idfur = Orders.id_furnitur 
	    and Izdelie.id_izdelie = Orders.id_izdelie";
            con.Open();
            SqlCommand cmd = new SqlCommand(Query, con);
            SqlDataReader Rez = cmd.ExecuteReader();
            if (DgvZakaz.Rows.Count > 0)
            {
                DgvZakaz.Rows.Clear();
            }
            while (Rez.Read())
            {
                DgvZakaz.Rows.Add(Rez["Name_izd"], Rez["Nazvanie"], Rez["namefur"], Rez["CountFurnit"], Rez["CountIzdel"], Rez["mana"], Rez["Orders"]);
            }
            con.Close();
        }

    }
}
