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

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

        private void UDF_CmbFill1()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.xtblBookHas
                        select new
                        {
                            Row.BN1,
                            Row.BN2
                        };

            comboBox1.DisplayMember = "BN2";
            comboBox1.ValueMember = "BN1";

            comboBox1.DataSource = Query;

        }

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblCashBooks
                        orderby Row.CashBookCOde descending
                        select new
                        {
                            Row.CashBookCOde,
                            Row.DateSabt,
                            Row.BookNumber,
                            Row.NumberBook,
                            Row.Deptor,
                            Row.Creditor

                        };
            Grd_BookSandogh.DataSource = Query;
        }

        private void UDF_ClearTextBox()
        {
            Msk_Txt_DateSabt.Clear();
            Txt_Creditor.Clear();
            Txt_Debtor.Clear();
            Txt_NumberBook.Clear();
        }

        private void Txt_Debtor_TextChanged(object sender, EventArgs e)
        {

        }

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Msk_Txt_DateSabt.Text == "" || Txt_Creditor.Text == "" || Txt_Debtor.Text == "" || Txt_NumberBook.Text == "")
            {
                MessageBox.Show("ورود تاریخ ، شماره دفتر ، بدهکار و بستانکار الزامی است.");
                return;
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblCashBook book = new tblCashBook();

            book.DateSabt = Msk_Txt_DateSabt.Text;
            book.BookNumber = int.Parse(comboBox1.SelectedValue.ToString());
            book.NumberBook = Decimal.Parse(Txt_NumberBook.Text.Trim());
            book.Deptor = Decimal.Parse(Txt_Debtor.Text.Trim());
            book.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());

            db.tblCashBooks.InsertOnSubmit(book);
            db.SubmitChanges();
            UDF_GrdFill();
            UDF_CmbFill1();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            UDF_ClearTextBox();
            MessageBox.Show("رکورد جدید درج گردید.");
        }

        private void Grd_BookSandogh_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_BookSandogh.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_BookSandogh.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_CashBookCOde.Text = Grd_BookSandogh.Rows[int.Parse(Grd_BookSandogh.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Msk_Txt_DateSabt.Text = Grd_BookSandogh.Rows[int.Parse(Grd_BookSandogh.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_NumberBook.Text = Grd_BookSandogh.Rows[int.Parse(Grd_BookSandogh.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_Debtor.Text = Grd_BookSandogh.Rows[int.Parse(Grd_BookSandogh.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Creditor.Text = Grd_BookSandogh.Rows[int.Parse(Grd_BookSandogh.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                    }
                    UDF_SelectCmbValue(Decimal.Parse(Txt_CashBookCOde.Text.Trim()));
                }
            }
        }

        private void UDF_SelectCmbValue(Decimal Dec_CodeBookSandogh)
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblCashBook bookch = db.tblCashBooks.Single(n => n.CashBookCOde == Dec_CodeBookSandogh);
            comboBox1.SelectedValue = bookch.BookNumber;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_NumberBook.Text == "")
            {
                MessageBox.Show("انتخاب دفتر صندوق مورد نظر الزامی است.");
                return;
            }

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblCashBook bookch = db.tblCashBooks.Single(n => n.CashBookCOde == Decimal.Parse(Txt_CashBookCOde.Text.Trim()));

                db.tblCashBooks.DeleteOnSubmit(bookch);
                db.SubmitChanges();

                UDF_ClearTextBox();
                UDF_CmbFill1();
                UDF_GrdFill();
            }
        }

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

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblCashBook bookch = db.tblCashBooks.Single(n => n.CashBookCOde == Decimal.Parse(Txt_CashBookCOde.Text.Trim()));

                bookch.DateSabt = Msk_Txt_DateSabt.Text;
                bookch.BookNumber = int.Parse(comboBox1.SelectedValue.ToString());
                bookch.NumberBook = Decimal.Parse(Txt_NumberBook.Text.Trim());
                bookch.Deptor = Decimal.Parse(Txt_Debtor.Text.Trim());
                bookch.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());
                db.SubmitChanges();

                UDF_GrdFill();
                UDF_CmbFill1();
                UDF_ClearTextBox();

            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text  == "")
            {
                MessageBox.Show("تاریخ ثبت دفتر و یا بخشی از تاریخ ثبت را وارد کنید");
                return;
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblCashBooks
                        where SqlMethods.Like(Row.DateSabt , "%" +  Txt_Search.Text.Trim() + "%")
                        select new
                        {
                            Row.CashBookCOde,
                            Row.DateSabt,
                            Row.BookNumber,
                            Row.NumberBook,
                            Row.Deptor,
                            Row.Creditor
                        };
            Grd_BookSandogh.DataSource = Query;

            if (Query.Count() <= 0)
            {
                Txt_Search.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();
        }
    }
}