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


namespace Entity_FrameWork
{
    public partial class Kala : Form
    {
       int CodFactor;
        public Kala(int _CodFactor)
        {
            InitializeComponent();
            CodFactor = _CodFactor;
            this.MinimumSize = new System.Drawing.Size(978, 491);
        }

        Entity_FrameWorkEntities5 mymodel = new Entity_FrameWorkEntities5();
        int k;
        private void Form1_Load(object sender, EventArgs e)
        {
             
            dataGridViewkala.DataSource = mymodel.Table_Kala.ToList();
            dataGridViewkala.Columns[2].HeaderText = "كد كالا";
            dataGridViewkala.Columns[3].HeaderText = "نام كالا ";
            dataGridViewkala.Columns[4].HeaderText = "تصويركالا ";
            dataGridViewkala.Columns[5].HeaderText = "مدل ";
            dataGridViewkala.Columns[6].HeaderText = "رنگ ";
            dataGridViewkala.Columns[7].HeaderText = " تعداد";
            dataGridViewkala.Columns[8].HeaderText = "مبلغ ";
            if (dataGridViewkala.Rows[k].Cells[7].Value.ToString() == "0")
            {
                dataGridViewkala.Rows[k].Cells[Check.Name].Value = Check.ReadOnly = true;
            }
           

            try
            {
                for (int j = 0; j <= dataGridViewkala.RowCount; j++)
                {
                    if (dataGridViewkala.Rows[j].Cells[7].Value.ToString() == "0")
                    {
                        dataGridViewkala.Rows[j].DefaultCellStyle.ForeColor = Color.Red;
                        dataGridViewkala.Rows[j].Cells[0].ReadOnly = true;
                    }
                }
            }
            catch
            {
            }
                for (int i = 0; i < dataGridViewkala.Columns.Count; i++)
                    if (dataGridViewkala.Columns[i] is DataGridViewImageColumn)
                    {
                        ((DataGridViewImageColumn)dataGridViewkala.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
                        break;
                    }

           
            


        }

        private void ribbonClientPanel1_Click(object sender, EventArgs e)
        {

        }

        private void button_insert_Click(object sender, EventArgs e)
        {
            try
            {
                MemoryStream Memory = new MemoryStream();
                pictureBox1.Image.Save(Memory,pictureBox1.Image.RawFormat);
                byte[] imagebyte = Memory.GetBuffer();

                Table_Kala kala = new Table_Kala()
                {
                    NameKala = textBox_NameKala.Text,
                    Model = textBox_Model.Text,
                    Color = textBox_Color.Text,
                    Tedad = textBox_Tedad.Text,
                    image=imagebyte,
                    Money = Convert.ToInt32(textBox_Money.Text),
                };


                mymodel.AddToTable_Kala(kala);
                mymodel.SaveChanges();

                FMessegeBox.FarsiMessegeBox.Show("عمليات ثبت با موفقيت انجام شد ");

                dataGridViewkala.DataSource = mymodel.Table_Kala.ToList();
            }
            catch
            {
                MessageBox.Show("مقدار مبلغ بايد از نوع عددي باشد لطفا مقادير را به طور كامل وارد كنيد ");
            }


        }

        private void dataGridViewkala_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                textBox_NameKala.Text = dataGridViewkala.CurrentRow.Cells[3].Value.ToString();
                textBox_Model.Text = dataGridViewkala.CurrentRow.Cells[5].Value.ToString();
                textBox_Color.Text = dataGridViewkala.CurrentRow.Cells[6].Value.ToString();
                textBox_Tedad.Text = dataGridViewkala.CurrentRow.Cells[7].Value.ToString();
                textBox_Money.Text = dataGridViewkala.CurrentRow.Cells[8].Value.ToString();

                int rowdata = Convert.ToInt32(dataGridViewkala.CurrentRow.Cells[2].Value.ToString());
                Table_Kala kala = new Table_Kala();
                kala = mymodel.Table_Kala.First(Row => Row.CodKala == rowdata);
                byte[] imagedata = (byte[])(kala.image.ToArray());
                MemoryStream memory = new MemoryStream(imagedata);
                pictureBox1.Image = Image.FromStream(memory);
            }
            catch
            {
            }
        }

        private void button_Update_Click(object sender, EventArgs e)
        {

            try
            {
                MemoryStream Memory = new MemoryStream();
                pictureBox1.Image.Save(Memory, pictureBox1.Image.RawFormat);
                byte[] imagebyte = Memory.GetBuffer();

                int RowData = Convert.ToInt32(dataGridViewkala.CurrentRow.Cells[2].Value.ToString());
                Table_Kala kala = mymodel.Table_Kala.First(Row => Row.CodKala == RowData);


                kala.NameKala = textBox_NameKala.Text;
                kala.image = imagebyte;
                kala.Model = textBox_Model.Text;
                kala.Color = textBox_Color.Text;
                kala.Tedad = textBox_Tedad.Text;
                kala.Money = Convert.ToInt32(textBox_Money.Text);

                mymodel.SaveChanges();
                FMessegeBox.FarsiMessegeBox.Show("ويرايش با موفقيت انجام شد");
                dataGridViewkala.DataSource = mymodel.Table_Kala.ToList();
                try
                {
                    for (int j = 0; j <= dataGridViewkala.RowCount; j++)
                    {
                        if (dataGridViewkala.Rows[j].Cells[7].Value.ToString() == "0")
                        {
                            dataGridViewkala.Rows[j].DefaultCellStyle.ForeColor = Color.Red;
                            dataGridViewkala.Rows[j].Cells[0].ReadOnly = true;
                        }
                    }
                }
                catch
                {
                }



            }
            catch
            {
            }
        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            if (FMessegeBox.FarsiMessegeBox.Show("آيا سطري جاري حذف شود؟", "پرسش", FMessegeBox.FMessegeBoxButtons.YesNo, FMessegeBox.FMessegeBoxIcons.Question) == DialogResult.Yes)
            {
                int row = Convert.ToInt32(dataGridViewkala.CurrentRow.Cells[0].Value.ToString());
                Table_Kala kala = mymodel.Table_Kala.First(rowdata => rowdata.CodKala == row);
                mymodel.DeleteObject(kala);
                mymodel.SaveChanges();
                dataGridViewkala.DataSource = mymodel.Table_Kala.ToList();
            }
        }



