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;
using System.Data.Linq.SqlClient;

namespace HesabdariSubSys
{
    public partial class frmBooks : Form
    {
        public frmBooks()
        {
            InitializeComponent();
        }

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblBookNumbers
                        orderby Row.BookNumber descending
                        select new
                        {
                            Row.BookNumber,
                            Row.BookName

                        };
            Grd_Book.DataSource = Query;
        }

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

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

            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblBookNumber BookNum = new tblBookNumber();
            BookNum.BookName = Txt_BookName.Text;

            db.tblBookNumbers.InsertOnSubmit(BookNum);
            db.SubmitChanges();

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

        }

        private void frmBooks_Load(object sender, EventArgs e)
        {
            UDF_GrdFill();
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_BookName.Text == "")
            {
                MessageBox.Show("انتخاب عنوان دفتر الزامی است");
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblBookNumber BookNum = db.tblBookNumbers.Single(n=>n.BookNumber == int.Parse(Txt_BookNumber.Text.Trim()));

                db.tblBookNumbers.DeleteOnSubmit(BookNum);
                db.SubmitChanges();

                Txt_BookName.Clear();
                Txt_BookNumber.Clear();
                UDF_GrdFill();
                
            }
        }

        private void Grd_Book_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_Book.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_Book.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_BookNumber.Text = Grd_Book.Rows[int.Parse(Grd_Book.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_BookName.Text = Grd_Book.Rows[int.Parse(Grd_Book.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();

                    }
                }
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_BookName.Text == "")
            {
                MessageBox.Show("عنوان یا بخشی از عنوان دفتر را وارد کنید");
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblBookNumbers
                        where SqlMethods.Like(Row.BookName , "%" + Txt_BookName.Text.Trim() + "%")
                        select new
                        {
                            Row.BookNumber,
                            Row.BookName
                        };
            Grd_Book.DataSource = Query;
            
            if (Query.Count() <= 0)
            {
                Txt_BookName.Text = "";
                Btn_Search.SendToBack();
                Btn_Refresh.BringToFront();
                MessageBox.Show("رکوردی یافته نشد");
            }
        }

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

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_BookNumber.Text == "")
            {
                MessageBox.Show("لطفا دفتر مورد نظر را انتخاب کنید");
                return;
            }

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblBookNumber BookNum = db.tblBookNumbers.Single(n=>n.BookNumber == int.Parse(Txt_BookNumber.Text.Trim()));

                BookNum.BookName = Txt_BookName.Text.Trim();
                db.SubmitChanges();
                
                UDF_GrdFill();
                Txt_BookName.Clear();
                Txt_BookNumber.Clear();
            }
        }
    }
}

