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

namespace Dictionery_proj
{
    public partial class frmEngToFars : Form
    {
        int int_d = 0;

        public frmEngToFars()
        {
            InitializeComponent();
        }
        private void UDF_Refresh()
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();  //ایجاد شی از کلاس لینک//
            var Query = from Row in db.tblDictioneries
                        orderby Row.DicId descending
                        select new
                        {
                            Row.DicId,
                            Row.DicLoghatEnglish,
                            Row.DicLoghatFarci,
                            Row.DicSynonym
                        };
            Dgv_InsertLoghat.DataSource = Query;   //دادن دستوربه دیتا سورس دیتاگریدویو//
        }

        private void UDF_Insert()
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();  //ایجاد شی از کلاس لینک//
            tblDictionery tbldic = new tblDictionery();    //ایجاد نمونه ای از جدول دیکشنری//
            tbldic.DicLoghatEnglish = Txt_English.Text.Trim();
            tbldic.DicLoghatFarci = Txt_Farci.Text.Trim();
            tbldic.DicSynonym = Txt_Synonym.Text.Trim();
            db.tblDictioneries.InsertOnSubmit(tbldic);
            db.SubmitChanges();
            UDF_Refresh();
            Txt_Synonym.Text = "";
            Txt_Farci.Text = "";
            Txt_English.Text = "";
        }

        private void UDF_Delete()
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();  //ایجاد شی از کلاس لینک//
            tblDictionery tbldic = db.tblDictioneries.Single(n => n.DicId == int.Parse(Txt_Id.Text.Trim()));
            db.tblDictioneries.DeleteOnSubmit(tbldic);
            db.SubmitChanges();
            Txt_Id.Text = "";
            Txt_Synonym.Text = "";
            Txt_Farci.Text = "";
            Txt_English.Text = "";
        }

        private void UDF_Update()
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();   //ایجاد شی از کلاس لینک//
            tblDictionery tbldic = db.tblDictioneries.Single(n => n.DicId == int.Parse(Txt_Id.Text.Trim()));
            tbldic.DicLoghatEnglish = Txt_English.Text.Trim();
            tbldic.DicLoghatFarci = Txt_Farci.Text.Trim();
            tbldic.DicSynonym = Txt_Synonym.Text.Trim();
            tbldic.DicId = int.Parse(Txt_Id.Text.Trim());
            Txt_Synonym.Text = "";
            Txt_Farci.Text = "";
            Txt_English.Text = "";
            Txt_Id.Text = "";
            db.SubmitChanges();
            UDF_Refresh();

        }

        private void UDF_SearchETof()
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();   //ایجاد شی از کلاس لینک//
            var Query = from Row in db.tblDictioneries
                        where SqlMethods.Like(Row.DicLoghatEnglish, "%" + Txt_Search2.Text.Trim() + "%")
                        orderby Row.DicId descending
                        select new
                        {
                            Row.DicId,
                            Row.DicLoghatEnglish,
                            Row.DicLoghatFarci,
                            Row.DicSynonym
                        };
            Dgv_InsertLoghat.DataSource = Query;
        }

        private void UDF_SearchFToE()  
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();   //ایجاد شی از کلاس لینک//
            var Query = from Row in db.tblDictioneries
                        where SqlMethods.Like(Row.DicLoghatFarci, "%" + Txt_Search2.Text.Trim() + "%") //گذاشتن شرط برای جستجو حتی با یک کاراکتر//
                        orderby Row.DicId descending
                        select new
                        {
                            Row.DicId,
                            Row.DicLoghatEnglish,
                            Row.DicLoghatFarci,
                            Row.DicSynonym
                        };
            Dgv_InsertLoghat.DataSource = Query;
        }

        private void UDF_SearchSynonym() 
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();    //ایجاد شی از کلاس لینک//
            var Query = from Row in db.tblDictioneries
                        where SqlMethods.Like(Row.DicSynonym, "%" + Txt_Search2.Text.Trim() + "%")  //گذاشتن شرط برای جستجو حتی با یک کاراکتر//
                        orderby Row.DicId descending
                        select new
                        {
                            Row.DicId,
                            Row.DicLoghatEnglish,
                            Row.DicLoghatFarci,
                            Row.DicSynonym
                        };
            Dgv_InsertLoghat.DataSource = Query;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Txt_English.Text == "" && Txt_Farci.Text == "")
            {
                MessageBox.Show("لطفا فیلد مربوط به لغت انگلیسی و معنی آن راپر نمایید");
            }
            else
            {

                Pnl_EToF1.Visible = false;
                Pnl_EToF2.Visible = true;
                int_d = 1;
            }
        }

        private void frm_EngToFars_Load(object sender, EventArgs e)
        {
            
            
            Pnl_EToF2.Visible = false;
            UDF_Refresh();
        }

        private void Lbl__Synonym_Click(object sender, EventArgs e)
        {

        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            Txt_Id.Enabled = true;

            if (Txt_English.Text == "" && Txt_Farci.Text == "")
            {
                MessageBox.Show("لطفا فیلد های مربوط به لغت انگلیسی و معنی را پر نمایید");
            }
            else
            {
                Pnl_EToF1.Visible = false;
                Pnl_EToF2.Visible = true;
                int_d = 2;
            }
        }

        private void Btn_Delete_Click_1(object sender, EventArgs e)
        {
            Txt_Id.Enabled = true;
            if (Txt_Id.Text == "")
            {
                MessageBox.Show("لطفا کد لغت را وارد نمایید");
            }
            else
            {
                Pnl_EToF1.Visible = false;
                Pnl_EToF2.Visible = true;
                int_d = 3;
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {

        }

        private void Btn_Taeed_Click(object sender, EventArgs e)
        {
            Pnl_EToF2.Visible = false;
            Pnl_EToF1.Visible = true;
            if (int_d == 1)
            {
                UDF_Insert();

            }
            else if (int_d == 2)
            {
                UDF_Update();
            }
            else if (int_d == 3)
            {
                UDF_Delete();
            }
            
            UDF_Refresh();
        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Pnl_EToF2.Visible = false;
            Pnl_EToF1.Visible = true;
            UDF_Refresh();
            Txt_Synonym.Text = "";
            Txt_Id.Text = "";
            Txt_Farci.Text = "";
            Txt_English.Text = "";
        }

        private void Dgv_EngToFars_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();
            Int32 selectedCellCount = Dgv_InsertLoghat.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Dgv_InsertLoghat.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_Id.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_English.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Farci.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_Synonym.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                       
                    }
                }
            }
        }

        private void Btn_Insert_Click(object sender, EventArgs e)
        {
            if (Txt_English.Text == "" && Txt_Farci.Text == "")
            {
                MessageBox.Show("لطفا فیلدهای مربوط به لغت انگلیسی و معنی راپر نمایید");
            }
            else
            {

                Pnl_EToF1.Visible = false;
                Pnl_EToF2.Visible = true;
                int_d = 1;
            }
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            Txt_Id.Enabled = true;

            if (Txt_English.Text == "" && Txt_Farci.Text == "")
            {
                MessageBox.Show("لطفا فیلد های مربوط به لغت انگلیسی و معنی را پر نمایید");
            }
            else
            {
                Pnl_EToF1.Visible = false;
                Pnl_EToF2.Visible = true;
                int_d = 2;
            }
        }

        private void Btn_Delete_Click_2(object sender, EventArgs e)
        {
            Txt_Id.Enabled = true;
            if (Txt_Id.Text == "")
            {
                MessageBox.Show("لطفا کد لغت را وارد نمایید");
            }
            else
            {
                Pnl_EToF1.Visible = false;
                Pnl_EToF2.Visible = true;
                int_d = 3;
            }
        }

        private void Btn_Taeed_Click_1(object sender, EventArgs e)
        {

            Pnl_EToF2.Visible = false;
            Pnl_EToF1.Visible = true;
            if (int_d == 1)
            {
                UDF_Insert();

            }
            else if (int_d == 2)
            {
                UDF_Update();
            }
            else if (int_d == 3)
            {
                UDF_Delete();
            }
         /*   else if (int_d == 4)
            {
                tblDictionery d = new tblDictionery();
                d.DicId = int.Parse(Txt_Id.Text.ToString().Trim());
                UDF_Search(d.DicId);
            }*/
            UDF_Refresh();
        }

        private void Btn_Cancel_Click_1(object sender, EventArgs e)
        {

        }

        private void Btn_Search_Click_1(object sender, EventArgs e)
        {
            if (Rdo_EnglishToFarci2.Checked == true)
            {
                UDF_SearchETof();
            }
            else if(Rdo_FarciToEnglish.Checked==true)
            {
                UDF_SearchFToE();
            }
            else if (Rdo_Synonym2.Checked == true)
            {
                UDF_SearchSynonym();
            }

            
        }

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

        private void Dgv_EngToFars_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            LSC_DictioneryDataContext db = new LSC_DictioneryDataContext();
            Int32 selectedCellCount = Dgv_InsertLoghat.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Dgv_InsertLoghat.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_Id.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_English.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Farci.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_Synonym.Text = Dgv_InsertLoghat.Rows[int.Parse(Dgv_InsertLoghat.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();

                    }
                }
            }
        }
    }
}

