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;

namespace Entity_FrameWork
{
    public partial class Sherkat : Form
    {
        public Sherkat()
        {
            InitializeComponent();
            this.MinimumSize = new System.Drawing.Size(783, 621);
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {

        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();
        private void Sherkat_Load(object sender, EventArgs e)
        {
            dataGridViewSherkat.DataSource = MyModel.Sp_SelectCompany().ToList();
            dataGridViewSherkat.Columns[0].HeaderText = "كد شركت";
            dataGridViewSherkat.Columns[1].HeaderText = "نام شركت";
            dataGridViewSherkat.Columns[2].HeaderText = "توضيحات";
        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            using (Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5())
            {
                Table_Sherkat sherkat = new Table_Sherkat()
                {
                 CompanyName=textBoxCompanyName.Text,
                   Comment=textBoxComment.Text
                };
                MyModel.AddToTable_Sherkat(sherkat);
                MyModel.SaveChanges();
                FMessegeBox.FarsiMessegeBox.Show("ذخيره شد");
                dataGridViewSherkat.DataSource = MyModel.Table_Sherkat.ToList();
            }
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            Table_Sherkat sherkat = new Table_Sherkat();
            int rowdata = Convert.ToInt32(dataGridViewSherkat.CurrentRow.Cells[0].Value.ToString());
            sherkat = MyModel.Table_Sherkat.First(row => row.CodCompany == rowdata);
           
            sherkat.CompanyName = textBoxCompanyName.Text;
            sherkat.Comment = textBoxComment.Text;
            MyModel.SaveChanges();
            FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
            dataGridViewSherkat.DataSource = MyModel.Table_Sherkat.ToList();

        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            Table_Sherkat sherakt = new Table_Sherkat();
            int rowdata = Convert.ToInt32(dataGridViewSherkat.CurrentRow.Cells[0].Value.ToString());
           sherakt= MyModel.Table_Sherkat.First(row=>row.CodCompany==rowdata);

           MyModel.DeleteObject(sherakt);
           MyModel.SaveChanges();
           FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
           dataGridViewSherkat.DataSource = MyModel.Table_Sherkat.ToList();
        }

        private void dataGridViewSherkat_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Kharid kharid = new Kharid(Convert.ToInt32(dataGridViewSherkat.CurrentRow.Cells[0].Value.ToString()));
            kharid.ShowDialog();
        }

        private void dataGridViewSherkat_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                textBoxCompanyName.Text = dataGridViewSherkat.CurrentRow.Cells[1].Value.ToString();
                textBoxComment.Text = dataGridViewSherkat.CurrentRow.Cells[2].Value.ToString();
            }
            catch
            {
            }
        }

        private void button_Sherkatha_Click(object sender, EventArgs e)
        {
            stiReportSherkat.Load(Application.StartupPath + @"\ReportSherkatha.mrt");
            spSelectCompanyResultBindingSource.DataSource = MyModel.Sp_SelectCompany().ToList();
            stiReportSherkat.RegReportDataSources();
            stiReportSherkat.Show();
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {

            dataGridViewSherkat.DataSource = MyModel.Sp_SearchCompany(textBoxSearch.Text);
        }

       
    }
}