        private void button_Search_Click(object sender, EventArgs e)
        {
            var query = mymodel.Table_Kala.Where(Search => Search.NameKala.Contains(textBox_Search.Text));
            dataGridViewkala.DataSource = query;

        }

        private void textBox_Search_TextChanged(object sender, EventArgs e)
        {
            if (textBox_Search.Text == "")
            {
                dataGridViewkala.DataSource = mymodel.Table_Kala.ToList();
            }
            else
            {
                var query = mymodel.Table_Kala.Where(Search => Search.NameKala.Contains(textBox_Search.Text));
                dataGridViewkala.DataSource = query;
            }
        }
        int Money,Mohsebe,Tedad;
        string Javab;
        int i;
        
        private void button_Trans_Click(object sender, EventArgs e)
        {

            Customer customer = new Customer();
            try
            {

                foreach (DataGridViewRow row in dataGridViewkala.Rows)
                {

                    if (Convert.ToBoolean(row.Cells[Check.Name].Value) == true)
                    {
                        if (CodFactor == 0)
                        {
                            FMessegeBox.FarsiMessegeBox.Show("براي ثبت خريد از فرم مشتري اقدام فرماييد", "پيام سيستم", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Exclamtion);
                        }
                        if (row.Cells[1].Value == null)
                        {
                            FMessegeBox.FarsiMessegeBox.Show("تعداد خريد را وارد كنيد ", "پيام سيستم", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Exclamtion);
                        }
                        else
                        {

                            Tedad = Convert.ToInt32(row.Cells[1].Value.ToString());
                            Money = Convert.ToInt32(row.Cells[8].Value.ToString());
                            Mohsebe = Tedad * Money;


                            mymodel.Sp_InsertFactor(CodFactor, row.Cells[3].Value.ToString(), row.Cells[5].Value.ToString(), row.Cells[6].Value.ToString(), row.Cells[1].Value.ToString(), Mohsebe);
                            mymodel.Sp_UpdateTedad(Convert.ToInt32(row.Cells[2].Value.ToString()), Convert.ToInt32(row.Cells[1].Value.ToString()));

                            
                        }

                    }





                }





            }



            catch
            {

                FMessegeBox.FarsiMessegeBox.Show("لطفا تعداد خريد را وارد كنيد ");
            }

            finally
            {

                FMessegeBox.FarsiMessegeBox.Show("اطلاعات خريد با موفقيت ثبت شد");
                Factor factor = new Factor(CodFactor);

                factor.ShowDialog();
            }

            }
        
       
       

        private void button_MasirTasvir_Click(object sender, EventArgs e)
        {
         
        }

        private void button_MasirTasvir_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.FilterIndex = 1;
            open.Filter = "Jpge files (*.jpg)|*jpg|Png File(*.png)|*.png|All files (*.*)|*.*";
            open.ShowDialog();
                     
            pictureBox1.ImageLocation = open.FileName;
            
          
        }

        private void buttonKala_Click(object sender, EventArgs e)
        {
            stiReportkala.Load(Application.StartupPath + @"\ReportKala.mrt");
            tableKalaBindingSource.DataSource = mymodel.Table_Kala.ToList();
            stiReportkala.RegReportDataSources();
            stiReportkala.Show();
            
        }

        private void dataGridViewkala_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            
        }
      
        private void dataGridViewkala_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          

          
             
           

                

            
            
        }

        private void button_Kharid_Click(object sender, EventArgs e)
        {
           DataBuy databuy = new DataBuy(CodFactor);
           try
           {

               if (CodFactor == 0)
               {
                   FMessegeBox.FarsiMessegeBox.Show("براي ثبت كالا از فرم خريد اقدام فرماييد", "پيام سيستم", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Exclamtion);
               }
               for (int c = 0; c <= dataGridViewkala.RowCount; c++)
               {



                   if (Convert.ToBoolean(dataGridViewkala.Rows[c].Cells[Check.Name].Value) == true)
                   {
                       if (dataGridViewkala.Rows[c].Cells[1].Value == null)
                       {
                           FMessegeBox.FarsiMessegeBox.Show("تعداد خريد را وارد كنيد ", "پيام سيستم", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Exclamtion);
                       }



                       mymodel.Sp_InsertDataBuy(CodFactor, Convert.ToInt32(dataGridViewkala.Rows[c].Cells[2].Value.ToString()), dataGridViewkala.Rows[c].Cells[3].Value.ToString(), dataGridViewkala.Rows[c].Cells[5].Value.ToString(), dataGridViewkala.Rows[c].Cells[6].Value.ToString(), dataGridViewkala.Rows[c].Cells[1].Value.ToString(), Convert.ToInt32(dataGridViewkala.Rows[c].Cells[8].Value.ToString()));
                       //dataGridViewkala.Rows[c].Cells[Check.Name].Value = Check.FalseValue = true;
                      
                          
                       }
                                                   

                   
               }


           }

           catch
           {

           }
           finally
           {
         FMessegeBox.FarsiMessegeBox.Show("اطلاعات خريد با موفقيت ثبت شد");
                           DataBuy data = new DataBuy(CodFactor);
                           databuy.ShowDialog();
           }
          
        }

       
       
        }
    }

