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 TelephoneCompany : Form
    {
       
        public TelephoneCompany()
        {
            InitializeComponent();
       
        
        }
    
    

        private void labelX3_Click(object sender, EventArgs e)
        {

        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();

        private void TelephoneCompany_Load(object sender, EventArgs e)
        {
            dataGridViewTelephoneCompany.DataSource = MyModel.Table_TelephoneCompany.ToList();
            dataGridViewTelephoneCompany.Columns[0].HeaderText = "كد سيستمي";
            dataGridViewTelephoneCompany.Columns[1].HeaderText = "نام شركت";
            dataGridViewTelephoneCompany.Columns[2].HeaderText = "نام بخش";
            dataGridViewTelephoneCompany.Columns[3].HeaderText = "نام مسئول";
            dataGridViewTelephoneCompany.Columns[4].HeaderText = "تلفن ثابت";
            dataGridViewTelephoneCompany.Columns[5].HeaderText = "تلفن همراه";
            dataGridViewTelephoneCompany.Columns[6].HeaderText = "آدرس";
        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            using (Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5())
            {
                Table_TelephoneCompany Company = new Table_TelephoneCompany()
                {
                    CompanyName=textBox_CompanyName.Text,
                    NameBakshe=textBox_NameBakshe.Text,
                    NameMassol=textBox_NameMassol.Text,
                    Telephone=textBox_Telephone.Text,
                    Mobile=textBox_Mobile.Text,
                    Address=textBox_Adress.Text


                };
                MyModel.AddToTable_TelephoneCompany(Company);
                MyModel.SaveChanges();
                FMessegeBox.FarsiMessegeBox.Show("ثبت شد");
                dataGridViewTelephoneCompany.DataSource = MyModel.Table_TelephoneCompany.ToList();
            }
        }

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

           Companyupdate =MyModel.Table_TelephoneCompany.First(Row => Row.id == rowdata);

            Companyupdate.CompanyName=textBox_CompanyName.Text;
            Companyupdate.NameBakshe=textBox_NameBakshe.Text;
            Companyupdate.NameMassol=textBox_NameMassol.Text;
            Companyupdate.Telephone=textBox_Telephone.Text;
            Companyupdate.Mobile=textBox_Mobile.Text;
            Companyupdate.Address = textBox_Adress.Text;
            MyModel.SaveChanges();

            FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
            dataGridViewTelephoneCompany.DataSource = MyModel.Table_TelephoneCompany.ToList();

        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            Table_TelephoneCompany ComapanyDelete = new Table_TelephoneCompany();
            int RowData = Convert.ToInt32(dataGridViewTelephoneCompany.CurrentRow.Cells[0].Value.ToString());
         ComapanyDelete=  MyModel.Table_TelephoneCompany.First(Row => Row.id == RowData);
         MyModel.DeleteObject(ComapanyDelete);
         MyModel.SaveChanges();
         FMessegeBox.FarsiMessegeBox.Show("حذف شد");
         dataGridViewTelephoneCompany.DataSource = MyModel.Table_TelephoneCompany.ToList();
        }

        private void dataGridViewTelephoneCompany_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                textBox_CompanyName.Text = dataGridViewTelephoneCompany.CurrentRow.Cells[1].Value.ToString();
                textBox_NameBakshe.Text = dataGridViewTelephoneCompany.CurrentRow.Cells[2].Value.ToString();
                textBox_NameMassol.Text = dataGridViewTelephoneCompany.CurrentRow.Cells[3].Value.ToString();
                textBox_Telephone.Text = dataGridViewTelephoneCompany.CurrentRow.Cells[4].Value.ToString();
                textBox_Mobile.Text = dataGridViewTelephoneCompany.CurrentRow.Cells[5].Value.ToString();
                textBox_Adress.Text = dataGridViewTelephoneCompany.CurrentRow.Cells[6].Value.ToString();
            }
            catch
            {
            }
        }

        private void radioButton_CompanyName_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_CompanyName.Checked == true)
            {
                textBox_Search.Enabled = true;
           
            }
        }

        private void textBox_Search_TextChanged(object sender, EventArgs e)
        {
            if (textBox_Search.Text == "")
            {
                dataGridViewTelephoneCompany.DataSource = MyModel.Table_TelephoneCompany.ToList();
            }
            if (radioButton_CompanyName.Checked == true)
            {
                var SearchCompany = MyModel.Table_TelephoneCompany.Where(Search => Search.CompanyName.Contains(textBox_Search.Text));
                dataGridViewTelephoneCompany.DataSource = SearchCompany;
            }
            if (radioButton_Massol.Checked == true)
            {
                var SearchCompany = MyModel.Table_TelephoneCompany.Where(Search => Search.NameMassol.Contains(textBox_Search.Text));
                dataGridViewTelephoneCompany.DataSource = SearchCompany;
            }
            if (radioButton_NameBakshe.Checked == true)
            {
              var query=  MyModel.Table_TelephoneCompany.Where(row=>row.NameBakshe.Contains(textBox_Search.Text));
              dataGridViewTelephoneCompany.DataSource = query;
            }
        }

        private void radioButton_Massol_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_Massol.Checked == true)
            {
                textBox_Search.Enabled = true;
            }
        }

       

        private void radioButton_NameBakshe_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_NameBakshe.Checked == true)
            {
                textBox_Search.Enabled = true;
            }
        }

        private void button_Search_Click(object sender, EventArgs e)
        {
            if (textBox_Search.Text == "")
            {
                dataGridViewTelephoneCompany.DataSource = MyModel.Table_TelephoneCompany.ToList();
            }
            if (radioButton_CompanyName.Checked == true)
            {
                var SearchCompany = MyModel.Table_TelephoneCompany.Where(Search => Search.CompanyName.Contains(textBox_Search.Text));
                dataGridViewTelephoneCompany.DataSource = SearchCompany;
            }
            if (radioButton_Massol.Checked == true)
            {
                var SearchCompany = MyModel.Table_TelephoneCompany.Where(Search => Search.NameMassol.Contains(textBox_Search.Text));
                dataGridViewTelephoneCompany.DataSource = SearchCompany;
            }
            if (radioButton_NameBakshe.Checked == true)
            {
                var query = MyModel.Table_TelephoneCompany.Where(row => row.NameBakshe.Contains(textBox_Search.Text));
                dataGridViewTelephoneCompany.DataSource = query;
            }
        }

        private void button_PrintTelephoneCompany_Click(object sender, EventArgs e)
        {
            Print_TelephoneCompany Print = new Print_TelephoneCompany();
            Print.ShowDialog();
        }

        private void buttonStimulTelephoneCompany_Click(object sender, EventArgs e)
        {
            PrintTelephoneCompany.Load(Application.StartupPath+@"\ReportPrintTelephoneCompany.mrt");
            tableTelephoneCompanyBindingSource.DataSource = MyModel.Table_TelephoneCompany.ToList();
            PrintTelephoneCompany.RegReportDataSources();
            PrintTelephoneCompany.Show();
        }

      
    }
}
