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 TelephonePerson : Form
    {
        public TelephonePerson()
        {
            InitializeComponent();
            this.MinimumSize = new System.Drawing.Size(879, 504);
        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();
        private void TelephonePersone_Load(object sender, EventArgs e)
        {
            dataGridViewTelephonePersone.DataSource = MyModel.Table_TelephonePerson.ToList();
            dataGridViewTelephonePersone.Columns[0].HeaderText = "كد سيستمي";
            dataGridViewTelephonePersone.Columns[1].HeaderText = "نام";
            dataGridViewTelephonePersone.Columns[2].HeaderText = "نام خانوادگي";
            dataGridViewTelephonePersone.Columns[3].HeaderText = "تلفن";
            dataGridViewTelephonePersone.Columns[4].HeaderText = "تلفن همراه";
           dataGridViewTelephonePersone.Columns[5].HeaderText = "آدرس";

        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            using (Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5())
            {
                Table_TelephonePerson persone =new Table_TelephonePerson()
                {
                    Name=textBox_Name.Text,
                    LastName=textBox_LastName.Text,
                    Telephone=textBox_Telephone.Text,
                    Mobile=textBox_Mobile.Text,
                    Address=textBox_Adress.Text
                };
                MyModel.AddToTable_TelephonePerson(persone);
                MyModel.SaveChanges();
                FMessegeBox.FarsiMessegeBox.Show("ثبت شد");
                dataGridViewTelephonePersone.DataSource = MyModel.Table_TelephonePerson.ToList();
            }
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            Table_TelephonePerson personeupdate = new Table_TelephonePerson();
            int rowdata = Convert.ToInt32(dataGridViewTelephonePersone.CurrentRow.Cells[0].Value.ToString());

           personeupdate= MyModel.Table_TelephonePerson.First(row => row.id == rowdata);
                  personeupdate.  Name=textBox_Name.Text;
                    personeupdate.   LastName=textBox_LastName.Text;
                    personeupdate.   Telephone=textBox_Telephone.Text;
                    personeupdate.   Mobile=textBox_Mobile.Text;
                    personeupdate.Address = textBox_Adress.Text;
           MyModel.SaveChanges();
           FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
           dataGridViewTelephonePersone.DataSource = MyModel.Table_TelephonePerson.ToList();

        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            Table_TelephonePerson personeDelete = new Table_TelephonePerson();

            int rowdata = Convert.ToInt32(dataGridViewTelephonePersone.CurrentRow.Cells[0].Value.ToString());
            personeDelete = MyModel.Table_TelephonePerson.First(row => row.id == rowdata);
            MyModel.DeleteObject(personeDelete);
            MyModel.SaveChanges();
            FMessegeBox.FarsiMessegeBox.Show("حذف شد");
            dataGridViewTelephonePersone.DataSource = MyModel.Table_TelephonePerson.ToList();


        }

        private void textBox_Search_TextChanged(object sender, EventArgs e)
        {
            if (textBox_Search.Text == "")
            {
                dataGridViewTelephonePersone.DataSource = MyModel.Table_TelephonePerson.ToList();

            }

            else
            {
                var Search = MyModel.Table_TelephonePerson.Where(row => row.LastName.Contains(textBox_Search.Text));
                dataGridViewTelephonePersone.DataSource = Search;
            }
        }

        private void button_Search_Click(object sender, EventArgs e)
        {
            if (textBox_Search.Text == "")
            {
                dataGridViewTelephonePersone.DataSource = MyModel.Table_TelephonePerson.ToList();

            }

            else
            {
                var Search = MyModel.Table_TelephonePerson.Where(row => row.LastName.Contains(textBox_Search.Text));
                dataGridViewTelephonePersone.DataSource = Search;
            }
        }

        private void dataGridViewTelephonePersone_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
                 textBox_Name.Text= dataGridViewTelephonePersone.CurrentRow.Cells[1].Value.ToString();
                textBox_LastName.Text=dataGridViewTelephonePersone.CurrentRow.Cells[2].Value.ToString();
                 textBox_Telephone.Text=dataGridViewTelephonePersone.CurrentRow.Cells[3].Value.ToString();
                textBox_Mobile.Text=dataGridViewTelephonePersone.CurrentRow.Cells[4].Value.ToString();
                textBox_Adress.Text = dataGridViewTelephonePersone.CurrentRow.Cells[5].Value.ToString();
               
        }

        private void button_Print_Click(object sender, EventArgs e)
        {
            PrintTelephonePerson.Load(Application.StartupPath + @"\ReportTelephonePerson.mrt");
            tableTelephonePersonBindingSource.DataSource = MyModel.Table_TelephonePerson.ToList();
            PrintTelephonePerson.RegReportDataSources();
            PrintTelephonePerson.Show();
        }

        private void dataGridViewTelephonePersone_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (FMessegeBox.FarsiMessegeBox.Show("", "", FMessegeBox.FMessegeBoxButtons.YesNo, FMessegeBox.FMessegeBoxIcons.Question) == DialogResult.Yes)
            {
            }
        }
    }
}
