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

namespace CofeeShop
{
    public partial class frmDefineFoods : Form
    {
        public frmDefineFoods()
        {
            InitializeComponent();
        }

        private void frmDefineFoods_Load(object sender, EventArgs e)
        {
            Class_tblLogs LogReg = new Class_tblLogs(this.Text + " فعال گردید.");
            LogReg.UDF_SaveLogToDb();
            UDF_CmbFill();
            UDF_GrdFill();
        }

        private void frmDefineFoods_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_tblLogs LogReg = new Class_tblLogs(this.Text + " بسته شد.");
            LogReg.UDF_SaveLogToDb();
        }

        private void UDF_CmbFill()
        {
            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            var Query1 = from Row in db.tblFoodUnits
                        orderby Row.FoodUnitID descending
                        select new
                        {
                            Row.FoodUnitID,
                            Row.FoodUnitDescription
                        };
            var Query2 = from Row in db.tblFoodCateguries
                         orderby Row.FoodCateguryID descending
                         select new
                         {
                             Row.FoodCateguryID,
                             Row.FoodCateguryDescription
                         };

            Cmb_FoodUnit.DisplayMember = "FoodUnitDescription";
            Cmb_FoodUnit.ValueMember = "FoodUnitID";

            Cmb_FoodCategury.DisplayMember = "FoodCateguryDescription";
            Cmb_FoodCategury.ValueMember = "FoodCateguryID";

            Cmb_FoodUnit.DataSource = Query1;
            Cmb_FoodCategury.DataSource = Query2;
        }

        private void UDF_GrdFill()
        {
            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            var Query = from Row in db.tblFoodsDescriptions
                         orderby Row.FoodID descending
                         select new
                         {
                             Row.FoodID,
                             Row.FoodDescription,
                             Row.tblFoodCategury.FoodCateguryDescription,
                             Row.tblFoodUnit.FoodUnitDescription
                         };
            dataGridView1.DataSource = Query;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Txt_FoodDescription.Text = String.Empty;
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            Txt_FoodDescription.Clear();
            MessageBox.Show("لطفا اطلاعات را وارد نمایید");
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_FoodDescription.Text == "")
            {
                MessageBox.Show("ورود عنوان الزامی می باشد");
                return;
            }

            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();

            try
            {
                tblFoodsDescription FoodDs = db.tblFoodsDescriptions.Single(n => n.FoodDescription.Trim() == Txt_FoodDescription.Text.Trim());
                MessageBox.Show("نام غذای وارد شده تکراری می باشد");
            }
            catch
            {
                tblFoodsDescription FoodDs = new tblFoodsDescription();

                FoodDs.FoodDescription = Txt_FoodDescription.Text.Trim();
                FoodDs.FoodCateguryID = Decimal.Parse(Cmb_FoodCategury.SelectedValue.ToString());
                FoodDs.FoodUnitID = int.Parse(Cmb_FoodUnit.SelectedValue.ToString());

                db.tblFoodsDescriptions.InsertOnSubmit(FoodDs);
                db.SubmitChanges();

                UDF_GrdFill();
                UDF_CmbFill();
                btnAdd.BringToFront();
                Btn_Save.SendToBack();
                Txt_FoodDescription.Clear();
                MessageBox.Show("رکورد جدید درج گردید.");
            }


        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_FoodDescription.Text == "" || Txt_FoodID.Text == "")
            {
                MessageBox.Show("لطفا عنوان را انتخاب فرمائید.");
                return;
            }
            if (Cmb_FoodUnit.Text == "" || Cmb_FoodCategury.Text =="")
            {
                MessageBox.Show("انتخاب دسته بندی و واحد شمارش الزامی می باشد");
                return;
            }

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
                tblFoodsDescription FoodDs = db.tblFoodsDescriptions.Single(n => n.FoodID == int.Parse(Txt_FoodID.Text.Trim()));

                FoodDs.FoodDescription = Txt_FoodDescription.Text.Trim();
                FoodDs.FoodUnitID = int.Parse(Cmb_FoodUnit.SelectedValue.ToString());
                FoodDs.FoodCateguryID = Decimal.Parse(Cmb_FoodCategury.SelectedValue.ToString());

                db.SubmitChanges();
                Class_tblLogs LogReg = new Class_tblLogs(this.Text + " رکورد " + Txt_FoodDescription.Text.Trim() + " -ID= " + Txt_FoodID.Text.Trim() + "ویرایش");
                LogReg.UDF_SaveLogToDb();

                Txt_FoodID.Clear();
                Txt_FoodDescription.Clear();
                UDF_CmbFill();
                UDF_GrdFill();
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);

            if (selectedCellCount > 0)
            {
                if (dataGridView1.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_FoodID.Text = dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_FoodDescription.Text = dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                    }
                    UDF_SelectCmbValue(int.Parse(Txt_FoodID.Text.Trim()));
                }
            }
        }

        private void UDF_SelectCmbValue(int int_FoodUD)
        {
            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            tblFoodsDescription FoodDs = db.tblFoodsDescriptions.Single(n => n.FoodID == int_FoodUD);
            Cmb_FoodUnit.SelectedValue = FoodDs.FoodUnitID;
            Cmb_FoodCategury.SelectedValue = FoodDs.FoodCateguryID;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_FoodDescription.Text == "" || Txt_FoodID.Text == "")
            {
                MessageBox.Show("لطفا عنوان را انتخاب فرمائید.");
                return;
            }

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
                tblFoodsDescription FoodDs = db.tblFoodsDescriptions.Single(n => n.FoodID == int.Parse(Txt_FoodID.Text.Trim()));

                Class_tblLogs LogReg = new Class_tblLogs(this.Text + " رکورد " + Txt_FoodDescription.Text.Trim() + " -ID= " + Txt_FoodID.Text.Trim() + "حذف");
                LogReg.UDF_SaveLogToDb();

                db.tblFoodsDescriptions.DeleteOnSubmit(FoodDs);
                db.SubmitChanges();

                Txt_FoodID.Clear();
                Txt_FoodDescription.Clear();
                UDF_CmbFill();
                UDF_GrdFill();
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_FoodDescription.Text == "")
            {
                MessageBox.Show("لطفا عنوان یا بخشی از عنوان را برای جستجو وارد نمائید.");
                return;
            }
            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            var Query = from Row in db.tblFoodsDescriptions
                       where SqlMethods.Like(Row.FoodDescription , "%" + Txt_FoodDescription.Text.Trim() + "%")
                       select new 
                       {
                           Row.FoodID,
                           Row.FoodDescription,
                           Row.tblFoodCategury.FoodCateguryDescription,
                           Row.tblFoodUnit.FoodUnitDescription
                       };
            dataGridView1.DataSource = Query;
            Class_tblLogs LogReg = new Class_tblLogs(this.Text + " - عملیات جستجو");
            LogReg.UDF_SaveLogToDb();
            if (Query.Count() <= 0)
            {
                Txt_FoodDescription.Text = "";
                Txt_FoodID.Clear();
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                MessageBox.Show("رکوردی یافته نشد");
            }
        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            UDF_GrdFill();
            UDF_CmbFill();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
            Txt_FoodDescription.Clear();
            Txt_FoodID.Clear();
        }

        private void Cmb_FoodUnit_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}